RPG角色生成器 c++

#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

string array_race[5]={"人类","精灵","兽人","矮人","元素"};
string array_professional[6]={"狂战士","圣骑士","刺客","猎手","祭司","巫师"};

class create_player   //生成角色类,若做游戏建议只用作生成,毕竟这种封装方式不适合继承,和因为技能和装备而改变属性,或者将属性归到保护类继承,然后写共有函数set属性 
{
	private:
		char name[50];
		char sex[2];
		int race;			//0人类,1精灵,2兽人,3矮人,4元素
		int professional;	//0狂战士,1圣骑士,2刺客,3猎手,4祭祀,5巫师
		int STR;    //力量 
		int DEX;    //敏捷
		int CON;    //体力
		int INT;    //智力
		int WIT;	//智慧
		int HP;		//血 
		int MP;		//蓝
	protected:
		void jichu()
		{
			cout<<"请输入角色的姓名"<<endl;
			cin>>name;
			cout<<"请输入性别(男/女)"<<endl;
			cin>>sex;
			cout<<"请输入种族(0人类,1精灵,2兽人,3矮人,4元素)"<<endl;
			cin>>race;
		}
		void set_professional()
		{
			cout<<"请选择你的职业"<<endl;
			switch(race)
			{
				case 0:
					{
						cout<<"0狂战士,1圣骑士,2刺客,3猎手,4祭司,5巫师";
						cin>>professional;
						if(professional<0||professional>5)
						{
							cout<<"输入错误或者你的种族无法转职成此职业"<<endl;
							set_professional();
						}
						break;
					}
				case 1:
					{
						cout<<"2刺客,3猎手,4祭司,5巫师";
						cin>>professional;
						if(professional<2||professional>5)
						{
							cout<<"输入错误或者你的种族无法转职成此职业"<<endl;
							set_professional();
						}
						break;
					}
				case 2:
					{
						cout<<"0狂战士,3猎手,4祭司";
						cin>>professional;
						if(professional!=0||professional!=3||professional!=4)
						{
							cout<<"输入错误或者你的种族无法转职成此职业"<<endl;
							set_professional();
						}
						break;
					}
				case 3:
					{
						cout<<"0狂战士,1圣骑士,4祭司";
						cin>>professional;
						if(professional!=0||professional!=1||professional!=4)
						{
							cout<<"输入错误或者你的种族无法转职成此职业"<<endl;
							set_professional();
						}
						break;
					}
				case 4:
					{
						cout<<"3祭司,4祭祀";
						cin>>professional;
						if(professional!=3||professional!=4)
						{
							cout<<"输入错误或者你的种族无法转职成此职业"<<endl;
							set_professional();
						}
						break;
					}
			}
		}
		
