先分析一下角色有哪些属性,一个游戏角色应该要有姓名,性别,种族,职业,同时还要有因为种族和职业的不同而产生的身体各项数据的不同。在我设计的程序中,我首先设置了两个基类,一个用来管理姓名和性别,另一个用来管理各项身体数据。最后的角色类继承于两个基类。
以下是UML类图
(这个版本没有判断用户是否对产生的数据是否满意部分,而且也没有文件存储部分)
完整代码:
/*
* author:zhangpeng
* time:2019.4.22
* 程序分析:本程序中有三个类Figure,Data,RaceAndOccupation。RaceAndOccupation是Figure和Data类的子类
* 其中Figure中包含任务的姓名,性别。Data类中包含任务的各种属性如生命值等。
*
*
*
*
*
*/
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
static string allsex[2] = {"男","女"};
static string Race[5] = {"人类","矮人","精灵","兽人","元素"};
static string Occupation[6] = {"狂战士","圣骑士","刺客","猎手","祭司","巫师"};
static int flag1 = 0; //用来判断是哪个种族
static int flag2 = 0; //用来判断是哪个职业
class Figuer //角色的基类
{
private:
char name[50]; //姓名
string sex; //性别
public:
void setNameSex()
{
cout<<"请输入姓名:"<<endl;
cin >> name;
while (strlen(name) > 50)
{
cout << "重新输入姓名:" << endl;
cin >> name;
}
cout<<"请选择性别(0.男/1.女)"<<endl;
int flag = 0;
cin >> flag;
while (flag < 0 || flag > 1)
{
cout << "请重新输入" << endl;
cin >> flag;
}
sex = allsex[flag];
}
void display() //显示姓名和性别
{
cout << "===================================================" << endl;
cout << "姓名" << name << endl;
cout << "===================================================" << endl;
cout << "性别" << sex << endl;
}
};
class Data
{
private:
int strengths; //力量
int swift; //敏捷
int energy; //体力
int intelligence; //智力
int wisdom; //智慧
int num_life; //生命值
int num_magic; //魔法值
public:
void setdata(int occupation) //产生各种数据
{
switch (occupation)
{
case 0:
{
srand((int)time(NULL));
strengths = rand() % (30 - 20 + 1) + 20;
swift = rand() % (20 - 10 + 1) + 10;
energy = rand() % (30 - 25 + 1) + 25;
intelligence = rand() % (20 - 15 + 1) + 15;
wisdom = 100 - strengths - swift - energy - intelligence;
num_life = energy * 20;
num_magic = (intelligence + wisdom) * 10;
break;
}
case 1:
{
srand((int)time(NULL));
strengths = rand() % (30 - 20 + 1) + 20;
swift = rand() % (20 - 10 + 1) + 10;
energy = rand() % (30 - 25 + 1) + 25;
intelligence = rand() % (20 - 15 + 1) + 15;
wisdom = 100 - strengths - swift - energy - intelligence;
num_life = energy * 20;
num_magic = (intelligence + wisdom) * 10;
break;
}
case 2:
{
srand((int)time(NULL));
strengths = rand() % (25 - 15 + 1) + 15;
swift = rand() % (40 - 30 + 1) + 30;
energy = rand() % (20 - 15 + 1) + 15;
intelligence = rand() % (10 - 0 + 1) + 0;
wisdom = 100 - strengths - swift - energy - intelligence;
num_life = energy * 20;
num_magic = (intelligence + wisdom) * 10;
break;
}
case 3:
{
srand((int)time(NULL));
strengths = rand() % (15 - 10 + 1) + 10;
swift = rand() % (45 - 35 + 1) + 35;
energy = rand() % (20 - 10 + 1) + 10;
intelligence = rand() % (10 - 0 + 1) + 0;
wisdom = 100 - strengths - swift - energy - intelligence;
num_life = energy * 20;
num_magic = (intelligence + wisdom) * 10;
break;
}
case 4:
{
srand((int)time(NULL));
strengths = rand() % (20 - 10 + 1) + 10;
swift = rand() % (20 - 15 + 1) + 15;
energy = rand() % (15 - 10 + 1) + 10;
intelligence = rand() % (40 - 30 + 1) + 30;
wisdom = 100 - strengths - swift - energy - intelligence;
num_life = energy * 20;
num_magic = (intelligence + wisdom) * 10;
break;
case 5:
{
srand((int)time(NULL));
strengths = rand() % (15 - 15 + 1) + 15;
swift = rand() % (20 - 15 + 1) + 15;
energy = rand() % (10 - 5 + 1) + 5;
intelligence = rand() % (25 - 20 + 1) + 20;
wisdom = 100 - strengths - swift - energy - intelligence;
num_life = energy * 20;
num_magic = (intelligence + wisdom) * 10;
break;
}
}
default:;
}
}
void data_show()
{
cout << "===================================================" << endl;
cout << "力量:" << strengths << endl;
cout << "===================================================" << endl;
cout << "敏捷:" << swift << endl;
cout << "===================================================" << endl;
cout << "体力:" << energy << endl;
cout << "===================================================" << endl;
cout << "智力:" << intelligence << endl;
cout << "===================================================" << endl;
cout << "智慧:" << wisdom << endl;
cout << "===================================================" << endl;
cout << "魔法:" << num_magic << endl;
cout << "===================================================" << endl;
cout << "生命值:" << num_life << endl;
cout << "===================================================" << endl;
}
};
class RaceAndOccupation:public Figuer,public Data
{
private:
string race; //种族
string occupation; //职业
public:
RaceAndOccupation()
{
setNameSex(); //输入姓名和性别
setRaceAndOccupation(); //输入种族和职业
setdata(flag2); //产生身体素质数据
show();
}
~RaceAndOccupation() //析构函数
{
}
void show()
{
display(); //调用基类的显示函数
cout << "===================================================" << endl;
cout<<"种族:"<<race<<endl;
cout << "===================================================" << endl;
cout<<"职业:"<<occupation<<endl;
data_show();
}
void setRaceAndOccupation() //选择种族和职业
{
cout << "请输入种族(0.人类1.矮人2.精灵3.兽人4.元素)" << endl;
cin>>flag1;
race = Race[flag1];
switch(flag1)
{
case 0:
while(true)
{
cout<<"请输入职业"<<endl;
cout<<"0.狂战士1.圣骑士2.刺客3.猎手4.祭司5.巫师"<<endl;
cin>>flag2;
if(0 <= flag2 && flag2 <= 6)
{
occupation = Occupation[flag2];
break;
}
cout<<"重新输入"<<endl;
}
break;
case 1:
while(true)
{
cout<<"请输入职业"<<endl;
cout<<"0.狂战士1.圣骑士4.祭司5.巫师"<<endl;
cin>>flag2;
if((flag2 != 2 || flag2 != 3) && (flag2 >= 0 && flag2 <= 6))
{
occupation = Occupation[flag2];
break;
}
cout<<"重新输入"<<endl;
}
break;
case 2:
while(true)
{
cout<<"请输入职业"<<endl;
cout<<"1.圣骑士2.刺客3.猎手5.巫师"<<endl;
cin>>flag2;
if((flag2 != 0 || flag2 != 1) && (flag2 >= 0 && flag2 <= 6))
{
occupation = Occupation[flag2];
break;
}
cout<<"重新输入"<<endl;
}
break;
case 3:
while(true)
{
cout<<"请输入职业"<<endl;
cout<<"2.刺客3.猎手4.祭司"<<endl;
cin>>flag2;
if((flag2 != 0 || flag2 != 1 || flag2 != 5) && (flag2 >= 0 && flag2 <= 6))
{
occupation = Occupation[flag2];
break;
}
cout<<"重新输入"<<endl;
}
break;
case 4:
while(true)
{
cout<<"请输入职业"<<endl;
cout<<"4.祭司5.巫师"<<endl;
cin>>flag2;
if(flag2 == 4 || flag2 == 5)
{
occupation = Occupation[flag2];
break;
}
cout<<"重新输入"<<endl;
}
break;
}
}
};
int main(int argc, char const *argv[])
{
RaceAndOccupation r1;
system("pause");
return 0;
}
以下是包含用户满意判断和文件存储
/*
* author:张鹏
* time:2019.4.26
* version:1.1
* 程序分析:本程序中有三个类Figure,Data,RaceAndOccupation。RaceAndOccupation是Figure和Data类的子类
* 其中Figure中包含任务的姓名,性别。Data类中包含任务的各种属性如生命值等。
*
*
*
*
*
*/
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<fstream>
using namespace std;
string allsex[2] = {"男","女"};
string Race[5] = {"人类","矮人","精灵","兽人","元素"};
string Occupation[6] = {"狂战士","圣骑士","刺客","猎手","祭司","巫师"};
int flag1 = 0; //用来判断是哪个种族
int flag2 = 0; //用来判断是哪个职业
ofstream file1;
class Figuer //角色的基类
{
private:
char name[50]; //姓名
string sex; //性别
public:
char* get_name() //获得姓名
{
return name;
}
string get_sex()
{
return sex;
}
void setNameSex()
{
cout<<"请输入姓名:"<<endl;
cin >> name;
while (strlen(name) > 50)
{
cout << "重新输入姓名:" << endl;
cin >> name;
}
cout<<"请选择性别(0.男/1.女)"<<endl;
int flag = 0;
cin >> flag;
while (flag < 0 || flag > 1)
{
cout << "请重新输入" << endl;
cin >> flag;
}
sex = allsex[flag];
}
void display() //显示姓名和性别
{
cout << "===================================================" << endl;
cout << "姓名" << name << endl;
cout << "===================================================" << endl;
cout << "性别" << sex << endl;
}
};
class Data
{
private:
int strengths; //力量
int swift; //敏捷
int energy; //体力
int intelligence; //智力
int wisdom; //智慧
int num_life; //生命值
int num_magic; //魔法值
public:
int get_strength() //获得力量值
{
return strengths;
}
int get_swift() //获得敏捷值
{
return swift;
}
int get_energy() //获得精力
{
return energy;
}
int get_intelligence() //获得智力
{
return intelligence;
}
int get_wisdom() //获得智慧
{
return wisdom;
}
int get_numlife() //获得生命值
{
return num_life;
}
int get_nummagic() //获得魔法值
{
return num_magic;
}
void setdata(int occupation) //产生各种数据
{
switch (occupation)
{
case 0:
{
srand((int)time(NULL));
strengths = rand() % (30 - 20 + 1) + 20;
swift = rand() % (20 - 10 + 1) + 10;
energy = rand() % (30 - 25 + 1) + 25;
intelligence = rand() % (20 - 15 + 1) + 15;
wisdom = 100 - strengths - swift - energy - intelligence;
num_life = energy * 20;
num_magic = (intelligence + wisdom) * 10;
break;
}
case 1:
{
srand((int)time(NULL));
strengths = rand() % (30 - 20 + 1) + 20;
swift = rand() % (20 - 10 + 1) + 10;
energy = rand() % (30 - 25 + 1) + 25;
intelligence = rand() % (20 - 15 + 1) + 15;
wisdom = 100 - strengths - swift - energy - intelligence;
num_life = energy * 20;
num_magic = (intelligence + wisdom) * 10;
break;
}
case 2:
{
srand((int)time(NULL));
strengths = rand() % (25 - 15 + 1) + 15;
swift = rand() % (40 - 30 + 1) + 30;
energy = rand() % (20 - 15 + 1) + 15;
intelligence = rand() % (10 - 0 + 1) + 0;
wisdom = 100 - strengths - swift - energy - intelligence;
num_life = energy * 20;
num_magic = (intelligence + wisdom) * 10;
break;
}
case 3:
{
srand((int)time(NULL));
strengths = rand() % (15 - 10 + 1) + 10;
swift = rand() % (45 - 35 + 1) + 35;
energy = rand() % (20 - 10 + 1) + 10;
intelligence = rand() % (10 - 0 + 1) + 0;
wisdom = 100 - strengths - swift - energy - intelligence;
num_life = energy * 20;
num_magic = (intelligence + wisdom) * 10;
break;
}
case 4:
{
srand((int)time(NULL));
strengths = rand() % (20 - 10 + 1) + 10;
swift = rand() % (20 - 15 + 1) + 15;
energy = rand() % (15 - 10 + 1) + 10;
intelligence = rand() % (40 - 30 + 1) + 30;
wisdom = 100 - strengths - swift - energy - intelligence;
num_life = energy * 20;
num_magic = (intelligence + wisdom) * 10;
break;
case 5:
{
srand((int)time(NULL));
strengths = rand() % (15 - 15 + 1) + 15;
swift = rand() % (20 - 15 + 1) + 15;
energy = rand() % (10 - 5 + 1) + 5;
intelligence = rand() % (25 - 20 + 1) + 20;
wisdom = 100 - strengths - swift - energy - intelligence;
num_life = energy * 20;
num_magic = (intelligence + wisdom) * 10;
break;
}
}
default:;
}
}
void data_show()
{
cout << "===================================================" << endl;
cout << "力量:" << strengths << endl;
cout << "===================================================" << endl;
cout << "敏捷:" << swift << endl;
cout << "===================================================" << endl;
cout << "体力:" << energy << endl;
cout << "===================================================" << endl;
cout << "智力:" << intelligence << endl;
cout << "===================================================" << endl;
cout << "智慧:" << wisdom << endl;
cout << "===================================================" << endl;
cout << "魔法:" << num_magic << endl;
cout << "===================================================" << endl;
cout << "生命值:" << num_life << endl;
cout << "===================================================" << endl;
}
};
class RaceAndOccupation:public Figuer,public Data
{
private:
string race; //种族
string occupation; //职业
public:
RaceAndOccupation()
{
setNameSex(); //输入姓名和性别
setRaceAndOccupation(); //输入种族和职业
setdata(flag2); //产生身体素质数据
show();
satisfaction(); //询问用户是否满意
access(); //存储信息
}
void access() //存储信息
{
file1.open("龙与地下城.txt",ios::app);
file1 << "姓名:" << get_name() << endl;
file1 << "性别:" << get_sex() << endl;
file1 << "种族:" << race << endl;
file1 << "职业:" << occupation << endl;
file1 << "力量:" << get_strength() << endl;
file1 << "敏捷:" << get_swift() << endl;
file1 << "体力:" << get_energy() << endl;
file1 << "智力:" << get_intelligence() << endl;
file1 << "智慧:" << get_wisdom() << endl;
file1 << "生命值:" << get_numlife() << endl;
file1 << "魔法值:" << get_nummagic() << endl;
}
void satisfaction() //询问用户是否满意
{
int satis;
cout << "是否满意:(0表示不满意/任意字符表示满意)" << endl;
cin >> satis;
while (satis == 0)
{
setNameSex();
setRaceAndOccupation();
setdata(flag2);
show();
satisfaction();
break;
}
}
~RaceAndOccupation() //析构函数
{
}
void show()
{
display(); //调用基类的显示函数
cout << "===================================================" << endl;
cout<<"种族:"<<race<<endl;
cout << "===================================================" << endl;
cout<<"职业:"<<occupation<<endl;
data_show();
}
void setRaceAndOccupation() //选择种族和职业
{
cout << "请输入种族(0.人类1.矮人2.精灵3.兽人4.元素)" << endl;
cin>>flag1;
race = Race[flag1];
switch(flag1)
{
case 0:
while(true)
{
cout<<"请输入职业"<<endl;
cout<<"0.狂战士1.圣骑士2.刺客3.猎手4.祭司5.巫师"<<endl;
cin>>flag2;
if(0 <= flag2 && flag2 <= 6)
{
occupation = Occupation[flag2];
break;
}
cout<<"重新输入"<<endl;
}
break;
case 1:
while(true)
{
cout<<"请输入职业"<<endl;
cout<<"0.狂战士1.圣骑士4.祭司5.巫师"<<endl;
cin>>flag2;
if((flag2 != 2 || flag2 != 3) && (flag2 >= 0 && flag2 <= 6))
{
occupation = Occupation[flag2];
break;
}
cout<<"重新输入"<<endl;
}
break;
case 2:
while(true)
{
cout<<"请输入职业"<<endl;
cout<<"1.圣骑士2.刺客3.猎手5.巫师"<<endl;
cin>>flag2;
if((flag2 != 0 || flag2 != 1) && (flag2 >= 0 && flag2 <= 6))
{
occupation = Occupation[flag2];
break;
}
cout<<"重新输入"<<endl;
}
break;
case 3:
while(true)
{
cout<<"请输入职业"<<endl;
cout<<"2.刺客3.猎手4.祭司"<<endl;
cin>>flag2;
if((flag2 != 0 || flag2 != 1 || flag2 != 5) && (flag2 >= 0 && flag2 <= 6))
{
occupation = Occupation[flag2];
break;
}
cout<<"重新输入"<<endl;
}
break;
case 4:
while(true)
{
cout<<"请输入职业"<<endl;
cout<<"4.祭司5.巫师"<<endl;
cin>>flag2;
if(flag2 == 4 || flag2 == 5)
{
occupation = Occupation[flag2];
break;
}
cout<<"重新输入"<<endl;
}
break;
}
}
};
int main(int argc, char const *argv[])
{
RaceAndOccupation r1;
system("pause");
return 0;
}