Game Development Reference
In-Depth Information
cin >> player.m_name;
cout << endl << "Hello " << player.m_name << endl;
}
We create a new function,
GivePlayerOptions
, to display the different choices players currently have
available to them.
void GivePlayerOptions()
{
cout <<
"What would you like to do? (Enter a corresponding number)"
<< endl << endl;
cout << "1: Quit" << endl << endl;
}
The
GetPlayerInput
function reads the player's commands from the console and stores them into a
string
reference.
void GetPlayerInput(string& playerInput)
{
cin >> playerInput;
}
We use an
enum
,
PlayerOptions
, to represent the choices the players can enter. For now we only
have representations of
Quit
and
None
.
enum class PlayerOptions
{
Quit,
None
};
This function takes a reference to a string that represents a choice that could be made by the player.
The literal string
"1"
is used to represent the
Quit
option at the moment.
We use an
if...else
statement here to decide whether the player has entered an option that we
recognize. Strings can be compared using the
compare
function and will return
0
if the two strings
match. If the player's input matched
"1"
we set the
chosenOption
value to
PlayerOptions::Quit
, and
it is initialized to
PlayerOptions::None. chosenOption
is used as the return value of the function.
PlayerOptions EvaluateInput(string& playerInput)
{
PlayerOptions chosenOption = PlayerOptions::None;
if (playerInput.compare("1") == 0)
{
cout << "You have chosen to Quit!" << endl << endl;
chosenOption = PlayerOptions::Quit;
}