实例变量未更新

时间:2015-06-12 23:29:15

标签: c++ video

当我开始我的小游戏时,战斗开始,但是每次转弯之后,玩家都会进行攻击但是健康状况不会下降(即游戏永远不会结束,并且它会一遍又一遍地保持相同的健康状态)。我知道它与inflictDamage系统有关。任何人都可以看到我的错误吗?

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <limits>

using namespace std;

class Hulk
{
private:
    string playerName;
    string color;
    double weight; //In kilograms
    double height; //In centimeters
    double health;
    int strength;
    int speed;
    vector<char> moveSet;
public:
    Hulk(const string& playerName, string color):
        playerName(playerName), color(color) {
            this->weight = 500.0;
            this->height = 200.0;
            this->health = 110.0;
            this->strength = 200;
            this->speed = 50;
            moveSet.push_back('P');
            moveSet.push_back('K');
            moveSet.push_back('S');
            if(playerName == " ")
            {
                cout << "Not a valid name!" << endl;
                exit(1);
            }
    }
    string getPlayerName()
    {
        return this->playerName;
    }
    double getWeight()
    {
        return this->weight;
    }
    double getHeight()
    {
        return this->height;
    }
    double getHealth()
    {
        return this->health;
    }
    void setHealth(double x)
    {
        this->health = x;
    }
    void takeDamage(double x)
    {
        this->health -= x;
    }
    void inflictDamage(double x, Hulk enemy)
    {
        enemy.takeDamage(x);
    }
    void smash(Hulk x)
    {
        this->inflictDamage(50.0, x);
    }
    void punch(Hulk x)
    {
        this->inflictDamage(25.0, x);
    }
    void kick(Hulk x)
    {
        this->inflictDamage(30.0, x);
    }
    vector<char> getMoveSet()
    {
        return this->moveSet;
    }
};
int main()
{
    /** Creates a random number. */
    srand(static_cast<unsigned>(time(0)));

    cout << "This is a hulk battle test." << endl;
    vector<string> names(3);
    names[0] = "Randy";
    names[1] = "Chris";
    names[2] = "Carlos";
    cout << "Please enter a name: ";
    string playerName;
    cin >> playerName;
    Hulk player(playerName, "green");
    int r = rand() % 3;
    Hulk enemy(names[r], "green");
    cout << player.getPlayerName() << " vs. " << enemy.getPlayerName() << "!" << endl;
    bool gameOver = false;
    while(gameOver == false)
    {
        char playerTurn;
        bool playerMove = false;
        while(playerMove == false)
        {
            cout << "Your turn, what will you do? ((P)unch, (K)ick, (S)mash): ";
            cin >> playerTurn;
            if(playerTurn == 'P')
            {
                player.punch(enemy);
                playerMove = true;
            }
            else if(playerTurn == 'K')
            {
                player.kick(enemy);
                playerMove = true;
            }
            else if(playerTurn == 'S')
            {
                player.smash(enemy);
                playerMove = true;
            }
            else
            {
                cout << "This is not a valid move. Please try again." << endl;
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
            }
        }
        if(enemy.getHealth() <= 0.0)
        {
            cout << "You have defeated " << enemy.getPlayerName() << ". Game over!" << endl;
            gameOver = true;
        }
        char enemyTurn;
        bool enemyMove = false;
        while(enemyMove == false)
        {
            vector<char> enemyMoves = enemy.getMoveSet();
            int moveNumber = rand() % 3;
            char move = enemyMoves[moveNumber];
            if(move == 'P')
            {
                enemy.punch(player);
                cout << "You have been punched and lost 25 health. You are now at " << player.getHealth() << " health." << endl;
                enemyMove = true;
            }
            else if(move == 'K')
            {
                enemy.kick(player);
                cout << "You have been kicked and lost 30 health. You are now at " << player.getHealth() << " health." << endl;
                enemyMove = true;
            }
            else if(move == 'S')
            {
                enemy.smash(player);
                cout << "You have been smashed and lost 50 health. You are now at " << player.getHealth() << " health." << endl;
                enemyMove = true;
            }
        }
        if(player.getHealth() <= 0.0)
        {
            cout << enemy.getPlayerName() << " has defeated you! Game over!" << endl;
            gameOver = true;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这样的方法
void inflictDamage(double x, Hulk enemy)
{
    enemy.takeDamage(x);
}

无法更改对象enemy,因为该对象通过值&#34;传递,这意味着,每次调用inflictDamage(x,e)时,都会创建e的副本1}}和takeDamage将应用于此副本,而不是原始e

你可能想要的是

void inflictDamage(double x, Hulk &enemy)
{
    enemy.takeDamage(x);
}

使enemy传递&#34;通过引用&#34;。