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

Builds pages (i.e. strings) to be printed to the terminal. More...

#include "PageBuilder.hpp"

Public Member Functions

 PageBuilder ()
 Constructs a new default PageBuilder object.
 PageBuilder (const Pages &p_page, const bool &p_useAnsiEscapeCodes)
 Constructs a new custom PageBuilder object.
void SetProperties (const Pages &p_page, const bool &p_useAnsiEscapeCodes)
 Set the properties of the PageBuilder object.
Pages GetCurrentPageType () const
 Get the current page type.
std::vector< std::string > GetOptionSelectionHomepages ()
 Creates the pages for displaying the option selection home page.
std::vector< std::string > GetGameSelectionMainMenuPages (const std::vector< std::string > &p_gameNames) const
 Creates the pages for displaying the main menu game selection page.
std::vector< std::string > GetPlayerCountOptionSelectionGamePages (const GameInformation &p_gameInformation)
 Creates the pages for displaying the number of players playing the game option selection page.
std::vector< std::string > GetUserPlayerChoiceOptionSelectionGamePages (const GameInformation &p_gameInformation)
 Creates the pages for displaying the user player choice for a game option selection page.
std::vector< std::string > GetComputerSpeedOptionSelectionGamePages (const GameInformation &p_gameInformation)
 Creates the pages for displaying the computer speed for a game option selection page.
std::string GetPageWithMessage (const GameInformation &p_gameInformation, const std::string &p_message)
 Creates a general game page with a custom message to display on the page.
std::string GetUserCommandPage (const GameInformation &p_gameInformation)
 Creates the game user command page which should prompt the user to enter a command.
std::string GetComputerCommandPage (const GameInformation &p_gameInformation)
 Creates the computer command page for when the computer is entering their command.
std::string GetGameOverPage (const GameInformation &p_gameInformation)
 Creates the game over page.
std::vector< std::string > GetQuitOptionSelectionPage () const
 Creates the quit option selection page.

Private Member Functions

std::string AddColour (const std::string &p_input, const Globals::Colours &p_colour) const
 Sets the colour of p_input using ANSI escape codes.
std::string GetEmptyLine () const
 Creates a new line on a page but with no input text.
std::string GetNewLineCentred (const std::string &p_input, const Globals::Colours &p_colour=Globals::Colours::WHITE, const std::string &p_selector="") const
 Creates a new line on a page with p_input automatically centred. If the spacing on the sides is unequal, the left side will always have the higher amount of spacing. With an optional parameters of p_colour to colour the input text and p_selector used when creating option selection pages.
std::string GetNewLineCentredOptimised (const std::string &p_input) const
 Creates a new line on a page with p_input automatically centred. If the spacing on the sides is unequal, the left side will always have the higher amount of spacing. This is a simplified more performant version of GetNewLineCentred.
std::string GetNewLineLeftJustified (const std::string &p_input, const Globals::Colours &p_colour=Globals::Colours::WHITE, const std::string &p_selector="") const
 Creates a new line on a page with p_input automatically left justified (with one space padding on to the left page edge). With an optional parameters of p_colour to colour the input text and p_selector used when creating option selection pages.
std::string GetTopLine () const
 Creates the top line of a box within a page.
std::string GetBottomLine () const
 Creates the bottom line of a box within a page.
std::string GetTopBox () const
 Creates the top box display which acts as the title bar for a page.
std::string GetBottomBox () const
 Creates the bottom box which acts as the footer for a page.
std::string GetRemainingEmptyLines (const std::string &p_commonTopString, const std::string &p_commonBottomString) const
 Used to pad a page vertically with empty lines.
std::vector< std::string > GetGeneralOptionSelectionPages (const std::vector< std::string > &p_options, const std::string &p_commonTopString, const std::string &p_commonBottomString, const bool &p_addEmptyLineBetweenOptions, const bool &p_centerOptionsHorizontally, const bool &p_centerOptionsVertically) const
 Creates the pages for displaying the option selection page for the given options.
std::vector< std::string > GetGameOptionSelectionPages (const GameInformation &p_gameInformation, const std::string &p_message, const std::vector< std::string > &p_options)
 Creates the pages for displaying the option selection page for the given options within a game page.
std::string GetGeneralGameSubPage (const GameInformation &p_gameInformation)
 Wrapper function around the game specific game sub-page functions.
std::string GetTicTacToeSubPage (const GameInformation &p_gameInformation)
 Creates the sub-page containing the current state of the TicTacToe game.
std::string GetHangmanSubPage (const GameInformation &p_gameInformation)
 Creates the sub-page containing the current state of the Hangman game.
std::string GetBattleshipsSubPage (const GameInformation &p_gameInformation)
 Creates the sub-page containing the current state of the Battleships game.
std::string GetGridLayout (const std::vector< uint32_t > &p_gridColumnWidths, const std::vector< std::vector< std::string > > &p_gridColumnLines, const uint32_t &p_gridColumnHeight)
 Calculates a grid layout based on the grid content in p_gridColumnLines and the grid sizes in p_gridColumnWidths. Only columns can be defined and all content is centred within its grid column. This is used by the game sub-page functions to simplify their code.

Static Private Member Functions

static std::string RemoveColour (const std::string &p_input)
 Removes colour from the input text by removing all ANSI colour escape codes.

Private Attributes

std::string m_topTitle
 The text to display in the top box which acts as the title bar within a page.
std::string m_bottomTitle
 The text to display in the bottom box which acts as a footer of a page.
uint32_t m_pageWidth
 The page width in terms of the total number of characters (inclusive of the page edge characters) per line.
uint32_t m_pageHeight
 The page height in terms of the total number of lines (inclusive of the page edge lines).
uint32_t m_lineMaximumCharacterCount
 The maximum number of characters (excluding page edge and padding characters) that can exist within a line.
Pages m_currentPage
 The current page type to build for.
bool m_useAnsiEscapeCodes
 Whether to use use ANSI escapes codes (true) or only extended ASCII characters (false).
uint32_t m_minimumLeftPadding
 The minimum amount of padding between the edge characters of the page and the inner content.
uint32_t m_minimumRightPadding
 The minimum amount of padding between the edge characters of the page and the inner content.

Detailed Description

Builds pages (i.e. strings) to be printed to the terminal.

Definition at line 38 of file PageBuilder.hpp.

Constructor & Destructor Documentation

◆ PageBuilder() [1/2]

TerminalGames::PageBuilder::PageBuilder ( )
explicit

Constructs a new default PageBuilder object.

Definition at line 15 of file PageBuilder.cpp.

◆ PageBuilder() [2/2]

TerminalGames::PageBuilder::PageBuilder ( const Pages & p_page,
const bool & p_useAnsiEscapeCodes )
explicit

Constructs a new custom PageBuilder object.

Parameters
p_pageWhich page type to configure the PageBuilder for.
p_useAnsiEscapeCodesWhether to use use ANSI escapes codes (true) or only extended ASCII characters (false).
Exceptions
Globals::Exceptions::NotImplementedErrorWhen passed a Pages value that is not supported.

Definition at line 24 of file PageBuilder.cpp.

Member Function Documentation

◆ SetProperties()

void TerminalGames::PageBuilder::SetProperties ( const Pages & p_page,
const bool & p_useAnsiEscapeCodes )

Set the properties of the PageBuilder object.

Parameters
p_pageWhich page type to configure the PageBuilder for.
p_useAnsiEscapeCodesWhether to use use ANSI escapes codes (true) or only extended ASCII characters (false).
Exceptions
Globals::Exceptions::NotImplementedErrorWhen passed a Pages value that is not supported.

Definition at line 36 of file PageBuilder.cpp.

◆ GetCurrentPageType()

Pages TerminalGames::PageBuilder::GetCurrentPageType ( ) const

