#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <memory>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <algorithm>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace std;
// ========== 前置声明 ==========
class Character;
class Player;
class Monster;
class Boss;
class GameManager;
// ========== 状态效果系统 ==========
class StatusEffect {
public:
enum Type { None, Poison, Burn, Slow, Stun };
Type type;
int duration;
int value; // 伤害值或效果强度
StatusEffect(Type t = None, int d = 0, int v = 0)
: type(t), duration(d), value(v) {}
string GetName() const {
static const char* names[] = {"无", "中毒", "燃烧", "减速", "眩晕"};
return names[static_cast<int>(type)];
}
};
// ========== 技能系统 ==========
class Skill {
protected:
string name;
string damageType; // "physical", "fire", "ice"等
int baseDamage;
float hitChance; // 命中率
StatusEffect effect;
public:
Skill(const string& n, const string& dt, int bd, float hc, StatusEffect eff = {})
: name(n), damageType(dt), baseDamage(bd), hitChance(hc), effect(eff) {}
virtual int CalculateDamage() const {
return baseDamage + (rand() % 6); // 基础伤害+1d5波动
}
virtual void ApplyEffects(Character& target) const;
string GetName() const { return name; }
string GetDamageType() const { return damageType; }
float GetHitChance() const { return hitChance; }
StatusEffect GetEffect() const { return effect; }
};
// ========== 角色基类 ==========
class Character {
protected:
string name;
int hp;
int maxHp;
map<string, int> resistances; // 抗性百分比
vector<StatusEffect> statusEffects;
public:
Character(const string& n, int h) : name(n), hp(h), maxHp(h) {
// 默认抗性
resistances["physical"] = 0;
resistances["fire"] = 0;
resistances["ice"] = 0;
}
virtual ~Character() = default;
void TakeDamage(int damage, const string& damageType) {
int resistance = resistances.count(damageType) ? resistances[damageType] : 0;
int actualDamage = damage * (100 - resistance) / 100;
hp = max(0, hp - actualDamage);
cout << name << "受到" << actualDamage << "点" << damageType << "伤害!" << endl;
}
void AddStatusEffect(const StatusEffect& effect) {
statusEffects.push_back(effect);
cout << name << "获得状态:" << effect.GetName() << "(" << effect.duration << "回合)" << endl;
}
void UpdateStatusEffects() {
for (auto& effect : statusEffects) {
if (effect.type == StatusEffect::Poison || effect.type == StatusEffect::Burn) {
TakeDamage(effect.value, effect.type == StatusEffect::Burn ? "fire" : "poison");
}
effect.duration--;
}
// 移除结束的效果
statusEffects.erase(
remove_if(statusEffects.begin(), statusEffects.end(),
[](const StatusEffect& e) { return e.duration <= 0; }),
statusEffects.end()
);
}
bool IsDead() const { return hp <= 0; }
string GetName() const { return name; }
int GetHp() const { return hp; }
int GetMaxHp() const { return maxHp; }
virtual vector<shared_ptr<Skill>> GetSkills() const = 0;
virtual void Attack(Character& target) = 0;
};
// ========== 玩家类 ==========
class Player : public Character {
private:
map<string, int> inventory;
vector<shared_ptr<Skill>> skills;
public:
Player(const string& n, int h) : Character(n, h) {
// 初始化玩家技能
skills.push_back(make_shared<Skill>("普通攻击", "physical", 10, 0.9));
skills.push_back(make_shared<Skill>("火焰斩", "fire", 15, 0.8, StatusEffect(StatusEffect::Burn, 3, 3)));
skills.push_back(make_shared<Skill>("寒冰箭", "ice", 12, 0.85, StatusEffect(StatusEffect::Slow, 2, 0)));
}
void AddItem(const string& item) {
inventory[item]++;
cout << "获得物品:" << item << endl;
}
bool HasItem(const string& item) const {
return inventory.count(item) && inventory.at(item) > 0;
}
void UseItem(const string& item) {
if (HasItem(item)) {
inventory[item]--;
if (item == "治疗药水") {
hp = min(maxHp, hp + 30);
cout << "恢复30点生命值!" << endl;
}
}
}
vector<shared_ptr<Skill>> GetSkills() const override { return skills; }
void Attack(Character& target) override {
cout << "选择技能:" << endl;
for (size_t i = 0; i < skills.size(); ++i) {
cout << i+1 << ". " << skills[i]->GetName() << endl;
}
int choice;
while (true) {
cin >> choice;
if (choice >= 1 && choice <= skills.size()) break;
cout << "无效输入,请重新选择!" << endl;
}
auto& skill = skills[choice-1];
if (rand() / (float)RAND_MAX <= skill->GetHitChance()) {
int damage = skill->CalculateDamage();
target.TakeDamage(damage, skill->GetDamageType());
if (skill->GetEffect().type != StatusEffect::None) {
target.AddStatusEffect(skill->GetEffect());
}
} else {
cout << "技能未命中!" << endl;
}
}
void PrintStatus() const {
cout << "\n=== 玩家状态 ===" << endl;
cout << "HP: " << hp << "/" << maxHp << endl;
cout << "状态效果:";
for (const auto& eff : statusEffects) {
cout << eff.GetName() << "(" << eff.duration << ") ";
}
cout << "\n物品:";
for (const auto& item : inventory) {
if (item.second > 0) cout << item.first << "x" << item.second << " ";
}
cout << "\n===============\n" << endl;
}
};
// ========== BOSS类 ==========
class Boss : public Character {
private:
int phase;
vector<tuple<int, string, map<string, int>>> phases; // 血量阈值,阶段描述,抗性变化
public:
Boss(const string& n, int h, const vector<tuple<int, string, map<string, int>>>& ph)
: Character(n, h), phase(1), phases(ph) {}
void EnterNextPhase() {
if (phase < phases.size()) {
phase++;
auto& [threshold, desc, resists] = phases[phase-1];
resistances = resists;
cout << "\n=== " << name << "进入阶段" << phase << " ===" << endl;
cout << desc << endl;
}
}
void Attack(Character& target) override {
// 简单AI:根据阶段选择不同攻击模式
int damage = 15 + phase * 5;
string damageType = phase == 1 ? "physical" : (phase == 2 ? "fire" : "ice");
target.TakeDamage(damage, damageType);
// 30%概率附加状态
if (rand() % 100 < 30) {
StatusEffect eff = phase == 2 ? StatusEffect(StatusEffect::Burn, 2, 3)
: StatusEffect(StatusEffect::Slow, 3, 0);
target.AddStatusEffect(eff);
}
// 检查阶段转换
if (phase < phases.size() && hp <= get<0>(phases[phase])) {
EnterNextPhase();
}
}
vector<shared_ptr<Skill>> GetSkills() const override { return {}; } // BOSS不使用玩家技能系统
void PrintStatus() const {
cout << "\n=== " << name << "状态 ===" << endl;
cout << "HP: " << hp << "/" << maxHp << endl;
cout << "阶段: " << phase << "/" << phases.size() << endl;
cout << "===============\n" << endl;
}
};
// ========== 游戏管理器 ==========
class GameManager {
private:
shared_ptr<Player> player;
vector<shared_ptr<Boss>> bosses;
int currentLevel;
public:
GameManager() {
srand(time(nullptr));
player = make_shared<Player>("勇者", 100);
InitializeBosses();
currentLevel = 0;
}
void InitializeBosses() {
// 伊弗利特 (3阶段)
vector<tuple<int, string, map<string, int>>> ifritPhases = {
{300, "伊弗利特的皮肤开始龟裂,岩浆喷涌而出!", {{"fire", 50}, {"ice", -20}}},
{150, "伊弗利特召唤熔岩护盾!", {{"fire", 80}, {"physical", 30}}},
{0, "伊弗利特狂暴化!地面开始崩塌!", {{"fire", 100}, {"ice", -30}}}
};
bosses.push_back(make_shared<Boss>("炎狱之王·伊弗利特", 500, ifritPhases));
// 更多BOSS可以在此添加...
}
void StartGame() {
cout << "===== 终焉回响 - 游戏开始 =====" << endl;
while (currentLevel < bosses.size() && !player->IsDead()) {
StartLevel(currentLevel);
currentLevel++;
}
if (player->IsDead()) {
cout << "游戏结束!你失败了..." << endl;
} else {
cout << "恭喜通关!你拯救了世界!" << endl;
}
}
void StartLevel(int level) {
auto& boss = bosses[level];
cout << "\n\n====== 第" << level+1 << "关: 面对" << boss->GetName() << "! ======\n" << endl;
// 战斗循环
while (!player->IsDead() && !boss->IsDead()) {
player->PrintStatus();
boss->PrintStatus();
// 玩家回合
cout << "\n=== 你的回合 ===" << endl;
cout << "1. 攻击 2. 使用物品" << endl;
int choice;
cin >> choice;
if (choice == 1) {
player->Attack(*boss);
} else if (choice == 2) {
UseItemMenu();
}
// BOSS回合
if (!boss->IsDead()) {
cout << "\n=== " << boss->GetName() << "的回合 ===" << endl;
boss->Attack(*player);
}
// 状态效果结算
player->UpdateStatusEffects();
boss->UpdateStatusEffects();
}
if (boss->IsDead()) {
cout << "你击败了" << boss->GetName() << "!" << endl;
player->AddItem("BOSS核心");
}
}
void UseItemMenu() {
cout << "选择物品:1.治疗药水 2.其他 (开发中)" << endl;
int choice;
cin >> choice;
if (choice == 1) {
player->UseItem("治疗药水");
}
}
};
// ========== 主函数 ==========
int main() {
GameManager game;
game.StartGame();
return 0;
}
好不容易憋出了一个小程序,结果在下载上出问题了