RPG游戏

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 <iomanip>
#include "string"
#include "ctime"
#include "fstream"
using namespace std;


int occupation_choice; //定义全局变量,角色职业的选择

//基础信息类,保存角色的姓名、性别
class Baseinformation  
{
  protected:
  	char name[50];                   //角色姓名
  	string sex;                      //性别
  	int sex_choice;                  //性别选择
 public:
  	void getBaseinformation();       //功能实现
  	friend class Output;             //定义友元类,输出角色信息
  	friend class File;               //定义友元类,将角色信息保存到文档
};


//角色姓名的输入和性别的选择
void Baseinformation::getBaseinformation()
{
	int i;
  	cout << "请输入您游戏角色的姓名:";
  	cin >> name;
while(i)
	{
  		cout << "请选择您游戏角色的性别:";
  		cout << "(1.男    2.女)" << endl;
  		cin >> sex_choice;
  		switch (sex_choice)
		{
            case 1:
				sex = "男"; 
				i=0;
				break;
  			case 2:
				sex = "女"; 
				i=0;
				break;
			default:
				cout<<"输入错误,请重新输入"<<endl;
				break;
		}
	}
}


//派生类,记录角色的种族和职业
class Race :public Baseinformation  
{
  protected:
  	string race;       //定义种族
  	string occupation; //定义职业
  	int race_choice;   //种族选择
 public:
  	void getRace();
  	friend class Output; //友元类
  	friend class File;   //友元类
};


//选择角色的种族和职业
void Race::getRace()
{
	
  	int i = 1;
  	while (i)
	{
  		cout << "请选择您游戏角色的种族:" << endl;
  		cout << "(1.人类  2.精灵  3.兽人  4.矮人  5.元素)" << endl;
  		cin >> race_choice;
  
  		switch (race_choice)
		{
  		case 1:
			race = "人类"; 
			i = 0;
			break;
  		case 2:
			race = "精灵"; 
			i = 0;
			break;
  		case 3:
			race = "兽人"; 
			i = 0; 
			break;
        case 4:
			race = "矮人";
			i = 0;
			break;
  		case 5:
			race = "元素";
			i = 0; 
			break;
  		default:
			cout << "输入错误,请重新输入!" << endl; break;
  		}
  	}

     while (1)
  	{
  		cout << "请选择角色的职业:" << endl;
  		switch (race_choice)
			{

  	     	case 1: 
			    cout << "1.狂战士  2.圣骑士  3.刺客  4.猎手  5.祭司  6.巫师" << endl; 
				break;
			case 2: 
				cout << "3.刺客  4.猎手  5.祭司  6.巫师" << endl;
				break;
  		    case 3: 
				cout << "1.狂战士  4.猎手  5.祭司  " << endl;
				break;
			case 4: 
				cout << "1.狂战士  2.圣骑士  5.祭司 " << endl;
				break;
  		    case 5: 
				cout << "5.祭司  6.巫师" << endl; 
				break;
  		}


		cin >> occupation_choice;
  		if (race_choice == 1 && (occupation_choice >= 1 && occupation_choice <= 6)) break;
		else if (race_choice == 2 && (occupation_choice >=3 && occupation_choice <=6)) break;
		else if (race_choice == 3 && (occupation_choice == 1 || occupation_choice == 4 || occupation_choice == 5)) break;
        else if (race_choice == 4 && (occupation_choice == 1 || occupation_choice == 2 || occupation_choice == 5))  break;
		else if (race_choice == 5 && (occupation_choice >=5 && occupation_choice <=6)) break;
  		else  cout << "输入错误,请重新输入" << endl;
	 }

	 if (occupation_choice == 1) 
	 {
		 occupation = "狂战士";
	 }
  	if (occupation_choice == 2)  
	{
		occupation = "圣骑士";
	}
	if (occupation_choice == 3)	  
	{
		occupation = "刺客";
	}
  	if (occupation_choice == 4) 
	{
		occupation = "猎手";

	}
	if (occupation_choice == 5)  
	{
		occupation = "祭司";
	}
  	if (occupation_choice == 6)  
	{
		occupation = "巫师";
	}
}


//派生类,记录角色的属性
class Attribute :public Race  
{
  protected:
  	int power;	     //力量
	int agile;	     //敏捷
  	int physical;	 //体力
  	int intelligence;//智力
  	int wisdom;	     //智慧
  	int LV;	         //生命值
  	int MV;	         //法力值
	public:
  	void getAttribute();
  	void getRandom(int a, int b, int c, int d, int e);    //随机产生属性值  
  	friend class Output;
  	friend class File;
};


// 随机产生每项属性的值
void Attribute::getRandom(int a, int b, int c, int d, int e)
{
    int sum;  //记录前四个属性之和
  	srand((unsigned)time(NULL));//产生随机数
  	do
	{
		//rand()%3:在0、1、2三个数字中随机产生一个数字
		power = a + rand() % 3;
  		agile = b + rand() % 3;
  		physical = c + rand() % 3;
		intelligence = d + rand() % 3;
  		sum = power + agile + physical + intelligence;
  	} while (((100 - e) < sum) && (sum < 100));
	wisdom = 100 - sum;
  	LV = physical * 20;
  	MV = (wisdom + intelligence) * 10;
  }


//根据选择的职业,向getRamdom传各职业最小值
  void Attribute::getAttribute()
  {
	 
  	if (occupation_choice == 1) //狂战士
	{
		getRandom(40, 20, 30, 5, 5);
	}
  	if (occupation_choice == 2) //圣骑士
	{
		getRandom(25, 15, 30, 20, 10);
	}
	if (occupation_choice == 3) //刺客
	{
		getRandom(20, 35, 20, 15, 10);
	}
  	if (occupation_choice == 4) //猎手
	{
		getRandom(15, 40, 15, 10, 20);
	}
	if (occupation_choice == 5)  //祭司
	{
		getRandom(15, 20, 15, 35, 15);
	}
  	if (occupation_choice == 6) //巫师
	{
		getRandom(10, 20, 10, 20, 40);
	}
}

//输出角色的属性
class Output  
{
  public:
  	void show(Baseinformation &, Race &, Attribute &);//访问友元类
};


void Output::show(Baseinformation &t1, Race &t2, Attribute &t3)
{
  	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<<"姓名:" << t1.name << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "性别:" << t1.sex << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "种族:" << t2.race << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "职业:" << t2.occupation << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "力量:" << t3.power << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "敏捷:" << t3.agile << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "体力:" << t3.physical << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "智力:" << t3.intelligence << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "智慧:" << t3.wisdom << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "生命值:" << t3.LV << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "魔法值:" << t3.MV << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
}

//将角色信息保存到文档
class File  
{
  public:
  	void file(Baseinformation &, Race &, Attribute &);
};

void File::file(Baseinformation &t1, Race &t2, Attribute &t3)
  {
  	ofstream outfile;
  	outfile.open("C://角色数据保存.txt", ios::trunc);
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<<"姓名:" << t1.name << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "性别:" << t1.sex << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "种族:" << t2.race << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "职业:" << t2.occupation << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "力量:" << t3.power << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "敏捷:" << t3.agile << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "体力:" << t3.physical << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "智力:" << t3.intelligence << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "智慧:" << t3.wisdom << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "生命值:" << t3.LV << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
  	cout <<setw(14)<< "魔法值:" << t3.MV << endl;
	cout << "-----------------------------" << endl;
	cout << "-----------------------------" << endl;
}

//主函数
int main()
{
  	Baseinformation role; //定义基类对象role
  	Race role_race;
  	Attribute role_att;   //定义属性类对象role_att
  	Output role_show;
  	File save;            //定义File类对象save
  	int role_choice;
	do
  	{
  		role.getBaseinformation();
		role_race.getRace();
  		role_att.getAttribute();
  		role_show.show(role, role_race, role_att);
		cout << endl;
  		cout << "对生成的角色是否满意?如不满意,则重新生成" << endl;
  		cout << "0.满意     1.不满意" << endl;
  		cin >> role_choice;
	} while (role_choice);
  	save.file(role, role_race, role_att);
  	return 0;
}

注:
setw(int  n):n是在输出时分配了n个字符的输出宽度,然后默认的是在n个字符宽度中右对齐输出

总结:
本次RPG游戏,具体思路如下:

  1. 先创建一个基类,用于保存角色的姓名和性别
  2. 在基类的接触上生成一个派生类种族类,用于角色种族和职业的选择
  3. 接着在种族类的基础上派生出一个属性类,用来记录各个角色的属性值
  4. 建立Output类,可以访问基类、种族类以及属性类,输出记录的数据
  5. 建立File类,将三个类中的数据进行保存输出
    类图:
    在这里插入图片描述
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值