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

Implementation of the Battleships board game: https://en.wikipedia.org/wiki/Battleship_(game) More...

#include "Battleships.hpp"

Inheritance diagram for TerminalGames::Battleships:
TerminalGames::Game

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_HEIGHTm_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_HEIGHTm_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.

Detailed Description

Implementation of the Battleships board game: https://en.wikipedia.org/wiki/Battleship_(game)

Definition at line 26 of file Battleships.hpp.

Constructor & Destructor Documentation

◆ Battleships()

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

Constructs a new Battleships object.

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

Definition at line 21 of file Battleships.cpp.

Member Function Documentation

◆ SetupGame()

void TerminalGames::Battleships::SetupGame ( )
overrideprivatevirtual

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

Implements TerminalGames::Game.

Definition at line 32 of file Battleships.cpp.

◆ GetUserOptions()

void TerminalGames::Battleships::GetUserOptions ( )
overrideprivatevirtual

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

Implements TerminalGames::Game.

Definition at line 60 of file Battleships.cpp.

◆ UpdateGameInformation()

void TerminalGames::Battleships::UpdateGameInformation ( )
overrideprivatevirtual

Updates GameInformation to match the current state of the game.

Implements TerminalGames::Game.

Definition at line 86 of file Battleships.cpp.

◆ IsGameOver()

bool TerminalGames::Battleships::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 101 of file Battleships.cpp.

◆ ToggleCurrentPlayer()

void TerminalGames::Battleships::ToggleCurrentPlayer ( )
overrideprivatevirtual

Change the current player to the other player.

Implements TerminalGames::Game.

Definition at line 108 of file Battleships.cpp.

◆ IsCurrentTurnUsers()

bool TerminalGames::Battleships::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 113 of file Battleships.cpp.

◆ ExecuteUserCommand()

void TerminalGames::Battleships::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 118 of file Battleships.cpp.

◆ ExecuteComputerCommand()

void TerminalGames::Battleships::ExecuteComputerCommand ( )
overrideprivatevirtual

Get a random command from the computer.

Implements TerminalGames::Game.

Definition at line 136 of file Battleships.cpp.

◆ GameOver()

void TerminalGames::Battleships::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 153 of file Battleships.cpp.

◆ RestartGame()

void TerminalGames::Battleships::RestartGame ( )
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.

◆ ResetGame()

void TerminalGames::Battleships::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 163 of file Battleships.cpp.

◆ GetPlayerCount()

void TerminalGames::Battleships::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 169 of file Battleships.cpp.

◆ GetComputerSpeed()

void TerminalGames::Battleships::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 178 of file Battleships.cpp.

◆ GetUserShipPositions()

void TerminalGames::Battleships::GetUserShipPositions ( )
private

Prompt the user to place all ships on the board.

Warning
This function does not catch any exceptions thrown by Terminal::GetUserCommandFromGameGrid() except for Globals::Exceptions::BackspaceKeyPressed.

Definition at line 188 of file Battleships.cpp.

◆ ValidateUserShipPosition()

bool TerminalGames::Battleships::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 )
private

Validates whether (true) or not (false) p_selectedShipGridLocation is a valid grid location to place a ship.

Parameters
p_currentShipPositionsThe grid locations selected for the current ship.
p_selectedShipGridLocationThe grid location to validate.
p_shipIsHorizontalWhether the ship is being placed horizontally (true) or not (false)
p_shipIsVerticalWhether the ship is being placed vertically (true) or not (false)
Returns
true If ship position is valid.
false If the ship position is invalid.

Definition at line 277 of file Battleships.cpp.

◆ GetRandomShipPositions()

void TerminalGames::Battleships::GetRandomShipPositions ( std::array< std::array< std::string, Globals::G_BATTLESHIPS_BOARD_WIDTH >, Globals::G_BATTLESHIPS_BOARD_HEIGHT > & p_board)
private

Randomly place all ships on the board and is used to place the computer's ships.

Parameters
p_boardThe board to place the ships.

Definition at line 347 of file Battleships.cpp.

◆ IsShipPresent()

bool TerminalGames::Battleships::IsShipPresent ( std::array< std::array< std::string, Globals::G_BATTLESHIPS_BOARD_WIDTH >, Globals::G_BATTLESHIPS_BOARD_HEIGHT > & p_board)
staticprivate

Checks whether at least a single ship is present on a game board.