Get the current page type.

Returns
Pages The current page type.

Definition at line 87 of file PageBuilder.cpp.

◆ GetOptionSelectionHomepages()

std::vector< std::string > TerminalGames::PageBuilder::GetOptionSelectionHomepages ( )

Creates the pages for displaying the option selection home page.

Returns
std::vector<std::string> Pages where each page has a different option selection.

Definition at line 92 of file PageBuilder.cpp.

◆ GetGameSelectionMainMenuPages()

std::vector< std::string > TerminalGames::PageBuilder::GetGameSelectionMainMenuPages ( const std::vector< std::string > & p_gameNames) const

Creates the pages for displaying the main menu game selection page.

Parameters
p_gameNamesThe name of the games that can be selected.
Returns
std::vector<std::string> Pages where each page has a different game selected.

Definition at line 134 of file PageBuilder.cpp.

◆ GetPlayerCountOptionSelectionGamePages()

std::vector< std::string > TerminalGames::PageBuilder::GetPlayerCountOptionSelectionGamePages ( const GameInformation & p_gameInformation)

Creates the pages for displaying the number of players playing the game option selection page.

Parameters
p_gameInformationInformation on the current state of the current game.
Returns
std::vector<std::string> Pages where each page has a different number of players selected.

Definition at line 142 of file PageBuilder.cpp.

◆ GetUserPlayerChoiceOptionSelectionGamePages()

std::vector< std::string > TerminalGames::PageBuilder::GetUserPlayerChoiceOptionSelectionGamePages ( const GameInformation & p_gameInformation)

Creates the pages for displaying the user player choice for a game option selection page.

Parameters
p_gameInformationInformation on the current state of the current game.
Returns
std::vector<std::string> Pages where each page has a different player choice selected.

Definition at line 158 of file PageBuilder.cpp.

◆ GetComputerSpeedOptionSelectionGamePages()

std::vector< std::string > TerminalGames::PageBuilder::GetComputerSpeedOptionSelectionGamePages ( const GameInformation & p_gameInformation)

Creates the pages for displaying the computer speed for a game option selection page.

Parameters
p_gameInformationInformation on the current state of the current game.
Returns
std::vector<std::string> Pages where each page has a different computer speed selected.

Definition at line 173 of file PageBuilder.cpp.

◆ GetPageWithMessage()

std::string TerminalGames::PageBuilder::GetPageWithMessage ( const GameInformation & p_gameInformation,
const std::string & p_message )

Creates a general game page with a custom message to display on the page.

Parameters
p_gameInformationInformation on the current state of the current game.
p_messageThe custom message to display.
Returns
std::string A page with the current state of the game and a custom message.
Warning
The message will be truncated if it is too long to be contained within a single line on the page.

Definition at line 187 of file PageBuilder.cpp.

◆ GetUserCommandPage()

std::string TerminalGames::PageBuilder::GetUserCommandPage ( const GameInformation & p_gameInformation)

Creates the game user command page which should prompt the user to enter a command.

Parameters
p_gameInformationInformation on the current state of the current game.
Returns
std::string A page with the current state of the game and a prompt to the user to enter a command.

Definition at line 195 of file PageBuilder.cpp.

◆ GetComputerCommandPage()

std::string TerminalGames::PageBuilder::GetComputerCommandPage ( const GameInformation & p_gameInformation)

Creates the computer command page for when the computer is entering their command.

Parameters
p_gameInformationInformation on the current state of the current game.
Returns
std::string A page with the current state of the game and a message that the computer is entering their command.

Definition at line 213 of file PageBuilder.cpp.

◆ GetGameOverPage()

std::string TerminalGames::PageBuilder::GetGameOverPage ( const GameInformation & p_gameInformation)

Creates the game over page.

Parameters
p_gameInformationInformation on the current state of the current game.
Returns
std::string A page with the final state of the game, a message on who won, how many turns took place, and how to quit the game or play again.

Definition at line 227 of file PageBuilder.cpp.

