- gf25071 的博客
冒险(ai)
- @ 2026-3-6 12:52:50
冒险
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
// ===================== 核心数据结构(C++98兼容) =====================
// 物品结构体
struct Item {
string name; // 物品名称
string description;// 物品描述
int attack; // 攻击加成(战斗用)
int defense; // 防御加成(战斗用)
bool isKey; // 是否是关键道具(解锁场景用)
// C++98构造函数(空构造)
Item() {
name = "";
description = "";
attack = 0;
defense = 0;
isKey = false;
}
};
// NPC结构体
struct NPC {
string name; // NPC名称
string dialogue; // 对话内容
bool isHostile; // 是否敌对(触发战斗)
int hp; // 血量
int attack; // 攻击力
bool isDefeated; // 是否已击败
// C++98构造函数(空构造)
NPC() {
name = "";
dialogue = "";
isHostile = false;
hp = 0;
attack = 0;
isDefeated = false;
}
};
// 场景结构体
struct Scene {
int id; // 场景ID
string name; // 场景名称
string description;// 场景描述
map<string, int> exits; // 出口:方向 -> 场景ID(如"north"->2)
vector<Item> items; // 场景中的物品
vector<NPC> npcs; // 场景中的NPC
bool isLocked; // 是否锁定(需要钥匙解锁)
string requiredKey; // 解锁需要的钥匙名称
// C++98构造函数(空构造)
Scene() {
id = 0;
name = "";
description = "";
isLocked = false;
requiredKey = "";
}
};
// 玩家状态(C++98兼容,构造函数初始化)
struct Player {
string name; // 玩家姓名
int hp; // 血量
int attack; // 基础攻击力
int defense; // 基础防御力
vector<Item> inventory; // 背包
int currentSceneId; // 当前所在场景ID
bool hasTreasure; // 是否找到宝藏(通关条件)
// C++98构造函数:手动初始化所有成员
Player() {
name = "";
hp = 100;
attack = 10;
defense = 5;
currentSceneId = 1;
hasTreasure = false;
}
};
// ===================== 全局变量 =====================
vector<Scene> scenes; // 所有场景
Player player; // 玩家实例
// ===================== 工具函数 =====================
// 清屏(跨平台兼容)
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// 等待玩家按回车继续
void pressEnterToContinue() {
cout << "\n按回车键继续...";
cin.ignore();
cin.get();
clearScreen();
}
// 查找物品(在背包中)
Item* findItemInInventory(const string& itemName) {
for (vector<Item>::iterator it = player.inventory.begin(); it != player.inventory.end(); ++it) {
if (it->name == itemName) {
return &(*it);
}
}
return NULL;
}
// 移除场景中的物品
void removeItemFromScene(int sceneId, const string& itemName) {
for (vector<Scene>::iterator it = scenes.begin(); it != scenes.end(); ++it) {
if (it->id == sceneId) {
for (vector<Item>::iterator itItem = it->items.begin(); itItem != it->items.end(); ++itItem) {
if (itItem->name == itemName) {
it->items.erase(itItem);
break;
}
}
break;
}
}
}
// 查找场景中的NPC
NPC* findNPCInScene(int sceneId, const string& npcName) {
for (vector<Scene>::iterator it = scenes.begin(); it != scenes.end(); ++it) {
if (it->id == sceneId) {
for (vector<NPC>::iterator itNpc = it->npcs.begin(); itNpc != it->npcs.end(); ++itNpc) {
if (itNpc->name == npcName) {
return &(*itNpc);
}
}
}
}
return NULL;
}
// ===================== 战斗系统 =====================
bool battle(NPC& enemy) {
clearScreen();
cout << "===== 战斗开始 =====" << endl;
cout << "你遇到了 " << enemy.name << "!" << endl;
cout << enemy.name << " 血量:" << enemy.hp << " | 攻击力:" << enemy.attack << endl;
cout << "你的血量:" << player.hp << " | 攻击力:" << player.attack << " | 防御力:" << player.defense << endl;
pressEnterToContinue();
while (true) {
// 玩家回合
cout << "\n【你的回合】" << endl;
cout << "1. 普通攻击" << endl;
cout << "2. 查看背包(使用物品)" << endl;
int choice;
cin >> choice;
cin.ignore();
int damage = 0;
if (choice == 1) {
damage = player.attack - (enemy.attack / 4); // 简易伤害公式
if (damage < 1) damage = 1;
enemy.hp -= damage;
cout << "你对 " << enemy.name << " 造成了 " << damage << " 点伤害!" << endl;
} else if (choice == 2) {
if (player.inventory.empty()) {
cout << "你的背包是空的!" << endl;
continue;
}
cout << "\n【你的背包】" << endl;
int idx = 0;
for (vector<Item>::iterator it = player.inventory.begin(); it != player.inventory.end(); ++it) {
idx++;
cout << idx << ". " << it->name << " - "
<< it->description << "(攻击+" << it->attack
<< " 防御+" << it->defense << ")" << endl;
}
cout << "0. 取消" << endl;
int itemChoice;
cin >> itemChoice;
cin.ignore();
if (itemChoice > 0 && itemChoice <= (int)player.inventory.size()) {
vector<Item>::iterator it = player.inventory.begin() + (itemChoice - 1);
player.attack += it->attack;
player.defense += it->defense;
cout << "你使用了 " << it->name << ",攻击+" << it->attack << ",防御+" << it->defense << "!" << endl;
// 使用后移除消耗品
player.inventory.erase(it);
}
continue;
}
// 检查敌人是否死亡
if (enemy.hp <= 0) {
cout << enemy.name << " 被击败了!" << endl;
enemy.isDefeated = true;
pressEnterToContinue();
return true;
}
// 敌人回合
cout << "\n【" << enemy.name << " 的回合】" << endl;
damage = enemy.attack - player.defense;
if (damage < 1) damage = 1;
player.hp -= damage;
cout << enemy.name << " 对你造成了 " << damage << " 点伤害!" << endl;
cout << "你的剩余血量:" << player.hp << endl;
// 检查玩家是否死亡
if (player.hp <= 0) {
cout << "你被击败了!游戏结束..." << endl;
pressEnterToContinue();
return false;
}
pressEnterToContinue();
}
}
// ===================== 场景初始化(纯C++98) =====================
void initScenes() {
// 场景1:森林入口
Scene scene1;
scene1.id = 1;
scene1.name = "森林入口";
scene1.description = "你站在一片昏暗的森林入口,周围是高大的橡树,树叶沙沙作响。\n北边有一条小路通向森林深处,东边是一片草地,西边是一条小溪。";
scene1.exits["north"] = 2;
scene1.exits["east"] = 3;
scene1.exits["west"] = 4;
scene1.isLocked = false;
scene1.requiredKey = "";
// 添加小刀物品
Item knife;
knife.name = "小刀";
knife.description = "一把锋利的小刀,能增加5点攻击力";
knife.attack = 5;
knife.defense = 0;
knife.isKey = false;
scene1.items.push_back(knife);
// 添加守林人NPC
NPC guard;
guard.name = "守林人";
guard.dialogue = "你好啊,旅行者!森林深处有只凶恶的狼,小心点!";
guard.isHostile = false;
guard.hp = 0;
guard.attack = 0;
guard.isDefeated = false;
scene1.npcs.push_back(guard);
// 场景2:森林深处(锁定,需要钥匙)
Scene scene2;
scene2.id = 2;
scene2.name = "森林深处";
scene2.description = "这里的树木更加密集,阳光几乎无法穿透。你看到一个破旧的木屋,门口有只狼守着。";
scene2.exits["south"] = 1;
scene2.exits["inside"] = 5;
scene2.isLocked = true;
scene2.requiredKey = "森林钥匙";
// 添加野狼NPC
NPC wolf;
wolf.name = "野狼";
wolf.dialogue = "";
wolf.isHostile = true;
wolf.hp = 30;
wolf.attack = 8;
wolf.isDefeated = false;
scene2.npcs.push_back(wolf);
// 场景3:阳光草地
Scene scene3;
scene3.id = 3;
scene3.name = "阳光草地";
scene3.description = "一片开阔的草地,阳光洒在身上暖洋洋的。草地上有一朵鲜艳的红花,还有一个老妇人坐在石头上。";
scene3.exits["west"] = 1;
scene3.isLocked = false;
scene3.requiredKey = "";
// 添加红花物品
Item flower;
flower.name = "红花";
flower.description = "一朵美丽的红花,能小幅恢复血量";
flower.attack = 0;
flower.defense = 2;
flower.isKey = false;
scene3.items.push_back(flower);
// 添加森林钥匙
Item forestKey;
forestKey.name = "森林钥匙";
forestKey.description = "打开森林深处的钥匙";
forestKey.attack = 0;
forestKey.defense = 0;
forestKey.isKey = true;
scene3.items.push_back(forestKey);
// 添加老妇人NPC
NPC oldWoman;
oldWoman.name = "老妇人";
oldWoman.dialogue = "这朵红花能治伤,送给你吧~";
oldWoman.isHostile = false;
oldWoman.hp = 0;
oldWoman.attack = 0;
oldWoman.isDefeated = false;
scene3.npcs.push_back(oldWoman);
// 场景4:小溪边
Scene scene4;
scene4.id = 4;
scene4.name = "小溪边";
scene4.description = "清澈的小溪潺潺流淌,水底有光滑的鹅卵石。溪边有一个木箱子,但被锁住了。";
scene4.exits["east"] = 1;
scene4.isLocked = true;
scene4.requiredKey = "箱子钥匙";
// 场景5:木屋宝藏室
Scene scene5;
scene5.id = 5;
scene5.name = "木屋宝藏室";
scene5.description = "木屋内部堆满了金银财宝,正中央有一个镶满宝石的宝箱!这就是你要找的宝藏!";
scene5.exits["outside"] = 2;
scene5.isLocked = false;
scene5.requiredKey = "";
// 添加宝箱物品
Item treasureBox;
treasureBox.name = "宝箱";
treasureBox.description = "装满金银财宝的宝箱,游戏通关!";
treasureBox.attack = 0;
treasureBox.defense = 0;
treasureBox.isKey = false;
scene5.items.push_back(treasureBox);
// 将所有场景加入列表
scenes.push_back(scene1);
scenes.push_back(scene2);
scenes.push_back(scene3);
scenes.push_back(scene4);
scenes.push_back(scene5);
}
// ===================== 游戏主逻辑 =====================
// 获取当前场景
Scene* getCurrentScene() {
for (vector<Scene>::iterator it = scenes.begin(); it != scenes.end(); ++it) {
if (it->id == player.currentSceneId) {
return &(*it);
}
}
return NULL;
}
// 显示当前场景信息
void showSceneInfo() {
Scene* currentScene = getCurrentScene();
if (!currentScene) return;
// 场景标题
cout << "========== " << currentScene->name << " ==========\n" << endl;
// 场景描述
cout << currentScene->description << "\n" << endl;
// 显示场景中的物品
if (!currentScene->items.empty()) {
cout << "【场景物品】:";
int idx = 0;
for (vector<Item>::iterator it = currentScene->items.begin(); it != currentScene->items.end(); ++it) {
if (idx > 0) cout << "、";
cout << it->name;
idx++;
}
cout << "\n" << endl;
}
// 显示场景中的NPC
if (!currentScene->npcs.empty()) {
cout << "【场景NPC】:";
int idx = 0;
for (vector<NPC>::iterator it = currentScene->npcs.begin(); it != currentScene->npcs.end(); ++it) {
if (idx > 0) cout << "、";
cout << it->name;
idx++;
}
cout << "\n" << endl;
}
// 显示可移动方向
cout << "【可移动方向】:";
for (map<string, int>::iterator it = currentScene->exits.begin(); it != currentScene->exits.end(); ++it) {
cout << it->first << " ";
}
cout << "\n" << endl;
// 玩家状态
cout << "【你的状态】:血量=" << player.hp << " 攻击=" << player.attack << " 防御=" << player.defense << "\n" << endl;
}
// 处理玩家指令
bool processCommand(const string& command) {
Scene* currentScene = getCurrentScene();
if (!currentScene) return false;
// 拆分指令(C++98兼容,不使用auto)
vector<string> parts;
string temp;
for (string::const_iterator it = command.begin(); it != command.end(); ++it) {
char c = *it;
if (c == ' ') {
if (!temp.empty()) {
parts.push_back(temp);
temp = "";
}
} else {
// 转为小写(兼容英文指令,中文不影响)
if (c >= 'A' && c <= 'Z') {
c += 32;
}
temp += c;
}
}
if (!temp.empty()) parts.push_back(temp);
if (parts.empty()) return false;
// 1. 移动指令:go + 方向
if (parts[0] == "go" && parts.size() >= 2) {
string direction = parts[1];
map<string, int>::iterator exitIt = currentScene->exits.find(direction);
if (exitIt != currentScene->exits.end()) {
int targetSceneId = exitIt->second;
// 检查目标场景是否锁定
Scene* targetScene = NULL;
for (vector<Scene>::iterator it = scenes.begin(); it != scenes.end(); ++it) {
if (it->id == targetSceneId) {
targetScene = &(*it);
break;
}
}
if (targetScene) {
if (targetScene->isLocked) {
// 检查是否有解锁钥匙
bool hasKey = false;
for (vector<Item>::iterator it = player.inventory.begin(); it != player.inventory.end(); ++it) {
if (it->name == targetScene->requiredKey) {
hasKey = true;
break;
}
}
if (!hasKey) {
cout << "这个方向被锁住了,需要「" << targetScene->requiredKey << "」才能打开!" << endl;
return false;
} else {
cout << "你用「" << targetScene->requiredKey << "」打开了锁!" << endl;
targetScene->isLocked = false; // 解锁场景
}
}
// 移动到目标场景
player.currentSceneId = targetSceneId;
cout << "你向" << direction << "移动,来到了「" << targetScene->name << "」!" << endl;
// 触发场景事件(比如遇到狼自动战斗)
if (targetSceneId == 2) { // 森林深处
NPC* wolf = findNPCInScene(2, "野狼");
if (wolf && !wolf->isDefeated) {
cout << "一只凶恶的狼跳了出来,准备攻击你!" << endl;
pressEnterToContinue();
bool win = battle(*wolf);
if (!win) {
return true; // 战斗失败,游戏结束
} else {
// 击败狼后获得箱子钥匙(C++98写法)
Item boxKey;
boxKey.name = "箱子钥匙";
boxKey.description = "打开小溪边箱子的钥匙";
boxKey.attack = 0;
boxKey.defense = 0;
boxKey.isKey = true;
player.inventory.push_back(boxKey);
cout << "你击败了狼,找到了「箱子钥匙」!" << endl;
}
}
}
return true;
}
} else {
cout << "不能往这个方向走!" << endl;
}
return false;
}
// 2. 拾取指令:take + 物品名
if (parts[0] == "take" && parts.size() >= 2) {
string itemName;
for (size_t i = 1; i < parts.size(); ++i) {
if (i > 1) itemName += " ";
itemName += parts[i];
}
// 匹配场景物品
bool found = false;
Item targetItem;
for (vector<Item>::iterator it = currentScene->items.begin(); it != currentScene->items.end(); ++it) {
string lowerName;
for (string::const_iterator cIt = it->name.begin(); cIt != it->name.end(); ++cIt) {
char c = *cIt;
if (c >= 'A' && c <= 'Z') c += 32;
lowerName += c;
}
if (lowerName == itemName) {
targetItem = *it;
removeItemFromScene(currentScene->id, it->name);
player.inventory.push_back(targetItem);
cout << "你拾取了「" << it->name << "」:" << it->description << endl;
// 特殊物品触发(比如宝箱=通关)
if (it->name == "宝箱") {
player.hasTreasure = true;
cout << "恭喜你找到了宝藏!游戏通关!" << endl;
return true;
}
found = true;
break;
}
}
if (!found) {
cout << "场景中没有这个物品!" << endl;
}
return false;
}
// 3. 查看背包:inventory / bag
if (parts[0] == "inventory" || parts[0] == "bag") {
if (player.inventory.empty()) {
cout << "你的背包是空的!" << endl;
} else {
cout << "【你的背包】:" << endl;
int idx = 0;
for (vector<Item>::iterator it = player.inventory.begin(); it != player.inventory.end(); ++it) {
idx++;
cout << idx << ". 「" << it->name << "」- "
<< it->description << endl;
}
}
return false;
}
// 4. 对话指令:talk + NPC名
if (parts[0] == "talk" && parts.size() >= 2) {
string npcName;
for (size_t i = 1; i < parts.size(); ++i) {
if (i > 1) npcName += " ";
npcName += parts[i];
}
// 匹配NPC
bool found = false;
for (vector<NPC>::iterator it = currentScene->npcs.begin(); it != currentScene->npcs.end(); ++it) {
string lowerName;
for (string::const_iterator cIt = it->name.begin(); cIt != it->name.end(); ++cIt) {
char c = *cIt;
if (c >= 'A' && c <= 'Z') c += 32;
lowerName += c;
}
if (lowerName == npcName) {
cout << "【" << it->name << "】:" << it->dialogue << endl;
found = true;
break;
}
}
if (!found) {
cout << "场景中没有这个NPC!" << endl;
}
return false;
}
// 5. 退出游戏:quit / exit
if (parts[0] == "quit" || parts[0] == "exit") {
cout << "你确定要退出游戏吗?(y/n):";
char choice;
cin >> choice;
if (choice == 'y' || choice == 'Y') {
return true;
}
cin.ignore();
return false;
}
// 未知指令
cout << "未知指令!支持的指令:\n"
<< "1. go <方向> (如 go north)- 移动\n"
<< "2. take <物品名> (如 take 小刀)- 拾取物品\n"
<< "3. talk <NPC名> (如 talk 守林人)- 对话\n"
<< "4. inventory/bag - 查看背包\n"
<< "5. quit/exit - 退出游戏" << endl;
return false;
}
// 游戏主循环
void gameLoop() {
clearScreen();
cout << "===== 文字冒险游戏 =====" << endl;
cout << "请输入你的名字:";
getline(cin, player.name);
clearScreen();
cout << "欢迎你," << player.name << "!" << endl;
cout << "游戏目标:找到森林深处木屋中的宝藏!" << endl;
cout << "输入「help」可查看指令说明,按回车开始游戏..." << endl;
pressEnterToContinue();
bool gameOver = false;
while (!gameOver) {
// 显示当前场景
showSceneInfo();
// 输入指令
cout << "请输入指令(输入help查看帮助):";
string command;
getline(cin, command);
// 处理指令
gameOver = processCommand(command);
// 检查通关条件
if (player.hasTreasure) {
clearScreen();
cout << "===== 游戏通关!=====" << endl;
cout << player.name << ",你成功找到了宝藏,成为了森林的英雄!" << endl;
cout << "你的最终状态:血量=" << player.hp << " 攻击=" << player.attack << endl;
gameOver = true;
}
// 检查玩家死亡
if (player.hp <= 0) {
clearScreen();
cout << "===== 游戏结束 =====" << endl;
cout << player.name << ",你在冒险中不幸身亡..." << endl;
gameOver = true;
}
if (!gameOver) pressEnterToContinue();
}
cout << "\n感谢游玩!按回车键退出..." << endl;
cin.get();
}
// ===================== 主函数 =====================
int main() {
srand((unsigned int)time(NULL)); // C++98兼容的随机数初始化
initScenes(); // 初始化场景
gameLoop(); // 启动游戏
return 0;
}