//程序名称:RPG角色生成器
//程序功能:能进行简单的角色创建
//作者: Smile So Luck
//开发环境:Visual Stdio 6.0
#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
using namespace std;
int occupation_choice; //玩家所选择的职业的序号
class Baseinformation //基础类,保存角色的姓名,性别
{
protected:
char name[10];
string sex;
int sex_choice;
public:
void getBaseinformation();
friend class Output; //友元类,用于输出角色信息
};
//输入角色名和性别
void Baseinformation::getBaseinformation()
{
int i = 1;
cout<<" 欢迎进入游戏 ! "<<endl;
cout << "请输入您所要创建的角色姓名:";
cin >> name;
while (i)
{
cout << "选择角色性别:";
cout << "1.男 2.女" << endl;
cin >> sex_choice;
switch (sex_choice)
{
case 1:sex = "男"; i = 0; break;
case 2:sex = "女"; i = 0; break;
default:cout << "输入错误,请重新输入" << endl; break;
}
}
}
class Race :public Baseinformation //派生类,记录角色的种族、职业
{
protected:
string race;
string occupation;
int race_choice;
public:
void getRace();
friend class Output;
friend class File;
};
//选择种族和职业
void Race::getRace()
{
int i = 1;
while (i)
{
cout << "请选择种族:" << endl;
cout << "1.人类 2.精灵 3.兽人 4.矮人 5.元素" << endl;
cin >> race_choice;
switch (race_choice)
{
case 1:race = "人类"; i = 0; break;
case 2:race = "精灵"; i = 0; break;
case 3:race = "兽人"; i = 0; break;
case 4:race = "矮人"; i = 0; break;
case 5:race = "元素"; i = 0; break;
default:cout << "输入错误,请重新输入!" << endl; break;
}
}
while (1)
{
cout << "可以使用的职业:" << endl;
switch (race_choice)
{
case 1: cout << "1.狂战士 2.圣骑士 3.刺客 4.猎手 5.祭司 6.巫师" << endl; break;
case 2: cout << "3.刺客 4.猎手 5.祭司 6.巫师" << endl; break;
case 3: cout << "1.狂战士 4.猎手 5.祭司 " << endl; break;
case 4: cout << "1.狂战士 2.圣骑士 5.祭司 " << endl; break;
case 5: cout << "5.祭司 6.巫师" << endl; break;
}
cin >> occupation_choice;
if (race_choice == 1 && (occupation_choice >= 1 && occupation_choice <= 6)) break;
else if (race_choice == 2 && (occupation_choice > 2 && occupation_choice < 7)) break;
else if (race_choice == 3 && (occupation_choice == 1 || occupation_choice == 4 || occupation_choice == 5)) break;
else if (race_choice == 4 && (occupation_choice == 1 || occupation_choice == 2 || occupation_choice == 5)) break;
else if (race_choice == 5 && (occupation_choice > 4 && occupation_choice < 7)) break;
else cout << "输入错误,请重新输入" << endl;
}
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 = "祭司";
if (occupation_choice == 6) occupation = "巫师";
}
class Attribute :public Race //派生类,记录角色的属性
{
protected:
int strength;
int agility;
int physical;
int intelligence;
int wisdom;
int HP;
int MP;
public:
void getAttribute();
void getRandom(int a, int b, int c, int d, int e);
friend class Output;
friend class File;
};
// 随机生成每项属性的值,abcd为该属性的最小值,e为第五个属性的最大值
void Attribute::getRandom(int a, int b, int c, int d, int e)
{
int sum; //定义NUM为前4项属性之和
srand((unsigned)time(NULL));
do
{
strength = a + rand() %3; //随机生成的属性值与原本固定的属性值相差3
agility = b + rand() %3;
physical = c + rand() %3;
intelligence = d + rand() %3;
sum = strength + agility + physical + intelligence;
}
while (((100 - e) < sum) && (sum < 100)); //智慧的属性值等于100减去前4项属性之和
wisdom = 100 - sum;
HP = physical * 20; //生命值等于体力的20倍
MP = (wisdom + intelligence) * 10;
} //法力值等于智力和智慧之和的10倍
//根据选择的职业,向getRamdom传不同的最小值
void Attribute::getAttribute()
{
if (occupation_choice == 1) getRandom(37, 17, 27, 2, 2);
if (occupation_choice == 2) getRandom(22, 12, 27, 17, 7);
if (occupation_choice == 3) getRandom(17, 32, 17, 12, 7);
if (occupation_choice == 4) getRandom(12, 37, 12, 7, 17);
if (occupation_choice == 5) getRandom(12, 17, 12, 32, 12);
if (occupation_choice == 6) getRandom(7, 17, 7, 17, 37);
}
class Output //将角色属性输出到显示器上
{
public:
void show(Baseinformation &, Race &, Attribute &);
};
void Output::show(Baseinformation &t1, Race &t2, Attribute &t3)
{
cout << "-----------------------------" << endl;
cout << "姓名:" << t1.name << endl;
cout << "性别:" << t1.sex << endl;
cout << "种族:" << t2.race << endl;
cout << "职业:" << t2.occupation << endl;
cout << "力量:" << t3.strength << endl;
cout << "敏捷:" << t3.agility << endl;
cout << "体力:" << t3.physical << endl;
cout << "智力:" << t3.intelligence << endl;
cout << "智慧:" << t3.wisdom << endl;
cout << "生命值:" << t3.HP << endl;
cout << "法力值:" << t3.MP << endl;
cout << "-----------------------------" << endl;
}
int main()
{
Baseinformation player;
Race player_race;
Attribute player_att;
Output player_show;
int player_choice;
do
{
player.getBaseinformation();
player_race.getRace();
player_att.getAttribute();
player_show.show(player, player_race, player_att);
cout << endl;
cout << "对生成的角色是否满意?如不满意,可以重新生成" << endl;
cout << "0.满意 1.不满意" << endl;
cin >> player_choice;
} while (player_choice);
return 0;
}