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

Implementation of the Tic-Tac-Toe game: https://en.wikipedia.org/wiki/Tic-tac-toe. More...

#include "TicTacToe.hpp"

Inheritance diagram for TerminalGames::TicTacToe:
TerminalGames::Game

Public Member Functions

 TicTacToe (const bool &p_useAnsiEscapeCodes)
 Constructs a new TicTacToe 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 computer decision making (this does not affect the difficulty of the computer).
bool ValidateCommand (const std::tuple< uint32_t, uint32_t > &p_command)
 Checks whether the command is valid.
void ExecuteGeneralCommand (const std::tuple< uint32_t, uint32_t > &p_command)
 Executes the command against the game grid.

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 choose the player to start the game and to randomly decide the computer's next command.
std::array< std::array< std::string, Globals::G_TICTACTOE_GRID_WIDTH >, Globals::G_TICTACTOE_GRID_HEIGHTm_gameGrid
 The Tic Tac Toe grid.
std::vector< std::tuple< uint32_t, uint32_t > > m_commandsRemaining
 The grid <row, column> values that have not been chosen yet.
std::tuple< uint32_t, uint32_t > m_previousCommand
 The previous grid <row, column> value that was chosen by the user. This is used to return the cursor to this position when the user is prompted to select their next command.
std::string m_computerSpeedName
 The name of the user selected computer speed.
std::string m_currentPlayer
 The name of the player whose turn it is.
std::string m_playerCount
 The count of the user selected number of players.
std::string m_userPlayerChoice
 The choice of which player the user has selected.
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.
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_hasWinner
 Whether the game has concluded with a winner (true) or whether it is a draw (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 Tic-Tac-Toe game: https://en.wikipedia.org/wiki/Tic-tac-toe.

Definition at line 25 of file TicTacToe.hpp.

Constructor & Destructor Documentation

◆ TicTacToe()

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

Constructs a new TicTacToe object.

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

Definition at line 16 of file TicTacToe.cpp.

Member Function Documentation

◆ SetupGame()

void TerminalGames::TicTacToe::SetupGame ( )
overrideprivatevirtual

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

Implements TerminalGames::Game.

Definition at line 27 of file TicTacToe.cpp.

◆ GetUserOptions()

void TerminalGames::TicTacToe::GetUserOptions ( )
overrideprivatevirtual

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

Implements TerminalGames::Game.

Definition at line 45 of file TicTacToe.cpp.

◆ UpdateGameInformation()

void TerminalGames::TicTacToe::UpdateGameInformation ( )
overrideprivatevirtual

Updates GameInformation to match the current state of the game.

Implements TerminalGames::Game.

Definition at line 71 of file TicTacToe.cpp.

◆ IsGameOver()

bool TerminalGames::TicTacToe::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 83 of file TicTacToe.cpp.

◆ ToggleCurrentPlayer()

void TerminalGames::TicTacToe::ToggleCurrentPlayer ( )
overrideprivatevirtual

Change the current player to the other player.

Implements TerminalGames::Game.

Definition at line 106 of file TicTacToe.cpp.

◆ IsCurrentTurnUsers()

bool TerminalGames::TicTacToe::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 111 of file TicTacToe.cpp.

◆ ExecuteUserCommand()

void TerminalGames::TicTacToe::ExecuteUserCommand ( )
overrideprivatevirtual

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

Warning
This function does not catch any exceptions thrown by Terminal::GetUserCommandFromGameGrid().
Bug
If the user presses the Globals::G_TERMINAL_BACKSPACE_KEY the thrown exception Globals::Exceptions::BackspaceKeyPressed will never be caught and the program will crash.

Implements TerminalGames::Game.

Definition at line 116 of file TicTacToe.cpp.

◆ ExecuteComputerCommand()

void TerminalGames::TicTacToe::ExecuteComputerCommand ( )
overrideprivatevirtual

Get a random command from the computer.

Implements TerminalGames::Game.

Definition at line 131 of file TicTacToe.cpp.

◆ GameOver()

void TerminalGames::TicTacToe::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 145 of file TicTacToe.cpp.

◆ RestartGame()

void TerminalGames::TicTacToe::RestartGame ( )
overrideprivatevirtual

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

Implements TerminalGames::Game.

Definition at line 150 of file TicTacToe.cpp.

◆ ResetGame()

void TerminalGames::TicTacToe::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 155 of file TicTacToe.cpp.

◆ GetPlayerCount()

void TerminalGames::TicTacToe::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 161 of file TicTacToe.cpp.

◆ GetUserPlayerChoice()

void TerminalGames::TicTacToe::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 170 of file TicTacToe.cpp.

◆ GetComputerSpeed()

void TerminalGames::TicTacToe::GetComputerSpeed ( )
private

Prompts the user to select how the speed of 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 179 of file TicTacToe.cpp.

◆ ValidateCommand()

bool TerminalGames::TicTacToe::ValidateCommand ( const std::tuple< uint32_t, uint32_t > & p_command)
private

Checks whether the command is valid.

Parameters
p_commandThe command in the form of <row, column>.
Returns
true If the command is valid.
false If the command is not valid.

Definition at line 189 of file TicTacToe.cpp.

◆ ExecuteGeneralCommand()

void TerminalGames::TicTacToe::ExecuteGeneralCommand ( const std::tuple< uint32_t, uint32_t > & p_command)
private

Executes the command against the game grid.

Parameters
p_commandThe command in the form of <row, column>.

Definition at line 196 of file TicTacToe.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::TicTacToe::m_pageBuilder
private

Used to build pages required by the game.

Definition at line 107 of file TicTacToe.hpp.

◆ m_gameInformation

GameInformation TerminalGames::TicTacToe::m_gameInformation
private

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

Definition at line 112 of file TicTacToe.hpp.

◆ m_randomNumberGenerator

std::default_random_engine TerminalGames::TicTacToe::m_randomNumberGenerator
private

Used to randomly choose the player to start the game and to randomly decide the computer's next command.

Definition at line 117 of file TicTacToe.hpp.

◆ m_gameGrid

std::array<std::array<std::string, Globals::G_TICTACTOE_GRID_WIDTH>, Globals::G_TICTACTOE_GRID_HEIGHT> TerminalGames::TicTacToe::m_gameGrid
private

The Tic Tac Toe grid.

Definition at line 122 of file TicTacToe.hpp.

◆ m_commandsRemaining

std::vector<std::tuple<uint32_t, uint32_t> > TerminalGames::TicTacToe::m_commandsRemaining
private

The grid <row, column> values that have not been chosen yet.

Definition at line 127 of file TicTacToe.hpp.

◆ m_previousCommand

std::tuple<uint32_t, uint32_t> TerminalGames::TicTacToe::m_previousCommand
private

The previous grid <row, column> value that was chosen by the user. This is used to return the cursor to this position when the user is prompted to select their next command.

Definition at line 133 of file TicTacToe.hpp.

◆ m_computerSpeedName

std::string TerminalGames::TicTacToe::m_computerSpeedName
private

The name of the user selected computer speed.

Definition at line 138 of file TicTacToe.hpp.

◆ m_currentPlayer

std::string TerminalGames::TicTacToe::m_currentPlayer
private

The name of the player whose turn it is.

Definition at line 143 of file TicTacToe.hpp.

◆ m_playerCount

std::string TerminalGames::TicTacToe::m_playerCount
private

The count of the user selected number of players.

Definition at line 148 of file TicTacToe.hpp.

◆ m_userPlayerChoice

std::string TerminalGames::TicTacToe::m_userPlayerChoice
private

The choice of which player the user has selected.

Definition at line 153 of file TicTacToe.hpp.

◆ m_computerSpeed

uint32_t TerminalGames::TicTacToe::m_computerSpeed
private

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

Definition at line 158 of file TicTacToe.hpp.

◆ m_turnCount

uint32_t TerminalGames::TicTacToe::m_turnCount
private

The number of turns that have occurred.

Definition at line 163 of file TicTacToe.hpp.

◆ m_hasSavedGameOptions

bool TerminalGames::TicTacToe::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 169 of file TicTacToe.hpp.

◆ m_hasWinner

bool TerminalGames::TicTacToe::m_hasWinner
private

Whether the game has concluded with a winner (true) or whether it is a draw (false).

Definition at line 174 of file TicTacToe.hpp.

◆ m_saveGameOptions

bool TerminalGames::TicTacToe::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 180 of file TicTacToe.hpp.


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