0

所以基本上,我必须编写一个类似于 RPG 游戏的程序,其中有不同类型的生物。每个生物都是 Creature 类的对象,并且具有生命值、力量等的成员变量。

我遇到的麻烦是编写处理类之间处理和承受伤害的函数。

#include <iostream>
#include <string>
#include <stdlib.h>

using std::cout;
using std::cin;
using std::endl;
using std::string;

//Creature Base Class (Type 0)
class Creature
{
  public:
    Creature(int, int, int);
    int gettype() {return type;}
    int getstrength(){return strength;}
    int gethitpoints() {return hitpoints;}
    void sethitpoints(int);
    string getspecies();
    void printstats();
    void dealdamage(Creature, Creature);
    void takedamage(int);

  private:
    int type;
    int strength;
    int hitpoints;
};

Creature::Creature(int t, int s, int hp)
{
  type = t;
  strength = s;
  hitpoints = hp;
}

void Creature::printstats()
{
  cout << "This creature has: " << endl;
  cout << strength << " strength" << endl;
  cout << hitpoints << " hitpoints" << endl;
  cout << "and is of type " << type << "(" << getspecies() << ")" << endl;
}

void Creature::sethitpoints(int a)
{
  hitpoints = a;
}

string Creature::getspecies()
{
  switch(type)
  {
    case 0: return "Creature";
    case 1: return "Human";
    case 2: return "Elf";
    case 3: return "Demon";
    case 4: return "Balrog";
    case 5: return "Cyberdemon";
  }
}

void Creature::dealdamage(Creature dealer, Creature target)
{
  srand(5);
  int damage;
  damage = rand() % strength+1;
  cout << dealer.getspecies() << " inflicts " << damage;
  cout << " damage to " << target.getspecies() << "!" << endl;
  target.takedamage(damage);
}

void Creature::takedamage(int damage)
{
  sethitpoints((gethitpoints()-damage));
}

int main()
{
  Creature monster1(0, 10, 100);
  Creature monster2(1, 7, 90);

  monster1.printstats();
  monster2.printstats();

  monster1.dealdamage(monster1, monster2);
  monster2.printstats();

  return 0;
}

现在,程序给我的输出是:

This creature has:
10 strength
100 hitpoints
and is of type 0(Creature)
This creature has:
7 strength
90 hitpoints
and is of type 1(Human)
Creature inflicts 5 damage to human!
This creature has:
7 strength
90 hitpoints
and is of type 1(Human)

所以 dealdamage() 函数似乎在工作,但是 takedamage() 函数并没有正确地改变受到伤害的生物的生命值。

任何帮助,将不胜感激。

4

1 回答 1

1

问题是无效的 Creature::dealdamage(生物经销商,生物目标)

首先,这被称为“按值传递”。构造新的“Creature”对象,并将您调用该函数的“Creature”对象的值复制到其中。程序执行,这些临时 Creature 对象 EOL - 原始 Creature 对象永远不会被触及。

您需要获取原始对象的指针或引用。但是除非你打算支持某种 3 路战斗,否则你不应该同时需要这两个项目——这是一个非静态成员函数,所以它已经在其中一个生物的上下文中运行,因此您调用它的语法: monster1.dealdamage(monster1, monster2);

像这样更改您的交易损失:

void Creature::dealdamage(Creature& target) // takes a reference
{
    //srand(5); <- this will cause rand() to always return the same value. dont do it.
    //int damage; <- don't separate declaration and assignment when you can avoid it.
    int damage = rand() % strength+1;
    cout << getspecies() << " inflicts " << damage
         << " damage to " << target.getspecies() << "!" << endl;
    target.takedamage(damage);
}

this->getspecies()如果您发现仅使用 getspecies() 不清楚,则可以使用。

代替 srand(constant value) 尝试类似 'srand(time(NULL))' 的东西,或者更好的是,在程序开始时执行一次。

于 2013-06-02T05:07:51.870 回答