算法实现
#include
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include
using namespace std;
//函数声明
int input_sex(); //输入性别
int input_race(); //输入种族
int input_occupation(int); //输入职业
void output_attribute(int occupation); //输出属性
char Isex[2][50]={“男性”,“女性”};
char Irace[5][50]={“人类”,“精灵”,“兽人”,“矮人”,“元素”};
char Ioccupation[6][50]={“狂战士”,“圣骑士”,“刺客”,“猎手”,“祭司”,“巫师”};
//基类,保存角色的姓名,性别
class Baseinfoamation
{
protected:
char name[50];
int sex;
int sex_choice;
public:
void getBaseinfoamation();
friend class Output; //友元类,用于输出角色信息
};
//主函数
int main()
{
while(1) {
srand((unsigned)time(NULL));
//生成随机数
char name[50];
int sex,race,occupation;
cout<<“请输入您游戏角色的姓名:”;
cin>>name;
while (1)
{
sex=input_sex();
if (sex==0||sex==1) //判断性别输入是否正确
break; //正确则跳出
else
cout<<"请输入0或1来选择性别\n";
}
while (1)
{
race=input_race();
if (race>=0&&race<=4) //判断种族输入是否正确
break; //正确则跳出
else
cout<<"请输入0到4之间的数字来选择种族\n";
}
occupation=input_occupation(race); //确定职业
cout<< "姓名\t\t\t"<<name<<"\n"<<"=====================================\n"
<<"性别\t\t\t"<<Isex[sex]<<"\n"<<"==================================\n"
<<"种族\t\t\t"<<Irace[race]<<"\n"<<"============================\n"
<<"职业\t\t\t"<<Ioccupation[occupation]<<"\n"<<"====================================\n"<<endl;
ofstream outfile;
outfile.open("data.txt");
outfile<<"姓名\t\t\t"<<name<<"\n"<<"=====================================\n"
<<"性别\t\t\t"<<Isex[sex]<<"\n"<<"==================================\n"
<<"种族\t\t\t"<<Irace[race]<<"\n"<<"============================\n"
<<"职业\t\t\t"<<Ioccupation[occupation]<<"\n"<<"====================================\n"<<endl;
outfile.close();
output_attribute(occupation); //输出属性
//用户核对信息,满意则结束,不满意重新选择
int a;
cout<<"请再次核对您的选择,满意请输入0,不满意请输入1重新选择:";
cin>>a;
if (a==0)
break;
}
}
//基类,记录角色的种族、职业
class Race
{
protected:
char name[50];
int sex;
int sex_choice;
int race;
int occupation;
int race_choice;
public:
void getBaseinfoamation();
friend class Output; //友元类,用于输出角色信息
void getRace();
};
//…函数的定义
//性别选择函数
int input_sex()
{
int sex;
cout<<“请选择您游戏角色的性别(0男性,1女性):”;
cin>>sex;
return sex;
}
//种族选择函数
int input_race()
{
int race;
cout<<“请选择您游戏角色的种族(0人类,1精灵,2兽人,3矮人,4元素):”;
cin>>race;
return race;
}
//职业选择函数
int input_occupation(int race)
{
int occupation;
switch (race)
{
case(0):
while (1)
{
cout<<“请选择您的职业(0狂战士,1圣骑士,2刺客,3猎手,4祭司,5巫师):”; //0人类
cin>>occupation;
if (occupation>=0&&occupation<=5)
break;
else
cout<<“请输入0到5之间的数字选择职业\n”;
}
break;
case(1):
while (1)
{
cout<<“请选择您的职业(2刺客,3猎手,4祭司,5巫师)”; //1精灵
cin>>occupation;
if (occupation>=2&&occupation<=5)
break;
else
cout<<“请输入2到5之间的数字选择职业\n”;
}
break;
case(2):
while (1)
{
cout<<“请选择您的职业(0狂战士,3猎手,4祭司)”; //2兽人
cin>>occupation;
if (occupation0||occupation3||occupation4)
break;
else
cout<<“请输入0或3或4来选择职业\n”;
}
break;
case(3):
while (1)
{
cout<<“请选择您的职业(0狂战士,1圣骑士,4祭司)”; //3矮人
cin>>occupation;
if (occupation0||occupation1||occupation4)
break;
else
cout<<“请输入0或1或4来选择职业\n”;
}
break;
case(4):
while (1)
{
cout<<“请选择您的职业(4祭司,5巫师)”; //4元素
cin>>occupation;
if (occupation>=4&&occupation<=5)
break;
else
cout<<“请输入4或5来选择职业\n”;
}
break;
default: ;
}
return occupation;
}
//属性输出函数
void output_attribute(int occupation)
{
int rand1,rand2,rand3,rand4,rand5,rand6; //产生随机数
rand1=rand()%11+35; //随机产生0到45的随机数
rand2=rand()%5+3; //随机产生0到7的随机数
rand3=rand()%9+21; //随机产生0到29的随机数
rand4=rand()%9+16; //随机产生0到24的随机数
rand5=rand()%7+7; //随机产生0到13的随机数
rand6=rand()%11+31; //随机产生0到31的随机数
switch (occupation) //生命值=体力*20 魔法值=(智力+智慧)*10。
{
//狂战士
case(0):
{
cout<<"力量\t\t\t"<<rand1<<"\n"<<"=====================================\n"
<<"敏捷\t\t\t"<<60-rand1<<"\n"<<"==================================\n"
<<"体力\t\t\t"<<40-rand2-rand2<<"\n"<<"============================\n"
<<"智力\t\t\t"<<rand2<<"\n" <<"====================================\n"
<<"智慧\t\t\t"<<rand2<<"\n"<<"=====================================\n"
<<"生命值\t\t\t"<<20*(40-rand2-rand2)<<"\n"<<"=====================\n"
<<"魔法值\t\t\t"<<10*(rand2+rand2)<<"\n"<<"========================\n";
ofstream outfile;
outfile.open("data.txt",ios::app);
outfile<<"力量\t\t\t"<<rand1<<"\n"<<"=====================================\n"
<<"敏捷\t\t\t"<<60-rand1<<"\n"<<"==================================\n"
<<"体力\t\t\t"<<40-rand2-rand2<<"\n"<<"============================\n"
<<"智力\t\t\t"<<rand2<<"\n" <<"====================================\n"
<<"智慧\t\t\t"<<rand2<<"\n"<<"=====================================\n"
<<"生命值\t\t\t"<<20*(40-rand2-rand2)<<"\n"<<"=====================\n"
<<"魔法值\t\t\t"<<10*(rand2+rand2)<<"\n"<<"========================\n"<<endl;
outfile.close();
}
break;
//圣骑士
case(1):{
cout<<"力量\t\t\t"<<rand3<<"\n"<<"=====================================\n"
<<"敏捷\t\t\t"<<40-rand3<<"\n"<<"==================================\n"
<<"体力\t\t\t"<<60-rand4-rand5<<"\n"<<"============================\n"
<<"智力\t\t\t"<<rand4<<"\n"<<"=====================================\n"
<<"智慧\t\t\t"<<rand5<<"\n"<<"=====================================\n"
<<"生命值\t\t\t"<<20*(60-rand4-rand5)<<"\n"<<"=====================\n"
<<"魔法值\t\t\t"<<10*(rand4+rand5)<<"\n"<<"========================\n";
ofstream outfile;
outfile.open("data.txt",ios::app);
outfile<<"力量\t\t\t"<<rand3<<"\n"<<"=====================================\n"
<<"敏捷\t\t\t"<<40-rand3<<"\n"<<"==================================\n"
<<"体力\t\t\t"<<60-rand4-rand5<<"\n"<<"============================\n"
<<"智力\t\t\t"<<rand4<<"\n"<<"=====================================\n"
<<"智慧\t\t\t"<<rand5<<"\n"<<"=====================================\n"
<<"生命值\t\t\t"<<20*(60-rand4-rand5)<<"\n"<<"=====================\n"
<<"魔法值\t\t\t"<<10*(rand4+rand5)<<"\n"<<"========================\n"<<endl;
outfile.close();
}break;
//刺客
case(2):
{
cout<<"力量\t\t\t"<<rand4<<"\n"<<"=====================================\n"
<<"敏捷\t\t\t"<<55-rand4<<"\n"<<"==================================\n"
<<"体力\t\t\t"<<rand4<<"\n"<<"=====================================\n"
<<"智力\t\t\t"<<45-rand4-rand5<<"\n"<<"============================\n"
<<"智慧\t\t\t"<<rand5<<"\n"<<"=====================================\n"
<<"生命值\t\t\t"<<20*rand4<<"\n"<<"================================\n"
<<"魔法值\t\t\t"<<10*(45-rand4)<<"\n"<<"===========================\n";
ofstream outfile;
outfile.open("data.txt",ios::app);
outfile<<"力量\t\t\t"<<rand4<<"\n"<<"=====================================\n"
<<"敏捷\t\t\t"<<55-rand4<<"\n"<<"==================================\n"
<<"体力\t\t\t"<<rand4<<"\n"<<"=====================================\n"
<<"智力\t\t\t"<<45-rand4-rand5<<"\n"<<"============================\n"
<<"智慧\t\t\t"<<rand5<<"\n"<<"=====================================\n"
<<"生命值\t\t\t"<<20*rand4<<"\n"<<"================================\n"
<<"魔法值\t\t\t"<<10*(45-rand4)<<"\n"<<"===========================\n"<<endl;
outfile.close();
}
break;
//猎手
case(3):
{
cout<<"力量\t\t\t"<<rand1<<"\n"<<"=====================================\n"
<<"敏捷\t\t\t"<<45-rand4-rand5<<"\n"<<"============================\n"
<<"体力\t\t\t"<<rand1<<"\n"<<"=====================================\n"
<<"智力\t\t\t"<<55-rand1<<"\n"<<"==================================\n"
<<"智慧\t\t\t"<<rand5<<"\n"<<"=====================================\n"
<<"生命值\t\t\t"<<20*rand1<<"\n"<<"================================\n"
<<"魔法值\t\t\t"<<10*(55-rand1+rand5)<<"\n"<<"=====================\n";
ofstream outfile;
outfile.open("data.txt",ios::app);
outfile<<"力量\t\t\t"<<rand1<<"\n"<<"=====================================\n"
<<"敏捷\t\t\t"<<45-rand4-rand5<<"\n"<<"============================\n"
<<"体力\t\t\t"<<rand1<<"\n"<<"=====================================\n"
<<"智力\t\t\t"<<55-rand1<<"\n"<<"==================================\n"
<<"智慧\t\t\t"<<rand5<<"\n"<<"=====================================\n"
<<"生命值\t\t\t"<<20*rand1<<"\n"<<"================================\n"
<<"魔法值\t\t\t"<<10*(55-rand1+rand5)<<"\n"<<"=====================\n"<<endl;
outfile.close();}
break;
//祭司
case(4):
{
cout<<"力量\t\t\t"<<45-rand4-rand5<<"\n"<<"============================\n"
<<"敏捷\t\t\t"<<50-2*(45-rand4-rand5)<<"\n"<<"=====================\n"
<<"体力\t\t\t"<<45-rand4-rand5<<"\n"<<"============================\n"
<<"智力\t\t\t"<<rand6<<"\n"<<"=====================================\n"
<<"智慧\t\t\t"<<50-rand6<<"\n"<<"==================================\n"
<<"生命值\t\t\t"<<20*(45-rand4-rand5)<<"\n"<<"=====================\n"
<<"魔法值\t\t\t"<<10*50<<"\n"<<"===================================\n";
ofstream outfile;
outfile.open("data.txt",ios::app);
outfile<<"力量\t\t\t"<<45-rand4-rand5<<"\n"<<"============================\n"
<<"敏捷\t\t\t"<<50-2*(45-rand4-rand5)<<"\n"<<"=====================\n"
<<"体力\t\t\t"<<45-rand4-rand5<<"\n"<<"============================\n"
<<"智力\t\t\t"<<rand6<<"\n"<<"=====================================\n"
<<"智慧\t\t\t"<<50-rand6<<"\n"<<"==================================\n"
<<"生命值\t\t\t"<<20*(45-rand4-rand5)<<"\n"<<"=====================\n"
<<"魔法值\t\t\t"<<10*50<<"\n"<<"===================================\n"<<endl;
outfile.close();
}
break;
//巫师
case(5):
{
cout<<"力量\t\t\t"<<rand5<<"\n"<<"=====================================\n"
<<"敏捷\t\t\t"<<rand4<<"\n"<<"=====================================\n"
<<"体力\t\t\t"<<40-rand4-rand5<<"\n"<<"============================\n"
<<"智力\t\t\t"<<rand4<<"\n"<<"=====================================\n"
<<"智慧\t\t\t"<<60-rand4<<"\n"<<"==================================\n"
<<"生命值\t\t\t"<<20*(40-rand4-rand5)<<"\n"<<"=====================\n"
<<"魔法值\t\t\t"<<10*60<<"\n"<<"===================================\n";
ofstream outfile;
outfile.open("data.txt",ios::app);
outfile<<"力量\t\t\t"<<rand5<<"\n"<<"=====================================\n"
<<"敏捷\t\t\t"<<rand4<<"\n"<<"=====================================\n"
<<"体力\t\t\t"<<40-rand4-rand5<<"\n"<<"============================\n"
<<"智力\t\t\t"<<rand4<<"\n"<<"=====================================\n"
<<"智慧\t\t\t"<<60-rand4<<"\n"<<"==================================\n"
<<"生命值\t\t\t"<<20*(40-rand4-rand5)<<"\n"<<"=====================\n"
<<"魔法值\t\t\t"<<10*60<<"\n"<<"===================================\n"<<endl;
outfile.close();
}
break;
default:;
}
}