◆ GetQuitOptionSelectionPage()

std::vector< std::string > TerminalGames::PageBuilder::GetQuitOptionSelectionPage ( ) const

Creates the quit option selection page.

Returns
std::vector<std::string> Pages where each page has a different quit option selected.

Definition at line 269 of file PageBuilder.cpp.

◆ AddColour()

std::string TerminalGames::PageBuilder::AddColour ( const std::string & p_input,
const Globals::Colours & p_colour ) const
private

Sets the colour of p_input using ANSI escape codes.

Parameters
p_inputThe input text to be coloured.
p_colourThe colour of the input text.
Returns
std::string The input text wrapped with ANSI colour escape codes.

Definition at line 276 of file PageBuilder.cpp.

◆ RemoveColour()

std::string TerminalGames::PageBuilder::RemoveColour ( const std::string & p_input)
staticprivate

Removes colour from the input text by removing all ANSI colour escape codes.

Parameters
p_inputThe input text to be un-coloured.
Returns
std::string The input stripped of ANSI colour escape codes.

Definition at line 286 of file PageBuilder.cpp.

◆ GetEmptyLine()

std::string TerminalGames::PageBuilder::GetEmptyLine ( ) const
private

Creates a new line on a page but with no input text.

Returns
std::string A new line with no input text.

Definition at line 298 of file PageBuilder.cpp.

◆ GetNewLineCentred()

std::string TerminalGames::PageBuilder::GetNewLineCentred ( const std::string & p_input,
const Globals::Colours & p_colour = Globals::Colours::WHITE,
const std::string & p_selector = "" ) const
private

Creates a new line on a page with p_input automatically centred. If the spacing on the sides is unequal, the left side will always have the higher amount of spacing. With an optional parameters of p_colour to colour the input text and p_selector used when creating option selection pages.

Parameters
p_inputThe text to display on the new line.
p_colourThe colour of the input text.
p_selectorAn identifier to show the line is selected.
Returns
std::string A new line with the input text centred.
Warning
p_input will be truncated if it is too long to be contained within a single line on the page.
Unexpected behaviour will occur if p_input is coloured (i.e. it contins ANSI colour escape codes).

Definition at line 308 of file PageBuilder.cpp.

◆ GetNewLineCentredOptimised()

std::string TerminalGames::PageBuilder::GetNewLineCentredOptimised ( const std::string & p_input) const
private

Creates a new line on a page with p_input automatically centred. If the spacing on the sides is unequal, the left side will always have the higher amount of spacing. This is a simplified more performant version of GetNewLineCentred.

Parameters
p_inputThe text to display on the new line.
Returns
std::string A new line with the input text centred.
Warning
Unexpected behaviour will occur if p_input is too long to be contained within a single line on the page or if p_input is coloured (i.e. it contins ANSI colour escape codes).

Definition at line 337 of file PageBuilder.cpp.

◆ GetNewLineLeftJustified()

std::string TerminalGames::PageBuilder::GetNewLineLeftJustified ( const std::string & p_input,
const Globals::Colours & p_colour = Globals::Colours::WHITE,
const std::string & p_selector = "" ) const
private

Creates a new line on a page with p_input automatically left justified (with one space padding on to the left page edge). With an optional parameters of p_colour to colour the input text and p_selector used when creating option selection pages.

Parameters
p_inputThe text to display on the new line.
p_colourThe colour of the input text.
p_selectorAn identifier to show the line is selected.
Returns
std::string A new line with the input text left justified.
Warning
p_input will be truncated if it is too long to be contained within a single line on the page.
Unexpected behaviour will occur if p_input is coloured (i.e. it contins ANSI colour escape codes).

Definition at line 356 of file PageBuilder.cpp.

◆ GetTopLine()

std::string TerminalGames::PageBuilder::GetTopLine ( ) const
private

Creates the top line of a box within a page.

Returns
std::string The top line of a box.

