RPG角色生成器
1.功能描述:
几乎所有的RPG游戏(一种源自《龙与地下城》的游戏类型)在进入游戏时都会让用户自己来创建自己喜欢的角色。完成一个简化的创建游戏角色的程序。
2.游戏角色应有的属性
本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。 职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。生命值=体力*20。魔法值=(智力+智慧)*10。
3.职业限制
很多职业会限制某些种族选择,例如兽人不能就职圣骑士等等,种族和职业的限制表如下:
种族/职业 | 狂战士 | 圣骑士 | 刺客 | 猎手 | 祭司 | 巫师 |
人类 | 允许 | 允许 | 允许 | 允许 | 允许 | 允许 |
精灵 | 不允许 | 不允许 | 允许 | 允许 | 允许 | 允许 |
兽人 | 允许 | 不允许 | 不允许 | 允许 | 允许 | 不允许 |
矮人 | 允许 | 允许 | 不允许 | 不允许 | 允许 | 不允许 |
元素 | 不允许 | 不允许 | 不允许 | 不允许 | 允许 | 允许 |
所以在要求用户选择职业时,输出信息里面只能有用户所选择种族可以就职的职业。
4.初始属性
本题目要求力量、敏捷、体力、智力和智慧要求是随机值(利用随机数函数来取得随机数),但是五项属性的总和应该是100,并且应该和职业相关。例如狂战士的体力和力量就要比较高,而巫师需要较高的智力,而祭司则需要较高的智慧。
5.显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。
程序设计思路:
本次程序设计开发使用C++语言,因为使用到了面向对象的思想进行编程,同时用到了类的封装与继承。
设计的类为基础类(记录角色姓名和性别),角色的种族和职业派生类(记录角色的种族和职业),角色属性派生类(记录角色的各项属性),另外还有两个友元类,一个是输出显示信息友元类Output,一个是存储信息友元类File。
使用友元类的原因是当类B被声明为类A的友元类时,类B中的所有函数都可以访问类A中的所有成员,这样便于输出基类和派生类中的信息。
程序源代码:
//文件名:RPG游戏.CPP
//作者:Vector_山水之间
//学号:1508010333
//编译器:Visual C++ 6.0
//版本:1.5
//时间:2017.4.26
#include "iostream"
#include "string"
#include "ctime"
#include "fstream"
using namespace std;
int occupation_choice; //玩家所选择的职业的序号
class Baseinformation //基础类,保存角色的姓名,性别
{
protected:
char name[50]; //姓名
string sex; //性别
int sex_choice; //性别选项
public:
void getBaseinformation();
friend class Output; //友元类,用于输出角色信息
friend class File; //友元类,将角色信息保存到文档中
};
//输入角色名和性别
void Baseinformation::getBaseinformation()
{
int i = 1;
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; //输入选项符合要求,将i置为0,跳出while循环
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<<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; //前4项属性之和
srand((unsigned)time(NULL)); //设置随机数生成种子,保证每次生成的随机数不一样
do //属性的生成规则
{
strength = a + rand() % 10; //rand()%10获得范围0-9的一个随机数
agility = b + rand() % 10;
physical = c + rand() % 10;
intelligence = d + rand() % 10;
sum = strength + agility + physical + intelligence;
} while (sum>99); //限制智力这个属性值得最小值为1
wisdom = 100 - sum;
HP = physical * 20;
MP = (wisdom + intelligence) * 10;
}
//根据选择的职业,向getRamdom传不同的最小值
void Attribute::getAttribute()
{
if (occupation_choice == 1) getRandom(35, 15, 25, 0, 10);
if (occupation_choice == 2) getRandom(20, 10, 25, 15, 15);
if (occupation_choice == 3) getRandom(15, 30, 15, 10, 15);
if (occupation_choice == 4) getRandom(10, 35, 10, 5, 25);
if (occupation_choice == 5) getRandom(10, 25, 10, 30, 20);
if (occupation_choice == 6) getRandom(5, 15, 5, 15, 45);
}
class Output //将角色属性输出到显示器上
{
public:
void show(Baseinformation &, Race &, Attribute &);
};
void Output::show(Baseinformation &t1, Race &t2, Attribute &t3)
{
cout << "即将创建的角色的信息如下:"<<endl;
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;
}
class File //将角色信息保存到文档
{
public:
void file(Baseinformation &, Race &, Attribute &);
};
void File::file(Baseinformation &t1, Race &t2, Attribute &t3)
{
ofstream outfile;
outfile.open("data.txt", ios::trunc);
outfile << "姓名:" << t1.name << endl;
outfile << "性别:" << t1.sex << endl;
outfile << "种族:" << t2.race << endl;
outfile << "职业:" << t2.occupation << endl;
outfile << "力量:" << t3.strength << endl;
outfile << "敏捷:" << t3.agility << endl;
outfile << "体力:" << t3.physical << endl;
outfile << "智力:" << t3.intelligence << endl;
outfile << "智慧:" << t3.wisdom << endl;
outfile << "生命值:" << t3.HP << endl;
outfile << "法力值:" << t3.MP << endl;
}
int main()
{
cout<<" 欢迎来到游戏龙之谷"<<endl;
cout<<"请按照提示完成游戏角色的创建"<<endl<<endl;
Baseinformation player;
Race player_race;
Attribute player_att;
Output player_show;
File save;
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);
save.file(player, player_race, player_att);
cout<<"恭喜您,角色已经成功建立!"<<endl;
return 0;
}