首页 > 解决方案 > 获取 0xC0000005 访问冲突错误并不确定如何修复它

问题描述

我在我的 Zork 程序中添加了一种方法,可以在玩家移动时随机移动游戏中的敌人。我通过首先检查敌人是否可以漫游来做到这一点。move 方法将方向数组打乱并循环遍历数组,如果敌人没有导致 nullptr(墙),则敌人会朝该方向前进。但是经过几次移动后,程序退出了。对于 C++ 和它的所有内存分配礼仪来说还是很新的。也在寻找更好的方法来随机移动敌人。谢谢!go 方法读取一个方向,如果它不是 nullptr,则将玩家当前房间更改为该方向的房间。

void Game::go(string direction) {
    Room *next = player.getCurrentRoom()->getExit(direction);

    if (next != nullptr) {
        if (!next->lockCheck(next)) {        //check for no lock on door
            player.setCurrentRoom(next);
            player.setStamina(player.getStamina() - 1);
            EventManager::getInstance().trigger("enterRoom", next);
            for (auto &enemy : enemies) {
                if (enemy->roamingCheck(enemy)) {
                    enemy->Move(enemy);
                    enemy->setStamina(enemy->getStamina() - 1);
                }
            }

void Character::Move(Character *enemy) {
    srand(time(NULL));
    Room *next;
    for(int i = 0; i < directions->size() - 1; i++) {
        int j = i + rand() % (directions->size() - i);      //Shuffle array of directions
        std::swap(directions[i], directions[j]);
    }
    for(int i = 0; i < directions->size(); i++) {
        if(enemy->getCurrentRoom()->getExit(directions[i]) != nullptr) {
            next = enemy->getCurrentRoom()->getExit(directions[i]);
            enemy->setCurrentRoom(next);
            break;
        }
    }
}

标签: c++c++11runtime-error

解决方案


推荐阅读