Definition at line 386 of file PageBuilder.cpp.

◆ GetBottomLine()

std::string TerminalGames::PageBuilder::GetBottomLine ( ) const
private

Creates the bottom line of a box within a page.

Returns
std::string The bottom line of a box.

Definition at line 396 of file PageBuilder.cpp.

◆ GetTopBox()

std::string TerminalGames::PageBuilder::GetTopBox ( ) const
private

Creates the top box display which acts as the title bar for a page.

Returns
std::string The top box with the m_topTitle centred within it.

Definition at line 406 of file PageBuilder.cpp.

◆ GetBottomBox()

std::string TerminalGames::PageBuilder::GetBottomBox ( ) const
private

Creates the bottom box which acts as the footer for a page.

Returns
std::string The bottom box with the m_bottomTitle centred within it.

Definition at line 411 of file PageBuilder.cpp.

◆ GetRemainingEmptyLines()

std::string TerminalGames::PageBuilder::GetRemainingEmptyLines ( const std::string & p_commonTopString,
const std::string & p_commonBottomString ) const
private

Used to pad a page vertically with empty lines.

Parameters
p_commonTopStringThe part of the page above where the empty lines are to be padded.
p_commonBottomStringThe part of the page below where the empty lines are to be padded.
Returns
std::string The empty lines that are needed in between the topString and bottomString.

Definition at line 418 of file PageBuilder.cpp.

◆ GetGeneralOptionSelectionPages()

std::vector< std::string > TerminalGames::PageBuilder::GetGeneralOptionSelectionPages ( const std::vector< std::string > & p_options,
const std::string & p_commonTopString,
const std::string & p_commonBottomString,
const bool & p_addEmptyLineBetweenOptions,
const bool & p_centerOptionsHorizontally,
const bool & p_centerOptionsVertically ) const
private

Creates the pages for displaying the option selection page for the given options.

Parameters
p_optionsThe options for the option selection page
p_commonTopStringThe common string between all pages found above the options.
p_commonBottomStringThe common string between all pages found below the options.
p_addEmptyLineBetweenOptionsWhether to add an empty line between the options (true) or not (false).
p_centerOptionsHorizontallyWhether to center the options horizontally (true) or left justify them (false).
p_centerOptionsVerticallyWhether to center the options vertically (true) or left justify them (false).
Returns
std::vector<std::string> Pages where each page has a different option selected.

Definition at line 433 of file PageBuilder.cpp.

◆ GetGameOptionSelectionPages()

std::vector< std::string > TerminalGames::PageBuilder::GetGameOptionSelectionPages ( const GameInformation & p_gameInformation,
const std::string & p_message,
const std::vector< std::string > & p_options )
private

Creates the pages for displaying the option selection page for the given options within a game page.

Parameters
p_gameInformationInformation on the current state of the current game.
p_messageThe custom message to display.
p_optionsThe selectable options to display.
Returns
std::vector<std::string> Pages where each page has a different option selected.
Warning
The message will be truncated if it is too long to be contained within a single line on the page.
The page height will be allowed to extended pass the pre-defined page height (i.e. m_pageHeight) to fit all of the options provided.

Definition at line 517 of file PageBuilder.cpp.

◆ GetGeneralGameSubPage()

std::string TerminalGames::PageBuilder::GetGeneralGameSubPage ( const GameInformation & p_gameInformation)
private

Wrapper function around the game specific game sub-page functions.

Parameters
p_gameInformationInformation on the current state of the current game.
Returns
std::string The sub-page containing the current state of the current game.

Definition at line 525 of file PageBuilder.cpp.

◆ GetTicTacToeSubPage()

std::string TerminalGames::PageBuilder::GetTicTacToeSubPage ( const GameInformation & p_gameInformation)
private

Creates the sub-page containing the current state of the TicTacToe game.

Parameters
p_gameInformationInformation on the current state of the current game.
Returns
std::string The sub-page containing the current state of the TicTacToe game.

