爱编程的鱼 2023-10-03 18:55 采纳率: 100%
浏览 11
已结题

请问这个代码为什么不能在DEVc++里运行

请问这个代码为什么不能在DEVc++里运行
#include <iostream>
#include <cstdlib>
#include <ctime>

const int MAP_WIDTH = 10;
const int MAP_HEIGHT = 10;

class Character {
public:
    Character(int startX, int startY) : x(startX), y(startY), health(100) {}

    int getX() const {
        return x;
    }

    int getY() const {
        return y;
    }

    void move(int dx, int dy) {
        if (x + dx >= 0 && x + dx < MAP_WIDTH) {
            x += dx;
        }
        if (y + dy >= 0 && y + dy < MAP_HEIGHT) {
            y += dy;
        }
    }

    void attack(class Zombie& target);

    bool isAlive() const {
        return health > 0;
    }

private:
    int x;
    int y;
    int health;
};

class Zombie {
public:
    Zombie(int startX, int startY) : x(startX), y(startY), health(50) {}

    int getX() const {
        return x;
    }

    int getY() const {
        return y;
    }

    void moveTowards(const Character& target) {
        int dx = target.getX() - x;
        int dy = target.getY() - y;

        if (dx < 0) {
            x -= 1;
        } else if (dx > 0) {
            x += 1;
        }

        if (dy < 0) {
            y -= 1;
        } else if (dy > 0) {
            y += 1;
        }
    }

    void attack(Character& target) {
        target.health -= 15;
        std::cout << "Zombie attacks! Target's health: " << target.health << std::endl;
    }

    bool isAlive() const {
        return health > 0;
    }

private:
    int x;
    int y;
    int health;
};

void printMap(const Character& character, const Zombie& zombie) {
    for (int i = 0; i < MAP_HEIGHT; i++) {
        for (int j = 0; j < MAP_WIDTH; j++) {
            if (i == character.getY() && j == character.getX()) {
                std::cout << "C ";
            } else if (i == zombie.getY() && j == zombie.getX()) {
                std::cout << "Z ";
            } else {
                std::cout << ". ";
            }
        }
        std::cout << std::endl;
    }
}

void Character::attack(Zombie& target) {
    target.health -= 20;
    std::cout << "Character attacks! Target's health: " << target.health << std::endl;
    if (!target.isAlive()) {
        std::cout << "Zombie is dead!" << std::endl;
    }
}

int main() {
    std::srand(std::time(0));

    Character character(2, 2);
    Zombie zombie(7, 7);

    while (character.isAlive() && zombie.isAlive()) {
        printMap(character, zombie);

        char action;
        std::cout << "Enter action (w/a/s/d to move, q to quit): ";
        std::cin >> action;

        if (action == 'q') {
            break;
        }

        int dx = 0, dy = 0;
        if (action == 'w') {
            dy = -1;
        } else if (action == 'a') {
            dx = -1;
        } else if (action == 's') {
            dy = 1;
        } else if (action == 'd') {
            dx = 1;
        }

        character.move(dx, dy);
        zombie.moveTowards(character);

        if (std::rand() % 2 == 0) {
            character.attack(zombie);
        } else {
            zombie.attack(character);
        }
    }

    std::cout << "Game over!" << std::endl;

    return 0;
}


```c++


```

  • 写回答

2条回答 默认 最新

  • [PE]经典八炮 2023-10-03 19:16
    关注

    什么叫不能运行?是编译错误还是什么?如果是,把错误信息发出来啊!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月12日
  • 已采纳回答 10月4日
  • 创建了问题 10月3日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