#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <limits>
using namespace std;
// 清屏函数,兼容不同操作系统
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// 修为境界定义
enum Realm {
MORTAL, // 凡人
FOUNDATION, // 筑基
GOLDEN, // 金丹
SOUL, // 元婴
TRANSFORMATION,// 化神
ASCENSION // 飞升
};
// 物品类型
enum ItemType {
CONSUMABLE, // 消耗品
EQUIPMENT, // 装备
MATERIAL // 材料
};
// 技能类型
enum SkillType {
ATTACK, // 攻击技能
DEFENSE, // 防御/增益技能
HEAL // 治疗技能
};
// int转string函数,兼容旧编译器
string intToString(int num) {
stringstream ss;
ss << num;
return ss.str();
}
// 技能类声明
class Skill {
private:
string name;
string description;
SkillType type;
int cost; // 灵力消耗
int power; // 技能威力/效果值
int cooldown; // 冷却时间(回合)
int currentCooldown; // 当前剩余冷却
public:
Skill(string n, string d, SkillType t, int c, int p, int cd)
: name(n), description(d), type(t), cost(c), power(p),
cooldown(cd), currentCooldown(0) {}
};
// 物品基类
class Item {
protected:
string name;
string description;
ItemType type;
int value; // 物品价值(用于交易)
public:
Item(string n, string d, ItemType t, int v)
: name(n), description(d), type(t), value(v) {}
};
// 消耗品(丹药等)
class Consumable : public Item {
private:
int cultivationGain; // 增加修为
int healthGain; // 恢复生命
int spiritGain; // 恢复灵力
public:
Consumable(string n, string d, int v, int cg, int hg, int sg = 0)
: Item(n, d, CONSUMABLE, v), cultivationGain(cg), healthGain(hg), spiritGain(sg) {}
};
// 装备
class Equipment : public Item {
private:
int attackBonus; // 攻击加成
int defenseBonus; // 防御加成
int spiritBonus; // 灵力加成
bool isEquipped; // 是否装备
public:
Equipment(string n, string d, int v, int ab, int db, int sb = 0)
: Item(n, d, EQUIPMENT, v), attackBonus(ab), defenseBonus(db),
spiritBonus(sb), isEquipped(false) {}
};
// 材料(用于突破、交易等)
class Material : public Item {
private:
int breakthroughBonus; // 提升突破成功率
public:
Material(string n, string d, int v, int bb)
: Item(n, d, MATERIAL, v), breakthroughBonus(bb) {}
};
// 门派
class Sect {
private:
string name;
string description;
int contribution; // 门派贡献度
public:
Sect(string n, string d) : name(n), description(d), contribution(0) {}
};
// 怪物结构
struct Monster {
string name;
string title; // 怪物称号,增加多样性
int health;
int maxHealth;
int attack;
int defense;
int dodgeRate;
int realmLevel; // 对应玩家境界等级
Item* drop;
int expReward; // 击杀经验/修为奖励
};
// 角色类
class Cultivator {
private:
string name;
Realm realm;
int cultivation; // 当前修为值
int maxCultivation; // 当前境界最大修为
int health;
int maxHealth;
int attack;
int defense;
int spirit; // 灵力(用于释放技能)
int maxSpirit; // 最大灵力
int dodgeRate; // 闪避率(百分比)
vector<Item*> inventory;
int inventoryCapacity; // 背包容量
vector<Skill*> skills; // 技能列表
vector<string> realmNames;
Sect* sect; // 所属门派
int reputation; // 声望
int turnBuff; // 回合增益(如防御提升)
public:
// 构造函数
Cultivator(string n);
};
// 门派菜单函数声明
void showSectMenu(Cultivator& player);
// 根据玩家属性生成稍强的怪物
Monster* generateChallengingMonster(const Cultivator& player) {
// 基于玩家属性生成稍强的怪物
int playerRealm = (int)player.getRealm();
int monsterRealm = min(playerRealm + 1, 4); // 怪物境界比玩家高1级或同等级,最高化神境
}
// 技能类方法实现
Skill::Skill(ifstream& saveFile) {
getline(saveFile, name);
getline(saveFile, description);
}
void Skill::save(ofstream& saveFile) const {
saveFile << name << endl;
saveFile << description << endl;
saveFile << type << endl;
saveFile << cost << endl;
saveFile << power << endl;
saveFile << cooldown << endl;
saveFile << currentCooldown << endl;
}
bool Skill::use(Cultivator& user, Monster* target) {
// 检查灵力是否足够
if (!user.consumeSpirit(cost)) {
return false;
}
}
// 物品类方法实现
Item::Item(ifstream& saveFile) {
getline(saveFile, name);
getline(saveFile, description);
}
// 消耗品类方法实现
Consumable::Consumable(ifstream& saveFile) : Item(saveFile) {
saveFile >> cultivationGain >> healthGain >> spiritGain;
}
void Consumable::use(Cultivator& user) {
cout << user.getName() << "使用了" << name << "!\n";
user.setHealth(min(user.getHealth() + healthGain, user.getMaxHealth()));
user.restoreSpirit(spiritGain);
// 修为增加
}
void Consumable::save(ofstream& saveFile) const {
saveFile << name << endl;
saveFile << description << endl;
saveFile << type << endl;
saveFile << value << endl;
saveFile << cultivationGain << endl;
saveFile << healthGain << endl;
saveFile << spiritGain << endl;
}
// 装备类方法实现
Equipment::Equipment(ifstream& saveFile) : Item(saveFile) {
saveFile >> attackBonus >> defenseBonus >> spiritBonus >> isEquipped;
}
void Equipment::use(Cultivator& user) {
if (isEquipped) {
cout << user.getName() << "卸下了" << name << "!\n";
isEquipped = false;
} else {
cout << user.getName() << "装备了" << name << "!\n";
isEquipped = true;
}
}
void Equipment::save(ofstream& saveFile) const {
saveFile << name << endl;
saveFile << description << endl;
saveFile << type << endl;
saveFile << value << endl;
saveFile << attackBonus << endl;
saveFile << defenseBonus << endl;
saveFile << spiritBonus << endl;
saveFile << isEquipped << endl;
}
// 材料类方法实现
Material::Material(ifstream& saveFile) : Item(saveFile) {
saveFile >> breakthroughBonus;
}
void Material::use(Cultivator& user) {
cout << user.getName() << "使用" << name << "辅助突破!\n";
user.breakthrough(breakthroughBonus);
}
void Material::save(ofstream& saveFile) const {
saveFile << name << endl;
saveFile << description << endl;
saveFile << type << endl;
saveFile << value << endl;
saveFile << breakthroughBonus << endl;
}
// 门派类方法实现
Sect::Sect(ifstream& saveFile) {
getline(saveFile, name);
getline(saveFile, description);
saveFile >> contribution;
}
void Sect::save(ofstream& saveFile) const {
saveFile << name << endl;
saveFile << description << endl;
saveFile << contribution << endl;
}
// 角色类方法实现
Cultivator::Cultivator(string n) {
name = n;
realm = MORTAL;
cultivation = 0;
maxCultivation = 100;
maxHealth = 100;
health = maxHealth;
attack = 10;
defense = 5;
maxSpirit = 50;
spirit = maxSpirit;
dodgeRate = 5;
inventoryCapacity = 10;
sect = NULL;
reputation = 0;
turnBuff = 0;
}
Cultivator::Cultivator(ifstream& saveFile) {
// 读取基本属性
getline(saveFile, name);
}
Cultivator::~Cultivator() {
// 清理物品内存
for (size_t i = 0; i < inventory.size(); ++i) {
delete inventory[i];
}
// 清理技能内存
for (size_t i = 0; i < skills.size(); ++i) {
delete skills[i];
}
delete sect;
}
bool Cultivator::save(const string& filename) const {
ofstream saveFile(filename.c_str());
if (!saveFile.is_open()) {
return false;
}
}
bool Cultivator::load(const string& filename) {
// 清除现有数据
for (size_t i = 0; i < inventory.size(); ++i) {
delete inventory[i];
}
inventory.clear();
}
void Cultivator::cultivate() {
// 基础修炼收益
int baseGain = rand() % 20 + 5;
// 根据境界提升修炼效率
float realmBonus = 1.0f + (int)realm * 0.2f;
// 门派加成
float sectBonus = sect ? 1.2f : 1.0f;
}
bool Cultivator::breakthrough(int materialBonus) {
if (realm == ASCENSION) return false;
if (cultivation < maxCultivation) {
cout << "修为不足,无法突破!\n";
return false;
}