Terminal Games
Simple games that run in the terminal.
|
Implementation of the Battleships board game: https://en.wikipedia.org/wiki/Battleship_(game) More...
#include "Battleships.hpp"
Public Member Functions | |
Battleships (const bool &p_useAnsiEscapeCodes) | |
Constructs a new Battleships 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 | GetComputerSpeed () |
Prompts the user to select how the speed of the computer decision making (this does not affect the difficulty of the computer). | |
void | GetUserShipPositions () |
Prompt the user to place all ships on the board. | |
bool | ValidateUserShipPosition (const std::vector< std::tuple< uint32_t, uint32_t > > &p_currentShipPositions, const std::tuple< uint32_t, uint32_t > &p_selectedShipGridLocation, bool &p_shipIsHorizontal, bool &p_shipIsVertical) |
Validates whether (true) or not (false) p_selectedShipGridLocation is a valid grid location to place a ship. | |
void | GetRandomShipPositions (std::array< std::array< std::string, Globals::G_BATTLESHIPS_BOARD_WIDTH >, Globals::G_BATTLESHIPS_BOARD_HEIGHT > &p_board) |
Randomly place all ships on the board and is used to place the computer's ships. | |
void | ExecuteGeneralCommand (std::array< std::array< std::string, Globals::G_BATTLESHIPS_BOARD_WIDTH >, Globals::G_BATTLESHIPS_BOARD_HEIGHT > &p_opponentBoard, std::unordered_map< std::string, uint32_t > &p_opponentShipsRemaining, std::vector< std::tuple< uint32_t, uint32_t > > &p_commandsRemaining, const std::tuple< uint32_t, uint32_t > &p_command) |
Executes the command on the opponent's board and updates their own board and commands remaining. |
Static Private Member Functions | |
static bool | IsShipPresent (std::array< std::array< std::string, Globals::G_BATTLESHIPS_BOARD_WIDTH >, Globals::G_BATTLESHIPS_BOARD_HEIGHT > &p_board) |
Checks whether at least a single ship is present on a game board. | |
static bool | ValidateCommand (const std::vector< std::tuple< uint32_t, uint32_t > > &p_commandsRemaining, const std::tuple< uint32_t, uint32_t > &p_command) |
Checks whether the command is valid. |
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 ships positions for the computer and randomly select board <row, column> values for the computer when attacking a ship. | |
std::tuple< uint32_t, uint32_t > | m_previousCommand |
The previous board <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. | |
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_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. | |
std::array< std::array< std::string, Globals::G_BATTLESHIPS_BOARD_WIDTH >, Globals::G_BATTLESHIPS_BOARD_HEIGHT > | m_boardPlayerOne |
The current state of the respective player's board. | |
std::array< std::array< std::string, Globals::G_BATTLESHIPS_BOARD_WIDTH >, Globals::G_BATTLESHIPS_BOARD_HEIGHT > | m_boardPlayerTwo |
The current state of the respective player's board. | |
std::vector< std::tuple< uint32_t, uint32_t > > | m_commandsRemainingPlayerOne |
The respective player's board <row, column> values they have not chosen yet. | |
std::vector< std::tuple< uint32_t, uint32_t > > | m_commandsRemainingPlayerTwo |
The respective player's board <row, column> values they have not chosen yet. | |
std::unordered_map< std::string, uint32_t > | m_shipsRemainingPlayerOne |
The respective player's health of each their ships. | |
std::unordered_map< std::string, uint32_t > | m_shipsRemainingPlayerTwo |
The respective player's health of each their ships. |
Implementation of the Battleships board game: https://en.wikipedia.org/wiki/Battleship_(game)
Definition at line 26 of file Battleships.hpp.
|
explicit |
Constructs a new Battleships object.
p_useAnsiEscapeCodes | Whether to use use ANSI escapes codes (true) or only extended ASCII characters (false). |
Definition at line 21 of file Battleships.cpp.
|
overrideprivatevirtual |
Clears and sets all member variables to their game start default.
Implements TerminalGames::Game.
Definition at line 32 of file Battleships.cpp.
|
overrideprivatevirtual |
Prompt the user for their choice on various game-related options.
Implements TerminalGames::Game.
Definition at line 60 of file Battleships.cpp.
|
overrideprivatevirtual |
Updates GameInformation to match the current state of the game.
Implements TerminalGames::Game.
Definition at line 86 of file Battleships.cpp.
|
overrideprivatevirtual |
Check whether the game is over.
Implements TerminalGames::Game.
Definition at line 101 of file Battleships.cpp.
|
overrideprivatevirtual |
Change the current player to the other player.
Implements TerminalGames::Game.
Definition at line 108 of file Battleships.cpp.
|
overrideprivatevirtual |
Check whether the current turn should be executed by the user.
Implements TerminalGames::Game.
Definition at line 113 of file Battleships.cpp.
|
overrideprivatevirtual |
Prompt the user to enter their command for the current turn.
Implements TerminalGames::Game.
Definition at line 118 of file Battleships.cpp.
|
overrideprivatevirtual |
Get a random command from the computer.
Implements TerminalGames::Game.
Definition at line 136 of file Battleships.cpp.
|
overrideprivatevirtual |
Display the game over message and prompt the user whether they would like to play again or quit the game.
Implements TerminalGames::Game.
Definition at line 153 of file Battleships.cpp.
|
overrideprivatevirtual |
Update variables to allow for the game to be restarted with the same user options.
Implements TerminalGames::Game.
Definition at line 158 of file Battleships.cpp.
|
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 163 of file Battleships.cpp.
|
private |
Prompts the user to select how many players will be playing the game.
Definition at line 169 of file Battleships.cpp.
|
private |
Prompts the user to select how the speed of the computer decision making (this does not affect the difficulty of the computer).
Definition at line 178 of file Battleships.cpp.
|
private |
Prompt the user to place all ships on the board.
Definition at line 188 of file Battleships.cpp.
|
private |
Validates whether (true) or not (false) p_selectedShipGridLocation is a valid grid location to place a ship.
p_currentShipPositions | The grid locations selected for the current ship. |
p_selectedShipGridLocation | The grid location to validate. |
p_shipIsHorizontal | Whether the ship is being placed horizontally (true) or not (false) |
p_shipIsVertical | Whether the ship is being placed vertically (true) or not (false) |
Definition at line 277 of file Battleships.cpp.
|
private |
Randomly place all ships on the board and is used to place the computer's ships.
p_board | The board to place the ships. |
Definition at line 347 of file Battleships.cpp.
|
staticprivate |
Checks whether at least a single ship is present on a game board.
p_board | The game board to check. |
Definition at line 429 of file Battleships.cpp.
|
staticprivate |
Checks whether the command is valid.
p_commandsRemaining | The command that haven't been done. |
p_command | The command in the form of <row, column>. |
Definition at line 445 of file Battleships.cpp.
|
private |
Executes the command on the opponent's board and updates their own board and commands remaining.
p_opponentBoard | The board containing the opponent's ship positions. |
p_opponentShipsRemaining | The number of squares remaining for each opponent ship. |
p_commandsRemaining | List of board spaces that remain un-attacked. |
p_command | The board row and column that should be attacked. |
Definition at line 452 of file Battleships.cpp.
|
inlinefinalvirtualinherited |
|
private |
Used to build pages required by the game.
Definition at line 150 of file Battleships.hpp.
|
private |
Used to package up the current state of the game so it can be used by m_pageBuilder.
Definition at line 155 of file Battleships.hpp.
|
private |
Used to randomly select ships positions for the computer and randomly select board <row, column> values for the computer when attacking a ship.
Definition at line 161 of file Battleships.hpp.
|
private |
The current state of the respective player's board.
Definition at line 167 of file Battleships.hpp.
|
private |
The current state of the respective player's board.
Definition at line 168 of file Battleships.hpp.
|
private |
The respective player's board <row, column> values they have not chosen yet.
Definition at line 175 of file Battleships.hpp.
|
private |
The respective player's board <row, column> values they have not chosen yet.
Definition at line 176 of file Battleships.hpp.
|
private |
The respective player's health of each their ships.
Definition at line 183 of file Battleships.hpp.
|
private |
The respective player's health of each their ships.
Definition at line 184 of file Battleships.hpp.
|
private |
The previous board <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 191 of file Battleships.hpp.
|
private |
The name of the user selected computer speed.
Definition at line 196 of file Battleships.hpp.
|
private |
The name of the player whose turn it is.
Definition at line 201 of file Battleships.hpp.
|
private |
The count of the user selected number of players.
Definition at line 206 of file Battleships.hpp.
|
private |
The computer speed determined by the amount of seconds the computer must wait before executing it's command.
Definition at line 211 of file Battleships.hpp.
|
private |
The number of turns that have occurred.
Definition at line 216 of file Battleships.hpp.
|
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 222 of file Battleships.hpp.
|
private |
Whether the game is over (true) or not (false).
Definition at line 227 of file Battleships.hpp.
|
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 233 of file Battleships.hpp.