Terminal Games
Simple games that run in the terminal.
Loading...
Searching...
No Matches
TerminalGames::Hangman Class Reference

Implementation of the Hangman game: https://en.wikipedia.org/wiki/Hangman_(game) More...

#include "Hangman.hpp"

Inheritance diagram for TerminalGames::Hangman:
TerminalGames::Game

Public Member Functions

 Hangman (const bool &p_useAnsiEscapeCodes)
 Constructs a new Hangman object.
virtual void Play () final
 The main orchestration loop for all games.

Private Member Functions

void SetupGame () override
 Clears and sets all member variables to their game start default.
void GetUserOptions () override
 Prompt the user for their choice on various game-related options.
void UpdateGameInformation () override
 Updates GameInformation to match the current state of the game.
bool IsGameOver () override
 Check whether the game is over.
void ToggleCurrentPlayer () override
 Change the current player to the other player.
bool IsCurrentTurnUsers () override
 Check whether the current turn should be executed by the user.
void ExecuteUserCommand () override
 Prompt the user to enter their command for the current turn.
void ExecuteComputerCommand () override
 Get a random command from the computer.
void GameOver () override
 Display the game over message and prompt the user whether they would like to play again or quit the game.
void RestartGame () override
 Update variables to allow for the game to be restarted with the same user options.
void ResetGame () override
 Update variables to allow for the game to be reset and so the user will be asked for new options.
void GetPlayerCount ()
 Prompts the user to select how many players will be playing the game.
void GetUserPlayerChoice ()
 Prompts the user to select which player the will be playing the game as (only supported if playing against the computer).
void GetComputerSpeed ()
 Prompts the user to select how the speed of the computer decision making (this does not affect the difficulty of the computer).
void GetWordFromUser ()
 Prompts the user to enter a word to be guessed (whether the word is actually a word is not checked) when the user is the word setter.
void GetWordFromComputer ()
 Gets a random word, from the word list loaded during the setup, when the computer is the word setter.
void ExecuteGeneralCommand (const char &p_guess)
 Checks the single-letter guess against the word, updates the current guess of the word and the error count depending upon if the guess if correct.

Private Attributes

PageBuilder m_pageBuilder
 Used to build pages required by the game.
GameInformation m_gameInformation
 Used to package up the current state of the game so it can be used by m_pageBuilder.
std::default_random_engine m_randomNumberGenerator
 Used to randomly select the word to be guessed from Globals::G_HANGMAN_COMPUTER_WORDS when the computer is the word setter and used to randomly select a letter to guess when the computer is the guesser.
std::vector< char > m_commandsRemaining
 The letters which remain to be guessed.
std::vector< char > m_incorrectGuesses
 The letters which were guessed by the guesser and are incorrect.
std::string m_computerSpeedName
 The name of the user selected computer speed.
std::string m_currentGuessOfWord
 The current guess state of the word to be guessed from the perspective of the guesser.
std::string m_playerCount
 The count of the user selected number of players.
std::string m_userPlayerChoice
 The choice of whether the user has selected to be the guesser or word setter.
std::string m_wordToBeGuessed
 The word to be guessed by the guesser.
uint32_t m_computerSpeed
 The computer speed determined by the amount of seconds the computer must wait before executing it's command.
uint32_t m_turnCount
 The number of turns that have occurred.
char m_currentLetterSelected
 The letter to display to the user to represent what their current selected guess is.
bool m_hasSavedGameOptions
 Whether the user has selected all the game options (true) or not/partially (false). This is used to determine whether to re-prompt the user to select the game options when restarting the game.
bool m_isGameOver
 Whether the game is over (true) or not (false).
bool m_saveGameOptions
 Whether to save the user's currently selected game options (true) and restart the game or not (false) and reset the game back to the initial state which will re-prompt the user for the game options.

Detailed Description

Implementation of the Hangman game: https://en.wikipedia.org/wiki/Hangman_(game)

Definition at line 22 of file Hangman.hpp.

Constructor & Destructor Documentation

◆ Hangman()

TerminalGames::Hangman::Hangman ( const bool & p_useAnsiEscapeCodes)
explicit

Constructs a new Hangman object.

Parameters
p_useAnsiEscapeCodesWhether to use use ANSI escapes codes (true) or only extended ASCII characters (false).

Definition at line 20 of file Hangman.cpp.

Member Function Documentation

◆ SetupGame()

