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

Handles all terminal related tasks such as getting user input, outputting to the terminal, clearing the terminal, etc. More...

#include "Terminal.hpp"

Public Member Functions

 Terminal ()=delete
 Disable constructing a Terminal object.

Static Public Member Functions

static bool GetUserChoiceFromHomepage (const std::vector< std::string > &p_menus, const bool &p_useAnsiEscapeCodes)
 Get the user choice from a list of homepage screens that are printed to the terminal.
static uint32_t GetUserChoiceFromMainMenus (const std::vector< std::string > &p_menus)
 Get the user choice from a list of main menus screens that are printed to the terminal.
static uint32_t GetUserChoiceFromGameMenus (const std::vector< std::string > &p_menus, const std::vector< std::string > &p_quitOptionMenus)
 Get the user choice from a list of game menus screens that are printed to the terminal.
static std::tuple< uint32_t, uint32_t > GetUserCommandFromGameGrid (const std::tuple< uint32_t, uint32_t > &p_startingGridLocation, const PageBuilder &p_pageBuilder, const GameInformation &p_gameInformation, const bool &p_displayGetUserCommandPage)
 Gets a user command based on the currently displayed game grid (wrapper function around the platform-specific GetUserCommandFromGameGrid() functions as the Windows variant uses Windows.h).
static std::tuple< uint32_t, uint32_t > GetUserCommandFromGameGridWindows (const std::tuple< uint32_t, uint32_t > &p_startingGridLocation, const PageBuilder &p_pageBuilder, const GameInformation &p_gameInformation, const bool &p_displayGetUserCommandPage)
 (Windows) Gets a user command based on the currently displayed game grid.
static std::tuple< uint32_t, uint32_t > GetUserCommandFromGameGridNonWindows (const std::tuple< uint32_t, uint32_t > &p_startingGridLocation, const PageBuilder &p_pageBuilder, const GameInformation &p_gameInformation, const bool &p_displayGetUserCommandPage)
 (Non-window) Gets a user command based on the currently displayed game grid.
static void GetUserChoiceFromGameOverMenu (const std::string &p_gameOverPage, const std::vector< std::string > &p_quitOptionMenus)
 Get the user choice whether to restart the game, reset the game or a choice from the GetUserChoiceFromQuitMenus() function.
static void GetUserChoiceFromQuitMenus (const std::vector< std::string > &p_menus)
 Gets the user choice from the quit menu. All user choices result in a different custom exception being thrown except for the "Cancel" option which results in the function returning normally.
static void PrintOutput (const std::string &p_output)
 Clears and then prints to the terminal.
static void Clear ()
 Clears the terminal based on this article: https://www.cplusplus.com/articles/4z18T05o.
static uint32_t GetNextKeyPress ()
 Wrapper for <Windows.h> API for the FlushConsoleInputBuffer() and _getch() functions.
static void SetCursorVisibility (const bool &p_cursorVisibility)
 Wrapper around the <Windows.h> API for the SetConsoleCursorInfo() function.
static void SetCursorPosition (const int16_t &p_xCoord, const int16_t &p_yCoord)
 Wrapper around the <Windows.h> API for the SetConsoleCursorPosition() function.
static void InitialiseTerminal ()
 Initialises the terminal for the program.
static void ResetTerminal ()
 Resets the terminal to its original state before the program was run.

Detailed Description

Handles all terminal related tasks such as getting user input, outputting to the terminal, clearing the terminal, etc.

Definition at line 21 of file Terminal.hpp.

Member Function Documentation

◆ GetUserChoiceFromHomepage()

bool TerminalGames::Terminal::GetUserChoiceFromHomepage ( const std::vector< std::string > & p_menus,
const bool & p_useAnsiEscapeCodes )
static

Get the user choice from a list of homepage screens that are printed to the terminal.

Parameters
p_menusAn array of screens used to print the current option selected to the terminal. The screen chosen depends on user input. The only difference between each screen is the selected option.
p_useAnsiEscapeCodesWhether to use use ANSI escapes codes (true) or only extended ASCII characters (false).
Returns
true If to use ANSI escape codes.
false If to not use ANSI escape code (only use extended ASCII characters).
Exceptions
Globals::Exceptions::QuitProgramWhen the user presses the Globals::G_TERMINAL_QUIT_KEY.

Definition at line 21 of file Terminal.cpp.

◆ GetUserChoiceFromMainMenus()

uint32_t TerminalGames::Terminal::GetUserChoiceFromMainMenus ( const std::vector< std::string > & p_menus)
static

Get the user choice from a list of main menus screens that are printed to the terminal.

Parameters
p_menusAn array of screens used to print the current option selected to the terminal. The screen chosen depends on user input. The only difference between each screen is the selected option.
Returns
uint32_t The index of the menu string that the user selected.
Exceptions
Globals::Exceptions::QuitMainMenuWhen the user presses the Globals::G_TERMINAL_QUIT_KEY.

Definition at line 53 of file Terminal.cpp.

◆ GetUserChoiceFromGameMenus()

