因此,我在其他语言方面有相当丰富的经验,但是我对c++还是很陌生。我正在尝试创建一个简单的基于文本的rpg,其中包含玩家和随机数量的敌人。所以问题是当我尝试在其中移动时,我从未遇到过敌人,因此在创建实际的敌人对象时遇到问题,或者在检查玩家是否与敌人位于同一位置的代码中没用,或者还有别的东西。我真的很感谢,有帮助。谢谢。

顺便说一句,我的格式有点困惑。

#include <iostream>
#include <random>
#include <string>

using namespace std;

#define UP 1
#define RIGHT

class Character
{
public:
    int health, armour, speed, x, y, attackPower;
    Character()
    {
        health = 100;
        armour = 100;
        speed = 1;
    }

    bool isTouching(Character character)
    {
        if (x == character.x && y == character.y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    void move(string dir)
    {
        if (dir == "up")
        {
            y += speed;
        }
        else if (dir == "down")
        {
            y -= speed;
        }
        else if (dir == "right")
        {
            x += speed;
        }
        else if (dir == "left")
        {
            x -= speed;
        }
    }

    void takeDamage(int damage)
    {
        if (armour > damage)
        {
            armour -= damage;
        }
        else
        {
            if (armour > 0)
            {
                int remainingDamage = damage - armour;
                armour = 0;
                health -= remainingDamage;
            }
        }
        health -= damage;
    }

    void attack(Character* character)
    {
        character->takeDamage(attackPower);
    }

};

class Player: public Character
{
public:
    Player()
    {
        health = 100;
        armour = 100;
        speed = 1;
        x = 10;
        y = 10;
        attackPower = 50;

    }
};


class Enemy : public Character
{
public:
    Enemy()
    {
        health = 100;
        armour = 0;
        speed = 1;
        x = rand() % 100;
        y = rand() % 100;
        attackPower = 20;

    }
};

int main()
{

    Character player;


    Enemy *enemies;
    int numEnemies = rand() % 30 + 20;
    enemies = new Enemy[numEnemies];

    while (true)
    {
        string input;
        cout << "Enter command: ";
        cin >> input;
        if (input == "exit" || input == "quit")
        {
            break;
        }
        if (input == "move")
        {
            string direction;
            cout << "pick a direction: ";
            cin >> direction;
            player.move(direction);

            for (int i = 0; i < sizeof(enemies) / sizeof(int); i++)
            {
                if (player.isTouching(enemies[i]))
                {
                    cout << "You ran into an enemy!" << endl;
                    cout << "What would you like to do?: ";
                    string interactionInput;
                    cin >> interactionInput;
                    if (interactionInput == "attack")
                    {
                        player.attack(&enemies[i]);
                        enemies[i].attack(&player);
                        cout << "Enemy now at " << enemies[i].armour << " armour and " << enemies[i].health << " health" << endl;
                        cout << "You are now at " << player.armour << " armour and " << player.health << " health" << endl;
                    }
                }
            }

        }



    }

    system("pause");
    return 0;
}

最佳答案

sizeof(enemies)等于4或8,具体取决于虚拟内存地址空间的大小。
sizeof(int)等于2或4,具体取决于您的编译器定义(基于基础硬件)。

因此sizeof(enemies) / sizeof(int)在1到4之间。

话虽如此,您可以简单地使用numEnemies代替。

如果您静态分配了enemies数组(Enemy enemies[...]),则可以使用:

sizeof(enemies)/sizeof(Enemy)
sizeof(enemies)/sizeof(*enemies)
sizeof(enemies)/sizeof(enemies[0]) // or any other index

但是由于您是动态分配的,因此将其视为指针(大小为4或8个字节)。

关于c++ - 在C++中声明随机数量的对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24461210/

10-11 16:09