0

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;
        }
    }
}

  • Unrelated: [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – user4581301 Apr 18 '20 at 16:10
  • Worth watching: [rand() Considered Harmful](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) – Jesper Juhl Apr 18 '20 at 16:12
  • [`std::shuffle`](https://en.cppreference.com/w/cpp/algorithm/random_shuffle) could save you a bit of code. – user4581301 Apr 18 '20 at 16:14
  • Nothing jumps out at me in this code. I recommend firing up the program in whatever debugger came with your development environment, putting a breakpoint in the `go` method, and stepping through it with an eye out for the weird. If the error manifests a few moves in, you shouldn't have to do this more than a few times to watch a crash as it happens. – user4581301 Apr 18 '20 at 16:23
  • Actually tried implementing shuffle to jumble the array of strings in move but it seemed to shuffle the letters in the first string of the array i.e string[] directions = {"north", "east", "south", "west"} and shuffle changes only the letters in north instead of shuffling the strings. I probably implemented it wrong I'll have another look. Thanks! – Mikel112543 Apr 18 '20 at 17:03
  • does the shuffle only work for vectors and arrays of integers or can it be used for strings as well? If it can is it just the same implementation as it would be with integers? – Mikel112543 Apr 18 '20 at 23:29
  • `shuffle` will work for anything that can provide iterators or something that can be converted into an iterator like the locations of array elements. See the example at the bottom of the documentation page linked above for `shuffle` in action on a `vector` – user4581301 Apr 19 '20 at 01:15

0 Answers0