vc++ RPD角色生成器

简介: vc++ RPD角色生成器

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.显示信息


最后向用户显示该角色的所有信息,然后询问用户是否满意,如用户不满意则重新创建,若用户满意则程序结束,并将用户创建角色的相关信息写入文件保存。


6.主要代码:

#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 output(Base &, Race &, Attribute &);//访问友元类
  };
  void Output::output(Base &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.strength << endl;
  cout << "__________________________________________" << endl;
  cout << "------------------------------------------" << endl;
    cout <<setw(14)<< "敏捷          " << t3.agility << 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.HP << endl;
    cout << "__________________________________________" << endl;
  cout << "------------------------------------------" << endl;
    cout <<setw(14)<< "法力值        " << t3.MP << endl;
    cout << "__________________________________________" << 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.output(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;
}

7.测试和调试

1)测试:

image.png

image.png

2)调试:

image.pngimage.pngimage.png

相关文章
|
16天前
|
机器学习/深度学习 人工智能 自然语言处理
C++构建 GAN 模型:生成器与判别器平衡训练的关键秘籍
生成对抗网络(GAN)是AI领域的明星,尤其在C++中构建时,平衡生成器与判别器的训练尤为关键。本文探讨了GAN的基本架构、训练原理及平衡训练的重要性,提出了包括合理初始化、精心设计损失函数、动态调整学习率、引入正则化技术和监测训练过程在内的五大策略,旨在确保GAN模型在C++环境下的高效、稳定训练,以生成高质量的结果,推动AI技术的发展。
44 10
|
5月前
|
C++ 运维
开发与运维C++问题之在重新设计后,LogFileReader类的角色发生了什么变化
开发与运维C++问题之在重新设计后,LogFileReader类的角色发生了什么变化
39 1
|
5月前
|
运维 NoSQL Redis
c++开发redis module问题之module根据Redis的角色采取不同的行为,如何解决
c++开发redis module问题之module根据Redis的角色采取不同的行为,如何解决
|
7月前
|
算法 安全 程序员
【C++ 随机数生成器】深入解析C++ 随机数生成器mersenne_twister_engine等
【C++ 随机数生成器】深入解析C++ 随机数生成器mersenne_twister_engine等
266 0
|
存储 算法 程序员
C++STL学习笔记(第一篇:stl是什么?为什么要学习stl?迭代器在stl中扮演着什么角色?)
C++STL学习笔记(第一篇:stl是什么?为什么要学习stl?迭代器在stl中扮演着什么角色?)
489 1
|
C++
C++游戏系列3:用多文件组织角色类
更多见:C++游戏系列目录 知识点:项目的多文件组织。一个项目,由多个源文件及相应的头文件构成,将声明与定义分开。 【项目-用多文件组织多个类的程序】   将上一周“项目2-带武器的游戏角色”用“一个项目多个文件”的方式实现,其中两个类的声明放在一个.h文件中,每个类的成员函数分别放一个文件,main()函数用一个文件。体会这样安排的优点。 参考解答: 1.ga
1278 0
|
C++
C++游戏系列2:角色装备武器
更多见:C++游戏系列目录 知识点:类的组合,A类的数据成员,是B类的对象,或B类的对象,做A类的数据成员    【项目-带武器的游戏角色】   设计一个武器类,其数据成员至少要有武器名、威力,还可以加上你想描述武器的其他数据。想一想要对武器实施什么处理,设计其成员函数。   在上周的游戏角色类Role基础上扩充,为每个角色创建一个武器,并在攻击(attack)
1273 0
|
C++
C++游戏系列1-角色类
更多见:C++游戏系列目录 知识点: 1、类的定义、对象 2、构造函数、析构函数 【项目-游戏中的角色类】 (1)基于下面设计的游戏中角色类,补充完整需要的成员函数,使角色能一定的规则行动或改变状态。下面代码中包含的是最基本的要求,可以根据你的设计进行扩充。 class Role { public: …… private: string n
1419 0
|
22天前
|
存储 编译器 C语言
【c++丨STL】string类的使用
本文介绍了C++中`string`类的基本概念及其主要接口。`string`类在C++标准库中扮演着重要角色,它提供了比C语言中字符串处理函数更丰富、安全和便捷的功能。文章详细讲解了`string`类的构造函数、赋值运算符、容量管理接口、元素访问及遍历方法、字符串修改操作、字符串运算接口、常量成员和非成员函数等内容。通过实例演示了如何使用这些接口进行字符串的创建、修改、查找和比较等操作,帮助读者更好地理解和掌握`string`类的应用。
31 2