void TerminalGames::Hangman::SetupGame ( )
overrideprivatevirtual

Clears and sets all member variables to their game start default.

Implements TerminalGames::Game.

Definition at line 32 of file Hangman.cpp.

◆ GetUserOptions()

void TerminalGames::Hangman::GetUserOptions ( )
overrideprivatevirtual

Prompt the user for their choice on various game-related options.

Implements TerminalGames::Game.

Definition at line 43 of file Hangman.cpp.

◆ UpdateGameInformation()

void TerminalGames::Hangman::UpdateGameInformation ( )
overrideprivatevirtual

Updates GameInformation to match the current state of the game.

Implements TerminalGames::Game.

Definition at line 80 of file Hangman.cpp.

◆ IsGameOver()

bool TerminalGames::Hangman::IsGameOver ( )
overrideprivatevirtual

Check whether the game is over.

Returns
true If the game is over.
false If the game is NOT over.

Implements TerminalGames::Game.

Definition at line 94 of file Hangman.cpp.

◆ ToggleCurrentPlayer()

void TerminalGames::Hangman::ToggleCurrentPlayer ( )
overrideprivatevirtual

Change the current player to the other player.

Implements TerminalGames::Game.

Definition at line 114 of file Hangman.cpp.

◆ IsCurrentTurnUsers()

bool TerminalGames::Hangman::IsCurrentTurnUsers ( )
overrideprivatevirtual

Check whether the current turn should be executed by the user.

Returns
true If the current turn is the users'.
false If the current turn is NOT the users'.

Implements TerminalGames::Game.

Definition at line 116 of file Hangman.cpp.

◆ ExecuteUserCommand()

void TerminalGames::Hangman::ExecuteUserCommand ( )
overrideprivatevirtual

Prompt the user to enter their command for the current turn.

Warning
This function does not catch any exceptions thrown by Terminal::GetUserChoiceFromQuitMenus().

Implements TerminalGames::Game.

Definition at line 121 of file Hangman.cpp.

◆ ExecuteComputerCommand()

void TerminalGames::Hangman::ExecuteComputerCommand ( )
overrideprivatevirtual

Get a random command from the computer.

Implements TerminalGames::Game.

Definition at line 165 of file Hangman.cpp.

◆ GameOver()

void TerminalGames::Hangman::GameOver ( )
overrideprivatevirtual

Display the game over message and prompt the user whether they would like to play again or quit the game.

Warning
This function does not catch any exceptions thrown by Terminal::GetUserChoiceFromGameOverMenu().

Implements TerminalGames::Game.

Definition at line 176 of file Hangman.cpp.

◆ RestartGame()

void TerminalGames::Hangman::RestartGame ( )
overrideprivatevirtual

Update variables to allow for the game to be restarted with the same user options.

Implements TerminalGames::Game.

Definition at line 181 of file Hangman.cpp.

◆ ResetGame()

void TerminalGames::Hangman::ResetGame ( )
overrideprivatevirtual

Update variables to allow for the game to be reset and so the user will be asked for new options.

Implements TerminalGames::Game.

Definition at line 186 of file Hangman.cpp.

◆ GetPlayerCount()

void TerminalGames::Hangman::GetPlayerCount ( )
private

Prompts the user to select how many players will be playing the game.

Warning
This function does not catch any exceptions thrown by Terminal::GetUserChoiceFromGameMenus().

Definition at line 192 of file Hangman.cpp.

◆ GetUserPlayerChoice()

void TerminalGames::Hangman::GetUserPlayerChoice ( )
private

Prompts the user to select which player the will be playing the game as (only supported if playing against the computer).

Warning
This function does not catch any exceptions thrown by Terminal::GetUserChoiceFromGameMenus().

Definition at line 201 of file Hangman.cpp.

◆ GetComputerSpeed()

void TerminalGames::Hangman::GetComputerSpeed ( )
private

Prompts the user to select how the speed of the computer decision making (this does not affect the difficulty of the computer).

Warning
This function does not catch any exceptions thrown by Terminal::GetUserChoiceFromGameMenus().

Definition at line 210 of file Hangman.cpp.

◆ GetWordFromUser()

void TerminalGames::Hangman::GetWordFromUser ( )
private

Prompts the user to enter a word to be guessed (whether the word is actually a word is not checked) when the user is the word setter.

Warning
This function does not catch any exceptions thrown by Terminal::GetUserChoiceFromQuitMenus().