		void shuxing()
		{
			DEX=0,STR=0,CON=0,INT=0,WIT=0;
			srand((int)time(0));
//			STR=rand() % (50 - 30 + 1) + 30;
			switch (professional)
			{
				case 0:	//狂战 
					{
						for(int i=0;i<100;i++)
						{
							int t=rand()%100;
							if(t>=0&&t<=40)
								STR++;
							if(t>40&&t<=60)
								DEX++;
							if(t>60&&t<=90)
								CON++;
							if(t>90&&t<=95)
								INT++;
							if(t>95&&t<=100)
								WIT++;
						}
						Calculat_HPMP();			
						break;
					}
				case 1:	//圣骑士 
					{
						for(int i=0;i<100;i++)
						{
							int t=rand()%100;
							if(t>=0&&t<=25)
								STR++;
							if(t>25&&t<=40)
								DEX++;
							if(t>40&&t<=70)
								CON++;
							if(t>70&&t<=90)
								INT++;
							if(t>90&&t<=100)
								WIT++;
						}			
							Calculat_HPMP();
							break;
					}
				case 2:	//刺客 
					{
						for(int i=0;i<100;i++)
						{
							int t=rand()%100;
							if(t>=0&&t<=20)
								STR++;
							if(t>20&&t<=55)
								DEX++;
							if(t>55&&t<=75)
								CON++;
							if(t>75&&t<=90)
								INT++;
							if(t>90&&t<=100)
								WIT++;
						}			
							Calculat_HPMP();
								break;
					}
				case 3: //猎手 
					{
						for(int i=0;i<100;i++)
						{
							int t=rand()%100;
							if(t>=0&&t<=15)
								STR++;
							if(t>15&&t<=55)
								DEX++;
							if(t>55&&t<=70)
								CON++;
							if(t>70&&t<=80)
								INT++;
							if(t>80&&t<=100)
								WIT++;
						}			
							Calculat_HPMP();
								break;
					}
				case 4://祭司 
					{
						for(int i=0;i<100;i++)
						{
							int t=rand()%100;
							if(t>=0&&t<=15)
								STR++;
							if(t>15&&t<=35)
								DEX++;
							if(t>35&&t<=50)
								CON++;
							if(t>50&&t<=85)
								INT++;
							if(t>85&&t<=100)
								WIT++;
						}			
							Calculat_HPMP();
								break;
					}
				case 5:		//巫师 
					{
						for(int i=0;i<100;i++)
						{
							int t=rand()%100;
							if(t>=0&&t<=10)
								STR++;
							if(t>10&&t<=30)
								DEX++;
							if(t>30&&t<=40)
								CON++;
							if(t>40&&t<=60)
								INT++;
							if(t>60&&t<=100)
								WIT++;
						}			
							Calculat_HPMP();
								break;
					}
			}

		}
		void output()
		{
			int flag;
			cout<<"名字:"<<name<<endl;
			cout<<"性别:"<<sex<<endl;
			cout<<"种族:"<<array_race[race]<<endl;
			cout<<"职业:"<<array_professional[professional]<<endl; 
			cout<<"力量:"<<STR<<"    "<<"敏捷:"<<DEX<<endl;
			cout<<"体力:"<<CON<<"    "<<"智力:"<<INT<<endl;
			cout<<"智慧:"<<WIT<<endl;
			cout<<"HP:"<<HP<<"     "<<"MP:"<<MP<<endl; 
			cout<<"是否重新生成属性(1是/0否)"<<endl;
			cin>>flag;
			if(flag==1)
			{
				shuxing();
				output();	
			}
			else
			{	
				save();
				cout<<"角色生成成功"<<endl;	
			}
			 
		}
		void save()
		{
			FILE *fp; 
			if((fp=fopen("player1.txt","w"))==NULL)	//打开输出文件 
			{
      			printf("没有文件\n");
     			exit(0);
    		}
			fprintf(fp,"%s\n",name);				//输出 
        	fprintf(fp,"%s\n",sex);
		  	fprintf(fp,"%d\n",race);
		  	fprintf(fp,"%d\n",professional);
		  	fprintf(fp,"%d\n",STR);
		  	fprintf(fp,"%d\n",DEX);
		  	fprintf(fp,"%d\n",CON);
		  	fprintf(fp,"%d\n",INT);
		  	fprintf(fp,"%d\n",WIT);
        	fclose(fp);							//关闭文件 
		}
	public:
		void Calculat_HPMP()    
		{
			HP=CON*20;
			MP=(INT+WIT)*10;
		}
		create_player()
		{
			jichu();		//基础输入 
			set_professional(); //选择种族职业 
			shuxing();		//属性生成 
			output();		//属性显示,满意则保存 
		}
		    
		 
};
int main()
{
	create_player p1;
	
}

RPG角色生成器 c++RPG角色生成器 c++

RPG角色生成器 c++

属性生成用的算法是,产生100个1~100的随机数,按照比例,若在属性区间内,则让其属性自增1

最后当玩家满意属性的时候,将信息保存到文件