uint32_t TerminalGames::Terminal::GetUserChoiceFromGameMenus ( const std::vector< std::string > & p_menus,
const std::vector< std::string > & p_quitOptionMenus )
static

Get the user choice from a list of game menus screens that are printed to the terminal.

Parameters
p_menusAn array of screens used to print the current option selected to the terminal. The screen chosen depends on user input. The only difference between each screen is the selected option.
p_quitOptionMenusThe quit option selection screens returned from PageBuilder::GetQuitOptionSelectionPage().
Returns
uint32_t The index of the menu string that the user selected.
Exceptions
Globals::Exceptions::RestartGameWhen the user selects the restart game option from the quit menu.
Globals::Exceptions::ResetGameWhen the user selects the reset game option from the quit menu.
Globals::Exceptions::QuitGameWhen the user selects the quit game option from the quit menu.
Globals::Exceptions::QuitMainMenuWhen the user selects the quit to main menu option from the quit menu.
Globals::Exceptions::QuitProgramWhen the user selects the quit program option from the quit menu.

Definition at line 83 of file Terminal.cpp.

◆ GetUserCommandFromGameGrid()

std::tuple< uint32_t, uint32_t > TerminalGames::Terminal::GetUserCommandFromGameGrid ( const std::tuple< uint32_t, uint32_t > & p_startingGridLocation,
const PageBuilder & p_pageBuilder,
const GameInformation & p_gameInformation,
const bool & p_displayGetUserCommandPage )
static

Gets a user command based on the currently displayed game grid (wrapper function around the platform-specific GetUserCommandFromGameGrid() functions as the Windows variant uses Windows.h).

Parameters
p_startingGridLocationThe initial row and column to place the cursor.
p_pageBuilderThe PageBuilder object to allow for refreshing the terminal based on user input.
p_gameInformationThe GameInformation object to pass to the PageBuilder object.
p_displayGetUserCommandPageWhether to display the get user command page.
Returns
std::tuple<uint32_t, uint32_t> The user command from the user in the form <row, column>.
Exceptions
Globals::Exceptions::NotImplementedErrorWhen the user passes a PageBuilder with a page type not supported by this function.
Globals::Exceptions::BackspaceKeyPressedWhen the user presses the Globals::G_TERMINAL_BACKSPACE_KEY.
Globals::Exceptions::RestartGameWhen the user selects the restart game option from the quit menu.
Globals::Exceptions::ResetGameWhen the user selects the reset game option from the quit menu.
Globals::Exceptions::QuitGameWhen the user selects the quit game option from the quit menu.
Globals::Exceptions::QuitMainMenuWhen the user selects the quit to main menu option from the quit menu.
Globals::Exceptions::QuitProgramWhen the user selects the quit program option from the quit menu.

Definition at line 114 of file Terminal.cpp.

◆ GetUserCommandFromGameGridWindows()

std::tuple< uint32_t, uint32_t > TerminalGames::Terminal::GetUserCommandFromGameGridWindows ( const std::tuple< uint32_t, uint32_t > & p_startingGridLocation,
const PageBuilder & p_pageBuilder,
const GameInformation & p_gameInformation,
const bool & p_displayGetUserCommandPage )
static

(Windows) Gets a user command based on the currently displayed game grid.

Parameters
p_startingGridLocationThe initial row and column to place the cursor.
p_pageBuilderThe PageBuilder object to allow for refreshing the terminal based on user input.
p_gameInformationThe GameInformation object to pass to the PageBuilder object.
p_displayGetUserCommandPageWhether to display the get user command page.
Returns
std::tuple<uint32_t, uint32_t> The user command from the user in the form <row, column>.
Exceptions
Globals::Exceptions::NotImplementedErrorWhen the user passes a PageBuilder with a page type not supported by this function.
Globals::Exceptions::BackspaceKeyPressedWhen the user presses the Globals::G_TERMINAL_BACKSPACE_KEY.
Globals::Exceptions::RestartGameWhen the user selects the restart game option from the quit menu.
Globals::Exceptions::ResetGameWhen the user selects the reset game option from the quit menu.
Globals::Exceptions::QuitGameWhen the user selects the quit game option from the quit menu.
Globals::Exceptions::QuitMainMenuWhen the user selects the quit to main menu option from the quit menu.
Globals::Exceptions::QuitProgramWhen the user selects the quit program option from the quit menu.

Definition at line 128 of file Terminal.cpp.

◆ GetUserCommandFromGameGridNonWindows()

std::tuple< uint32_t, uint32_t > TerminalGames::Terminal::GetUserCommandFromGameGridNonWindows ( const std::tuple< uint32_t, uint32_t > & p_startingGridLocation,
const PageBuilder & p_pageBuilder,
const GameInformation & p_gameInformation,
const bool & p_displayGetUserCommandPage )
static

(Non-window) Gets a user command based on the currently displayed game grid.

