Terminal Games
Simple games that run in the terminal.
Loading...
Searching...
No Matches
MainMenu.cpp
1#include <cstdint>
2#include <cstdlib>
3#include <iostream>
4#include <memory>
5#include <string>
6#include <vector>
7
8#include "games/Battleships.hpp"
9#include "games/Hangman.hpp"
10#include "games/TicTacToe.hpp"
11#include "helpers/Globals.hpp"
12#include "helpers/PageBuilder.hpp"
13#include "helpers/Terminal.hpp"
14
15#include "helpers/MainMenu.hpp"
16
17namespace TerminalGames
18{
19 MainMenu::MainMenu(const bool& p_useAnsiEscapeCodes) :
20 m_useAnsiEscapeCodes(p_useAnsiEscapeCodes)
21 {
23 }
24
25 MainMenu::MainMenu(const std::vector<std::string>& p_commandLineArguments) :
27 {
28 for (const std::string& argument : p_commandLineArguments)
29 {
30 if (argument == "--a" || argument == "--ascii-only")
31 {
33 break;
34 }
35
36 if (argument == "-h" || argument == "--help")
37 {
39 exit(1);
40 }
41 }
42
44 }
45
50
52 {
54
55 uint32_t gameChoice = 0;
56 bool goToHomePage = true;
57
58 while (true)
59 {
60 try
61 {
62 if (goToHomePage)
63 {
66 }
67
69
70 m_games[gameChoice]->Play();
71 }
72
74 {
75 goToHomePage = false;
76 }
77
79 {
80 goToHomePage = true;
81 }
82
84 {
86 return;
87 }
88 }
89 }
90
96
98 {
99 const PageBuilder MENUS_PAGE_BUILDER(Pages::MAINMENU, m_useAnsiEscapeCodes);
100 m_mainMenus = MENUS_PAGE_BUILDER.GetGameSelectionMainMenuPages({"Tic Tac Toe", "Hangman", "Battleships"});
101
102 // The order of games in m_Games should match the order of games in GetGameSelectionMainMenuPages function call.
103 m_games.clear();
104 m_games.emplace_back(std::make_unique<TicTacToe>(m_useAnsiEscapeCodes));
105 m_games.emplace_back(std::make_unique<Hangman>(m_useAnsiEscapeCodes));
106 m_games.emplace_back(std::make_unique<Battleships>(m_useAnsiEscapeCodes));
107 }
108}
std::vector< std::string > m_mainMenus
Contains the different options available on the main menu page.
Definition MainMenu.hpp:115
void SetupHomepages()
Create the option selection home pages.
Definition MainMenu.cpp:91
MainMenu()=delete
Disable constructing a new Main Menu object with no arguments.
void SetupMainMenuPagesAndGames()
Create the main menu game selection pages and the game array.
Definition MainMenu.cpp:97
~MainMenu()
Destructs a MainMenu object.
Definition MainMenu.cpp:46
bool m_useAnsiEscapeCodes
Whether to use use ANSI escapes codes (true) or only extended ASCII characters (false).
Definition MainMenu.hpp:120
void Run()
Orchestration function which contains the main program loop.
Definition MainMenu.cpp:51
std::vector< std::string > m_homepages
Contains the different options available on the homepage.
Definition MainMenu.hpp:110
std::vector< std::unique_ptr< Game > > m_games
Games that can be selected and played.
Definition MainMenu.hpp:105
Builds pages (i.e. strings) to be printed to the terminal.
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 > GetOptionSelectionHomepages()
Creates the pages for displaying the option selection home page.
static void ResetTerminal()
Resets the terminal to its original state before the program was run.
Definition Terminal.cpp:495
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.
Definition Terminal.cpp:53
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.
Definition Terminal.cpp:21
static void InitialiseTerminal()
Initialises the terminal for the program.
Definition Terminal.cpp:489
static const std::string G_MAIN_MENU_CLI_HELP_MESSAGE
CLI help message.
Definition Globals.hpp:172
Contains all Terminal-Games objects.
@ MAINMENU
Page supported by PageBuilder.
@ HOMEPAGE
Page supported by PageBuilder.