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

请问这个代码为什么不能在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 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型