Terminal Games
Simple games that run in the terminal.
Loading...
Searching...
No Matches
TicTacToe.hpp
1#pragma once
2
3#include <array>
4#include <cstdint>
5#include <random>
6#include <string>
7#include <tuple>
8#include <vector>
9
10#include "games/Game.hpp"
11#include "helpers/GameInformation.hpp"
12#include "helpers/Globals.hpp"
13#include "helpers/PageBuilder.hpp"
14
19namespace TerminalGames
20{
25 class TicTacToe : public Game
26 {
27 public:
33 explicit TicTacToe(const bool& p_useAnsiEscapeCodes);
34
35 private:
36 void SetupGame() override;
37
38 void GetUserOptions() override;
39
40 void UpdateGameInformation() override;
41
42 bool IsGameOver() override;
43
44 void ToggleCurrentPlayer() override;
45
46 bool IsCurrentTurnUsers() override;
47
54 void ExecuteUserCommand() override;
55
56 void ExecuteComputerCommand() override;
57
62 void GameOver() override;
63
64 void RestartGame() override;
65
66 void ResetGame() override;
67
72 void GetPlayerCount();
73
80
86 void GetComputerSpeed();
87
95 bool ValidateCommand(const std::tuple<uint32_t, uint32_t>& p_command);
96
102 void ExecuteGeneralCommand(const std::tuple<uint32_t, uint32_t>& p_command);
103
108
113
117 std::default_random_engine m_randomNumberGenerator;
118
122 std::array<std::array<std::string, Globals::G_TICTACTOE_GRID_WIDTH>, Globals::G_TICTACTOE_GRID_HEIGHT> m_gameGrid;
123
127 std::vector<std::tuple<uint32_t, uint32_t>> m_commandsRemaining;
128
133 std::tuple<uint32_t, uint32_t> m_previousCommand;
134
139
143 std::string m_currentPlayer;
144
148 std::string m_playerCount;
149
154
159
163 uint32_t m_turnCount;
164
170
175
181 };
182}
Game()=default
Constructs a new Game object.
Builds pages (i.e. strings) to be printed to the terminal.
void GetUserOptions() override
Prompt the user for their choice on various game-related options.
Definition TicTacToe.cpp:45
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 ExecuteUserCommand() override
Prompt the user to enter their command for the current turn.
void GetComputerSpeed()
Prompts the user to select how the speed of computer decision making (this does not affect the diffic...
void ExecuteGeneralCommand(const std::tuple< uint32_t, uint32_t > &p_command)
Executes the command against the game grid.
void GetUserPlayerChoice()
Prompts the user to select which player the will be playing the game as (only supported if playing ag...
uint32_t m_turnCount
The number of turns that have occurred.
TicTacToe(const bool &p_useAnsiEscapeCodes)
Constructs a new TicTacToe object.
Definition TicTacToe.cpp:16
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 ...
bool IsGameOver() override
Check whether the game is over.
Definition TicTacToe.cpp:83
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 g...
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.
GameInformation m_gameInformation
Used to package up the current state of the game so it can be used by m_pageBuilder.
std::string m_currentPlayer
The name of the player whose turn it is.
std::vector< std::tuple< uint32_t, uint32_t > > m_commandsRemaining
The grid <row, column> values that have not been chosen yet.
bool m_hasSavedGameOptions
Whether the user has selected all the game options (true) or not/partially (false)....
void SetupGame() override
Clears and sets all member variables to their game start default.
Definition TicTacToe.cpp:27
std::string m_userPlayerChoice
The choice of which player the user has selected.
void RestartGame() override
Update variables to allow for the game to be restarted with the same user options.
std::string m_computerSpeedName
The name of the user selected computer speed.
PageBuilder m_pageBuilder
Used to build pages required by the game.
void UpdateGameInformation() override
Updates GameInformation to match the current state of the game.
Definition TicTacToe.cpp:71
std::string m_playerCount
The count of the user selected number of players.
std::array< std::array< std::string, Globals::G_TICTACTOE_GRID_WIDTH >, Globals::G_TICTACTOE_GRID_HEIGHT > m_gameGrid
The Tic Tac Toe grid.
bool ValidateCommand(const std::tuple< uint32_t, uint32_t > &p_command)
Checks whether the command is valid.
uint32_t m_computerSpeed
The computer speed determined by the amount of seconds the computer must wait before executing it's c...
bool m_hasWinner
Whether the game has concluded with a winner (true) or whether it is a draw (false).
std::default_random_engine m_randomNumberGenerator
Used to randomly choose the player to start the game and to randomly decide the computer's next comma...
bool m_saveGameOptions
Whether to save the user's currently selected game options (true) and restart the game or not (false)...
static const uint32_t G_TICTACTOE_GRID_HEIGHT
TicTacToe grid attributes.
Definition Globals.hpp:395
Contains all Terminal-Games objects.
Used by game objects to package themselves into a format that can be used by PageBuilder to construct...