一、题目分析:
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.显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。
二、类图设计
三、代码实现
#include<iostream>
#include<time.h>
#include<cmath>
#include<stdlib.h>
#include<fstream>
#include<string>
using namespace std;
class Role{
public:
char name[50];
int sex;
string Sex;
void get_Name();
void get_sex();
};
void Role::get_Name()//获取姓名
{
cout<<"请输入角色姓名:";
cin>>name;
}
void Role::get_sex()//选择性别
{
int flag=1;
cout<<"请选择创建角色的性别:";
cout<<"1.男性 2.女性"<<endl;
cin>>sex;
switch(sex)
{
case 1:
Sex="男性";
cout<<"您选择的角色性别为:男性"<<endl;
flag=0;
break;
case 2:
Sex="女性";
cout<<"您选择的角色性别为:女性"<<endl;
flag=0;
break;
}
}
class Race:public Role//继承Role
{
public:
int pro;//选择职业
int race;//选择种族
void get_Race();
void get_Pro();
string PRO;//输出职业
string RACE;//输出种族
};
void Race::get_Race()//创建种族
{
cout<<"请选择您游戏角色的种族(0.人类,1.精灵,2.兽人,3.矮人,4.元素):"<<endl;
cin>>race;
switch(race)
{
case 0:
RACE="人类";
cout<<"您选择的种族为:人类"<<endl;
break;
case 1:
RACE="精灵";
cout<<"您选择的种族为:精灵"<<endl;
break;
case 2:
RACE="兽人";
cout<<"您选择的种族为:兽人"<<endl;
break;
case 3:
RACE="矮人";
cout<<"您选择的种族为:矮人"<<endl;
break;
case 4:
RACE="元素";
cout<<"您选择的种族为:元素"<<endl;
break;
default:
cout<<"选择错误,请重新选择"<<endl;
break;
}
}
void Race::get_Pro()
{
switch(race)
{
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>>pro;
switch(pro)
{
case 0:PRO="狂战士";cout<<"您选择的游戏角色的职业为:狂战士"<<endl;break;
case 1:PRO="圣骑士";cout<<"您选择的游戏角色的职业为:圣骑士"<<endl;break;
case 2:PRO="刺客";cout<<"您选择的游戏角色的职业为:刺客"<<endl;break;
case 3:PRO="猎手";cout<<"您选择的游戏角色的职业为:猎手"<<endl;break;
case 4:PRO="祭司";cout<<"您选择的游戏角色的职业为:祭司"<<endl;break;
case 5:PRO="巫师";cout<<"您选择的游戏角色的职业为:巫师"<<endl;break;
default:cout<<"您的输入错误,请重新输入"<<endl;break;
}
}
class Attribute :public Race //派生类,记录角色的属性
{
public:
int strength; //力量
int agility; //敏捷
int physical; //体力
int intelligence; //智力
int wisdom; //智慧
int HP; //生命值
int MP; //法力值
int attribute_Random(int a,int b,int c,int d,int e);
int attribute(int pro);
};
// 随机生成每项属性的值,abcd为该属性的最小值,e为第五个属性的最大值
int Attribute::attribute_Random(int a, int b, int c, int d, int e)
{
int sum;//属性值总和
srand((unsigned)time(NULL));
do
{
strength = rand() % 10 + a;//力量
agility = rand() % 10 + b;//敏捷
physical = rand() % 10 + c;//体力
intelligence = rand() % 10 + d;//智力
// wisdom = rand() % 10 + e;//智慧
sum = strength + agility + physical + intelligence;//总和
}while(((100-e)<sum)&&(sum<100));
wisdom=100-sum;
HP = physical * 20;//生命值
MP = (intelligence + wisdom) * 10;//魔法值
return 0;
}
//根据选择的职业,向getRamdom传各职业最小值
int Attribute::attribute(int pro)
{
if (pro == 0) attribute_Random(40, 20, 30, 5, 5);//狂战士
if (pro == 1) attribute_Random(25, 15, 30, 20, 10);//圣骑士
if (pro == 2) attribute_Random(20, 35, 20, 15, 10);//刺客
if (pro == 3) attribute_Random(15, 40, 15, 10, 20);//猎手
if (pro == 4) attribute_Random(15, 20, 15, 35, 15);//祭司
if (pro == 5) attribute_Random(10, 20, 10, 20, 40);//巫师
}
void show(Role &l, Race &n, Attribute &p)
{
cout << "==================================" << endl;
cout <<" 姓名: " << l.name << endl;
cout << "==================================" << endl;
cout <<" 性别: " << l.Sex << endl;
cout << "==================================" << endl;
cout <<" 种族: " << n.RACE << endl;
cout << "==================================" << endl;
cout <<" 职业: " << n.PRO << endl;
cout << "==================================" << endl;
cout <<" 力量: " << p.strength << endl;
cout << "==================================" << endl;
cout <<" 敏捷: " << p.agility << endl;
cout << "==================================" << endl;
cout <<" 体力: " << p.physical << endl;
cout << "==================================" << endl;
cout <<" 智力: " << p.intelligence << endl;
cout << "==================================" << endl;
cout <<" 智慧: " << p.wisdom << endl;
cout << "==================================" << endl;
cout <<" 生命值: " << p.HP << endl;
cout << "==================================" << endl;
cout <<" 法力值: " << p.MP << endl;
cout << "==================================" << endl;
}
void File(Role &l, Race &n, Attribute &p)
{
ofstream f1("data.txt");
f1 << "姓名:" << l.name << endl;
f1 << "性别:" << l.Sex << endl;
f1 << "种族:" << n.RACE << endl;
f1 << "职业:" << n.PRO << endl;
f1 << "力量:" << p.strength << endl;
f1 << "敏捷:" << p.agility << endl;
f1 << "体力:" << p.physical << endl;
f1 << "智力:" << p.intelligence << endl;
f1 << "智慧:" << p.wisdom << endl;
f1 << "生命值:" << p.HP << endl;
f1 << "法力值:" << p.MP << endl;
}
int main()
{
int x;
int z=1;
while(z)
{
Role l;
Race n;
Attribute p;
l.get_Name();
l.get_sex();
n.get_Race();
n.get_Pro ();
p.attribute(n.pro);//调用 函数
show(l,n,p);
cout<<"请核对您所创建的角色信息,满意输入0,不满意输入1"<<endl;
cin>>z;
if(z==0)
{
File(l,n,p);
cout<<"您所创建的角色已保存"<<endl;
}
else
{
cout<<"请您重新创建角色"<<endl;
}
}
system("pause");
return 0;
}
四、经验归纳
因为我是转专业的学生,还没学完C++,所以之前的作业一直用的C,知道C++比C好写但是还是不太会用,但是这次的上机就必须要用C++了,正好,我们C++也学到类与对象这一部分了,对我而言,正好可以起到巩固所学知识的作用。
1、调试中出现了两个错误,一个忘记调用attribute函数从而导致输出值的不对。
还有是个属性的赋值,应该是随机生成随机数的赋值,因为我的失误,变成了固定值了。
2、在这次的代码书写里,我使用了类与对象,还有继承,继承可以体现事物的普遍性和特殊性,像类Attribute,它是Race的一个特殊性,每个Race的Attribute都不一样,但是都来自于Race,所以用Race派生出Attribute类。减少了冗余代码。而且Race也派生自Role类,这样一环套一环,也挺有趣的。
3、这次上机实验让对类和继承的概念和使用有了清晰的理解。还了解了面向对象编程七大原则中的开闭原则。