Parameters
p_startingGridLocationThe initial row and column to place the cursor.
p_pageBuilderThe PageBuilder object to allow for refreshing the terminal based on user input.
p_gameInformationThe GameInformation object to pass to the PageBuilder object.
p_displayGetUserCommandPageWhether to display the get user command page.
Returns
std::tuple<uint32_t, uint32_t> The user command from the user in the form <row, column>.
Exceptions
Globals::Exceptions::NotImplementedErrorWhen the user passes a PageBuilder with a page type not supported by this function.
Globals::Exceptions::BackspaceKeyPressedWhen the user presses the Globals::G_TERMINAL_BACKSPACE_KEY.
Globals::Exceptions::RestartGameWhen the user selects the restart game option from the quit menu.
Globals::Exceptions::ResetGameWhen the user selects the reset game option from the quit menu.
Globals::Exceptions::QuitGameWhen the user selects the quit game option from the quit menu.
Globals::Exceptions::QuitMainMenuWhen the user selects the quit to main menu option from the quit menu.
Globals::Exceptions::QuitProgramWhen the user selects the quit program option from the quit menu.

Definition at line 220 of file Terminal.cpp.

◆ GetUserChoiceFromGameOverMenu()

void TerminalGames::Terminal::GetUserChoiceFromGameOverMenu ( const std::string & p_gameOverPage,
const std::vector< std::string > & p_quitOptionMenus )
static

Get the user choice whether to restart the game, reset the game or a choice from the GetUserChoiceFromQuitMenus() function.

Parameters
p_gameOverPageThe game over page to display returned from PageBuilder::GetGameOverPage().
p_quitOptionMenusThe quit option selection pages returned from PageBuilder::GetQuitOptionSelectionPage().
Exceptions
Globals::Exceptions::RestartGameWhen the user presses the Globals::G_TERMINAL_RESTART_KEY or when the user selects the restart game option for the quit menu.
Globals::Exceptions::ResetGameWhen the user presses any key other than the Globals::G_TERMINAL_RESTART_KEY or when the user selects the reset game option from the quit menu.
Globals::Exceptions::QuitGameWhen the user selects the quit game option from the quit menu.
Globals::Exceptions::QuitMainMenuWhen the user selects the quit to main menu option from the quit menu.
Globals::Exceptions::QuitProgramWhen the user selects the quit program option from the quit menu.

Definition at line 311 of file Terminal.cpp.

◆ GetUserChoiceFromQuitMenus()

void TerminalGames::Terminal::GetUserChoiceFromQuitMenus ( const std::vector< std::string > & p_menus)
static

Gets the user choice from the quit menu. All user choices result in a different custom exception being thrown except for the "Cancel" option which results in the function returning normally.

Parameters
p_menusThe quit option selection pages returned from PageBuilder::GetQuitOptionSelectionPage().
Exceptions
Globals::Exceptions::RestartGameWhen the user selects the restart game option.
Globals::Exceptions::ResetGameWhen the user selects the reset game option.
Globals::Exceptions::QuitGameWhen the user selects the quit game option.
Globals::Exceptions::QuitMainMenuWhen the user selects the quit to main menu option.
Globals::Exceptions::QuitProgramWhen the user selects the quit program option.

Definition at line 332 of file Terminal.cpp.

◆ PrintOutput()

void TerminalGames::Terminal::PrintOutput ( const std::string & p_output)
static

Clears and then prints to the terminal.

Parameters
p_outputThe string to output to the terminal.

Definition at line 381 of file Terminal.cpp.

◆ Clear()

void TerminalGames::Terminal::Clear ( )
static

Clears the terminal based on this article: https://www.cplusplus.com/articles/4z18T05o.

Definition at line 387 of file Terminal.cpp.

◆ GetNextKeyPress()

uint32_t TerminalGames::Terminal::GetNextKeyPress ( )
static

Wrapper for <Windows.h> API for the FlushConsoleInputBuffer() and _getch() functions.

Returns
uint32_t The key which was pressed represented as an integer.

Definition at line 429 of file Terminal.cpp.

◆ SetCursorVisibility()

void TerminalGames::Terminal::SetCursorVisibility ( const bool & p_cursorVisibility)
static

Wrapper around the <Windows.h> API for the SetConsoleCursorInfo() function.

Parameters
p_cursorVisibilityWhether the cursor should be visible (true) or not (false).

Definition at line 473 of file Terminal.cpp.

◆ SetCursorPosition()

void TerminalGames::Terminal::SetCursorPosition ( const int16_t & p_xCoord,
const int16_t & p_yCoord )
static

Wrapper around the <Windows.h> API for the SetConsoleCursorPosition() function.

Parameters
p_xCoordThe horizontal position to set the cursor to.
p_yCoordThe vertical position to set the cursor to.

Definition at line 481 of file Terminal.cpp.

◆ InitialiseTerminal()

void TerminalGames::Terminal::InitialiseTerminal ( )
static

Initialises the terminal for the program.

Definition at line 489 of file Terminal.cpp.

◆ ResetTerminal()

void TerminalGames::Terminal::ResetTerminal ( )
static

Resets the terminal to its original state before the program was run.

Definition at line 495 of file Terminal.cpp.


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