上机实验
#include<iostream>
#include<string>
using namespace std;
int cp; //定义全局变量,玩家所选择的职业序号
/*定义函数的基础类Cosplay,保存角色的姓名*/
class Cosplay
{
friend class Out; //定义友元类,输出游戏角色信息
protected:
char name[50]; //姓名最多为50个字符长度
string sex; //性别 男性 女性
int sexs; //性别 0 1
public:
void getCosplay();
};
void Cosplay::getCosplay()
{
int a = 1;
cout<<" 欢迎 "<<endl;
cout<<"请按照喜好创建游戏角色"<<endl;
cout << "请输入您游戏角色的姓名:";
cin >> name;
while (a) //性别选项为0 1,输入其他的数字时会提示“性别选择出错,请重新选择”
{
cout << "请选择您游戏的性别 0:男性 1:女性";
cin >> sexs;
switch (sexs)
{
case 0: sex = "男性";a = 0; break;
case 1: sex = "女性";a = 0; break;
default:
cout << "性别选择出错,请重新选择" << endl; break;
}
}
}
class Race :public Cosplay
{
friend class Out;
protected:
string race; //种族
string position; //职业
int races; //种族的选择,在0-4之间
public:
void getRace();
};
void Race::getRace()
{
int b = 1;
while (b)
{ cout << "请在之间选择您游戏角色的种族0:人类 1:精灵 2:兽人 3:矮人 4:元素"<<endl;
cin >> races;switch (races)
{
case 0:race = "人类;b = 0; break; //种族是人类
case 1: race = "精灵";b = 0; break; //种族是精灵
case 2: race = "兽人";b = 0; break; //种族是兽人
case 3: race = "矮人";b = 0; break; //种族是矮人
case 4: race = "元素";b = 0; break; //种族是元素
default:
cout << "您选择的种族错误!请重新输入" << endl;break;
}}
while (1)
{
cout << "请在0-5之间选择您游戏角色的职业 0:狂战士 1:圣骑士 2:刺客 3:猎手 4:祭司 5:巫师" << endl;
switch (races) //根据种族选择,选择职业
{case 0:cout << "您所选择的种族是人类,请在下列选项中选择职业! 0 1 2 3 4 5 " << endl;break;
case 1:cout << " 您所选择的种族是精灵,请在下列选项中选择职业! 2 3 4 5 " << endl;break;
case 2:cout << " 您所选择的种族是兽人,请在下列选项中选择职业! 0 3 4 " << endl;break;
case 3:cout << " 您所选择的种族是矮人,请在下列选项中选择职业! 0 1 4 " << endl;break;
case 4:cout << " 您所选择的种族是元素,请在下列选项中选择职业! 4 5 " << endl;break;
}
cin >> cp; //输入职业序号
if (races== 0 && (cp>= 0 && cp<= 5)) break; //种族是人类的职业选择
else if (races == 1 && (cp>=2 && cp<=5))break; //种族是精灵的职业选择
else if (races == 2 && (cp == 0 || cp == 3 || cp == 4))break; //种族是兽人的职业选择
else if (races == 3 && (cp == 0 || cp == 1 || cp == 4))break; //种族是矮人的职业选择
else if (races== 4 && (cp>=4 && cp<=5))break; //种族是元素的职业选择
else cout << "您所选择的职业错误!请重新输入" << endl;
}
if (cp == 0) position = "狂战士";
if (cp == 1) position = "圣骑士";
if (cp == 2) position = "刺客"
if (cp == 3) position = "猎手";
if (cp == 4) position = "祭司";
if (cp == 5) position = "巫师";
}
class Attribute:public Race
{
friend class Out;
protected:
int c,d,e,f; //力量、敏捷、体力、智力
int g; //智慧
int h,m; //生命值、魔法值
public:
void getAttribute();
void getrands(int i, int j, int k, int x, int y);
}
void Attribute::getrands(int i,int j,int k,int x,int y)
{
int w;
do
{
c =i+rand()%4; //力量
d =j+rand()%4; //敏捷
e =k+rand()%4; //体力
f =x+rand()%4; //智力
w =c+d+e+f; //w是力量、敏捷、体力、智力的相加和
}
while(((100-y)<w)&&(w<100)); //四个随机数,再加上智慧,目的是保证结果为100整
g=100-w; //智慧与力量、敏捷、体力、智力相加为100 h=e*20; //生命值=体力*20
m=(f+g)*10; //魔法值=(智力+智慧)*10
}
void Attribute::getAttribute ()
{
if(cp==0) getrands(40,20,30,5, 5);
if(cp==1) getrands(25,15,30,20,10);
if(cp==2) getrands(20,35,20,15,10);
if(cp==3) getrands(15,40,15,10,20);
if(cp==4) getrands(15,20,15,35,15);
if(cp==5) getrands(10,20,10,20,40);
}
class Out
{
public:
void show(Cosplay &,Race &,Attribute &);
};
void Out::show(Cosplay &f1,Race &f2,Attribute &f3)
{
cout<<" "<<endl;cout<<"游戏角色如下所示: "<<endl;
cout<<" "<<endl;
cout<<std::left <<"姓名: "<<std::left<<f1.name<<endl;
cout<<" "<<endl;
cout<<std::left <<"性别: "<<std::left<<f1.sex<<endl;
cout<<" "<<endl;
cout<<std::left <<"种族: "<<std::left<<f2.race<<endl;
cout<<" "<<endl;
cout<<std::left <<"职业: "<<std::left<<f2.position<<endl;
cout<<" "<<endl;
cout<<std::left <<"力量: "<<std::left<<f3.c <<endl;
cout<<" "<<endl;
cout<<std::left <<"敏捷: "<<std::left<<f3.d<<endl;
cout<<" "<<endl;
cout<<std::left <<"体力: "<<std::left<<f3.e <<endl;
cout<<" "<<endl;
cout<<std::left <<"智力: "<<std::left<<f3.f <<endl;
cout<<" "<<endl;
cout<<std::left <<"智慧: "<<std::left<<f3.g<<endl;
cout<<" "<<endl;
cout<<std::left <<"生命值: "<<std::left<<f3.h<<endl;
cout<<" "<<endl;
cout<<std::left <<"魔法值: "<<std::left<<f3.m <<endl;
cout<<" "<<endl;
}
int main()
{
Cosplay player;
Race player_race;
Attribute player_att;
Out player_show;
int players;
do {
player.getCosplay(); //cosplay基类
player_race.getRace(); //Race是Cosplay的派生类
player_att.getAttribute(); //Attribute是Race的派生类
player_show.show(player, player_race, player_att);
cout << endl
cout << "您对所设计游戏角色是否满意?"<<endl;
cout<<"是否继续?请选择操作!" << endl;
cout << "0.结束 1.继续" << endl;
cin >> players;
}
while (players);
return 0;
}
运行结果