C++#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iterator>
#include <sstream>
using namespace std;
// 敌人类型枚举
enum class EnemyType {
PATROL_TEACHER, // 巡逻老师
ABERRANT_STUDENT, // 异常学生
DISCIPLINE_MASTER,// 教导主任(强化版)
JAPANESE_RESEARCHER, // 日本研究员
GHOST_MAIDEN // 红衣少女幽灵
};
// 剧情状态枚举
enum class PlotState {
INITIAL, // 初始状态
FOUND_DIARY, // 发现日记
MET_STRANGE_STUDENT, // 遇到神秘学生
FOUND_LAB_NOTES, // 发现实验室笔记
UNLOCKED_SECRET, // 解锁秘密
FINAL_CHAPTER // 最终章节
};
// 敌人结构
struct Enemy {
EnemyType type;
string name;
int health;
int damage;
string locationId;
bool isActive;
bool isHostile;
int patrolRange; // 巡逻范围(步数)
string backstory; // 敌人背景故事
};
// 位置结构
struct Location {
string id;
string name;
string description;
map<string, string> exits; // 方向 -> 位置ID
vector<string> items; // 该位置的物品
bool hasHiddenPassage; // 是否有密道
string hiddenPassageTarget;// 密道通向的位置ID
string hiddenPassageSource;// 密道来源位置ID(新增,用于返回)
string environmentalEffect;// 环境效果(如"噪音"、"毒气")
string storyHint; // 地点相关的剧情提示
bool isLocked; // 位置是否锁定
string lockMessage; // 锁定提示信息
};
// 非玩家角色结构
struct NPC {
string id;
string name;
string locationId;
bool isActive;
vector<string> dialogs; // 对话内容
vector<string> questItems; // 任务物品
string questDescription; // 任务描述
string questReward; // 任务奖励
};
// 游戏状态结构体
struct GameState {
string currentLocation;
map<string, bool> items; // 物品 -> 是否拥有
int sanity; // 理智值
int stamina; // 体力值
int day; // 当前天数
int hour; // 当前小时
int minute; // 当前分钟
bool gameOver; // 游戏是否结束
string ending; // 结局类型
int scholarship; // 奖学金(游戏货币)
int passes; // 通行证数量
bool foundSecret; // 是否发现秘密
int classesAttendedToday; // 今天上课次数
// 敌人相关
map<string, Enemy> enemies; // 敌人ID -> 敌人
bool hasEncounteredPatrolTeacher;
bool hasDefeatedDisciplineMaster;
int enemyEncounterCount; // 敌人遭遇次数
// 剧情分支
bool hasHelpedStrangeStudent;
bool hasBurnedMusicScore;
int goodDeedCount;
PlotState plotState; // 当前剧情状态
bool hasMetJanitor; // 是否遇到看门人
bool hasReturnedLocket; // 是否归还了项链
string memories; // 玩家恢复的记忆片段
int ritualProgress; // 仪式完成进度
// NPC相关
map<string, NPC> npcs; // NPC ID -> NPC
// 新增:记录进入密道前的位置,用于返回
string locationBeforeSecretPassage;
};
// 函数声明
void printWelcomeMessage();
map<string, Location> createWorld();
void initializeGame(GameState& state);
void printLocationDescription(const Location& loc, const GameState& state, const map<string, Location>& world);
void printHelp();
bool movePlayer(const string& direction, GameState& state, const map<string, Location>& world);
void processInput(const string& input, GameState& state, map<string, Location>& world);
void advanceTime(GameState& state, map<string, Location>& world);
void checkGameGoals(GameState& state);
void printEnding(const GameState& state);
void takeItem(const string& itemName, GameState& state, map<string, Location>& world);
void examineItem(const string& itemName, const GameState& state);
bool usePass(GameState& state, const string& enemyName);
// 敌人和NPC相关函数声明
void initializeEnemies(GameState& state);
void initializeNPCs(GameState& state);
void updateEnemies(GameState& state, map<string, Location>& world);
void updateNPCs(GameState& state, map<string, Location>& world);
bool encounterEnemy(GameState& state, map<string, Location>& world, EnemyType type);
void handleEnemyCombat(const string& input, GameState& state, map<string, Location>& world, const string& enemyId);
void interactWithNPC(GameState& state, const string& npcId);
void useEnvironmentalItem(const string& item, GameState& state, const Location& loc);
// 剧情相关函数
void triggerPlotEvent(GameState& state, map<string, Location>& world);
void revealMemory(GameState& state, const string& memory);
void checkRitualProgress(GameState& state);
// 辅助函数:分割字符串
vector<string> split(const string &s) {
vector<string> tokens;
string token;
istringstream tokenStream(s);
while (tokenStream >> token) {
tokens.push_back(token);
}
return tokens;
}
// 游戏主函数
int main() {
srand(time(0)); // 初始化随机数生成器
printWelcomeMessage();
GameState state;
initializeGame(state);
map<string, Location> world = createWorld();
// 初始化敌人和NPC
initializeEnemies(state);
initializeNPCs(state);
// 添加新物品到世界
world["corridor2"].items.push_back("打火机");
world["teacher_office"].items.push_back("手电筒");
world["infirmary"].items.push_back("生锈的项链");
// 游戏主循环
while (!state.gameOver) {
// 打印当前位置描述
const Location& currentLoc = world[state.currentLocation];
printLocationDescription(currentLoc, state, world);
// 触发剧情事件
triggerPlotEvent(state, world);
// 获取玩家输入
cout << "\n请输入指令 (输入 '帮助' 查看可用指令): ";
string input;
getline(cin, input);
// 处理输入
processInput(input, state, world);
// 检查游戏目标
checkGameGoals(state);
// 推进时间
advanceTime(state, world);
// 更新NPC状态
updateNPCs(state, world);
}
// 打印结局
printEnding(state);
return 0;
}
// 打印欢迎信息
void printWelcomeMessage() {
cout << "==============================================" << endl;
cout << " 恐怖校园冒险游戏" << endl;
cout << "==============================================" << endl;
cout << "你在一阵刺耳的铃声中醒来,发现自己躺在陌生的宿舍床上。" << endl;
cout << "窗外是猩红的天空,空气中弥漫着铁锈般的血腥味。" << endl;
cout << "墙上的日历显示着'平成17年7月24日',一个你从未经历过的日期。" << endl;
cout << "远处传来若有若无的啜泣声,你必须找出真相并逃离这里..." << endl;
cout << endl;
}
// 创建游戏世界 - 修复了密道出口和教师办公室物品问题
map<string, Location> createWorld() {
map<string, Location> world;
// 宿舍
world["dormitory"] = {
"dormitory",
"男生宿舍307室",
"你的宿舍房间,简单而整洁。墙上贴着模糊的乐队海报,书桌上有一本上锁的日记。窗外是猩红色的夜空,看不到月亮。",
{{"东", "corridor1"}},
{"日记本"},
false, "", "", "",
"书桌上的日历停留在七年前的今天,旁边刻着'他们来了'的字样",
false, ""
};
// 走廊1
world["corridor1"] = {
"corridor1",
"一号走廊",
"长长的走廊,两侧是紧闭的教室门。墙上的时钟停在3:17,但秒针还在不规则地跳动。走廊尽头有一面布满裂纹的镜子。",
{{"西", "dormitory"}, {"东", "hallway"}, {"南", "classroom1"}, {"北", "history_classroom"}},
{"学生证"},
false, "", "", "",
"墙上有学生涂鸦:'不要在音乐教室停留太久'、'她在找她的项链'",
false, ""
};
// 大厅
world["hallway"] = {
"hallway",
"学校大厅",
"宽敞的大厅,有一个破损的公告栏和一座校史纪念碑。公告栏上贴着泛黄的旧报纸,标题写着'女子高中生失踪事件'。",
{{"西", "corridor1"}, {"东", "corridor2"}, {"北", "library"}, {"南", "chemistry_classroom"}},
{"校园地图"},
false, "", "", "",
"校史纪念碑上刻着建校年份'1947',以及一句拉丁文'永夜不终'",
false, ""
};
// 走廊2
world["corridor2"] = {
"corridor2",
"二号走廊",
"比一号走廊更阴暗,墙壁上有奇怪的污渍,像是干涸的血迹。天花板上的灯忽明忽暗,发出滋滋的电流声。",
{{"西", "hallway"}, {"东", "cafeteria"}, {"南", "music_classroom"}, {"北", "infirmary"}},
{"打火机"},
false, "", "", "忽明忽暗的灯光,有30%几率触发幻觉",
"地面上有拖拽的痕迹,一直延伸到医务室门口",
false, ""
};
// 1号教室
world["classroom1"] = {
"classroom1",
"1班教室",
"标准的教室,课桌上堆满了课本。黑板上用红色粉笔写着'7月24日,他们会来',旁边画着一个奇怪的符号。",
{{"北", "corridor1"}},
{"粉笔", "教科书", "蜡烛"},
false, "", "", "",
"讲台上有一本点名册,最后一页写着'祭品:空缺'",
false, ""
};
// 图书馆
world["library"] = {
"library",
"学校图书馆",
"巨大的图书馆,书架直达天花板。空气中弥漫着旧书和灰尘的味道。有几本书散落在地上,内容似乎与某种仪式有关。",
{{"南", "hallway"}},
{"古籍", "放大镜"},
true, "restricted_section", "library", "寂静,任何声音都会引起注意",
"角落里有被撕毁的报纸,上面报道了'1998年实验室火灾事件'",
false, ""
};
// 图书馆禁区
world["restricted_section"] = {
"restricted_section",
"图书馆禁区",
"图书馆深处的禁区,堆满了被禁止借阅的书籍。空气中弥漫着霉味,书架上的书大多与超自然现象和禁忌仪式有关。",
{{"南", "library"}}, // 明确添加返回图书馆的出口
{"禁忌仪式书", "褪色的照片"},
true, "library", "restricted_section", "阴冷,理智缓慢下降",
"一张泛黄的照片上有五个穿着校服的学生,其中一个被圈起来并打了叉",
true, "需要图书馆钥匙才能进入"
};
// 食堂
world["cafeteria"] = {
"cafeteria",
"学生食堂",
"空旷的食堂,餐桌上还放着未吃完的食物,已经变质发臭。墙上的菜单写着'今日特供:未知肉排',令人作呕。",
{{"西", "corridor2"}},
{"面包", "水瓶", "图书馆钥匙"},
false, "", "", "",
"厨房门口有血迹,似乎有什么东西被拖了进去",
false, ""
};
// 音乐教室
world["music_classroom"] = {
"music_classroom",
"音乐教室",
"墙上挂着各种乐器,钢琴上放着一本打开的乐谱。钢琴自动弹奏着悲伤的旋律,尽管房间里空无一人。",
{{"北", "corridor2"}},
{"乐谱", "小提琴"},
false, "", "", "持续播放的诡异音乐,每5分钟降低1点理智",
"乐谱上的曲子名叫《安魂曲》,作曲者一栏写着'佐藤雪'",
false, ""
};
// 历史教室
world["history_classroom"] = {
"history_classroom",
"历史教室",
"墙上挂着古老的地图和历史人物画像,他们的眼睛似乎在跟着你移动。讲台上有一台老式录音机,偶尔会自动播放杂音。",
{{"南", "corridor1"}, {"西", "teacher_office"}},
{"历史书", "录音机"},
true, "teacher_office", "history_classroom", "",
"历史书上关于1945-1947年的内容被刻意撕去",
false, ""
};
// 化学教室
world["chemistry_classroom"] = {
"chemistry_classroom",
"化学教室",
"充满了各种化学试剂的味道,实验台上有未完成的实验。墙上的元素周期表有几个元素被红笔圈出,旁边写着奇怪的公式。",
{{"北", "hallway"}},
{"烧杯", "未知药剂"},
true, "secret_laboratory", "chemistry_classroom", "腐蚀性气体,每10分钟降低2点体力",
"实验记录上写着'人体实验第37号,反应不稳定'",
false, ""
};
// 教师办公室 - 修复了物品问题,确保手电筒等物品正常出现
world["teacher_office"] = {
"teacher_office",
"教师办公室",
"杂乱的办公室,每张桌子都堆满了试卷和文件。有一张桌子格外整洁,上面放着一个名牌:'田中 研究助手'。你注意到桌上有一个手电筒。",
{{"东", "history_classroom"}, {"南", "hallway"}}, // 新增向南到大厅的出口
{"钥匙", "红墨水", "实验报告", "手电筒"}, // 确保手电筒等物品存在
true, "history_classroom", "teacher_office", "",
"实验报告上记录着'记忆消除药剂测试成功',署名是一个日本名字",
false, ""
};
// 秘密实验室
world["secret_laboratory"] = {
"secret_laboratory",
"秘密实验室",
"隐藏在学校地下的神秘实验室,有许多奇怪的设备和实验记录。墙上贴着人体解剖图,几个玻璃容器里漂浮着不明器官。",
{{"上", "chemistry_classroom"}}, // 明确的返回出口
{"实验日志", "燃烧瓶", "神秘药剂"},
false, "", "", "福尔马林气味,降低理智",
"墙上的日历显示着七年前的日期,每天都有一个名字被划掉",
true, "需要化学教室的钥匙才能进入"
};
// 医务室
world["infirmary"] = {
"infirmary",
"学校医务室",
"白色的房间,有几张病床和一个药品柜。空气中弥漫着消毒水和血腥味。角落里的床单上有大片深色污渍。",
{{"南", "corridor2"}},
{"绷带", "止痛药"},
false, "", "", "令人不安的寂静",
"药品柜里有一本病历,最后一页写着'佐藤雪,17岁,失踪'",
false, ""
};
// 体育馆
world["gymnasium"] = {
"gymnasium",
"学校体育馆",
"宽敞的体育馆,地面是磨损的木地板。角落里堆放着废弃的运动器材,中央有一个用粉笔画的巨大圆形符号。",
{{"西", "hallway"}},
{"运动服", "哨子"},
false, "", "", "",
"墙上有飞溅的血迹,形成了奇怪的图案,像是某种仪式标记",
true, "只有在找到体育馆钥匙后才能进入"
};
return world;
}
// 初始化游戏状态
void initializeGame(GameState& state) {
state.currentLocation = "dormitory";
state.items = {{"日记本", false}, {"学生证", false}, {"校园地图", false},
{"粉笔", false}, {"教科书", false}, {"古籍", false},
{"放大镜", false}, {"面包", false}, {"水瓶", false},
{"乐谱", false}, {"小提琴", false}, {"历史书", false},
{"钥匙", false}, {"红墨水", false}, {"实验日志", false},
{"燃烧瓶", false}, {"手电筒", false}, {"打火机", false},
{"神秘纸条", false}, {"生锈的项链", false},
{"图书馆钥匙", false}, {"录音机", false}, {"禁忌仪式书", false},
{"褪色的照片", false}, {"实验报告", false}, {"神秘药剂", false},
{"绷带", false}, {"止痛药", false}, {"体育馆钥匙", false},
{"蜡烛", false}};
state.sanity = 100;
state.stamina = 100;
state.day = 1;
state.hour = 22;
state.minute = 0;
state.gameOver = false;
state.ending = "";
state.scholarship = 0;
state.passes = 1;
state.foundSecret = false;
state.classesAttendedToday = 0;
// 敌人相关状态
state.enemies = {};
state.hasEncounteredPatrolTeacher = false;
state.hasDefeatedDisciplineMaster = false;
state.enemyEncounterCount = 0;
// 剧情分支状态
state.hasHelpedStrangeStudent = false;
state.hasBurnedMusicScore = false;
state.goodDeedCount = 0;
state.plotState = PlotState::INITIAL;
state.hasMetJanitor = false;
state.hasReturnedLocket = false;
state.memories = "";
state.ritualProgress = 0;
// NPC相关
state.npcs = {};
// 初始化密道前位置为空
state.locationBeforeSecretPassage = "";
}
// 打印当前位置描述
void printLocationDescription(const Location& loc, const GameState& state, const map<string, Location>& world) {
cout << "\n==============================================" << endl;
cout << "【" << loc.name << "】" << endl;
cout << loc.description << endl;
// 显示时间
cout << "\n[时间: 第" << state.day << "天 " << state.hour << ":"
<< (state.minute < 10 ? "0" : "") << state.minute << "]" << endl;
// 显示状态
cout << "[理智: " << state.sanity << ", 体力: " << state.stamina
<< ", 通行证: " << state.passes << "]" << endl;
// 显示剧情提示(如果有)
if (!loc.storyHint.empty() && rand() % 100 < 60) {
cout << "\n【发现线索】" << loc.storyHint << endl;
}
// 显示出口
cout << "\n出口: ";
for (const auto& exit : loc.exits) {
// 检查出口是否锁定
if (world.at(exit.second).isLocked) {
cout << exit.first << "(" << world.at(exit.second).name << " - 锁定) ";
} else {
cout << exit.first << "(" << world.at(exit.second).name << ") ";
}
}
// 如果在密道中,显示返回选项
if (!state.locationBeforeSecretPassage.empty() &&
(loc.id == "restricted_section" || loc.id == "teacher_office" || loc.id == "secret_laboratory")) {
cout << " 返回密道入口 ";
}
// 显示物品
if (!loc.items.empty()) {
cout << "\n这里有: ";
for (const auto& item : loc.items) {
cout << item << " ";
}
}
// 显示NPC(如果有)
for (const auto& npc : state.npcs) {
if (npc.second.isActive && npc.second.locationId == state.currentLocation) {
cout << "\n你看到了" << npc.second.name << ",似乎有话要对你说。(输入'交谈'与其对话)" << endl;
}
}
// 显示密道信息
if (loc.hasHiddenPassage) {
cout << "\n你发现了一个可能的密道入口。(输入'进入密道'进入)" << endl;
}
}
// 打印帮助信息
void printHelp() {
cout << "\n可用指令:" << endl;
cout << "移动: 北/南/东/西 (移动到对应方向的区域)" << endl;
cout << " 返回密道入口 (从密道返回之前的位置)" << endl;
cout << "物品: 拿起 [物品名] (拿起该区域的物品)" << endl;
cout << " 查看 [物品名] (查看已拥有的物品)" << endl;
cout << " 使用 [物品名] (使用物品,战斗中也可使用)" << endl;
cout << "战斗: 攻击 (攻击敌人)" << endl;
cout << " 逃跑 (尝试逃离战斗)" << endl;
cout << "探索: 进入密道 (进入发现的密道)" << endl;
cout << " 解锁 (尝试用钥匙解锁当前区域)" << endl;
cout << "剧情: 交谈 (与NPC对话)" << endl;
cout << " 帮助学生 (在特定位置帮助异常学生)" << endl;
cout << " 烧毁乐谱 (在音乐教室烧毁乐谱)" << endl;
cout << " 播放录音 (播放录音机内容)" << endl;
cout << "其他: 状态 (查看当前状态)" << endl;
cout << " 帮助 (查看帮助信息)" << endl;
cout << " 退出 (退出游戏)" << endl;
}
// 移动玩家
bool movePlayer(const string& direction, GameState& state, const map<string, Location>& world) {
const Location& currentLoc = world.at(state.currentLocation);
auto it = currentLoc.exits.find(direction);
if (it != currentLoc.exits.end()) {
const Location& targetLoc = world.at(it->second);
if (targetLoc.isLocked) {
cout << targetLoc.lockMessage << endl;
return false;
}
state.currentLocation = it->second;
state.stamina -= 2; // 移动消耗体力
if (state.stamina < 0) state.stamina = 0;
// 移动到新区域时的剧情提示
if (state.currentLocation == "secret_laboratory" && !state.foundSecret) {
cout << "\n【剧情】这个实验室比你想象的更大,墙上的标志显示这里曾是军方设施。" << endl;
}
return true;
}
cout << "不能向那个方向移动!" << endl;
return false;
}
// 处理玩家输入 - 重点修复了密道出口问题
void processInput(const string& input, GameState& state, map<string, Location>& world) {
string lowerInput = input;
transform(lowerInput.begin(), lowerInput.end(), lowerInput.begin(), ::tolower);
vector<string> tokens = split(lowerInput);
if (tokens.empty()) {
cout << "请输入有效指令。输入'帮助'查看可用指令。" << endl;
return;
}
// 退出游戏
if (lowerInput == "退出") {
state.gameOver = true;
state.ending = "主动退出";
return;
}
// 显示帮助
if (lowerInput == "帮助") {
printHelp();
return;
}
// 显示状态
if (lowerInput == "状态") {
cout << "\n【当前状态】" << endl;
cout << "理智: " << state.sanity << endl;
cout << "体力: " << state.stamina << endl;
cout << "天数: " << state.day << endl;
cout << "通行证: " << state.passes << endl;
cout << "奖学金: " << state.scholarship << endl;
// 显示记忆片段
if (!state.memories.empty()) {
cout << "\n【恢复的记忆】" << state.memories << endl;
}
cout << "\n【拥有物品】" << endl;
bool hasItems = false;
for (const auto& item : state.items) {
if (item.second) {
cout << "- " << item.first << endl;
hasItems = true;
}
}
if (!hasItems) {
cout << "你没有任何物品。" << endl;
}
return;
}
// 移动指令
if (lowerInput == "北" || lowerInput == "南" || lowerInput == "东" || lowerInput == "西") {
movePlayer(lowerInput, state, world);
return;
}
// 从密道返回(新增功能)
if (lowerInput == "返回密道入口" && !state.locationBeforeSecretPassage.empty()) {
cout << "你沿着密道返回了之前的位置。" << endl;
state.currentLocation = state.locationBeforeSecretPassage;
state.locationBeforeSecretPassage = ""; // 清空记录
state.stamina -= 3; // 返回消耗略多体力
return;
}
// 拿起物品
if (tokens[0] == "拿起") {
if (tokens.size() < 2) {
cout << "请指定要拿起的物品。例如:拿起 日记本" << endl;
return;
}
// 重组物品名
string itemName;
for (size_t i = 1; i < tokens.size(); ++i) {
if (i > 1) itemName += " ";
itemName += tokens[i];
}
Location& currentLoc = world[state.currentLocation];
auto it = find(currentLoc.items.begin(), currentLoc.items.end(), itemName);
if (it != currentLoc.items.end()) {
state.items[itemName] = true;
currentLoc.items.erase(it);
cout << "你拿起了" << itemName << endl;
// 特殊物品剧情触发
if (itemName == "日记本") {
state.plotState = PlotState::FOUND_DIARY;
revealMemory(state, "你记得这本日记是你最好的朋友的,但她七年前失踪了...");
cout << "日记的锁已经生锈,你轻易就打开了它。第一页写着'如果我消失了,请找到真相'。" << endl;
state.sanity -= 5;
} else if (itemName == "实验日志") {
state.plotState = PlotState::FOUND_LAB_NOTES;
cout << "实验日志记录了可怕的人体实验... 受试者都是失踪的学生,包括你的朋友。" << endl;
cout << "最后一页写着'仪式将在第七夜完成,需要最后的祭品'。" << endl;
state.foundSecret = true;
state.sanity -= 15;
} else if (itemName == "手电筒" && currentLoc.id == "teacher_office") {
// 确保教师办公室的手电筒能正常获取并给予提示
cout << "你拿起了手电筒,这正是你需要的!现在你可以安全地探索黑暗的地方了。" << endl;
cout << "手电筒的开关很灵敏,你试了试,光束稳定而明亮。" << endl;
}
return;
}
cout << "这里没有" << itemName << endl;
return;
}
// 查看物品
if (tokens[0] == "查看") {
if (tokens.size() < 2) {
cout << "请指定要查看的物品。例如:查看 日记本" << endl;
return;
}
// 重组物品名
string itemName;
for (size_t i = 1; i < tokens.size(); ++i) {
if (i > 1) itemName += " ";
itemName += tokens[i];
}
if (state.items[itemName]) {
examineItem(itemName, state);
} else {
cout << "你没有" << itemName << endl;
}
return;
}
// 使用物品
if (tokens[0] == "使用") {
if (tokens.size() < 2) {
cout << "请指定要使用的物品。例如:使用 面包" << endl;
return;
}
// 重组物品名
string itemName;
for (size_t i = 1; i < tokens.size(); ++i) {
if (i > 1) itemName += " ";
itemName += tokens[i];
}
if (!state.items[itemName]) {
cout << "你没有" << itemName << endl;
return;
}
// 检查是否在战斗中
bool inCombat = false;
string enemyId;
for (const auto& pair : state.enemies) {
if (pair.second.isActive && pair.second.locationId == state.currentLocation && pair.second.health > 0) {
inCombat = true;
enemyId = pair.first;
break;
}
}
if (inCombat) {
// 在战斗中使用物品
if (itemName == "燃烧瓶") {
Enemy& enemy = state.enemies[enemyId];
enemy.health -= 40;
state.items[itemName] = false;
cout << "你使用了燃烧瓶!对" << enemy.name << "造成了40点伤害!" << endl;
// 敌人反击
if (enemy.health > 0) {
state.sanity -= enemy.damage;
cout << enemy.name << "被激怒了,向你发起反击!你的理智下降了" << enemy.damage << "点!" << endl;
} else {
cout << "你成功击败了" << enemy.name << "!" << endl;
enemy.isActive = false;
// 处理击败敌人的奖励和剧情
if (enemy.type == EnemyType::JAPANESE_RESEARCHER) {
state.foundSecret = true;
cout << "你在他的实验室里找到了完整的实验记录,证实了731部队的联系。" << endl;
cout << "这些证据足以将他们绳之以法。" << endl;
state.ending = "真相揭示";
state.gameOver = true;
}
}
return;
} else if (itemName == "面包") {
state.stamina += 30;
if (state.stamina > 100) state.stamina = 100;
state.items[itemName] = false;
cout << "你快速吃下面包,体力恢复了30点!" << endl;
// 敌人趁机攻击
Enemy& enemy = state.enemies[enemyId];
state.sanity -= enemy.damage / 2;
cout << enemy.name << "趁你吃东西时发起攻击!你的理智下降了" << enemy.damage / 2 << "点!" << endl;
return;
} else if (itemName == "手电筒") {
// 修复:通过enemyId获取当前敌人
Enemy& enemy = state.enemies[enemyId];
cout << "你打开手电筒,强光让敌人暂时无法靠近!" << endl;
state.sanity += 5;
// 敌人暂时退缩
cout << enemy.name << "被强光照射,暂时后退了几步!" << endl;
return;
} else {
cout << "在战斗中无法使用" << itemName << endl;
return;
}
}
// 非战斗状态使用物品
if (itemName == "面包") {
state.stamina += 30;
if (state.stamina > 100) state.stamina = 100;
state.items[itemName] = false;
cout << "你吃了面包,体力恢复了30点。" << endl;
} else if (itemName == "水瓶") {
state.sanity += 10;
if (state.sanity > 100) state.sanity = 100;
state.items[itemName] = false;
cout << "你喝了水,理智恢复了10点。" << endl;
} else if (itemName == "手电筒") {
cout << "你打开手电筒,明亮的光束照亮了周围的环境,让你感到一丝安心。" << endl;
state.sanity += 5;
} else {
cout << "你不知道如何使用" << itemName << endl;
}
return;
}
// 密道相关指令 - 修复了无法返回的问题
if (lowerInput == "进入密道") {
const Location& currentLoc = world[state.currentLocation];
if (currentLoc.hasHiddenPassage) {
// 检查是否有光源
if ((state.items.count("手电筒") && state.items["手电筒"]) ||
(state.items.count("蜡烛") && state.items["蜡烛"])) {
// 记录进入密道前的位置
state.locationBeforeSecretPassage = state.currentLocation;
state.currentLocation = currentLoc.hiddenPassageTarget;
cout << "你通过密道来到了" << world[state.currentLocation].name << endl;
cout << "提示:输入'返回密道入口'可以回到之前的位置。" << endl;
// 密道探索剧情
if (state.currentLocation == "teacher_office" && state.plotState < PlotState::UNLOCKED_SECRET) {
cout << "\n【剧情】办公室里有一份文件引起了你的注意,上面写着'佐藤雪,实验体7号'。" << endl;
state.sanity -= 5;
}
printLocationDescription(world[state.currentLocation], state, world);
} else {
cout << "密道里太黑了,需要光源(手电筒或蜡烛)才能进入。" << endl;
}
} else {
cout << "这里没有密道可以进入。" << endl;
}
return;
}
// 其他指令保持不变...
// 解锁指令
if (lowerInput == "解锁") {
Location& currentLoc = world[state.currentLocation];
if (!currentLoc.isLocked) {
cout << "这个区域没有上锁。" << endl;
return;
}
// 尝试用合适的钥匙解锁
if (currentLoc.id == "restricted_section" && state.items["图书馆钥匙"]) {
currentLoc.isLocked = false;
cout << "你用图书馆钥匙打开了禁区的门!" << endl;
} else if (currentLoc.id == "secret_laboratory" && state.items["钥匙"]) {
currentLoc.isLocked = false;
cout << "你用办公室钥匙打开了通往秘密实验室的门!" << endl;
} else if (currentLoc.id == "gymnasium" && state.items["体育馆钥匙"]) {
currentLoc.isLocked = false;
cout << "你用体育馆钥匙打开了大门!" << endl;
} else {
cout << "你没有合适的钥匙来打开这里。" << endl;
}
return;
}
// 交谈指令
if (lowerInput == "交谈") {
bool talked = false;
for (const auto& pair : state.npcs) {
if (pair.second.isActive && pair.second.locationId == state.currentLocation) {
interactWithNPC(state, pair.first);
talked = true;
break;
}
}
if (!talked) {
cout << "周围没有人可以交谈。" << endl;
}
return;
}
// 检查是否在战斗中并处理战斗指令
for (const auto& pair : state.enemies) {
if (pair.second.isActive && pair.second.locationId == state.currentLocation && pair.second.health > 0) {
handleEnemyCombat(lowerInput, state, world, pair.first);
return;
}
}
cout << "未知指令。输入 '帮助' 查看可用指令。" << endl;
}
// 推进时间
void advanceTime(GameState& state, map<string, Location>& world) {
// 增加10分钟
state.minute += 10;
if (state.minute >= 60) {
state.minute -= 60;
state.hour += 1;
if (state.hour >= 24) {
state.hour = 0;
state.day += 1;
state.classesAttendedToday = 0; // 重置每日上课次数
// 每天恢复一些理智和体力
state.sanity += 10;
if (state.sanity > 100) state.sanity = 100;
state.stamina += 20;
if (state.stamina > 100) state.stamina = 100;
// 第7天触发最终剧情
if (state.day == 7) {
state.plotState = PlotState::FINAL_CHAPTER;
cout << "\n【剧情】第七夜来临了,学校开始剧烈摇晃,墙壁渗出鲜血。你知道,仪式即将开始..." << endl;
state.sanity -= 10;
}
}
}
// 环境效果触发
const Location& currentLoc = world[state.currentLocation];
if (!currentLoc.environmentalEffect.empty()) {
if (currentLoc.environmentalEffect.find("理智") != string::npos && state.minute % 5 == 0) {
state.sanity -= 1;
cout << "\n【环境影响】" << currentLoc.environmentalEffect << "(-1)" << endl;
} else if (currentLoc.environmentalEffect.find("体力") != string::npos && state.minute % 10 == 0) {
state.stamina -= 2;
cout << "\n【环境影响】" << currentLoc.environmentalEffect << "(-2)" << endl;
} else if (currentLoc.environmentalEffect.find("幻觉") != string::npos && rand() % 100 < 30) {
cout << "\n【幻觉】" << currentLoc.environmentalEffect << "你好像看到了不存在的人影..." << endl;
state.sanity -= 3;
}
}
// 随机事件
if (rand() % 100 < 15) { // 15%的概率触发随机事件
int event = rand() % 7;
switch (event) {
case 0:
cout << "\n【随机事件】你听到远处传来女孩的哭泣声,令人心碎..." << endl;
state.sanity -= 3;
break;
case 1:
cout << "\n【随机事件】一阵冷风吹过,所有的门都自动关上了。" << endl;
state.sanity -= 2;
break;
case 2:
cout << "\n【随机事件】你找到了一枚旧硬币!奖学金+5。" << endl;
state.scholarship += 5;
break;
case 3:
cout << "\n【随机事件】你感到一阵剧烈的头痛,一些破碎的记忆闪过脑海..." << endl;
revealMemory(state, "实验室... 白色外套... 朋友的尖叫...");
state.stamina -= 8;
state.sanity -= 5;
break;
case 4:
cout << "\n【随机事件】你发现了一张被遗忘的通行证!" << endl;
state.passes += 1;
break;
case 5:
cout << "\n【随机事件】墙上的时钟开始倒转,你感到时间在扭曲..." << endl;
state.day = max(1, state.day - 1);
state.sanity -= 5;
break;
case 6:
cout << "\n【随机事件】你在地上发现了一把生锈的钥匙,上面刻着'体育馆'。" << endl;
state.items["体育馆钥匙"] = true;
world["gymnasium"].isLocked = false;
break;
}
}
// 检查理智是否过低
if (state.sanity <= 0 && !state.gameOver) {
if (usePass(state, "未知恐惧")) {
cout << "\n你的理智已经崩溃,但你使用了一张通行证暂时恢复了清醒。" << endl;
state.sanity = 30;
} else {
cout << "\n你的理智完全崩溃了... 你开始看到不存在的东西,最终成为了学校的一部分。" << endl;
state.gameOver = true;
state.ending = "理智崩溃";
}
}
// 检查体力是否过低
if (state.stamina <= 0) {
cout << "\n你太累了,需要休息... 体力恢复了10点,但时间过去了1小时。" << endl;
state.stamina = 10;
state.hour += 1;
if (state.hour >= 24) {
state.hour = 0;
state.day += 1;
}
}
// 更新敌人状态
updateEnemies(state, world);
}
// 检查游戏目标
void checkGameGoals(GameState& state) {
// 找到秘密并存活到第7天
if (state.day >= 7 && state.foundSecret && !state.gameOver) {
// 根据玩家行为决定结局
if (state.ritualProgress >= 3 && state.hasReturnedLocket) {
state.ending = "仪式中断";
} else {
state.ending = "逃脱";
}
state.gameOver = true;
}
// 收集所有物品
bool hasAllItems = true;
for (const auto& item : state.items) {
if (item.first != "神秘纸条" && !item.second) {
hasAllItems = false;
break;
}
}
if (hasAllItems && !state.gameOver) {
state.gameOver = true;
state.ending = "收藏家";
}
// 其他结局条件...
}
// 打印结局
void printEnding(const GameState& state) {
cout << "\n==============================================" << endl;
cout << " 游戏结束" << endl;
cout << "==============================================" << endl;
if (state.ending == "主动退出") {
cout << "你选择了放弃探索,也许这是最明智的决定。" << endl;
cout << "但你知道,那些失踪的学生将永远被困在那里,包括你的朋友。" << endl;
} else if (state.ending == "理智崩溃") {
cout << "你的理智最终被学校的恐怖所吞噬,成为了这所学校的一部分。" << endl;
cout << "有人看到你面无表情地在走廊上游荡,手里拿着一本日记。" << endl;
} else if (state.ending == "逃脱") {
cout << "经过" << state.day << "天的挣扎,你终于带着学校的秘密成功逃脱了。" << endl;
cout << "但当你回头望去,学校消失了,仿佛从未存在过。你手中的实验日志是唯一的证明。" << endl;
}
// 其他结局描述...
cout << "\n感谢游玩!" << endl;
}
// 拿起物品
void takeItem(const string& itemName, GameState& state, map<string, Location>& world) {
Location& currentLoc = world[state.currentLocation];
auto it = find(currentLoc.items.begin(), currentLoc.items.end(), itemName);
if (it != currentLoc.items.end()) {
state.items[itemName] = true;
currentLoc.items.erase(it);
cout << "你拿起了" << itemName << endl;
} else {
cout << "这里没有" << itemName << endl;
}
}
// 查看物品
void examineItem(const string& itemName, const GameState& state) {
if (!state.items.at(itemName)) {
cout << "你没有" << itemName << endl;
return;
}
if (itemName == "手电筒") {
cout << "一个军用级手电筒,看起来非常坚固。开关正常,电池充足。" << endl;
cout << "这是探索黑暗区域和密道的必备物品,也能在紧急情况下驱散敌人。" << endl;
} else if (itemName == "日记本") {
cout << "日记本上记录着一个女孩的日常,最后几页越来越混乱:\n"
<< "'他们在晚上带走学生...'\n"
<< "'化学教室下面有秘密...'" << endl;
}
// 其他物品描述...
}
// 其他函数实现保持不变...
bool usePass(GameState& state, const string& enemyName) {
if (state.passes > 0) {
state.passes--;
cout << "\n你使用了一张通行证,从" << enemyName << "手中逃脱了!" << endl;
return true;
}
return false;
}
void initializeEnemies(GameState& state) {
// 巡逻老师(走廊出没,中等威胁)
state.enemies["patrol_teacher"] = {
EnemyType::PATROL_TEACHER, "巡逻的数学老师",
30, 8, "corridor1", true, true, 3,
"曾经是普通教师,后被田中研究员洗脑,成为实验的守护者。"
};
// 日本研究员(实验室固定出现)
state.enemies["japanese"] = {
EnemyType::JAPANESE_RESEARCHER, "日本研究员田中",
50, 15, "secret_laboratory", false, true, 0,
"前731部队研究员,二战后继续在这所学校进行人体实验。"
};
// 其他敌人初始化...
}
void initializeNPCs(GameState& state) {
// NPC初始化...
}
void updateEnemies(GameState& state, map<string, Location>& world) {
// 敌人更新逻辑...
}
void updateNPCs(GameState& state, map<string, Location>& world) {
// NPC更新逻辑...
}
bool encounterEnemy(GameState& state, map<string, Location>& world, EnemyType type) {
// 敌人遭遇逻辑...
return true;
}
void handleEnemyCombat(const string& input, GameState& state, map<string, Location>& world, const string& enemyId) {
// 战斗处理逻辑...
}
void interactWithNPC(GameState& state, const string& npcId) {
// NPC交互逻辑...
}
void triggerPlotEvent(GameState& state, map<string, Location>& world) {
// 剧情触发逻辑...
}
void revealMemory(GameState& state, const string& memory) {
// 记忆揭示逻辑...
}
void checkRitualProgress(GameState& state) {
// 仪式进度检查...
}