一.实验目的
1.掌握面向对象程序设计的方法。明确类与对象的概念,掌握面向对象设计七大原则;
2.掌握常见的设计模式以及类图的描述。
二.题目分析
1.功能描述
几乎所有的RPG游戏(一种源自《龙与地下城》的游戏类型)在进入游戏时都会让用户自己来创建自己喜欢的角色。本次上机要求编写一个简化的创建游戏角色的程序。
2.游戏角色应有的属性
本题目要求的游戏角色应有以下属性:名字、性别、种族、职业、力量、敏捷、体力、智力、智慧、生命值和魔法值。
名字:不超过50个字符。
性别:可以选择男性和女性。
种族:一共可选五个种族,人类、精灵、兽人、矮人和元素。
职业:可选六种职业,狂战士、圣骑士、刺客、猎手、祭司和巫师。
其余属性均为整数。
本题目要求首先用户输入角色姓名,然后由用户选择角色性别,然后由用户选择种族,然后选择职业,然后自动分配力量、敏捷、体力、智力和智慧属性,并计算生命值和魔法值。
生命值=体力*20。
魔法值=(智力+智慧)*10。
3.职业限制
很多职业会限制某些种族选择,例如兽人不能就职圣骑士等等,种族和职业的限制表如下:
种族/职业 | 狂战士 | 圣骑士 | 刺客 | 猎手 | 祭司 | 巫师 |
人类 | 允许 | 允许 | 允许 | 允许 | 允许 | 允许 |
精灵 | 不允许 | 不允许 | 允许 | 允许 | 允许 | 允许 |
兽人 | 允许 | 不允许 | 不允许 | 允许 | 允许 | 不允许 |
矮人 | 允许 | 允许 | 不允许 | 不允许 | 允许 | 不允许 |
元素 | 不允许 | 不允许 | 不允许 | 不允许 | 允许 | 允许 |
所以在要求用户选择职业时,输出信息里面只能有用户所选择种族可以就职的职业。
4.初始属性
本题目要求力量、敏捷、体力、智力和智慧要求是随机值(利用随机数函数来取得随机数),但是五项属性的总和应该是100,并且应该和职业相关。例如狂战士的体力和力量就要比较高,而巫师需要较高的智力,而祭司则需要较高的智慧。各职业初始属性的大致比例应遵从下表:
职业/属性 | 力量 | 敏捷 | 体力 | 智力 | 智慧 |
狂战士 | 40 | 20 | 30 | 5 | 5 |
圣骑士 | 25 | 15 | 30 | 20 | 10 |
刺客 | 20 | 35 | 20 | 15 | 10 |
猎手 | 15 | 40 | 15 | 10 | 20 |
祭司 | 15 | 20 | 15 | 35 | 15 |
巫师 | 10 | 20 | 10 | 20 | 40 |
例如,前面示意图中的祭司的初始属性,大致满足该比例,但是应该是随机的。
然后利用属性值计算生命值和魔法值。
5.显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。
三.类图设计
role |
+name[50]:char +sex,race,occupation:int +getSex():int +getRace():int +liliang,minjie,tili,zhili,zhihui,shengmingzhi,mofazhi:int +getOccupation(int):int +Attribute(int occupation):void +display():void |
四.测试结果
1.运行结果
2.保存角色
源代码:
#include<iostream>
#include<string>
#include<stdlib.h>
#include<fstream>
#include<time.h>
#include<math.h>
using namespace std;
const char Isex[2][50]={"男","女"};
const char Irace[5][50]={"人类","精灵","兽人","矮人","元素"};
const char Ioccupation[6][50]={"狂战士","圣骑士","刺客","猎手","祭司","巫师"};
class role
{
public:
char name[50];
int sex,race,occupation;
int getSex(); //输入性别
int getRace(); //输入种族
int liliang;
int minjie;
int tili;
int zhili;
int zhihui;
int shengmingzhi;
int mofazhi;
int getOccupation(int); //输入职业
void Attribute(int occupation); //输入属性
void display(); //显示主界面
};
int role:: getSex()
{
int sex;
cout<<"请选择您游戏角色性别(0男性,1女性):";
cin>>sex;
while(1)
{
if(sex==0||sex==1)
break;
else
cout<<"输入错误,请重新输入!\n";
}
return sex;
}
int role:: getRace()
{
int race;
cout<<"请选择您游戏角色的种族(0人类,1精灵,2兽人,3矮人,4元素):";
cin>>race;
while(1)
{
if(race>=0&&race<=4)
break;
else
cout<<"输入错误,请重新输入!\n";
}
return race;
}
int role::getOccupation(int race)
{
switch(race)
{
case 0:
while(1)
{
cout<<"请选择您的职业(0狂战士,1圣骑士,2刺客,3猎手,4祭司,5巫师):";
cin>>occupation;
if(occupation>=0&&occupation<=5)
break;
else
cout<<"输入错误,请重新输入!\n";
}
break;
case 1:
while(1)
{
cout<<"请选择您的职业(2刺客,3猎手,4祭司,5巫师):";
cin>>occupation;
if(occupation>=2&&occupation<=5)
break;
else
cout<<"输入错误,请重新输入!\n";
}
break;
case 2:
while(1)
{
cout<<"请选择您的职业(0狂战士,3猎手,4祭司):";
cin>>occupation;
if(occupation==0||occupation==3||occupation==4)
break;
else
cout<<"输入错误,请重新输入!\n";
}
break;
case 3:
while(1)
{
cout<<"请选择您的职业(0狂战士,1圣骑士,4祭司):";
cin>>occupation;
if(occupation==0||occupation==1||occupation==4)
break;
else
cout<<"输入错误,请重新输入!\n";
}
break;
case 4:
while(1)
{
cout<<"请选择您的职业(4祭司,5巫师):";
cin>>occupation;
if(occupation>=4&&occupation<=5)
break;
else
cout<<"输入错误,请重新输入!\n";
}
break;
default:;
}
return occupation;
}
void role::Attribute(int occupation)
{
int rand1=rand()%11+25; //生成一个25到35的随机数
int rand2=rand()%11+0; //生成一个0到10的随机数
int rand3=rand()%11+15; //生成一个15到25的随机数
int rand4=rand()%11+5; //生成一个5到15的随机数
int rand5=rand()%11+10; //生成一个10到20的随机数
int rand6=rand()%11+30; //生成一个30到40的随机数
int rand7=rand()%11+35; //生成一个35到45的随机数
switch(occupation)
{
case 0:
liliang=rand()%11+35;
cout<<"力量\t\t\t"<<liliang<<endl<<endl;
cout<<"======================================="<<endl;
minjie=rand()%11+15;
cout<<"敏捷\t\t\t"<<minjie<<endl<<endl;
cout<<"======================================="<<endl;
tili=rand1;
cout<<"体力\t\t\t"<<tili<<endl<<endl;
cout<<"======================================="<<endl;
zhili=rand2;
cout<<"智力\t\t\t"<<zhili<<endl<<endl;
cout<<"======================================="<<endl;
zhihui=rand4;
cout<<"智慧\t\t\t"<<zhihui<<endl<<endl;
cout<<"======================================="<<endl;
shengmingzhi=20*tili;
cout<<"生命值\t\t\t"<<shengmingzhi<<endl<<endl; //生命=体力*20
cout<<"======================================="<<endl;
mofazhi=10*(zhili+zhihui);
cout<<"魔法值\t\t\t"<<mofazhi<<endl<<endl;
cout<<"======================================="<<endl;
break;
case 1:
liliang=rand()%11+20;
cout<<"力量\t\t\t"<<liliang<<endl<<endl;
cout<<"======================================="<<endl;
minjie=rand()%11+10;
cout<<"敏捷\t\t\t"<<minjie<<endl<<endl;
cout<<"======================================="<<endl;
tili=rand1;
cout<<"体力\t\t\t"<<tili<<endl<<endl;
cout<<"======================================="<<endl;
zhili=rand3;
cout<<"智力\t\t\t"<<zhili<<endl<<endl;
cout<<"======================================="<<endl;
zhihui=rand4;
cout<<"智慧\t\t\t"<<zhihui<<endl<<endl;
cout<<"======================================="<<endl;
shengmingzhi=20*tili;
cout<<"生命值\t\t\t"<<shengmingzhi<<endl<<endl; //生命=体力*20
cout<<"======================================="<<endl;
mofazhi=10*(zhihui+zhili);
cout<<"魔法值\t\t\t"<<mofazhi<<endl<<endl;
cout<<"======================================="<<endl;
break;
case 2:
liliang=rand1;
cout<<"力量\t\t\t"<<liliang<<endl<<endl;
cout<<"======================================="<<endl;
minjie=rand()%11+30;
cout<<"敏捷\t\t\t"<<minjie<<endl<<endl;
cout<<"======================================="<<endl;
tili=rand3;
cout<<"体力\t\t\t"<<tili<<endl<<endl;
cout<<"======================================="<<endl;
zhili=rand5;
cout<<"智力\t\t\t"<<zhili<<endl<<endl;
cout<<"======================================="<<endl;
zhihui=rand4;
cout<<"智慧\t\t\t"<<zhihui<<endl<<endl;
cout<<"======================================="<<endl;
shengmingzhi=20*tili;
cout<<"生命值\t\t\t"<<shengmingzhi<<endl<<endl; //生命=体力*20
cout<<"======================================="<<endl;
mofazhi=10*(zhili+zhihui);
cout<<"魔法值\t\t\t"<<mofazhi<<endl<<endl;
cout<<"============================================"<<endl;
break;
case 3:
liliang=rand5;
cout<<"力量\t\t\t"<<liliang<<endl<<endl;
cout<<"======================================="<<endl;
minjie=rand()%11+35;
cout<<"敏捷\t\t\t"<<minjie<<endl<<endl;
cout<<"======================================="<<endl;
tili=rand5;
cout<<"体力\t\t\t"<<tili<<endl<<endl;
cout<<"======================================="<<endl;
zhili=rand4;
cout<<"智力\t\t\t"<<zhili<<endl<<endl;
cout<<"======================================="<<endl;
zhihui=rand3;
cout<<"智慧\t\t\t"<<zhihui<<endl<<endl;
cout<<"======================================="<<endl;
shengmingzhi=20*tili;
cout<<"生命值\t\t\t"<<shengmingzhi<<endl<<endl; //生命=体力*20
cout<<"======================================="<<endl;
mofazhi=10*(zhili+zhihui);
cout<<"魔法值\t\t\t"<<mofazhi<<endl<<endl;
cout<<"======================================="<<endl;
break;
case 4:
liliang=rand5;
cout<<"力量\t\t\t"<<liliang<<endl<<endl;
cout<<"======================================="<<endl;
minjie=rand3;
cout<<"敏捷\t\t\t"<<minjie<<endl<<endl;
cout<<"======================================="<<endl;
tili=rand5;
cout<<"体力\t\t\t"<<tili<<endl<<endl;
cout<<"======================================="<<endl;
zhili=rand6;
cout<<"智力\t\t\t"<<zhili<<endl<<endl;
cout<<"======================================="<<endl;
zhihui=rand4;
cout<<"智慧\t\t\t"<<zhihui<<endl<<endl;
cout<<"======================================="<<endl;
shengmingzhi=20*tili;
cout<<"生命值\t\t\t"<<shengmingzhi<<endl<<endl; //生命=体力*20
cout<<"======================================="<<endl;
mofazhi=10*(zhili+zhihui);
cout<<"魔法值\t\t\t"<<mofazhi<<endl<<endl;
cout<<"======================================="<<endl;
break;
case 5:
liliang=rand4;
cout<<"力量\t\t\t"<<liliang<<endl<<endl;
cout<<"======================================="<<endl;
minjie=rand()%11+15;
cout<<"敏捷\t\t\t"<<minjie<<endl<<endl;
cout<<"======================================="<<endl;
tili=rand4;
cout<<"体力\t\t\t"<<tili<<endl<<endl;
cout<<"======================================="<<endl;
zhili=rand3;
cout<<"智力\t\t\t"<<zhili<<endl<<endl;
cout<<"======================================="<<endl;
zhihui=rand7;
cout<<"智慧\t\t\t"<<zhihui<<endl<<endl;
cout<<"======================================="<<endl;
shengmingzhi=20*tili;
cout<<"生命值\t\t\t"<<shengmingzhi<<endl<<endl; //生命=体力*20
cout<<"======================================="<<endl;
mofazhi=10*(zhili+zhihui);
cout<<"魔法值\t\t\t"<<mofazhi<<endl<<endl;
cout<<"======================================="<<endl;
break;
}
}
void role::display()
{ while(1)
{
srand((unsigned)time(NULL)); //随机数
cout<<"请输入您游戏角色的姓名:";
cin>>name;
sex=getSex();
race=getRace();
occupation=getOccupation(race); //职业
cout<<"======================================="<<endl;
cout<<"姓名\t\t\t"<<name<<endl;
cout<<endl;
cout<<"======================================="<<endl;
cout<<"性别\t\t\t"<<Isex[sex]<<endl;
cout<<endl;
cout<<"======================================="<<endl;
cout<<"种族\t\t\t"<<Irace[race]<<endl;
cout<<endl;
cout<<"======================================="<<endl;
cout<<"职业\t\t\t"<<Ioccupation[occupation]<<endl;
cout<<endl;
cout<<"======================================="<<endl;
Attribute(occupation);
int a;
cout<<"如果您对此次生成角色满意,请输入0,不满意请输入1重新选择:";
cin>>a;
if(a==0)
{
ofstream outfile;
outfile.open("input.txt",ios::out);
outfile<<"游戏角色姓名:"<<name<<endl;
outfile<<"角色性别:"<<Isex[sex]<<endl;
outfile<<"角色种族:"<<Irace[race]<<endl;
outfile<<"角色职业:"<<Ioccupation[occupation]<<endl;
outfile<<"力量:"<<liliang<<endl;
outfile<<"敏捷:"<<minjie<<endl;
outfile<<"体力:"<<tili<<endl;
outfile<<"智力:"<<zhili<<endl;
outfile<<"智慧:"<<zhihui<<endl;
outfile<<"生命值:"<<shengmingzhi<<endl;
outfile<<"魔法值:"<<mofazhi<<endl;
outfile.close();
break;
}
}
}
void main()
{
role l;
l.display();
}