I added a method to my Zork program to randomly move the enemies in the game when ever the player moves. I did this by first checking if the enemy can roam. The move method shuffles an array of directions and loops through the array and the enemy goes in that direction if it doesn't lead to a nullptr (a wall). But after a couple moves the program exits. Still quite new to C++ and all of its memory allocation etiquette. Also looking for a better way to randomly move the enemies. Thanks! The go method reads in a direction and changes the players current room to the room in that direction if it is not a 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;
}
}
}