Game Development Reference
In-Depth Information
The user's input has been captured into a
stringstream
that is used to get the unsigned
int
option
selected. There is then a
try…catch
block. This is exception handling in C++. Exceptions can be
generated by any code you call that does not result in a valid operation. In our case we could be
passing an invalid option to the
Room::EvaluateInput
method. This invalid option would be used to
try to get a pointer from the static options
map
and when the key could not be found, the
map
code
would throw an
out_of_range
exception.
Note
If we failed to catch this exception, our program execution would stop, but by handling the exception
we have been able to show the user an appropriate error message and carry on.
The last method to get an update is the
RunGame
method. Listing 19-20 shows the updates to
this method.
Listing 19-20.
Game::RunGame
void Game::RunGame()
{
InitializeRooms();
WelcomePlayer();
bool playerWon = false;
bool playerQuit = false;
while (playerQuit == false && playerWon == false)
{
GivePlayerOptions();
stringstream playerInputStream;
GetPlayerInput(playerInputStream);
PlayerOptions selectedOption = EvaluateInput(playerInputStream);
playerQuit = selectedOption == PlayerOptions::Quit;
playerWon = m_dragon.IsAlive() == false && m_orc.IsAlive() == false;
}
if (playerWon == true)
{
cout << "Congratulations, you rid the dungeon of monsters!" << endl;
cout << "Type goodbye to end" << endl;
std::string input;
cin >>input;
}
}