Definition at line 220 of file Hangman.cpp.

◆ GetWordFromComputer()

void TerminalGames::Hangman::GetWordFromComputer ( )
private

Gets a random word, from the word list loaded during the setup, when the computer is the word setter.

Definition at line 256 of file Hangman.cpp.

◆ ExecuteGeneralCommand()

void TerminalGames::Hangman::ExecuteGeneralCommand ( const char & p_guess)
private

Checks the single-letter guess against the word, updates the current guess of the word and the error count depending upon if the guess if correct.

Parameters
p_guessA single-letter to check against the word to be guessed.

Definition at line 261 of file Hangman.cpp.

◆ Play()

virtual void TerminalGames::Game::Play ( )
inlinefinalvirtualinherited

The main orchestration loop for all games.

Definition at line 31 of file Game.hpp.

Member Data Documentation

◆ m_pageBuilder

PageBuilder TerminalGames::Hangman::m_pageBuilder
private

Used to build pages required by the game.

Definition at line 106 of file Hangman.hpp.

◆ m_gameInformation

GameInformation TerminalGames::Hangman::m_gameInformation
private

Used to package up the current state of the game so it can be used by m_pageBuilder.

Definition at line 111 of file Hangman.hpp.

◆ m_randomNumberGenerator

std::default_random_engine TerminalGames::Hangman::m_randomNumberGenerator
private

Used to randomly select the word to be guessed from Globals::G_HANGMAN_COMPUTER_WORDS when the computer is the word setter and used to randomly select a letter to guess when the computer is the guesser.

Definition at line 117 of file Hangman.hpp.

◆ m_commandsRemaining

std::vector<char> TerminalGames::Hangman::m_commandsRemaining
private

The letters which remain to be guessed.

Definition at line 122 of file Hangman.hpp.

◆ m_incorrectGuesses

std::vector<char> TerminalGames::Hangman::m_incorrectGuesses
private

The letters which were guessed by the guesser and are incorrect.

Definition at line 127 of file Hangman.hpp.

◆ m_computerSpeedName

std::string TerminalGames::Hangman::m_computerSpeedName
private

The name of the user selected computer speed.

Definition at line 132 of file Hangman.hpp.

◆ m_currentGuessOfWord

std::string TerminalGames::Hangman::m_currentGuessOfWord
private

The current guess state of the word to be guessed from the perspective of the guesser.

Definition at line 137 of file Hangman.hpp.

◆ m_playerCount

std::string TerminalGames::Hangman::m_playerCount
private

The count of the user selected number of players.

Definition at line 142 of file Hangman.hpp.

◆ m_userPlayerChoice

std::string TerminalGames::Hangman::m_userPlayerChoice
private

The choice of whether the user has selected to be the guesser or word setter.

Definition at line 147 of file Hangman.hpp.

◆ m_wordToBeGuessed

std::string TerminalGames::Hangman::m_wordToBeGuessed
private

The word to be guessed by the guesser.

Definition at line 152 of file Hangman.hpp.

◆ m_computerSpeed

uint32_t TerminalGames::Hangman::m_computerSpeed
private

The computer speed determined by the amount of seconds the computer must wait before executing it's command.

Definition at line 157 of file Hangman.hpp.

◆ m_turnCount

uint32_t TerminalGames::Hangman::m_turnCount
private

The number of turns that have occurred.

Definition at line 162 of file Hangman.hpp.

◆ m_currentLetterSelected

char TerminalGames::Hangman::m_currentLetterSelected
private

The letter to display to the user to represent what their current selected guess is.

Definition at line 167 of file Hangman.hpp.

◆ m_hasSavedGameOptions

bool TerminalGames::Hangman::m_hasSavedGameOptions
private

Whether the user has selected all the game options (true) or not/partially (false). This is used to determine whether to re-prompt the user to select the game options when restarting the game.

Definition at line 173 of file Hangman.hpp.

◆ m_isGameOver

bool TerminalGames::Hangman::m_isGameOver
private

Whether the game is over (true) or not (false).

Definition at line 178 of file Hangman.hpp.

◆ m_saveGameOptions

bool TerminalGames::Hangman::m_saveGameOptions
private

Whether to save the user's currently selected game options (true) and restart the game or not (false) and reset the game back to the initial state which will re-prompt the user for the game options.

Definition at line 184 of file Hangman.hpp.


The documentation for this class was generated from the following files: