#include #include #include #include #include #include using namespace std; //先建立基类,记录姓名性别 class game { public: char name[100]; //定义姓名 string sex; //定义性别 int sex_choice; //整型数据对性别进行选择 void show(); }; void game::show() { cout << "请输入您所要创建的角色姓名:"; cin >> name; int tag = 1; while (tag) { cout << "请选择角色性别:(0.男 1.女):" << endl;; cin >> sex_choice; switch (sex_choice) { case 0: sex = "男"; tag = 0; break; case 1: sex = "女"; tag = 0; break; default: cout << "输入错误,请重新输入" << endl; break; } } } //game2类记录种族和职业 class game2: public game { public: string race; //定义种族 string occupation; //定义职业 int race_choice; //种族选择 int occupation_choice; //职业选择 void Rrace(); }; void game2::Rrace() { //成员函数选择种族和职业 int tag = 1; while (tag) { cout << "请选择种族:0.人类 1.精灵 2.兽人 3.矮人 4.元素" << endl; cin >> race_choice; switch (race_choice) { case 0: race = "人类"; tag = 0; break; case 1: race = "精灵"; tag = 0; break; case 2: race = "兽人"; tag = 0; break; case 3: race = "矮人"; tag = 0; break; case 4: race = "元素"; tag = 0; break; default: cout << "输入错误,请重新输入!" << endl; break; } } while (1) { switch (race_choice) { case 1: cout << "选择您的职业:0.狂战士 1.圣骑士 2.刺客 3.猎手 4.祭司 5.巫师" << endl; break; case 2: cout << "选择您的职业:2.刺客 3.猎手 4.祭司 5.巫师" << endl; break; case 3: cout << "选择您的职业:0.狂战士 3.猎手 4.祭司 " << endl; break; case 4: cout << "选择您的职业:0.狂战士 1.圣骑士 4.祭司 " << endl; break; case 5: cout << "选择您的职业:4.祭司 5.巫师" << endl; break; } cin >> occupation_choice; if (race_choice == 0 && (occupation_choice >= 0 && occupation_choice <= 5)) break; else if (race_choice == 1 && (occupation_choice > 1 && occupation_choice < 6)) break; else if (race_choice == 2 && (occupation_choice == 0 || occupation_choice == 3 || occupation_choice == 4)) break; else if (race_choice == 3 && (occupation_choice == 0 || occupation_choice == 1 || occupation_choice == 4)) break; else if (race_choice == 4 && (occupation_choice > 3 && occupation_choice < 6)) break; else cout << "输入错误,请重新输入" << endl; } if (occupation_choice == 0) occupation = "狂战士"; if (occupation_choice == 1) occupation = "圣骑士"; if (occupation_choice == 2) occupation = "刺客"; if (occupation_choice == 3) occupation = "猎手"; if (occupation_choice == 4) occupation = "祭司"; if (occupation_choice == 5) occupation = "巫师"; } class Attribute : public game2 { //定义种族类的派生类,生成随机属性 public: int strength;//力量 int agility;//敏捷 int physical;//体力 int intelligence;//智力 int wisdom;//智慧 int HP;//生命值 int MP;//魔法值 void output_attribute();//取决于职业的属性确定函数 void rad(int rand1, int rand2, int rand3, int rand4, int rand5);//随机生成数函数 }; void Attribute::rad(int rand1, int rand2, int rand3, int rand4, int rand5) { int sum; srand((unsigned)time(NULL)); //随机属性值 do { strength = rand() % 10 + rand1; agility = rand() % 10 + rand2; physical = rand() % 10 + rand3; intelligence = rand() % 10 + rand4; wisdom = rand() % 10 + rand5; sum = strength + agility + physical + intelligence + wisdom; } while (sum != 100);//5项属性总值为100 HP = physical * 20;//生命值为体力的20倍 MP = (wisdom + intelligence) * 10;//魔法值为智力与智慧的10倍 } void Attribute::output_attribute() { //初始属性 if (occupation == "狂战士") rad(35, 15, 25, 5, 5); if (occupation == "圣骑士") rad(20, 10, 25, 15, 15); if (occupation == "刺客") rad(15, 30, 15, 10, 15); if (occupation == "猎手") rad(10, 35, 10, 5, 25); if (occupation == "祭司") rad(10, 25, 10, 30, 20); if (occupation == "巫师") rad(5, 15, 5, 15, 45); } class Battle : public Attribute { public: void fight(Attribute &me, Attribute &opponent); }; void Battle::fight(Attribute &me, Attribute &opponent) { cout << "战斗开始!" << endl; while (me.HP > 0 && opponent.HP > 0) { int attack = rand() % me.strength; int defense = rand() % opponent.agility; int damage = max(0, attack - defense); opponent.HP -= damage; cout << me.name << " 攻击 " << opponent.name << " 造成 " << damage << " 点伤害!" << endl; if (opponent.HP <= 0) { cout << opponent.name << " 被击败了!" << endl; break; } // 反击 attack = rand() % opponent.strength; defense = rand() % me.agility; damage = max(0, attack - defense); me.HP -= damage; cout << opponent.name << " 反击 " << me.name << " 造成 " << damage << " 点伤害!" << endl; if (me.HP <= 0) { cout << me.name << " 被击败了!" << endl; break; } } } int main() { srand((unsigned)time(NULL)); // 初始化随机数种子 Attribute player1, player2; player1.show(); // 创建第一个角色 player1.Rrace(); player1.output_attribute(); player2.show(); // 创建第二个角色 player2.Rrace(); player2.output_attribute(); Battle battle; battle.fight(player1, player2); // 两个角色对战 return 0; }