Game Development Reference
In-Depth Information
Listing 10-14. Adding a
Room
Array to
Game
class Game
{
private:
static const unsigned int m_numberOfRooms = 4;
Room m_rooms[m_numberOfRooms];
Player m_player;
void InitializeRooms();
void WelcomePlayer();
void GivePlayerOptions() const;
void GetPlayerInput(std::string& playerInput) const;
PlayerOptions EvaluateInput(std::string& playerInput) const;
public:
void RunGame();
};
The
Game
class now has an array that stores all of the
Room
objects our game requires. We can
increase the number of rooms by increasing the value stored in the
static const
variable
m_numberOfRooms
. The new method,
InitializeRooms
, is shown in Listing 10-15 and is responsible
for creating the connections between the different
Room
objects.
Listing 10-15.
Game::InitializeRooms
void Game::InitializeRooms()
{
// Room 0 heads North to Room 1
m_rooms[0].AddRoom(Room::JoiningDirections::North, &(m_rooms[1]));
// Room 1 heads East to Room 2, South to Room 0 and West to Room 3
m_rooms[1].AddRoom(Room::JoiningDirections::East, &(m_rooms[2]));
m_rooms[1].AddRoom(Room::JoiningDirections::South, &(m_rooms[0]));
m_rooms[1].AddRoom(Room::JoiningDirections::West, &(m_rooms[3]));
// Room 2 heads West to Room 1
m_rooms[2].AddRoom(Room::JoiningDirections::West, &(m_rooms[1]));
// Room 3 heads East to Room 1
m_rooms[3].AddRoom(Room::JoiningDirections::East, &(m_rooms[1]));
}
It is important to make sure that any
Room
that connects to another also has that
Room
connect back
or the player might be able to get into a
Room
and not able to get back out. Listing 10-16 shows an
option for where you can call
InitializeRooms
.