c++ - 获取 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;
}
}
}
解决方案
推荐阅读
- sql-server - 通过混合连接从 Azure 应用程序连接到本地本地 SQL 服务器时出现错误:10054
- html - 边框半径仅围绕div的顶部?
- git - ![remote denied] master -> master (pre-receive hook denied) (尝试推送大文件后)
- python - Statsmodels 无法导入模块
- javascript - 当状态改变时,我的整个 React 组件是否会不必要地重新渲染?
- c# - 如何在 Angular 6/8 的弹出对话框屏幕中显示项目列表
- javascript - 无法读取未定义的某些属性(React/Javascript)
- r - 使用 pdf_text 将几个 PDF 文件读入 R
- php - 从 PHP 连接到 MSSQL Server
- php - 我已将 php 版本 7.2 升级到 7.4,现在在 myphpadmin 面板中出现错误。我附上屏幕短片