RPG角色生成器(C++)

来源:互联网 发布:软件无线电技术与实现 编辑:程序博客网 时间:2024/05/11 10:03

角色生成器需要记录玩家输入的名字,选择的性别,种族,职业,并且根据不同职业所随机生产的属性也不相同。

1.设计思路

首先建一个基类Base用来保存和实现,保存玩家输入的姓名和性别的选择,同时需要声明两个友缘类Output,File可以访问该类中的数据。

在基类Base基础上建一个派生类Race,用来记录玩家选择的种族,和职业,同时规定什么种族可以选择什么职业,人类可以选择全部职业;精灵不能选择狂战士,圣骑士;兽人不能选择圣骑士,刺客;矮人不能选择刺客,猎手;元素只能选择祭司,同时需要声明两个友缘类Output,File可以访问该类中的数据。

在派送类Race基础上派生一个Attribute类用来记录角色随机生产的属性,首选规定各个职业每个属性的最小值,在这个最小值的基础上,随机生成差距在3左右的属性,再根据随机生产的属性计算出该角色的血量值,法力值,同时需要声明两个友缘类Output,File可以访问该类中的数据。

建立一个Output类可以访问Base,Race,Attribute三个类中的数据,输出三个类中记录的数据。

建立一个File类可以访问Base,Race,Attribute三个类中的数据,并且将三个类中的数据进行文本输出保存。

2.类关系图

3.面对对象原则应用

      该程序中用到了,开闭原则,单一职责。

//西安科技大学计算机科学与技术 软件工程//软工1503班 //2017.5.1#include "iostream"#include <iomanip>#include "string"#include "ctime"#include "fstream"using namespace std;  int occupation_choice; //玩家所选择的职业的序号  class Base  //基础类,保存角色的姓名,性别{  protected:  char name[50];//记录角色名字  string sex; //记录性别  int sex_choice;//记录性别选择  public:  void getBase();//功能实现  friend class Output;  //友元类,用于输出角色信息  friend class File;    //友元类,将角色信息保存到文档中};  void Base::getBase()//输入角色名和性别{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 Base  //派生类,记录角色的种族、职业{  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 strength;//力量  int agility;//敏捷  int physical;//体力  int intelligence;//智力  int wisdom;//智慧  int HP;//血量  int MP;//法力值  public:  void getAttribute();  void getRandom(int a, int b, int c, int d, int e);        friend class Output;  friend class File;};    // 随机生成每项属性的值,abcd为该属性的最小值,e为第五个属性的最大值void Attribute::getRandom(int a, int b, int c, int d, int e){  int sum;  //前4项属性之和  srand((unsigned)time(NULL));  do  {  strength = a + rand() % 3;  agility = b + rand() % 3;  physical = c + rand() % 3;  intelligence = d + rand() % 3;  sum = strength + agility + physical + intelligence;  } while (((100 - e) < sum) && (sum < 100));  wisdom = 100 - sum;  HP = physical * 20;  MP = (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(Base &, Race &, Attribute &);//访问友元类  };    void Output::show(Base &t1, Race &t2, Attribute &t3)  {  cout << "*****************************" << endl;  cout <<setw(14)<<"姓名:" << t1.name << endl;cout << "*****************************" << endl;  cout <<setw(14)<< "性别:" << t1.sex << endl;cout << "*****************************" << endl;  cout <<setw(14)<< "种族:" << t2.race << endl;cout << "*****************************" << endl;  cout <<setw(14)<< "职业:" << t2.occupation << endl;cout << "*****************************" << endl;  cout <<setw(14)<< "力量:" << t3.strength << endl;cout << "*****************************" << endl;  cout <<setw(14)<< "敏捷:" << t3.agility << endl;cout << "*****************************" << endl;  cout <<setw(14)<< "体力:" << t3.physical << endl;cout << "*****************************" << endl;  cout <<setw(14)<< "智力:" << t3.intelligence << endl;cout << "*****************************" << endl;  cout <<setw(14)<< "智慧:" << t3.wisdom << endl;cout << "*****************************" << endl;  cout <<setw(14)<< "生命值:" << t3.HP << endl;cout << "*****************************" << endl;  cout <<setw(14)<< "法力值:" << t3.MP << endl;cout << "*****************************" << endl;}  class File  //将角色信息保存到文档{  public:  void file(Base &, Race &, Attribute &);  };    void File::file(Base &t1, Race &t2, Attribute &t3)  {  ofstream outfile;  outfile.open("角色数据保存", ios::trunc);cout << "*****************************" << endl;  outfile <<setw(14)<< "姓名:" << t1.name << endl;cout << "*****************************" << endl;  outfile <<setw(14)<< "性别:" << t1.sex << endl;cout << "*****************************" << endl;  outfile <<setw(14)<< "种族:" << t2.race << endl;cout << "*****************************" << endl;  outfile <<setw(14)<< "职业:" << t2.occupation << endl;cout << "*****************************" << endl;  outfile <<setw(14)<< "力量:" << t3.strength << endl;cout << "*****************************" << endl;  outfile <<setw(14)<< "敏捷:" << t3.agility << endl;cout << "*****************************" << endl;  outfile <<setw(14)<< "体力:" << t3.physical << endl;cout << "*****************************" << endl;  outfile <<setw(14)<< "智力:" << t3.intelligence << endl;cout << "*****************************" << endl;  outfile <<setw(14)<< "智慧:" << t3.wisdom << endl;cout << "*****************************" << endl;  outfile <<setw(14)<< "生命值:" << t3.HP << endl;cout << "*****************************" << endl;  outfile <<setw(14)<< "法力值:" << t3.MP << endl;cout << "*****************************" << endl;}  int main(){  Base player;  Race player_race;  Attribute player_att;  Output player_show;  File keep;  int player_choice;  do  {  player.getBase();  player_race.getRace();  player_att.getAttribute();  player_show.show(player, player_race, player_att);  cout << endl;  cout << "对生成的角色是否满意?如不满意,则重新生成" << endl;  cout << "0.满意     1.不满意" << endl;  cin >> player_choice;  } while (player_choice);  keep.file(player, player_race, player_att);  return 0;}    


0 0
原创粉丝点击