一、实验内容
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.显示信息
最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户
满意则程序结束,并将用户创建角色的相关信息写入文件保存。
二.程序设计
建立三个基类,分别为Base类,保存性别和姓名,定义输入性别函数inputsex();zhongzu类,保存种族和职业,定义inputzhongzu()函数和Inputwork()函数在输入职业函数中,通过传递种族参数,选择合适的职业;shuxing类,在inputshuxing()函数中,通过传递职业参数,将各职业属性最小值传递给random()函数中rand1,rand2,rand3,rand4,rand5,随机生成属性值并判断全局变量a1的值,采用追加的方式判断是否写入文件;在主函数中,判断性别、种族选择的合理性并判断是否写入文件。
三.测试及调试
判断性别、种族、职业的正确性
询问是否满意创建的角色,若满意则存入test.txt文件
随机生成a,b,c,d值,同时根据判断获得e的值
四.总结
将一个整体的内容分为三个小基类,逐个进行编写;同时采用传递参数的方式调用不同函数。在做的过程中,出现的问题有:生成随机属性值时,调用了random()函数,但输出值却是初始设定的,通过断点调试发现是在主函数中输出的方式有问题;在将属性值写入文件时,不能写入同一个文件,采用追加的方式,但只能加在.txt文件中的头部而不是尾部。
五.源程序
#include<iostream>
#include<fstream>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include<stdlib.h>
using namespace std;
int a1;//全局变量,判断是否写入文件
int inputsex();//输入性别函数
int inputzhongzhu();//输入种族函数
int inputwork(int);//输入职业函数
void inputshuxing(int);//生成属性函数
char Isex[2][50]={"男性","女性"};
char Izhongzu[5][50]={"人类","精灵","兽人","矮人","元素"};
char Iwork[6][50]={"狂战士","圣骑士","刺客","猎手","祭司","巫师"};
class Base//基类,保存性别和姓名
{
protected:
char name[50];
int sex;
public:
void getBase();
friend class Output;//友元类,输出角色信息
};
int inputsex()
{
int sex;
cout<<"请选择游戏角色的性别(0 男性,1 女性):";
cin>>sex;
return sex;
}
class zhongzu//基类,保存种族和职业
{
protected:
char name[50];
int sex;
int work;
public:
void getBase();
friend class Output;
void getzhongzu();
};
int inputzhongzu()
{
int zhongzu;
cout<<"请选择游戏角色的种族(0 人类,1 精灵,2 兽人,3 矮人,4 元素):";
cin>>zhongzu;
return zhongzu;
}
int inputwork(int zhongzu)
{
int work;
switch(zhongzu)
{
case 0:
{
cout<<"请选择游戏角色的职业(0 狂战士,1 圣骑士,2 刺客,3 猎手,4 祭司,5 巫师):";
cin>>work;
if (work>=0&&work<=5)
break;
else
cout<<"选择的职业不在所选范围内!"<<endl;
}
break;
case 1:
{
cout<<"请选择游戏角色的职业(0 刺客,1 猎手,2 祭司,3 巫师):";
cin>>work;
if (work>=0&&work<=3)
break;
else
cout<<"选择的职业不在所选范围内!"<<endl;
}
break;
case 2:
{
cout<<"请选择游戏角色的职业(0 狂战士,1 猎手,2 祭司):";
cin>>work;
if (work>=0&&work<=2)
break;
else
cout<<"选择的职业不在所选范围内!"<<endl;
}
break;
case 3:
{
cout<<"请选择游戏角色的职业(0 狂战士,1 圣骑士,2 祭司):";
cin>>work;
if (work>=0&&work<=2)
break;
else
cout<<"选择的职业不在所选范围内!"<<endl;
}
break;
case 4:
{
cout<<"请选择游戏角色的职业(0 祭司,1 巫师):";
cin>>work;
if (work>=0&&work<=1)
break;
else
cout<<"选择的职业不在所选范围内!";
}
break;
}
return work;
}
class shuxing//基类,保存属性
{
protected:
char name[50];
int sex;
int work;
int a,b,c,d,e;//a代表力量b代表敏捷c代表体力d代表智力e代表智慧
int life;
int magic;
int sum;
public:
void getBase();
friend class Output;
void getzhongzu();
void getshuxing();
void random(int rand1,int rand2,int rand3,int rand4,int rand5);
};
void random(int rand1,int rand2,int rand3,int rand4,int rand5)//随机生成属性值
{
int sum;
int a,b,c,d,e;//a代表力量b代表敏捷c代表体力d代表智力e代表智慧
int life;
int magic;
srand((unsigned)time(NULL));
do
{
a = rand() % 2+rand1;
b = rand() % 2+rand2;
c = rand() % 2+rand3;
d = rand() % 2+rand4;
sum = a+b+c+d;
e=100-sum;
} while(e<=0);
life=c*20;
magic=(d+e)*10;
cout << "\t力量\t\t\t" << a<< endl;
cout << "\t=====================================" << endl;
cout << "\t敏捷\t\t\t" << b << endl;
cout << "\t=====================================" << endl;
cout << "\t体力\t\t\t" << c<< endl;
cout << "\t=====================================" << endl;
cout << "\t智力\t\t\t" << d << endl;
cout << "\t=====================================" << endl;
cout << "\t智慧\t\t\t" << e << endl;
cout << "\t=====================================" << endl;
cout << "\t生命值\t\t\t" << life << endl;
cout << "\t=====================================" << endl;
cout << "\t法力值\t\t\t" << magic << endl;
cout << "\t=====================================" << endl;
if(a1==0)//判断是否写入文件
{
ofstream acout("test.txt");
acout << "\t=====================================" << endl;
acout << "\t力量\t\t\t" << a<< endl;
acout << "\t=====================================" << endl;
acout << "\t敏捷\t\t\t" << b << endl;
acout << "\t=====================================" << endl;
acout << "\t体力\t\t\t" << c<< endl;
acout << "\t=====================================" << endl;
acout << "\t智力\t\t\t" << d << endl;
acout << "\t=====================================" << endl;
acout << "\t智慧\t\t\t" << e << endl;
acout << "\t=====================================" << endl;
acout << "\t生命值\t\t\t" << life << endl;
acout << "\t=====================================" << endl;
acout << "\t法力值\t\t\t" << magic << endl;
acout.close();
}
}
void inputshuxing(int work)//根据选择的职业,向random()传递不同的最小值
{
if(work==0) random(40,20,30,5,5);
if(work==1) random(25,15,30,20,10);
if(work==2) random(20,35,20,15,10);
if(work==3) random(15,40,15,10,20);
if(work==4) random(15,20,15,35,15);
if(work==5) random(10,20,10,20,40);
}
void main()
{
while(1)
{
srand((unsigned)time(NULL));
char name[50];
int sex,zhongzu,work;
cout<<"请输入您的游戏名字:";
cin>>name;
while(1)//判断性别是否选择正确
{
sex=inputsex();
if(sex==0||sex==1)
break;
else
cout<<"请输入正确性别选项!";
}
while(1)//判断种族是否选择正确
{
zhongzu=inputzhongzu();
if(zhongzu>=0&&zhongzu<=4)
break;
else
cout<<"请输入种族选项!";
}
work=inputwork(zhongzu);//输出职业
cout << "\t=====================================" << endl;
cout << "\t姓名\t\t\t" << name << endl;
cout << "\t=====================================" << endl;
cout << "\t性别\t\t\t" << Isex[sex]<< endl;
cout << "\t=====================================" << endl;
cout << "\t种族\t\t\t" << Izhongzu[zhongzu] << endl;
cout << "\t=====================================" << endl;
cout << "\t职业\t\t\t" << Iwork[work] << endl;
cout << "\t=====================================" << endl;
inputshuxing(work); //调用生成属性函数,输出属性
int a1;
cout<<"对产生结果是否满意?满意请输入0,不满意请输入1"<<endl;
cin>>a1;
if(a1==0)
{
ofstream acout("test.txt",ios::app);
acout << "\t=====================================" << endl;
acout << "\t姓名\t\t\t" << name << endl;
acout << "\t=====================================" << endl;
acout << "\t性别\t\t\t" << Isex[sex]<< endl;
acout << "\t=====================================" << endl;
acout << "\t种族\t\t\t" << Izhongzu[zhongzu] << endl;
acout << "\t=====================================" << endl;
acout << "\t职业\t\t\t" <<Iwork[work] << endl;
acout << "\t=====================================" << endl;
acout.close();
break;
}
}
}