Parameters
p_boardThe game board to check.
Returns
true If at least a single ship is present on the board.
false If no ships are present on the board

Definition at line 429 of file Battleships.cpp.

◆ ValidateCommand()

bool TerminalGames::Battleships::ValidateCommand ( const std::vector< std::tuple< uint32_t, uint32_t > > & p_commandsRemaining,
const std::tuple< uint32_t, uint32_t > & p_command )
staticprivate

Checks whether the command is valid.

Parameters
p_commandsRemainingThe command that haven't been done.
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 445 of file Battleships.cpp.

◆ ExecuteGeneralCommand()

void TerminalGames::Battleships::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 )
private

Executes the command on the opponent's board and updates their own board and commands remaining.

Parameters
p_opponentBoardThe board containing the opponent's ship positions.
p_opponentShipsRemainingThe number of squares remaining for each opponent ship.
p_commandsRemainingList of board spaces that remain un-attacked.
p_commandThe board row and column that should be attacked.

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

Used to build pages required by the game.

Definition at line 150 of file Battleships.hpp.

◆ m_gameInformation

GameInformation TerminalGames::Battleships::m_gameInformation
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.

◆ m_randomNumberGenerator

std::default_random_engine TerminalGames::Battleships::m_randomNumberGenerator
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.

◆ m_boardPlayerOne

std::array<std::array<std::string, Globals::G_BATTLESHIPS_BOARD_WIDTH>, Globals::G_BATTLESHIPS_BOARD_HEIGHT> TerminalGames::Battleships::m_boardPlayerOne
private

The current state of the respective player's board.

Definition at line 167 of file Battleships.hpp.

◆ m_boardPlayerTwo

std::array<std::array<std::string, Globals::G_BATTLESHIPS_BOARD_WIDTH>, Globals::G_BATTLESHIPS_BOARD_HEIGHT> TerminalGames::Battleships::m_boardPlayerTwo
private

The current state of the respective player's board.

Definition at line 168 of file Battleships.hpp.

◆ m_commandsRemainingPlayerOne

std::vector<std::tuple<uint32_t, uint32_t> > TerminalGames::Battleships::m_commandsRemainingPlayerOne
private

The respective player's board <row, column> values they have not chosen yet.

Definition at line 175 of file Battleships.hpp.

◆ m_commandsRemainingPlayerTwo

std::vector<std::tuple<uint32_t, uint32_t> > TerminalGames::Battleships::m_commandsRemainingPlayerTwo
private

The respective player's board <row, column> values they have not chosen yet.

Definition at line 176 of file Battleships.hpp.

◆ m_shipsRemainingPlayerOne

std::unordered_map<std::string, uint32_t> TerminalGames::Battleships::m_shipsRemainingPlayerOne
private

The respective player's health of each their ships.

Definition at line 183 of file Battleships.hpp.

◆ m_shipsRemainingPlayerTwo

std::unordered_map<std::string, uint32_t> TerminalGames::Battleships::m_shipsRemainingPlayerTwo
private

The respective player's health of each their ships.

Definition at line 184 of file Battleships.hpp.

◆ m_previousCommand

std::tuple<uint32_t, uint32_t> TerminalGames::Battleships::m_previousCommand
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.

◆ m_computerSpeedName

std::string TerminalGames::Battleships::m_computerSpeedName
private

The name of the user selected computer speed.

Definition at line 196 of file Battleships.hpp.

◆ m_currentPlayer

std::string TerminalGames::Battleships::m_currentPlayer
private

The name of the player whose turn it is.

Definition at line 201 of file Battleships.hpp.

◆ m_playerCount

std::string TerminalGames::Battleships::m_playerCount
private

The count of the user selected number of players.

Definition at line 206 of file Battleships.hpp.

◆ m_computerSpeed

uint32_t TerminalGames::Battleships::m_computerSpeed
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.

◆ m_turnCount

uint32_t TerminalGames::Battleships::m_turnCount
private

The number of turns that have occurred.

Definition at line 216 of file Battleships.hpp.

◆ m_hasSavedGameOptions

bool TerminalGames::Battleships::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 222 of file Battleships.hpp.

◆ m_isGameOver

bool TerminalGames::Battleships::m_isGameOver
private

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

Definition at line 227 of file Battleships.hpp.

◆ m_saveGameOptions

bool TerminalGames::Battleships::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 233 of file Battleships.hpp.


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