void Hero::move(const Direction& direction) { // Get new room RoomLink* newRoomLink{ _location->getDirection(direction) }; string moveError{ "Cannot move here!" }; string accessibleError{ "This direction is not accessible!" }; if (!newRoomLink) { IOHelper::writeLine(moveError); return; } if (!newRoomLink->isAccessible()) { IOHelper::writeLine(accessibleError); return; } Room* newRoom = &newRoomLink->getRoom(); if (!newRoom) { IOHelper::writeLine(moveError); return; } newRoomLink->setVisited(); newRoom->getDirection(newRoom->swapDirection(direction))->setVisited(); // Move the hero to the new location newRoom->setHero(this); _location->setHero(nullptr); _location = newRoom; }
void RestCommand::execute(Game& game) { if (game.getHero().getLocation().hasEnemies() && game.getHero().getLocation().hasAliveEnemies()) { IOHelper::writeLine("You cannot rest when there are enemies in your room."); IOHelper::writeLine(""); return; } IOHelper::writeLine("You decided to take a nap in the middle of a scary dungeon. Right."); IOHelper::writeLine(""); game.getHero().restoreFullHealthAndMana(); if (!Random::roll(30)) { IOHelper::writeLine(""); return; } Room& heroRoom = game.getHero().getLocation(); // Mark all the vertices as not visited vector<vector<bool>>visited; for (int x{ 0 }; x < game.getXSize(); x++) { vector<bool> xVec; for (int y{ 0 }; y < game.getYSize(); y++) { xVec.push_back(false); } visited.push_back(xVec); } // Create a queue list<Room*> queue; // Mark the current node as visited and enqueue it Room* currentRoom = &game.getHero().getLocation(); visited[currentRoom->getX()][currentRoom->getY()] = true; queue.push_back(currentRoom); while (!queue.empty()) { // Dequeue a vertex from queue and check it currentRoom = queue.front(); queue.pop_front(); if (currentRoom->hasEnemies() && currentRoom->hasAliveEnemies()) { for (int i{ Random::getRandomNumber(0, currentRoom->getEnemies().size() - 1) }; i >= 0; i--) { // Skip dead enemies if (!currentRoom->getEnemies().at(i)->isDead()) { heroRoom.getEnemies().push_back(currentRoom->getEnemies().at(i)); IOHelper::writeLine(currentRoom->getEnemies().at(i)->getName() + " enters your room!!!"); currentRoom->getEnemies().erase(remove(currentRoom->getEnemies().begin(), currentRoom->getEnemies().end(), currentRoom->getEnemies().at(i))); } } IOHelper::writeLine(""); return; } for (int i = 0; i < 4; i++) { Direction dir; switch (i) { case 0: dir = Direction::East; break; case 1: dir = Direction::West; break; case 2: dir = Direction::South; break; case 3: dir = Direction::North; break; } // Can we go in that direction? if (currentRoom->getDirection(dir) && currentRoom->getDirection(dir)->isAccessible()) { Room& room = currentRoom->getDirection(dir)->getRoom(); // If we haven't been there, add it to the queue if (!visited[room.getX()][room.getY()]) { visited[room.getX()][room.getY()] = true; queue.push_back(&room); } } } } }