Definition at line 543 of file PageBuilder.cpp.

◆ GetHangmanSubPage()

std::string TerminalGames::PageBuilder::GetHangmanSubPage ( const GameInformation & p_gameInformation)
private

Creates the sub-page containing the current state of the Hangman game.

Parameters
p_gameInformationInformation on the current state of the current game.
Returns
std::string The sub-page containing the current state of the Hangman game.

Definition at line 589 of file PageBuilder.cpp.

◆ GetBattleshipsSubPage()

std::string TerminalGames::PageBuilder::GetBattleshipsSubPage ( const GameInformation & p_gameInformation)
private

Creates the sub-page containing the current state of the Battleships game.

Parameters
p_gameInformationInformation on the current state of the current game.
Returns
std::string The sub-page containing the current state of the Battleships game.

Definition at line 674 of file PageBuilder.cpp.

◆ GetGridLayout()

std::string TerminalGames::PageBuilder::GetGridLayout ( const std::vector< uint32_t > & p_gridColumnWidths,
const std::vector< std::vector< std::string > > & p_gridColumnLines,
const uint32_t & p_gridColumnHeight )
private

Calculates a grid layout based on the grid content in p_gridColumnLines and the grid sizes in p_gridColumnWidths. Only columns can be defined and all content is centred within its grid column. This is used by the game sub-page functions to simplify their code.

Parameters
p_gridColumnWidthsThe width of each grid column.
p_gridColumnLinesThe content of each grid column where each grid column is an element in p_gridColumnLines.
p_gridColumnHeightThe height of the grid columns (i.e. the number of lines within each grid column). Note that this must be the same for all grid columns.
Returns
std::string The grid layout based sub-page.
Warning
p_gridColumnWidths.size() must be equal to p_gridColumnLines.size().
The length all elements in p_gridColumnLines (i.e. p_gridColumnLines[i].size()) must be equal to p_gridColumnHeight.

Definition at line 836 of file PageBuilder.cpp.

Member Data Documentation

◆ m_topTitle

std::string TerminalGames::PageBuilder::m_topTitle
private

The text to display in the top box which acts as the title bar within a page.

Definition at line 338 of file PageBuilder.hpp.

◆ m_bottomTitle

std::string TerminalGames::PageBuilder::m_bottomTitle
private

The text to display in the bottom box which acts as a footer of a page.

Definition at line 343 of file PageBuilder.hpp.

◆ m_pageWidth

uint32_t TerminalGames::PageBuilder::m_pageWidth
private

The page width in terms of the total number of characters (inclusive of the page edge characters) per line.

Definition at line 348 of file PageBuilder.hpp.

◆ m_pageHeight

uint32_t TerminalGames::PageBuilder::m_pageHeight
private

The page height in terms of the total number of lines (inclusive of the page edge lines).

Definition at line 353 of file PageBuilder.hpp.

◆ m_lineMaximumCharacterCount

uint32_t TerminalGames::PageBuilder::m_lineMaximumCharacterCount
private

The maximum number of characters (excluding page edge and padding characters) that can exist within a line.

Definition at line 358 of file PageBuilder.hpp.

◆ m_minimumLeftPadding

uint32_t TerminalGames::PageBuilder::m_minimumLeftPadding
private

The minimum amount of padding between the edge characters of the page and the inner content.

Definition at line 364 of file PageBuilder.hpp.

◆ m_minimumRightPadding

uint32_t TerminalGames::PageBuilder::m_minimumRightPadding
private

The minimum amount of padding between the edge characters of the page and the inner content.

Definition at line 365 of file PageBuilder.hpp.

◆ m_currentPage

Pages TerminalGames::PageBuilder::m_currentPage
private

The current page type to build for.

Definition at line 371 of file PageBuilder.hpp.

◆ m_useAnsiEscapeCodes

bool TerminalGames::PageBuilder::m_useAnsiEscapeCodes
private

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

Definition at line 376 of file PageBuilder.hpp.


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