From 435e1f282dd4ec0ef1003820db58657828bc8c44 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Sat, 24 Aug 2019 13:56:16 +0200 Subject: [PATCH 01/34] fix: Menu: moved text-prompt functions to game-graphics TU --- src/game-graphics.cpp | 59 ++++++++++++++++++++++++++++++ src/headers/game-graphics.hpp | 6 ++++ src/menu.cpp | 68 ++++------------------------------- 3 files changed, 71 insertions(+), 62 deletions(-) diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index 0b6dc5d5..4190671b 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -148,5 +148,64 @@ std::string InputCommandListFooterPrompt() { return str_os.str(); } +namespace Menu { + +std::string MainMenuTitlePrompt() { + constexpr auto greetings_text = "Welcome to "; + constexpr auto gamename_text = "2048!"; + constexpr auto sp = " "; + + std::ostringstream str_os; + std::ostringstream title_richtext; + title_richtext << bold_on << sp << greetings_text << blue << gamename_text + << def << bold_off << "\n"; + + str_os << title_richtext.str(); + return str_os.str(); +} + +std::string MainMenuOptionsPrompt() { + const auto menu_list_txt = {"1. Play a New Game", "2. Continue Previous Game", + "3. View Highscores and Statistics", "4. Exit"}; + constexpr auto sp = " "; + + std::ostringstream str_os; + + str_os << "\n"; + for (const auto txt : menu_list_txt) { + str_os << sp << txt << "\n"; + } + str_os << "\n"; + + return str_os.str(); +} + +std::string InputMenuErrorInvalidInputPrompt() { + constexpr auto err_input_text = "Invalid input. Please try again."; + constexpr auto sp = " "; + + std::ostringstream str_os; + std::ostringstream err_input_richtext; + err_input_richtext << red << sp << err_input_text << def << "\n\n"; + + str_os << err_input_richtext.str(); + return str_os.str(); +} + +std::string InputMenuPrompt() { + constexpr auto prompt_choice_text = "Enter Choice: "; + constexpr auto sp = " "; + + std::ostringstream str_os; + std::ostringstream prompt_choice_richtext; + + prompt_choice_richtext << sp << prompt_choice_text; + + str_os << prompt_choice_richtext.str(); + + return str_os.str(); +} + +} // namespace Menu } // namespace Graphics } // namespace Game diff --git a/src/headers/game-graphics.hpp b/src/headers/game-graphics.hpp index b273f7c0..a97e9a40 100644 --- a/src/headers/game-graphics.hpp +++ b/src/headers/game-graphics.hpp @@ -25,6 +25,12 @@ std::string BoardSizeErrorPrompt(); std::string InputCommandListPrompt(); std::string EndlessModeCommandListPrompt(); std::string InputCommandListFooterPrompt(); +namespace Menu { +std::string MainMenuTitlePrompt(); +std::string MainMenuOptionsPrompt(); +std::string InputMenuErrorInvalidInputPrompt(); +std::string InputMenuPrompt(); +} // namespace Menu } // namespace Graphics } // namespace Game diff --git a/src/menu.cpp b/src/menu.cpp index 6a27cfaf..900b8362 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -1,5 +1,6 @@ #include "menu.hpp" #include "color.hpp" +#include "game-graphics.hpp" #include "game.hpp" #include "global.hpp" #include "scores.hpp" @@ -42,71 +43,14 @@ void showScores() { Menu::startMenu(); } -void drawMainMenuTitle(std::ostream &out_os) { - constexpr auto greetings_text = "Welcome to "; - constexpr auto gamename_text = "2048!"; - constexpr auto sp = " "; - - std::ostringstream str_os; - std::ostringstream title_richtext; - title_richtext << bold_on << sp << greetings_text << blue << gamename_text - << def << bold_off << "\n"; - - str_os << title_richtext.str(); - out_os << str_os.str(); -} - -void drawMainMenuOptions(std::ostream &out_os) { - const auto menu_list_txt = {"1. Play a New Game", "2. Continue Previous Game", - "3. View Highscores and Statistics", "4. Exit"}; - constexpr auto sp = " "; - - std::ostringstream str_os; - - str_os << "\n"; - for (const auto txt : menu_list_txt) { - str_os << sp << txt << "\n"; - } - str_os << "\n"; - - out_os << str_os.str(); -} - -void drawInputMenuErrorInvalidInput(std::ostream &out_os, bool err) { - if (err) { - constexpr auto err_input_text = "Invalid input. Please try again."; - constexpr auto sp = " "; - - std::ostringstream str_os; - std::ostringstream err_input_richtext; - err_input_richtext << red << sp << err_input_text << def << "\n\n"; - - str_os << err_input_richtext.str(); - out_os << str_os.str(); - } -} - -void drawInputMenuPrompt(std::ostream &out_os) { - constexpr auto prompt_choice_text = "Enter Choice: "; - constexpr auto sp = " "; - - std::ostringstream str_os; - std::ostringstream prompt_choice_richtext; - - prompt_choice_richtext << sp << prompt_choice_text; - - str_os << prompt_choice_richtext.str(); - - out_os << str_os.str(); -} - void drawMainMenuGraphics(std::ostream &out_os) { drawAscii(); - drawMainMenuTitle(out_os); - drawMainMenuOptions(out_os); + DrawAlways(out_os, Game::Graphics::Menu::MainMenuTitlePrompt); + DrawAlways(out_os, Game::Graphics::Menu::MainMenuOptionsPrompt); // Only outputs if there is an input error... - drawInputMenuErrorInvalidInput(out_os, FlagInputErrornousChoice); - drawInputMenuPrompt(out_os); + DrawOnlyWhen(out_os, FlagInputErrornousChoice, + Game::Graphics::Menu::InputMenuErrorInvalidInputPrompt); + DrawAlways(out_os, Game::Graphics::Menu::InputMenuPrompt); } void receive_input_flags(std::istream &in_os) { From 002b382dcd05cadaf8bd346624f73adbe21e81b2 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Sat, 24 Aug 2019 14:15:52 +0200 Subject: [PATCH 02/34] fix: Global: drawAscii() renamed and moved to game-graphics TU drawAscii() renamed to AsciiArt2048(). --- src/game-graphics.cpp | 18 ++++++++++++++++++ src/game-pregamemenu.cpp | 2 +- src/game.cpp | 2 +- src/global.cpp | 18 ------------------ src/headers/game-graphics.hpp | 1 + src/headers/global.hpp | 1 - src/menu.cpp | 4 ++-- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index 4190671b..2f305a03 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -4,6 +4,24 @@ namespace Game { namespace Graphics { +std::string AsciiArt2048() { + constexpr auto title_card_2048 = R"( + /\\\\\\\\\ /\\\\\\\ /\\\ /\\\\\\\\\ + /\\\///////\\\ /\\\/////\\\ /\\\\\ /\\\///////\\\ + \/// \//\\\ /\\\ \//\\\ /\\\/\\\ \/\\\ \/\\\ + /\\\/ \/\\\ \/\\\ /\\\/\/\\\ \///\\\\\\\\\/ + /\\\// \/\\\ \/\\\ /\\\/ \/\\\ /\\\///////\\\ + /\\\// \/\\\ \/\\\ /\\\\\\\\\\\\\\\\ /\\\ \//\\\ + /\\\/ \//\\\ /\\\ \///////////\\\// \//\\\ /\\\ + /\\\\\\\\\\\\\\\ \///\\\\\\\/ \/\\\ \///\\\\\\\\\/ + \/////////////// \/////// \/// \///////// + )"; + std::ostringstream title_card_richtext; + title_card_richtext << green << bold_on << title_card_2048 << bold_off << def; + title_card_richtext << "\n\n\n"; + return title_card_richtext.str(); +} + std::string MessageScoreSavedPrompt() { constexpr auto score_saved_text = "Score saved!"; constexpr auto sp = " "; diff --git a/src/game-pregamemenu.cpp b/src/game-pregamemenu.cpp index f44bfabf..9b464e69 100644 --- a/src/game-pregamemenu.cpp +++ b/src/game-pregamemenu.cpp @@ -104,7 +104,7 @@ bool soloLoop() { pregamesetup_status = pregamesetup_status_t{}; clearScreen(); - drawAscii(); + DrawAlways(std::cout, Game::Graphics::AsciiArt2048); DrawAsOneTimeFlag(std::cout, noSave, Graphics::GameBoardNoSaveErrorPrompt); DrawAlways(std::cout, QuestionAboutBoardSizePrompt); diff --git a/src/game.cpp b/src/game.cpp index aaa35ebb..8f132fa5 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -119,7 +119,7 @@ void drawScoreBoard(std::ostream &os) { void drawBoard(std::ostream &os) { clearScreen(); - drawAscii(); + DrawAlways(os, Game::Graphics::AsciiArt2048); drawScoreBoard(os); os << gamePlayBoard; } diff --git a/src/global.cpp b/src/global.cpp index e8495724..db8fc09a 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -53,24 +53,6 @@ void clearScreen() { system("clear"); }; -void drawAscii() { - constexpr auto title_card_2048 = R"( - /\\\\\\\\\ /\\\\\\\ /\\\ /\\\\\\\\\ - /\\\///////\\\ /\\\/////\\\ /\\\\\ /\\\///////\\\ - \/// \//\\\ /\\\ \//\\\ /\\\/\\\ \/\\\ \/\\\ - /\\\/ \/\\\ \/\\\ /\\\/\/\\\ \///\\\\\\\\\/ - /\\\// \/\\\ \/\\\ /\\\/ \/\\\ /\\\///////\\\ - /\\\// \/\\\ \/\\\ /\\\\\\\\\\\\\\\\ /\\\ \//\\\ - /\\\/ \//\\\ /\\\ \///////////\\\// \//\\\ /\\\ - /\\\\\\\\\\\\\\\ \///\\\\\\\/ \/\\\ \///\\\\\\\\\/ - \/////////////// \/////// \/// \///////// - )"; - std::ostringstream title_card_richtext; - title_card_richtext << green << bold_on << title_card_2048 << bold_off << def; - title_card_richtext << "\n\n\n"; - std::cout << title_card_richtext.str(); -} - std::string secondsFormat(double sec) { double s = sec; int m = s / 60; diff --git a/src/headers/game-graphics.hpp b/src/headers/game-graphics.hpp index a97e9a40..7878bcfd 100644 --- a/src/headers/game-graphics.hpp +++ b/src/headers/game-graphics.hpp @@ -11,6 +11,7 @@ enum { COMPETITION_GAME_BOARD_PLAY_SIZE = 4 }; namespace Game { namespace Graphics { +std::string AsciiArt2048(); std::string MessageScoreSavedPrompt(); std::string AskForPlayerNamePrompt(); std::string BoardInputPrompt(); diff --git a/src/headers/global.hpp b/src/headers/global.hpp index 0d7e4528..b6ff9c36 100644 --- a/src/headers/global.hpp +++ b/src/headers/global.hpp @@ -30,7 +30,6 @@ void DrawAsOneTimeFlag(std::ostream &os, bool &trigger, T f) { void pause_for_keypress(); void wait_for_any_letter_input(std::istream &is); void clearScreen(); -void drawAscii(); std::string secondsFormat(double); #endif diff --git a/src/menu.cpp b/src/menu.cpp index 900b8362..1039d811 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -35,7 +35,7 @@ void continueGame() { void showScores() { clearScreen(); - drawAscii(); + DrawAlways(std::cout, Game::Graphics::AsciiArt2048); Scoreboard::prettyPrintScoreboard(std::cout); Statistics::prettyPrintStats(std::cout); std::cout << std::flush; @@ -44,7 +44,7 @@ void showScores() { } void drawMainMenuGraphics(std::ostream &out_os) { - drawAscii(); + DrawAlways(out_os, Game::Graphics::AsciiArt2048); DrawAlways(out_os, Game::Graphics::Menu::MainMenuTitlePrompt); DrawAlways(out_os, Game::Graphics::Menu::MainMenuOptionsPrompt); // Only outputs if there is an input error... From 1d35b9ba7ef3a1914f8977299bded17361292537 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Sun, 25 Aug 2019 14:07:03 +0200 Subject: [PATCH 03/34] fix: Global: added DataSuppliment(), DataSupplimentInternalType{} DataSuppliment() is a simple dependency injection tool for (future) functions in the "game-graphics" (and other similar / related) translation units. This DI-like function helps the separation of the coupling between data and the UI. --- src/headers/global.hpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/headers/global.hpp b/src/headers/global.hpp index b6ff9c36..0a406b39 100644 --- a/src/headers/global.hpp +++ b/src/headers/global.hpp @@ -27,6 +27,25 @@ void DrawAsOneTimeFlag(std::ostream &os, bool &trigger, T f) { } } +template +struct DataSupplimentInternalType { + suppliment_t suppliment_data; + template + std::string operator()(function_t f) const { + return f(suppliment_data); + } +}; + +template +auto DataSuppliment(suppliment_t needed_data, function_t f) { + using dsit_t = DataSupplimentInternalType; + const auto lambda_f_to_return = [=]() { + const dsit_t depinject_func = dsit_t{needed_data}; + return depinject_func(f); + }; + return lambda_f_to_return; +} + void pause_for_keypress(); void wait_for_any_letter_input(std::istream &is); void clearScreen(); From a0e95b3096a633ed075b2c4aad51141171008e84 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Sun, 25 Aug 2019 14:23:18 +0200 Subject: [PATCH 04/34] fix: Menu: drawMainMenuGraphics() decoupled from global variable * Using DataSuppliment(), decoupled global variable state from drawMainMenuGraphics(). * drawMainMenuGraphics() now uses DrawAlways() to render to screen. --- src/menu.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/menu.cpp b/src/menu.cpp index 1039d811..549d2910 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -43,14 +43,16 @@ void showScores() { Menu::startMenu(); } -void drawMainMenuGraphics(std::ostream &out_os) { - DrawAlways(out_os, Game::Graphics::AsciiArt2048); - DrawAlways(out_os, Game::Graphics::Menu::MainMenuTitlePrompt); - DrawAlways(out_os, Game::Graphics::Menu::MainMenuOptionsPrompt); +std::string drawMainMenuGraphics(bool input_error_choice_invalid) { + std::ostringstream str_os; + DrawAlways(str_os, Game::Graphics::AsciiArt2048); + DrawAlways(str_os, Game::Graphics::Menu::MainMenuTitlePrompt); + DrawAlways(str_os, Game::Graphics::Menu::MainMenuOptionsPrompt); // Only outputs if there is an input error... - DrawOnlyWhen(out_os, FlagInputErrornousChoice, + DrawOnlyWhen(str_os, input_error_choice_invalid, Game::Graphics::Menu::InputMenuErrorInvalidInputPrompt); - DrawAlways(out_os, Game::Graphics::Menu::InputMenuPrompt); + DrawAlways(str_os, Game::Graphics::Menu::InputMenuPrompt); + return str_os.str(); } void receive_input_flags(std::istream &in_os) { @@ -97,7 +99,8 @@ bool soloLoop() { // No choice in Menu selected, reset all flags... mainmenustatus = mainmenustatus_t{}; clearScreen(); - drawMainMenuGraphics(std::cout); + DrawAlways(std::cout, + DataSuppliment(FlagInputErrornousChoice, drawMainMenuGraphics)); receive_input_flags(std::cin); process_MainMenu(); return FlagInputErrornousChoice; From 31c96992ccf11c1c0f3282c4a68a02afe433b688 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Mon, 26 Aug 2019 20:12:20 +0200 Subject: [PATCH 05/34] fix: Menu: Text prompt moved to game-graphics TU --- src/game-graphics.cpp | 13 +++++++++++++ src/headers/game-graphics.hpp | 1 + src/menu.cpp | 15 ++------------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index 2f305a03..fa66da25 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -1,5 +1,6 @@ #include "game-graphics.hpp" #include "color.hpp" +#include "global.hpp" #include namespace Game { @@ -224,6 +225,18 @@ std::string InputMenuPrompt() { return str_os.str(); } +std::string MainMenuGraphicsOverlay(bool input_error_choice_invalid) { + std::ostringstream str_os; + DrawAlways(str_os, Game::Graphics::AsciiArt2048); + DrawAlways(str_os, Game::Graphics::Menu::MainMenuTitlePrompt); + DrawAlways(str_os, Game::Graphics::Menu::MainMenuOptionsPrompt); + // Only outputs if there is an input error... + DrawOnlyWhen(str_os, input_error_choice_invalid, + Game::Graphics::Menu::InputMenuErrorInvalidInputPrompt); + DrawAlways(str_os, Game::Graphics::Menu::InputMenuPrompt); + return str_os.str(); +} + } // namespace Menu } // namespace Graphics } // namespace Game diff --git a/src/headers/game-graphics.hpp b/src/headers/game-graphics.hpp index 7878bcfd..d234a8af 100644 --- a/src/headers/game-graphics.hpp +++ b/src/headers/game-graphics.hpp @@ -31,6 +31,7 @@ std::string MainMenuTitlePrompt(); std::string MainMenuOptionsPrompt(); std::string InputMenuErrorInvalidInputPrompt(); std::string InputMenuPrompt(); +std::string MainMenuGraphicsOverlay(bool input_error_choice_invalid); } // namespace Menu } // namespace Graphics } // namespace Game diff --git a/src/menu.cpp b/src/menu.cpp index 549d2910..9ee4aa62 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -43,18 +43,6 @@ void showScores() { Menu::startMenu(); } -std::string drawMainMenuGraphics(bool input_error_choice_invalid) { - std::ostringstream str_os; - DrawAlways(str_os, Game::Graphics::AsciiArt2048); - DrawAlways(str_os, Game::Graphics::Menu::MainMenuTitlePrompt); - DrawAlways(str_os, Game::Graphics::Menu::MainMenuOptionsPrompt); - // Only outputs if there is an input error... - DrawOnlyWhen(str_os, input_error_choice_invalid, - Game::Graphics::Menu::InputMenuErrorInvalidInputPrompt); - DrawAlways(str_os, Game::Graphics::Menu::InputMenuPrompt); - return str_os.str(); -} - void receive_input_flags(std::istream &in_os) { // Reset ErrornousChoice flag... FlagInputErrornousChoice = bool{}; @@ -100,7 +88,8 @@ bool soloLoop() { mainmenustatus = mainmenustatus_t{}; clearScreen(); DrawAlways(std::cout, - DataSuppliment(FlagInputErrornousChoice, drawMainMenuGraphics)); + DataSuppliment(FlagInputErrornousChoice, + Game::Graphics::Menu::MainMenuGraphicsOverlay)); receive_input_flags(std::cin); process_MainMenu(); return FlagInputErrornousChoice; From 6ed44ac8ee864b92e070613a54b1f47c7d1f974a Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Mon, 26 Aug 2019 21:07:25 +0200 Subject: [PATCH 06/34] cleanup: Game-Graphics: functions now use namespace-relative function calls --- src/game-graphics.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index fa66da25..d2035cf9 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -227,13 +227,13 @@ std::string InputMenuPrompt() { std::string MainMenuGraphicsOverlay(bool input_error_choice_invalid) { std::ostringstream str_os; - DrawAlways(str_os, Game::Graphics::AsciiArt2048); - DrawAlways(str_os, Game::Graphics::Menu::MainMenuTitlePrompt); - DrawAlways(str_os, Game::Graphics::Menu::MainMenuOptionsPrompt); + DrawAlways(str_os, AsciiArt2048); + DrawAlways(str_os, MainMenuTitlePrompt); + DrawAlways(str_os, MainMenuOptionsPrompt); // Only outputs if there is an input error... DrawOnlyWhen(str_os, input_error_choice_invalid, - Game::Graphics::Menu::InputMenuErrorInvalidInputPrompt); - DrawAlways(str_os, Game::Graphics::Menu::InputMenuPrompt); + InputMenuErrorInvalidInputPrompt); + DrawAlways(str_os, InputMenuPrompt); return str_os.str(); } From 1f83194be22899f086b3d8f0598bb23820af508e Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Tue, 1 Oct 2019 20:51:07 +0200 Subject: [PATCH 07/34] added: menu-graphics TU --- CMakeLists.txt | 2 +- meson.build | 2 +- src/game-graphics.cpp | 71 ------------------------------- src/headers/game-graphics.hpp | 7 ---- src/headers/menu-graphics.hpp | 18 ++++++++ src/menu-graphics.cpp | 79 +++++++++++++++++++++++++++++++++++ src/menu.cpp | 2 + 7 files changed, 101 insertions(+), 80 deletions(-) create mode 100644 src/headers/menu-graphics.hpp create mode 100644 src/menu-graphics.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 403f169b..a13240b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.7) # 3.7: CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT, 3.3: CXX_STANDARD project(2048 CXX) -set(SOURCES src/2048.cpp src/gameboard.cpp src/game.cpp src/game-graphics.cpp src/game-input.cpp src/game-pregamemenu.cpp src/global.cpp src/loadresource.cpp src/menu.cpp src/saveresource.cpp src/scores.cpp src/statistics.cpp src/tile.cpp) +set(SOURCES src/2048.cpp src/gameboard.cpp src/game.cpp src/game-graphics.cpp src/game-input.cpp src/game-pregamemenu.cpp src/global.cpp src/loadresource.cpp src/menu.cpp src/menu-graphics.cpp src/saveresource.cpp src/scores.cpp src/statistics.cpp src/tile.cpp) if(CMAKE_CXX_COMPILER_ID STREQUAL GNU) list(APPEND FLAGS -Wall) diff --git a/meson.build b/meson.build index 7e4b0a70..a4a74b9a 100644 --- a/meson.build +++ b/meson.build @@ -10,7 +10,7 @@ elif cxx.get_id() == 'intel' endif main_target_name = '2048' -sources = ['src/2048.cpp', 'src/gameboard.cpp', 'src/game.cpp', 'src/game-input.cpp', 'src/game-graphics.cpp', 'src/game-pregamemenu.cpp', 'src/global.cpp', 'src/loadresource.cpp', 'src/menu.cpp', 'src/saveresource.cpp', 'src/scores.cpp', 'src/statistics.cpp', 'src/tile.cpp'] +sources = ['src/2048.cpp', 'src/gameboard.cpp', 'src/game.cpp', 'src/game-input.cpp', 'src/game-graphics.cpp', 'src/game-pregamemenu.cpp', 'src/global.cpp', 'src/loadresource.cpp', 'src/menu.cpp', 'src/menu-graphics.cpp', 'src/saveresource.cpp', 'src/scores.cpp', 'src/statistics.cpp', 'src/tile.cpp'] hdrs = include_directories('src/headers') executable(main_target_name, sources, diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index d2035cf9..0792db6e 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -167,76 +167,5 @@ std::string InputCommandListFooterPrompt() { return str_os.str(); } -namespace Menu { - -std::string MainMenuTitlePrompt() { - constexpr auto greetings_text = "Welcome to "; - constexpr auto gamename_text = "2048!"; - constexpr auto sp = " "; - - std::ostringstream str_os; - std::ostringstream title_richtext; - title_richtext << bold_on << sp << greetings_text << blue << gamename_text - << def << bold_off << "\n"; - - str_os << title_richtext.str(); - return str_os.str(); -} - -std::string MainMenuOptionsPrompt() { - const auto menu_list_txt = {"1. Play a New Game", "2. Continue Previous Game", - "3. View Highscores and Statistics", "4. Exit"}; - constexpr auto sp = " "; - - std::ostringstream str_os; - - str_os << "\n"; - for (const auto txt : menu_list_txt) { - str_os << sp << txt << "\n"; - } - str_os << "\n"; - - return str_os.str(); -} - -std::string InputMenuErrorInvalidInputPrompt() { - constexpr auto err_input_text = "Invalid input. Please try again."; - constexpr auto sp = " "; - - std::ostringstream str_os; - std::ostringstream err_input_richtext; - err_input_richtext << red << sp << err_input_text << def << "\n\n"; - - str_os << err_input_richtext.str(); - return str_os.str(); -} - -std::string InputMenuPrompt() { - constexpr auto prompt_choice_text = "Enter Choice: "; - constexpr auto sp = " "; - - std::ostringstream str_os; - std::ostringstream prompt_choice_richtext; - - prompt_choice_richtext << sp << prompt_choice_text; - - str_os << prompt_choice_richtext.str(); - - return str_os.str(); -} - -std::string MainMenuGraphicsOverlay(bool input_error_choice_invalid) { - std::ostringstream str_os; - DrawAlways(str_os, AsciiArt2048); - DrawAlways(str_os, MainMenuTitlePrompt); - DrawAlways(str_os, MainMenuOptionsPrompt); - // Only outputs if there is an input error... - DrawOnlyWhen(str_os, input_error_choice_invalid, - InputMenuErrorInvalidInputPrompt); - DrawAlways(str_os, InputMenuPrompt); - return str_os.str(); -} - -} // namespace Menu } // namespace Graphics } // namespace Game diff --git a/src/headers/game-graphics.hpp b/src/headers/game-graphics.hpp index d234a8af..32539e11 100644 --- a/src/headers/game-graphics.hpp +++ b/src/headers/game-graphics.hpp @@ -26,13 +26,6 @@ std::string BoardSizeErrorPrompt(); std::string InputCommandListPrompt(); std::string EndlessModeCommandListPrompt(); std::string InputCommandListFooterPrompt(); -namespace Menu { -std::string MainMenuTitlePrompt(); -std::string MainMenuOptionsPrompt(); -std::string InputMenuErrorInvalidInputPrompt(); -std::string InputMenuPrompt(); -std::string MainMenuGraphicsOverlay(bool input_error_choice_invalid); -} // namespace Menu } // namespace Graphics } // namespace Game diff --git a/src/headers/menu-graphics.hpp b/src/headers/menu-graphics.hpp new file mode 100644 index 00000000..49f0646f --- /dev/null +++ b/src/headers/menu-graphics.hpp @@ -0,0 +1,18 @@ +#ifndef MENUGRAPHICS_H +#define MENUGRAPHICS_H + +#include + +namespace Game { +namespace Graphics { +namespace Menu { +std::string MainMenuTitlePrompt(); +std::string MainMenuOptionsPrompt(); +std::string InputMenuErrorInvalidInputPrompt(); +std::string InputMenuPrompt(); +std::string MainMenuGraphicsOverlay(bool input_error_choice_invalid); +} // namespace Menu +} // namespace Graphics +} // namespace Game + +#endif diff --git a/src/menu-graphics.cpp b/src/menu-graphics.cpp new file mode 100644 index 00000000..5071c935 --- /dev/null +++ b/src/menu-graphics.cpp @@ -0,0 +1,79 @@ +#include "menu-graphics.hpp" +#include "color.hpp" +#include "global.hpp" +#include + +namespace Game { +namespace Graphics { +namespace Menu { + +std::string MainMenuTitlePrompt() { + constexpr auto greetings_text = "Welcome to "; + constexpr auto gamename_text = "2048!"; + constexpr auto sp = " "; + + std::ostringstream str_os; + std::ostringstream title_richtext; + title_richtext << bold_on << sp << greetings_text << blue << gamename_text + << def << bold_off << "\n"; + + str_os << title_richtext.str(); + return str_os.str(); +} + +std::string MainMenuOptionsPrompt() { + const auto menu_list_txt = {"1. Play a New Game", "2. Continue Previous Game", + "3. View Highscores and Statistics", "4. Exit"}; + constexpr auto sp = " "; + + std::ostringstream str_os; + + str_os << "\n"; + for (const auto txt : menu_list_txt) { + str_os << sp << txt << "\n"; + } + str_os << "\n"; + + return str_os.str(); +} + +std::string InputMenuErrorInvalidInputPrompt() { + constexpr auto err_input_text = "Invalid input. Please try again."; + constexpr auto sp = " "; + + std::ostringstream str_os; + std::ostringstream err_input_richtext; + err_input_richtext << red << sp << err_input_text << def << "\n\n"; + + str_os << err_input_richtext.str(); + return str_os.str(); +} + +std::string InputMenuPrompt() { + constexpr auto prompt_choice_text = "Enter Choice: "; + constexpr auto sp = " "; + + std::ostringstream str_os; + std::ostringstream prompt_choice_richtext; + + prompt_choice_richtext << sp << prompt_choice_text; + + str_os << prompt_choice_richtext.str(); + + return str_os.str(); +} + +std::string MainMenuGraphicsOverlay(bool input_error_choice_invalid) { + std::ostringstream str_os; + DrawAlways(str_os, MainMenuTitlePrompt); + DrawAlways(str_os, MainMenuOptionsPrompt); + // Only outputs if there is an input error... + DrawOnlyWhen(str_os, input_error_choice_invalid, + InputMenuErrorInvalidInputPrompt); + DrawAlways(str_os, InputMenuPrompt); + return str_os.str(); +} + +} // namespace Menu +} // namespace Graphics +} // namespace Game diff --git a/src/menu.cpp b/src/menu.cpp index 9ee4aa62..dc03792c 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -3,6 +3,7 @@ #include "game-graphics.hpp" #include "game.hpp" #include "global.hpp" +#include "menu-graphics.hpp" #include "scores.hpp" #include "statistics.hpp" #include @@ -87,6 +88,7 @@ bool soloLoop() { // No choice in Menu selected, reset all flags... mainmenustatus = mainmenustatus_t{}; clearScreen(); + DrawAlways(std::cout, Game::Graphics::AsciiArt2048); DrawAlways(std::cout, DataSuppliment(FlagInputErrornousChoice, Game::Graphics::Menu::MainMenuGraphicsOverlay)); From 7f4631547124d6e7ee066290448fd699a4179069 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Tue, 1 Oct 2019 21:58:16 +0200 Subject: [PATCH 08/34] fix: Game: drawEndGameStatistics() renamed and moved to game-graphics TU drawEndGameStatistics() renamed to EndGameStatisticsPrompt(). --- src/game-graphics.cpp | 40 +++++++++++++++++++++++++++++++++++ src/game.cpp | 35 ++---------------------------- src/headers/game-graphics.hpp | 5 +++++ 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index 0792db6e..46071bb1 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -1,6 +1,8 @@ #include "game-graphics.hpp" #include "color.hpp" #include "global.hpp" +#include "scores.hpp" +#include #include namespace Game { @@ -167,5 +169,43 @@ std::string InputCommandListFooterPrompt() { return str_os.str(); } +std::string EndGameStatisticsPrompt(Scoreboard::Score finalscore) { + std::ostringstream str_os; + constexpr auto stats_title_text = "STATISTICS"; + constexpr auto divider_text = "──────────"; + const auto stats_attributes_text = { + "Final score:", "Largest Tile:", "Number of moves:", "Time taken:"}; + constexpr auto num_of_stats_attributes_text = 4; + constexpr auto sp = " "; + + auto data_stats = std::array{}; + data_stats = { + std::to_string(finalscore.score), std::to_string(finalscore.largestTile), + std::to_string(finalscore.moveCount), secondsFormat(finalscore.duration)}; + + std::ostringstream stats_richtext; + stats_richtext << yellow << sp << stats_title_text << def << "\n"; + stats_richtext << yellow << sp << divider_text << def << "\n"; + + auto counter{0}; + const auto populate_stats_info = [data_stats, stats_attributes_text, &counter, + &stats_richtext](const std::string) { + stats_richtext << sp << std::left << std::setw(19) + << std::begin(stats_attributes_text)[counter] << bold_on + << std::begin(data_stats)[counter] << bold_off << "\n"; + counter++; + }; + + for (const auto s : stats_attributes_text) { + populate_stats_info(s); + } + // std::for_each(std::begin(stats_attributes_text), + // std::end(stats_attributes_text), populate_stats_info); + + str_os << stats_richtext.str(); + str_os << "\n\n"; + return str_os.str(); +} + } // namespace Graphics } // namespace Game diff --git a/src/game.cpp b/src/game.cpp index 8f132fa5..2b4ad5b4 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -339,38 +339,6 @@ void endlessGameLoop() { drawEndScreen(std::cout, world_gamestatus); } -void drawEndGameStatistics(std::ostream &os, Scoreboard::Score finalscore) { - constexpr auto stats_title_text = "STATISTICS"; - constexpr auto divider_text = "──────────"; - const auto stats_attributes_text = { - "Final score:", "Largest Tile:", "Number of moves:", "Time taken:"}; - constexpr auto num_of_stats_attributes_text = 4; - constexpr auto sp = " "; - - auto data_stats = std::array{}; - data_stats = { - std::to_string(finalscore.score), std::to_string(finalscore.largestTile), - std::to_string(finalscore.moveCount), secondsFormat(finalscore.duration)}; - - std::ostringstream stats_richtext; - stats_richtext << yellow << sp << stats_title_text << def << "\n"; - stats_richtext << yellow << sp << divider_text << def << "\n"; - - auto counter{0}; - const auto populate_stats_info = [data_stats, stats_attributes_text, &counter, - &stats_richtext](const std::string) { - stats_richtext << sp << std::left << std::setw(19) - << std::begin(stats_attributes_text)[counter] << bold_on - << std::begin(data_stats)[counter] << bold_off << "\n"; - counter++; - }; - std::for_each(std::begin(stats_attributes_text), - std::end(stats_attributes_text), populate_stats_info); - - os << stats_richtext.str(); - os << "\n\n"; -} - void saveEndGameStats(Scoreboard::Score finalscore) { using namespace Statistics; total_game_stats_t stats; @@ -401,7 +369,8 @@ void DoPostGameSaveStuff(double duration) { finalscore.largestTile = gamePlayBoard.largestTile; finalscore.duration = duration; - drawEndGameStatistics(std::cout, finalscore); + DrawAlways(std::cout, + DataSuppliment(finalscore, Graphics::EndGameStatisticsPrompt)); saveEndGameStats(finalscore); DrawAlways(std::cout, Graphics::AskForPlayerNamePrompt); diff --git a/src/headers/game-graphics.hpp b/src/headers/game-graphics.hpp index 32539e11..1558e1e6 100644 --- a/src/headers/game-graphics.hpp +++ b/src/headers/game-graphics.hpp @@ -9,6 +9,10 @@ enum GameBoardDimensions { }; enum { COMPETITION_GAME_BOARD_PLAY_SIZE = 4 }; +namespace Scoreboard { +struct Score; +} + namespace Game { namespace Graphics { std::string AsciiArt2048(); @@ -26,6 +30,7 @@ std::string BoardSizeErrorPrompt(); std::string InputCommandListPrompt(); std::string EndlessModeCommandListPrompt(); std::string InputCommandListFooterPrompt(); +std::string EndGameStatisticsPrompt(Scoreboard::Score finalscore); } // namespace Graphics } // namespace Game From 5abf89ec06d6f1c1cbdf15f9539a5b321d5a966b Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Wed, 2 Oct 2019 22:39:24 +0200 Subject: [PATCH 09/34] fix: Game: drawScoreBoard renamed and moved to game-graphics TU drawScoreBoard() renamed to CurrentGameScoreBoardPrompt(). Accepts a custom tuple-struct type (scoreboard_display_data_t) as additional data for its printing purposes. --- src/game-graphics.cpp | 75 +++++++++++++++++++++++++++++++++++ src/game.cpp | 71 +++++---------------------------- src/headers/game-graphics.hpp | 5 +++ 3 files changed, 90 insertions(+), 61 deletions(-) diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index 46071bb1..1f922e3e 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -207,5 +207,80 @@ std::string EndGameStatisticsPrompt(Scoreboard::Score finalscore) { return str_os.str(); } +std::string CurrentGameScoreBoardPrompt(scoreboard_display_data_t scdd) { + std::ostringstream str_os; + constexpr auto score_text_label = "SCORE:"; + constexpr auto bestscore_text_label = "BEST SCORE:"; + constexpr auto moves_text_label = "MOVES:"; + + // * border padding: vvv + // | l-outer: 2, r-outer: 0 + // | l-inner: 1, r-inner: 1 + // * top border / bottom border: vvv + // | tl_corner + horizontal_sep + tr_corner = length: 1 + 27 + 1 + // | bl_corner + horizontal_sep + br_corner = length: 1 + 27 + 1 + enum { + UI_SCOREBOARD_SIZE = 27, + UI_BORDER_OUTER_PADDING = 2, + UI_BORDER_INNER_PADDING = 1 + }; // length of horizontal board - (corners + border padding) + constexpr auto border_padding_char = ' '; + constexpr auto vertical_border_pattern = "│"; + constexpr auto top_board = + "┌───────────────────────────┐"; // Multibyte character set + constexpr auto bottom_board = + "└───────────────────────────┘"; // Multibyte character set + const auto outer_border_padding = + std::string(UI_BORDER_OUTER_PADDING, border_padding_char); + const auto inner_border_padding = + std::string(UI_BORDER_INNER_PADDING, border_padding_char); + const auto inner_padding_length = + UI_SCOREBOARD_SIZE - (std::string{inner_border_padding}.length() * 2); + + enum ScoreBoardDisplayDataFields { + IDX_COMPETITION_MODE, + IDX_GAMEBOARD_SCORE, + IDX_BESTSCORE, + IDX_MOVECOUNT, + MAX_SCOREBOARDDISPLAYDATA_INDEXES + }; + + const auto competition_mode = std::get(scdd); + const auto gameboard_score = std::get(scdd); + const auto temp_bestscore = std::get(scdd); + const auto movecount = std::get(scdd); + + str_os << outer_border_padding << top_board << "\n"; + str_os << outer_border_padding << vertical_border_pattern + << inner_border_padding << bold_on << score_text_label << bold_off + << std::string(inner_padding_length - + std::string{score_text_label}.length() - + gameboard_score.length(), + border_padding_char) + << gameboard_score << inner_border_padding << vertical_border_pattern + << "\n"; + if (competition_mode) { + str_os << outer_border_padding << vertical_border_pattern + << inner_border_padding << bold_on << bestscore_text_label + << bold_off + << std::string(inner_padding_length - + std::string{bestscore_text_label}.length() - + temp_bestscore.length(), + border_padding_char) + << temp_bestscore << inner_border_padding << vertical_border_pattern + << "\n"; + } + str_os << outer_border_padding << vertical_border_pattern + << inner_border_padding << bold_on << moves_text_label << bold_off + << std::string(inner_padding_length - + std::string{moves_text_label}.length() - + movecount.length(), + border_padding_char) + << movecount << inner_border_padding << vertical_border_pattern + << "\n"; + str_os << outer_border_padding << bottom_board << "\n \n"; + return str_os.str(); +} + } // namespace Graphics } // namespace Game diff --git a/src/game.cpp b/src/game.cpp index 2b4ad5b4..3003dc31 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -57,70 +57,19 @@ ull load_game_best_score() { return tempscore; } -void drawScoreBoard(std::ostream &os) { - constexpr auto score_text_label = "SCORE:"; - constexpr auto bestscore_text_label = "BEST SCORE:"; - constexpr auto moves_text_label = "MOVES:"; - - // * border padding: vvv - // | l-outer: 2, r-outer: 0 - // | l-inner: 1, r-inner: 1 - // * top border / bottom border: vvv - // | tl_corner + horizontal_sep + tr_corner = length: 1 + 27 + 1 - // | bl_corner + horizontal_sep + br_corner = length: 1 + 27 + 1 - enum { - UI_SCOREBOARD_SIZE = 27, - UI_BORDER_OUTER_PADDING = 2, - UI_BORDER_INNER_PADDING = 1 - }; // length of horizontal board - (corners + border padding) - constexpr auto border_padding_char = ' '; - constexpr auto vertical_border_pattern = "│"; - constexpr auto top_board = - "┌───────────────────────────┐"; // Multibyte character set - constexpr auto bottom_board = - "└───────────────────────────┘"; // Multibyte character set - const auto outer_border_padding = - std::string(UI_BORDER_OUTER_PADDING, border_padding_char); - const auto inner_border_padding = - std::string(UI_BORDER_INNER_PADDING, border_padding_char); - const auto inner_padding_length = - UI_SCOREBOARD_SIZE - (std::string{inner_border_padding}.length() * 2); - os << outer_border_padding << top_board << "\n"; - os << outer_border_padding << vertical_border_pattern << inner_border_padding - << bold_on << score_text_label << bold_off - << std::string(inner_padding_length - - std::string{score_text_label}.length() - - std::to_string(gamePlayBoard.score).length(), - border_padding_char) - << gamePlayBoard.score << inner_border_padding << vertical_border_pattern - << "\n"; - if (std::get<0>(gamePlayBoard.gbda) == COMPETITION_GAME_BOARD_PLAY_SIZE) { - const auto tempBestScore = - (bestScore < gamePlayBoard.score ? gamePlayBoard.score : bestScore); - os << outer_border_padding << vertical_border_pattern - << inner_border_padding << bold_on << bestscore_text_label << bold_off - << std::string(inner_padding_length - - std::string{bestscore_text_label}.length() - - std::to_string(tempBestScore).length(), - border_padding_char) - << tempBestScore << inner_border_padding << vertical_border_pattern - << "\n"; - } - os << outer_border_padding << vertical_border_pattern << inner_border_padding - << bold_on << moves_text_label << bold_off - << std::string( - inner_padding_length - std::string{moves_text_label}.length() - - std::to_string(MoveCountOnGameBoard(gamePlayBoard)).length(), - border_padding_char) - << MoveCountOnGameBoard(gamePlayBoard) << inner_border_padding - << vertical_border_pattern << "\n"; - os << outer_border_padding << bottom_board << "\n \n"; -} - void drawBoard(std::ostream &os) { clearScreen(); DrawAlways(os, Game::Graphics::AsciiArt2048); - drawScoreBoard(os); + const auto gameboard_score = gamePlayBoard.score; + const auto tempBestScore = + (bestScore < gamePlayBoard.score ? gamePlayBoard.score : bestScore); + const auto comp_mode = + std::get<0>(gamePlayBoard.gbda) == COMPETITION_GAME_BOARD_PLAY_SIZE; + const auto movecount = MoveCountOnGameBoard(gamePlayBoard); + const auto scdd = + std::make_tuple(comp_mode, std::to_string(gameboard_score), + std::to_string(tempBestScore), std::to_string(movecount)); + DrawAlways(os, DataSuppliment(scdd, Graphics::CurrentGameScoreBoardPrompt)); os << gamePlayBoard; } diff --git a/src/headers/game-graphics.hpp b/src/headers/game-graphics.hpp index 1558e1e6..24464b32 100644 --- a/src/headers/game-graphics.hpp +++ b/src/headers/game-graphics.hpp @@ -2,6 +2,7 @@ #define GAMEGRAPHICS_H #include +#include enum GameBoardDimensions { MIN_GAME_BOARD_PLAY_SIZE = 3, @@ -31,6 +32,10 @@ std::string InputCommandListPrompt(); std::string EndlessModeCommandListPrompt(); std::string InputCommandListFooterPrompt(); std::string EndGameStatisticsPrompt(Scoreboard::Score finalscore); +using scoreboard_display_data_t = + std::tuple; +std::string CurrentGameScoreBoardPrompt(scoreboard_display_data_t scdd); + } // namespace Graphics } // namespace Game From 764bbd562538356ab9fa333dca90e5e52257f312 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Wed, 2 Oct 2019 22:55:47 +0200 Subject: [PATCH 10/34] fix: Game: refactored drawBoard() scoreboard_display_data_t{} made outside of drawBoard(). --- src/game.cpp | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 3003dc31..5ee2d6c8 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -57,20 +57,9 @@ ull load_game_best_score() { return tempscore; } -void drawBoard(std::ostream &os) { - clearScreen(); +void drawBoard(std::ostream &os, Graphics::scoreboard_display_data_t scdd) { DrawAlways(os, Game::Graphics::AsciiArt2048); - const auto gameboard_score = gamePlayBoard.score; - const auto tempBestScore = - (bestScore < gamePlayBoard.score ? gamePlayBoard.score : bestScore); - const auto comp_mode = - std::get<0>(gamePlayBoard.gbda) == COMPETITION_GAME_BOARD_PLAY_SIZE; - const auto movecount = MoveCountOnGameBoard(gamePlayBoard); - const auto scdd = - std::make_tuple(comp_mode, std::to_string(gameboard_score), - std::to_string(tempBestScore), std::to_string(movecount)); DrawAlways(os, DataSuppliment(scdd, Graphics::CurrentGameScoreBoardPrompt)); - os << gamePlayBoard; } void drawInputControls(std::ostream &os, gamestatus_t gamestatus) { @@ -106,7 +95,18 @@ gamestatus_t process_gamelogic(gamestatus_t gamestatus) { } gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { - drawBoard(os); + const auto gameboard_score = gamePlayBoard.score; + const auto tempBestScore = + (bestScore < gamePlayBoard.score ? gamePlayBoard.score : bestScore); + const auto comp_mode = + std::get<0>(gamePlayBoard.gbda) == COMPETITION_GAME_BOARD_PLAY_SIZE; + const auto movecount = MoveCountOnGameBoard(gamePlayBoard); + const auto scdd = + std::make_tuple(comp_mode, std::to_string(gameboard_score), + std::to_string(tempBestScore), std::to_string(movecount)); + clearScreen(); + drawBoard(os, scdd); + os << gamePlayBoard; DrawAsOneTimeFlag(os, gamestatus[FLAG_SAVED_GAME], Graphics::GameStateNowSavedPrompt); DrawOnlyWhen(os, gamestatus[FLAG_QUESTION_STAY_OR_QUIT], @@ -284,7 +284,18 @@ void endlessGameLoop() { std::tie(loop_again, world_gamestatus) = soloGameLoop(world_gamestatus); } - drawBoard(std::cout); + const auto gameboard_score = gamePlayBoard.score; + const auto tempBestScore = + (bestScore < gamePlayBoard.score ? gamePlayBoard.score : bestScore); + const auto comp_mode = + std::get<0>(gamePlayBoard.gbda) == COMPETITION_GAME_BOARD_PLAY_SIZE; + const auto movecount = MoveCountOnGameBoard(gamePlayBoard); + const auto scdd = + std::make_tuple(comp_mode, std::to_string(gameboard_score), + std::to_string(tempBestScore), std::to_string(movecount)); + clearScreen(); + drawBoard(std::cout, scdd); + std::cout << gamePlayBoard; drawEndScreen(std::cout, world_gamestatus); } From 60fec6b2bcb004e4c2e18ac59bd109bab6af198f Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Thu, 3 Oct 2019 00:58:46 +0200 Subject: [PATCH 11/34] fix: Game: drawBoard renamed and moved to game-graphics TU - drawBoard() renamed to GameScoreBoardOverlay(). - CurrentGameScorePrompt() renamed to GameScoreBoardBox(). - Accepts a custom tuple-struct type (scoreboard_display_data_t) as additional data for its printing purposes. --- src/game-graphics.cpp | 9 ++++++++- src/game.cpp | 9 ++------- src/headers/game-graphics.hpp | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index 1f922e3e..1aac134e 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -207,7 +207,7 @@ std::string EndGameStatisticsPrompt(Scoreboard::Score finalscore) { return str_os.str(); } -std::string CurrentGameScoreBoardPrompt(scoreboard_display_data_t scdd) { +std::string GameScoreBoardBox(scoreboard_display_data_t scdd) { std::ostringstream str_os; constexpr auto score_text_label = "SCORE:"; constexpr auto bestscore_text_label = "BEST SCORE:"; @@ -282,5 +282,12 @@ std::string CurrentGameScoreBoardPrompt(scoreboard_display_data_t scdd) { return str_os.str(); } +std::string GameScoreBoardOverlay(scoreboard_display_data_t scdd) { + std::ostringstream str_os; + DrawAlways(str_os, AsciiArt2048); + DrawAlways(str_os, DataSuppliment(scdd, GameScoreBoardBox)); + return str_os.str(); +} + } // namespace Graphics } // namespace Game diff --git a/src/game.cpp b/src/game.cpp index 5ee2d6c8..b0802013 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -57,11 +57,6 @@ ull load_game_best_score() { return tempscore; } -void drawBoard(std::ostream &os, Graphics::scoreboard_display_data_t scdd) { - DrawAlways(os, Game::Graphics::AsciiArt2048); - DrawAlways(os, DataSuppliment(scdd, Graphics::CurrentGameScoreBoardPrompt)); -} - void drawInputControls(std::ostream &os, gamestatus_t gamestatus) { const auto InputControlLists = [&gamestatus] { std::ostringstream str_os; @@ -105,7 +100,7 @@ gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { std::make_tuple(comp_mode, std::to_string(gameboard_score), std::to_string(tempBestScore), std::to_string(movecount)); clearScreen(); - drawBoard(os, scdd); + DrawAlways(os, DataSuppliment(scdd, Graphics::GameScoreBoardOverlay)); os << gamePlayBoard; DrawAsOneTimeFlag(os, gamestatus[FLAG_SAVED_GAME], Graphics::GameStateNowSavedPrompt); @@ -294,7 +289,7 @@ void endlessGameLoop() { std::make_tuple(comp_mode, std::to_string(gameboard_score), std::to_string(tempBestScore), std::to_string(movecount)); clearScreen(); - drawBoard(std::cout, scdd); + DrawAlways(std::cout, DataSuppliment(scdd, Graphics::GameScoreBoardOverlay)); std::cout << gamePlayBoard; drawEndScreen(std::cout, world_gamestatus); } diff --git a/src/headers/game-graphics.hpp b/src/headers/game-graphics.hpp index 24464b32..ab371901 100644 --- a/src/headers/game-graphics.hpp +++ b/src/headers/game-graphics.hpp @@ -34,8 +34,8 @@ std::string InputCommandListFooterPrompt(); std::string EndGameStatisticsPrompt(Scoreboard::Score finalscore); using scoreboard_display_data_t = std::tuple; -std::string CurrentGameScoreBoardPrompt(scoreboard_display_data_t scdd); - +std::string GameScoreBoardBox(scoreboard_display_data_t scdd); +std::string GameScoreBoardOverlay(scoreboard_display_data_t scdd); } // namespace Graphics } // namespace Game From 37cca36109eb325beaf232fe4dc7e9b7d92b074f Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Thu, 3 Oct 2019 01:49:16 +0200 Subject: [PATCH 12/34] fix: Game-Graphics: EndGameStatisticsPrompt uses custom tuple for extra data - EndGameStatisticsPrompt() uses finalscore_display_data_t{} (a custom tuple) to display text data for certrain fields. - Removed header-include to "score.hpp" for game-graphics TU. --- src/game-graphics.cpp | 27 ++++++++++++++++----------- src/game.cpp | 10 ++++++++-- src/headers/game-graphics.hpp | 8 +++----- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index 1aac134e..55aec992 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -1,7 +1,7 @@ #include "game-graphics.hpp" #include "color.hpp" #include "global.hpp" -#include "scores.hpp" +#include #include #include @@ -169,19 +169,26 @@ std::string InputCommandListFooterPrompt() { return str_os.str(); } -std::string EndGameStatisticsPrompt(Scoreboard::Score finalscore) { +std::string EndGameStatisticsPrompt(finalscore_display_data_t finalscore) { std::ostringstream str_os; constexpr auto stats_title_text = "STATISTICS"; constexpr auto divider_text = "──────────"; + constexpr auto sp = " "; const auto stats_attributes_text = { "Final score:", "Largest Tile:", "Number of moves:", "Time taken:"}; - constexpr auto num_of_stats_attributes_text = 4; - constexpr auto sp = " "; - - auto data_stats = std::array{}; - data_stats = { - std::to_string(finalscore.score), std::to_string(finalscore.largestTile), - std::to_string(finalscore.moveCount), secondsFormat(finalscore.duration)}; + enum FinalScoreDisplayDataFields { + IDX_FINAL_SCORE_VALUE, + IDX_LARGEST_TILE, + IDX_MOVE_COUNT, + IDX_DURATION, + MAX_NUM_OF_FINALSCOREDISPLAYDATA_INDEXES + }; + const auto data_stats = + std::array{ + std::get(finalscore), + std::get(finalscore), + std::get(finalscore), + std::get(finalscore)}; std::ostringstream stats_richtext; stats_richtext << yellow << sp << stats_title_text << def << "\n"; @@ -199,8 +206,6 @@ std::string EndGameStatisticsPrompt(Scoreboard::Score finalscore) { for (const auto s : stats_attributes_text) { populate_stats_info(s); } - // std::for_each(std::begin(stats_attributes_text), - // std::end(stats_attributes_text), populate_stats_info); str_os << stats_richtext.str(); str_os << "\n\n"; diff --git a/src/game.cpp b/src/game.cpp index b0802013..8aaddd1e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -324,8 +324,14 @@ void DoPostGameSaveStuff(double duration) { finalscore.largestTile = gamePlayBoard.largestTile; finalscore.duration = duration; - DrawAlways(std::cout, - DataSuppliment(finalscore, Graphics::EndGameStatisticsPrompt)); + const auto finalscore_display_data = + std::make_tuple(std::to_string(finalscore.score), + std::to_string(finalscore.largestTile), + std::to_string(finalscore.moveCount), + secondsFormat(finalscore.duration)); + + DrawAlways(std::cout, DataSuppliment(finalscore_display_data, + Graphics::EndGameStatisticsPrompt)); saveEndGameStats(finalscore); DrawAlways(std::cout, Graphics::AskForPlayerNamePrompt); diff --git a/src/headers/game-graphics.hpp b/src/headers/game-graphics.hpp index ab371901..410f180a 100644 --- a/src/headers/game-graphics.hpp +++ b/src/headers/game-graphics.hpp @@ -10,10 +10,6 @@ enum GameBoardDimensions { }; enum { COMPETITION_GAME_BOARD_PLAY_SIZE = 4 }; -namespace Scoreboard { -struct Score; -} - namespace Game { namespace Graphics { std::string AsciiArt2048(); @@ -31,7 +27,9 @@ std::string BoardSizeErrorPrompt(); std::string InputCommandListPrompt(); std::string EndlessModeCommandListPrompt(); std::string InputCommandListFooterPrompt(); -std::string EndGameStatisticsPrompt(Scoreboard::Score finalscore); +using finalscore_display_data_t = + std::tuple; +std::string EndGameStatisticsPrompt(finalscore_display_data_t finalscore); using scoreboard_display_data_t = std::tuple; std::string GameScoreBoardBox(scoreboard_display_data_t scdd); From 25ba7085b9dffd6a8fe715e0db4222b478d43f33 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Thu, 3 Oct 2019 02:19:19 +0200 Subject: [PATCH 13/34] fix: Game: removed unneeded header-includes --- src/game.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 8aaddd1e..7b87d09c 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -14,8 +14,6 @@ #include #include #include -#include -#include #include #include From c7422f5505c3e001ad23a5112e6cef68d3ab9c15 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Thu, 3 Oct 2019 02:49:08 +0200 Subject: [PATCH 14/34] fix: Game: drawEndScreen renamed and moved to game-graphics TU --- src/game-graphics.cpp | 23 +++++++++++++++++++++++ src/game.cpp | 17 +++-------------- src/headers/game-graphics.hpp | 2 ++ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index 55aec992..9123e0ba 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -294,5 +294,28 @@ std::string GameScoreBoardOverlay(scoreboard_display_data_t scdd) { return str_os.str(); } +std::string drawEndScreen(end_screen_display_data_t esdd) { + enum EndScreenDisplayDataFields { + IDX_FLAG_WIN, + IDX_FLAG_ENDLESS_MODE, + MAX_ENDSCREENDISPLAYDATA_INDEXES + }; + const auto did_win = std::get(esdd); + const auto is_endless_mode = std::get(esdd); + + std::ostringstream str_os; + const auto standardWinLosePrompt = [=] { + std::ostringstream str_os; + DrawOnlyWhen(str_os, did_win, YouWinPrompt); + // else.. + DrawOnlyWhen(str_os, !did_win, GameOverPrompt); + return str_os.str(); + }; + DrawOnlyWhen(str_os, !is_endless_mode, standardWinLosePrompt); + // else.. + DrawOnlyWhen(str_os, is_endless_mode, EndOfEndlessPrompt); + return str_os.str(); +} + } // namespace Graphics } // namespace Game diff --git a/src/game.cpp b/src/game.cpp index 7b87d09c..94b6f7a6 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -256,19 +256,6 @@ wrapper_bool_gamestatus_t soloGameLoop(gamestatus_t gamestatus) { return loop_again; } -void drawEndScreen(std::ostream &os, gamestatus_t gamestatus) { - const auto standardWinLosePrompt = [&gamestatus] { - std::ostringstream str_os; - DrawOnlyWhen(str_os, gamestatus[FLAG_WIN], Graphics::YouWinPrompt); - // else.. - DrawOnlyWhen(str_os, !gamestatus[FLAG_WIN], Graphics::GameOverPrompt); - return str_os.str(); - }; - DrawOnlyWhen(os, !gamestatus[FLAG_ENDLESS_MODE], standardWinLosePrompt); - // else.. - DrawOnlyWhen(os, gamestatus[FLAG_ENDLESS_MODE], Graphics::EndOfEndlessPrompt); -} - void endlessGameLoop() { auto loop_again{true}; gamestatus_t world_gamestatus{}; @@ -289,7 +276,9 @@ void endlessGameLoop() { clearScreen(); DrawAlways(std::cout, DataSuppliment(scdd, Graphics::GameScoreBoardOverlay)); std::cout << gamePlayBoard; - drawEndScreen(std::cout, world_gamestatus); + const auto esdd = std::make_tuple(world_gamestatus[FLAG_WIN], + world_gamestatus[FLAG_ENDLESS_MODE]); + DrawAlways(std::cout, DataSuppliment(esdd, Graphics::drawEndScreen)); } void saveEndGameStats(Scoreboard::Score finalscore) { diff --git a/src/headers/game-graphics.hpp b/src/headers/game-graphics.hpp index 410f180a..3dcc02ee 100644 --- a/src/headers/game-graphics.hpp +++ b/src/headers/game-graphics.hpp @@ -34,6 +34,8 @@ using scoreboard_display_data_t = std::tuple; std::string GameScoreBoardBox(scoreboard_display_data_t scdd); std::string GameScoreBoardOverlay(scoreboard_display_data_t scdd); +using end_screen_display_data_t = std::tuple; +std::string drawEndScreen(end_screen_display_data_t esdd); } // namespace Graphics } // namespace Game From 9d487be138bfe7979462cd637f57284bd7172d96 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Thu, 3 Oct 2019 23:09:19 +0200 Subject: [PATCH 15/34] fix: Game: drawInputControls moved to game-graphics TU --- src/game-graphics.cpp | 17 +++++++++++++++++ src/game.cpp | 18 ++++-------------- src/headers/game-graphics.hpp | 2 ++ 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index 9123e0ba..4951cfd9 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -317,5 +317,22 @@ std::string drawEndScreen(end_screen_display_data_t esdd) { return str_os.str(); } +std::string drawInputControls(input_controls_display_data_t gamestatus) { + const auto is_in_endless_mode = std::get<0>(gamestatus); + const auto is_in_question_mode = std::get<1>(gamestatus); + std::ostringstream str_os; + const auto InputControlLists = [=] { + std::ostringstream str_os; + DrawAlways(str_os, Graphics::InputCommandListPrompt); + DrawOnlyWhen(str_os, is_in_endless_mode, + Graphics::EndlessModeCommandListPrompt); + DrawAlways(str_os, Graphics::InputCommandListFooterPrompt); + return str_os.str(); + }; + // When game is paused to ask a question, hide regular inut prompts.. + DrawOnlyWhen(str_os, !is_in_question_mode, InputControlLists); + return str_os.str(); +} + } // namespace Graphics } // namespace Game diff --git a/src/game.cpp b/src/game.cpp index 94b6f7a6..6e28a414 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -55,19 +55,6 @@ ull load_game_best_score() { return tempscore; } -void drawInputControls(std::ostream &os, gamestatus_t gamestatus) { - const auto InputControlLists = [&gamestatus] { - std::ostringstream str_os; - DrawAlways(str_os, Graphics::InputCommandListPrompt); - DrawOnlyWhen(str_os, gamestatus[FLAG_ENDLESS_MODE], - Graphics::EndlessModeCommandListPrompt); - DrawAlways(str_os, Graphics::InputCommandListFooterPrompt); - return str_os.str(); - }; - // When game is paused to ask a question, hide regular inut prompts.. - DrawOnlyWhen(os, !gamestatus[FLAG_QUESTION_STAY_OR_QUIT], InputControlLists); -} - gamestatus_t process_gamelogic(gamestatus_t gamestatus) { unblockTilesOnGameboard(gamePlayBoard); if (gamePlayBoard.moved) { @@ -104,7 +91,10 @@ gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { Graphics::GameStateNowSavedPrompt); DrawOnlyWhen(os, gamestatus[FLAG_QUESTION_STAY_OR_QUIT], Graphics::QuestionEndOfWinningGamePrompt); - drawInputControls(os, gamestatus); + const auto input_controls_display_data = std::make_tuple( + gamestatus[FLAG_ENDLESS_MODE], gamestatus[FLAG_QUESTION_STAY_OR_QUIT]); + DrawAlways(os, DataSuppliment(input_controls_display_data, + Graphics::drawInputControls)); DrawAsOneTimeFlag(os, gamestatus[FLAG_INPUT_ERROR], Graphics::InvalidInputGameBoardErrorPrompt); return gamestatus; diff --git a/src/headers/game-graphics.hpp b/src/headers/game-graphics.hpp index 3dcc02ee..b55f35a6 100644 --- a/src/headers/game-graphics.hpp +++ b/src/headers/game-graphics.hpp @@ -36,6 +36,8 @@ std::string GameScoreBoardBox(scoreboard_display_data_t scdd); std::string GameScoreBoardOverlay(scoreboard_display_data_t scdd); using end_screen_display_data_t = std::tuple; std::string drawEndScreen(end_screen_display_data_t esdd); +using input_controls_display_data_t = std::tuple; +std::string drawInputControls(input_controls_display_data_t gamestatus); } // namespace Graphics } // namespace Game From 49e2b483200553f93920e438c0ae520a793b6dac Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Thu, 3 Oct 2019 23:10:54 +0200 Subject: [PATCH 16/34] fix: Game: removed unneeded header-includes --- src/game.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 6e28a414..eea875bd 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,5 +1,4 @@ #include "game.hpp" -#include "color.hpp" #include "game-graphics.hpp" #include "game-input.hpp" #include "game-pregamemenu.hpp" @@ -7,11 +6,9 @@ #include "global.hpp" #include "loadresource.hpp" #include "menu.hpp" -#include "point2d.hpp" #include "saveresource.hpp" #include "scores.hpp" #include "statistics.hpp" -#include #include #include #include From 8bae924276ad5e6856269267be15090466353ad4 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Sat, 5 Oct 2019 20:08:51 +0200 Subject: [PATCH 17/34] fix: Game: wrap creation of *_display_data_t into "make" functions --- src/game.cpp | 52 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index eea875bd..9804568c 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -71,7 +71,7 @@ gamestatus_t process_gamelogic(gamestatus_t gamestatus) { return gamestatus; } -gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { +Graphics::scoreboard_display_data_t make_scoreboard_display_data() { const auto gameboard_score = gamePlayBoard.score; const auto tempBestScore = (bestScore < gamePlayBoard.score ? gamePlayBoard.score : bestScore); @@ -81,6 +81,18 @@ gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { const auto scdd = std::make_tuple(comp_mode, std::to_string(gameboard_score), std::to_string(tempBestScore), std::to_string(movecount)); + return scdd; +}; + +Graphics::input_controls_display_data_t +make_input_controls_display_data(gamestatus_t gamestatus) { + const auto icdd = std::make_tuple(gamestatus[FLAG_ENDLESS_MODE], + gamestatus[FLAG_QUESTION_STAY_OR_QUIT]); + return icdd; +}; + +gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { + const auto scdd = make_scoreboard_display_data(); clearScreen(); DrawAlways(os, DataSuppliment(scdd, Graphics::GameScoreBoardOverlay)); os << gamePlayBoard; @@ -88,8 +100,8 @@ gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { Graphics::GameStateNowSavedPrompt); DrawOnlyWhen(os, gamestatus[FLAG_QUESTION_STAY_OR_QUIT], Graphics::QuestionEndOfWinningGamePrompt); - const auto input_controls_display_data = std::make_tuple( - gamestatus[FLAG_ENDLESS_MODE], gamestatus[FLAG_QUESTION_STAY_OR_QUIT]); + const auto input_controls_display_data = + make_input_controls_display_data(gamestatus); DrawAlways(os, DataSuppliment(input_controls_display_data, Graphics::drawInputControls)); DrawAsOneTimeFlag(os, gamestatus[FLAG_INPUT_ERROR], @@ -243,6 +255,13 @@ wrapper_bool_gamestatus_t soloGameLoop(gamestatus_t gamestatus) { return loop_again; } +Graphics::end_screen_display_data_t +make_end_screen_display_data(gamestatus_t world_gamestatus) { + const auto esdd = std::make_tuple(world_gamestatus[FLAG_WIN], + world_gamestatus[FLAG_ENDLESS_MODE]); + return esdd; +}; + void endlessGameLoop() { auto loop_again{true}; gamestatus_t world_gamestatus{}; @@ -251,20 +270,11 @@ void endlessGameLoop() { std::tie(loop_again, world_gamestatus) = soloGameLoop(world_gamestatus); } - const auto gameboard_score = gamePlayBoard.score; - const auto tempBestScore = - (bestScore < gamePlayBoard.score ? gamePlayBoard.score : bestScore); - const auto comp_mode = - std::get<0>(gamePlayBoard.gbda) == COMPETITION_GAME_BOARD_PLAY_SIZE; - const auto movecount = MoveCountOnGameBoard(gamePlayBoard); - const auto scdd = - std::make_tuple(comp_mode, std::to_string(gameboard_score), - std::to_string(tempBestScore), std::to_string(movecount)); + const auto scdd = make_scoreboard_display_data(); clearScreen(); DrawAlways(std::cout, DataSuppliment(scdd, Graphics::GameScoreBoardOverlay)); std::cout << gamePlayBoard; - const auto esdd = std::make_tuple(world_gamestatus[FLAG_WIN], - world_gamestatus[FLAG_ENDLESS_MODE]); + const auto esdd = make_end_screen_display_data(world_gamestatus); DrawAlways(std::cout, DataSuppliment(esdd, Graphics::drawEndScreen)); } @@ -289,6 +299,14 @@ void saveScore(Scoreboard::Score finalscore) { Scoreboard::saveToFileScore("../data/scores.txt", finalscore); } +Graphics::finalscore_display_data_t +make_finalscore_display_data(Scoreboard::Score finalscore) { + const auto fsdd = std::make_tuple( + std::to_string(finalscore.score), std::to_string(finalscore.largestTile), + std::to_string(finalscore.moveCount), secondsFormat(finalscore.duration)); + return fsdd; +}; + void DoPostGameSaveStuff(double duration) { if (std::get<0>(gamePlayBoard.gbda) == COMPETITION_GAME_BOARD_PLAY_SIZE) { Scoreboard::Score finalscore{}; @@ -299,11 +317,7 @@ void DoPostGameSaveStuff(double duration) { finalscore.duration = duration; const auto finalscore_display_data = - std::make_tuple(std::to_string(finalscore.score), - std::to_string(finalscore.largestTile), - std::to_string(finalscore.moveCount), - secondsFormat(finalscore.duration)); - + make_finalscore_display_data(finalscore); DrawAlways(std::cout, DataSuppliment(finalscore_display_data, Graphics::EndGameStatisticsPrompt)); saveEndGameStats(finalscore); From cca1e7fac5052c19b4ddfd91b521064a8b16e5a6 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Sat, 5 Oct 2019 20:52:00 +0200 Subject: [PATCH 18/34] fix: Game-Graphics: renamed some function names to be more descriptive - drawEndScreen() is now GameEndScreenOverlay(). - drawInputControls() is now GameInputControlsOverlay(). --- src/game-graphics.cpp | 4 ++-- src/game.cpp | 4 ++-- src/headers/game-graphics.hpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index 4951cfd9..64d6d086 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -294,7 +294,7 @@ std::string GameScoreBoardOverlay(scoreboard_display_data_t scdd) { return str_os.str(); } -std::string drawEndScreen(end_screen_display_data_t esdd) { +std::string GameEndScreenOverlay(end_screen_display_data_t esdd) { enum EndScreenDisplayDataFields { IDX_FLAG_WIN, IDX_FLAG_ENDLESS_MODE, @@ -317,7 +317,7 @@ std::string drawEndScreen(end_screen_display_data_t esdd) { return str_os.str(); } -std::string drawInputControls(input_controls_display_data_t gamestatus) { +std::string GameInputControlsOverlay(input_controls_display_data_t gamestatus) { const auto is_in_endless_mode = std::get<0>(gamestatus); const auto is_in_question_mode = std::get<1>(gamestatus); std::ostringstream str_os; diff --git a/src/game.cpp b/src/game.cpp index 9804568c..11a82476 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -103,7 +103,7 @@ gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { const auto input_controls_display_data = make_input_controls_display_data(gamestatus); DrawAlways(os, DataSuppliment(input_controls_display_data, - Graphics::drawInputControls)); + Graphics::GameInputControlsOverlay)); DrawAsOneTimeFlag(os, gamestatus[FLAG_INPUT_ERROR], Graphics::InvalidInputGameBoardErrorPrompt); return gamestatus; @@ -275,7 +275,7 @@ void endlessGameLoop() { DrawAlways(std::cout, DataSuppliment(scdd, Graphics::GameScoreBoardOverlay)); std::cout << gamePlayBoard; const auto esdd = make_end_screen_display_data(world_gamestatus); - DrawAlways(std::cout, DataSuppliment(esdd, Graphics::drawEndScreen)); + DrawAlways(std::cout, DataSuppliment(esdd, Graphics::GameEndScreenOverlay)); } void saveEndGameStats(Scoreboard::Score finalscore) { diff --git a/src/headers/game-graphics.hpp b/src/headers/game-graphics.hpp index b55f35a6..d8760aa3 100644 --- a/src/headers/game-graphics.hpp +++ b/src/headers/game-graphics.hpp @@ -35,9 +35,9 @@ using scoreboard_display_data_t = std::string GameScoreBoardBox(scoreboard_display_data_t scdd); std::string GameScoreBoardOverlay(scoreboard_display_data_t scdd); using end_screen_display_data_t = std::tuple; -std::string drawEndScreen(end_screen_display_data_t esdd); +std::string GameEndScreenOverlay(end_screen_display_data_t esdd); using input_controls_display_data_t = std::tuple; -std::string drawInputControls(input_controls_display_data_t gamestatus); +std::string GameInputControlsOverlay(input_controls_display_data_t gamestatus); } // namespace Graphics } // namespace Game From 05b9f0f48a96c38893f9b73f3d44432c839ac412 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Sun, 6 Oct 2019 13:46:12 +0200 Subject: [PATCH 19/34] added: scores-graphics TU --- CMakeLists.txt | 2 +- meson.build | 2 +- src/headers/scores-graphics.hpp | 4 ++++ src/scores-graphics.cpp | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 src/headers/scores-graphics.hpp create mode 100644 src/scores-graphics.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a13240b9..88de88b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.7) # 3.7: CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT, 3.3: CXX_STANDARD project(2048 CXX) -set(SOURCES src/2048.cpp src/gameboard.cpp src/game.cpp src/game-graphics.cpp src/game-input.cpp src/game-pregamemenu.cpp src/global.cpp src/loadresource.cpp src/menu.cpp src/menu-graphics.cpp src/saveresource.cpp src/scores.cpp src/statistics.cpp src/tile.cpp) +set(SOURCES src/2048.cpp src/gameboard.cpp src/game.cpp src/game-graphics.cpp src/game-input.cpp src/game-pregamemenu.cpp src/global.cpp src/loadresource.cpp src/menu.cpp src/menu-graphics.cpp src/saveresource.cpp src/scores.cpp src/scores-graphics.cpp src/statistics.cpp src/tile.cpp) if(CMAKE_CXX_COMPILER_ID STREQUAL GNU) list(APPEND FLAGS -Wall) diff --git a/meson.build b/meson.build index a4a74b9a..82ade976 100644 --- a/meson.build +++ b/meson.build @@ -10,7 +10,7 @@ elif cxx.get_id() == 'intel' endif main_target_name = '2048' -sources = ['src/2048.cpp', 'src/gameboard.cpp', 'src/game.cpp', 'src/game-input.cpp', 'src/game-graphics.cpp', 'src/game-pregamemenu.cpp', 'src/global.cpp', 'src/loadresource.cpp', 'src/menu.cpp', 'src/menu-graphics.cpp', 'src/saveresource.cpp', 'src/scores.cpp', 'src/statistics.cpp', 'src/tile.cpp'] +sources = ['src/2048.cpp', 'src/gameboard.cpp', 'src/game.cpp', 'src/game-input.cpp', 'src/game-graphics.cpp', 'src/game-pregamemenu.cpp', 'src/global.cpp', 'src/loadresource.cpp', 'src/menu.cpp', 'src/menu-graphics.cpp', 'src/saveresource.cpp', 'src/scores.cpp', 'src/scores-graphics.cpp', 'src/statistics.cpp', 'src/tile.cpp'] hdrs = include_directories('src/headers') executable(main_target_name, sources, diff --git a/src/headers/scores-graphics.hpp b/src/headers/scores-graphics.hpp new file mode 100644 index 00000000..47e14391 --- /dev/null +++ b/src/headers/scores-graphics.hpp @@ -0,0 +1,4 @@ +#ifndef SCORESGRAPHICS_H +#define SCORESGRAPHICS_H + +#endif diff --git a/src/scores-graphics.cpp b/src/scores-graphics.cpp new file mode 100644 index 00000000..a4f296f4 --- /dev/null +++ b/src/scores-graphics.cpp @@ -0,0 +1 @@ +#include "scores-graphics.hpp" From d509b9b329abc39d7fb78b7f05ea7b6122e7b634 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Sun, 6 Oct 2019 18:49:37 +0200 Subject: [PATCH 20/34] fix: Scores: prettyPrintScoreboard renamed and moved to scores-graphics TU - prettyPrintScoreboard() renamed to ScoreboardOverlay(). --- src/headers/scores-graphics.hpp | 8 ++++ src/headers/scores.hpp | 1 - src/menu.cpp | 3 +- src/scores-graphics.cpp | 85 +++++++++++++++++++++++++++++++++ src/scores.cpp | 73 ---------------------------- 5 files changed, 95 insertions(+), 75 deletions(-) diff --git a/src/headers/scores-graphics.hpp b/src/headers/scores-graphics.hpp index 47e14391..1181a926 100644 --- a/src/headers/scores-graphics.hpp +++ b/src/headers/scores-graphics.hpp @@ -1,4 +1,12 @@ #ifndef SCORESGRAPHICS_H #define SCORESGRAPHICS_H +#include + +namespace Scoreboard { +namespace Graphics { +std::string ScoreboardOverlay(); +} +} // namespace Scoreboard + #endif diff --git a/src/headers/scores.hpp b/src/headers/scores.hpp index 92169055..3471fbb6 100644 --- a/src/headers/scores.hpp +++ b/src/headers/scores.hpp @@ -26,7 +26,6 @@ using load_score_status_t = std::tuple; // Note: returns a tuple containing a std::vector of all read scores. load_score_status_t loadFromFileScore(std::string filename); bool saveToFileScore(std::string filename, Score s); -void prettyPrintScoreboard(std::ostream &os); } // namespace Scoreboard std::istream &operator>>(std::istream &is, Scoreboard::Score &s); diff --git a/src/menu.cpp b/src/menu.cpp index dc03792c..2a2cfe1c 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -4,6 +4,7 @@ #include "game.hpp" #include "global.hpp" #include "menu-graphics.hpp" +#include "scores-graphics.hpp" #include "scores.hpp" #include "statistics.hpp" #include @@ -37,7 +38,7 @@ void continueGame() { void showScores() { clearScreen(); DrawAlways(std::cout, Game::Graphics::AsciiArt2048); - Scoreboard::prettyPrintScoreboard(std::cout); + DrawAlways(std::cout, Scoreboard::Graphics::ScoreboardOverlay); Statistics::prettyPrintStats(std::cout); std::cout << std::flush; pause_for_keypress(); diff --git a/src/scores-graphics.cpp b/src/scores-graphics.cpp index a4f296f4..27bcc9a2 100644 --- a/src/scores-graphics.cpp +++ b/src/scores-graphics.cpp @@ -1 +1,86 @@ #include "scores-graphics.hpp" +#include "color.hpp" +#include "scores.hpp" +#include +#include +#include + +namespace Scoreboard { +namespace Graphics { + +std::string ScoreboardOverlay() { + constexpr auto no_save_text = "No saved scores."; + const auto score_attributes_text = { + "No.", "Name", "Score", "Won?", "Moves", "Largest Tile", "Duration"}; + // constexpr auto num_of_score_attributes_text = 7; + constexpr auto header_border_text = + "┌─────┬────────────────────┬──────────┬──────┬───────┬──────────────┬──────────────┐"; + constexpr auto mid_border_text = + "├─────┼────────────────────┼──────────┼──────┼───────┼──────────────┼──────────────┤"; + constexpr auto bottom_border_text = + "└─────┴────────────────────┴──────────┴──────┴───────┴──────────────┴──────────────┘"; + constexpr auto score_title_text = "SCOREBOARD"; + constexpr auto divider_text = "──────────"; + constexpr auto sp = " "; + + std::ostringstream str_os; + + std::vector scoreList{}; + // bool loaded_scorelist; + // Warning: Does not care if file exists or not! + std::tie(std::ignore, scoreList) = loadFromFileScore("../data/scores.txt"); + + str_os << green << bold_on << sp << score_title_text << bold_off << def + << "\n"; + str_os << green << bold_on << sp << divider_text << bold_off << def << "\n"; + + const auto number_of_scores = scoreList.size(); + if (number_of_scores) { + str_os << sp << header_border_text << "\n"; + str_os << std::left; + str_os << sp << "│ " << bold_on << std::begin(score_attributes_text)[0] + << bold_off << " │ " << bold_on << std::setw(18) + << std::begin(score_attributes_text)[1] << bold_off << " │ " + << bold_on << std::setw(8) << std::begin(score_attributes_text)[2] + << bold_off << " │ " << bold_on + << std::begin(score_attributes_text)[3] << bold_off << " │ " + << bold_on << std::begin(score_attributes_text)[4] << bold_off + << " │ " << bold_on << std::begin(score_attributes_text)[5] + << bold_off << " │ " << bold_on << std::setw(12) + << std::begin(score_attributes_text)[6] << bold_off << " │" + << "\n"; + str_os << std::right; + str_os << sp << mid_border_text << "\n"; + + auto counter{1}; + const auto print_score_stat = [&counter, &str_os](const Score i) { + constexpr auto number_of_fields = 7; + auto data_stats = std::array{}; + data_stats = {std::to_string(counter), i.name, + std::to_string(i.score), i.win ? "Yes" : "No", + std::to_string(i.moveCount), std::to_string(i.largestTile), + secondsFormat(i.duration)}; + str_os << sp << "│ " << std::setw(2) << data_stats[0] << ". │ " + << std::left << std::setw(18) << data_stats[1] << std::right + << " │ " << std::setw(8) << data_stats[2] << " │ " << std::setw(4) + << data_stats[3] << " │ " << std::setw(5) << data_stats[4] << " │ " + << std::setw(12) << data_stats[5] << " │ " << std::setw(12) + << data_stats[6] << " │" + << "\n"; + counter++; + }; + + for (const auto s : scoreList) { + print_score_stat(s); + } + str_os << sp << bottom_border_text << "\n"; + } else { + str_os << sp << no_save_text << "\n"; + } + str_os << "\n\n"; + return str_os.str(); +} + +} // namespace Graphics + +} // namespace Scoreboard diff --git a/src/scores.cpp b/src/scores.cpp index 2d894c6d..68f56e36 100644 --- a/src/scores.cpp +++ b/src/scores.cpp @@ -3,8 +3,6 @@ #include #include #include -#include -#include #include namespace { @@ -41,77 +39,6 @@ bool saveToFileScore(std::string filename, Score s) { return generateFilefromScoreData(os, s); } -void prettyPrintScoreboard(std::ostream &os) { - constexpr auto no_save_text = "No saved scores."; - const auto score_attributes_text = { - "No.", "Name", "Score", "Won?", "Moves", "Largest Tile", "Duration"}; - // constexpr auto num_of_score_attributes_text = 7; - constexpr auto header_border_text = - "┌─────┬────────────────────┬──────────┬──────┬───────┬──────────────┬──────────────┐"; - constexpr auto mid_border_text = - "├─────┼────────────────────┼──────────┼──────┼───────┼──────────────┼──────────────┤"; - constexpr auto bottom_border_text = - "└─────┴────────────────────┴──────────┴──────┴───────┴──────────────┴──────────────┘"; - constexpr auto score_title_text = "SCOREBOARD"; - constexpr auto divider_text = "──────────"; - constexpr auto sp = " "; - - std::ostringstream str_os; - - std::vector scoreList{}; - // bool loaded_scorelist; - // Warning: Does not care if file exists or not! - std::tie(std::ignore, scoreList) = loadFromFileScore("../data/scores.txt"); - - str_os << green << bold_on << sp << score_title_text << bold_off << def - << "\n"; - str_os << green << bold_on << sp << divider_text << bold_off << def << "\n"; - - const auto number_of_scores = scoreList.size(); - if (number_of_scores) { - str_os << sp << header_border_text << "\n"; - str_os << std::left; - str_os << sp << "│ " << bold_on << std::begin(score_attributes_text)[0] - << bold_off << " │ " << bold_on << std::setw(18) - << std::begin(score_attributes_text)[1] << bold_off << " │ " - << bold_on << std::setw(8) << std::begin(score_attributes_text)[2] - << bold_off << " │ " << bold_on - << std::begin(score_attributes_text)[3] << bold_off << " │ " - << bold_on << std::begin(score_attributes_text)[4] << bold_off - << " │ " << bold_on << std::begin(score_attributes_text)[5] - << bold_off << " │ " << bold_on << std::setw(12) - << std::begin(score_attributes_text)[6] << bold_off << " │" - << "\n"; - str_os << std::right; - str_os << sp << mid_border_text << "\n"; - - auto counter{1}; - const auto print_score_stat = [&counter, &str_os](const Score i) { - constexpr auto number_of_fields = 7; - auto data_stats = std::array{}; - data_stats = {std::to_string(counter), i.name, - std::to_string(i.score), i.win ? "Yes" : "No", - std::to_string(i.moveCount), std::to_string(i.largestTile), - secondsFormat(i.duration)}; - str_os << sp << "│ " << std::setw(2) << data_stats[0] << ". │ " - << std::left << std::setw(18) << data_stats[1] << std::right - << " │ " << std::setw(8) << data_stats[2] << " │ " << std::setw(4) - << data_stats[3] << " │ " << std::setw(5) << data_stats[4] << " │ " - << std::setw(12) << data_stats[5] << " │ " << std::setw(12) - << data_stats[6] << " │" - << "\n"; - counter++; - }; - - std::for_each(std::begin(scoreList), std::end(scoreList), print_score_stat); - str_os << sp << bottom_border_text << "\n"; - } else { - str_os << sp << no_save_text << "\n"; - } - str_os << "\n\n"; - os << str_os.str(); -} - bool operator>(const Score &a, const Score &b) { return a.score > b.score; } From 1db3f65247861e34e6779793f232d1cb2e24ebf8 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Mon, 7 Oct 2019 21:21:51 +0200 Subject: [PATCH 21/34] fix: Score-Graphics: ScoreboardOverlay accepts custom tuple as parameter - ScoreboardOverlay() uses scoreboard_display_data_list_t as parameter. --- src/headers/scores-graphics.hpp | 10 ++++++++-- src/menu.cpp | 31 ++++++++++++++++++++++++++++- src/scores-graphics.cpp | 35 ++++++++++----------------------- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/headers/scores-graphics.hpp b/src/headers/scores-graphics.hpp index 1181a926..09790dfe 100644 --- a/src/headers/scores-graphics.hpp +++ b/src/headers/scores-graphics.hpp @@ -2,11 +2,17 @@ #define SCORESGRAPHICS_H #include +#include namespace Scoreboard { namespace Graphics { -std::string ScoreboardOverlay(); -} +using scoreboard_display_data_t = + std::tuple; + +using scoreboard_display_data_list_t = std::vector; +std::string ScoreboardOverlay(scoreboard_display_data_list_t sbddl); +} // namespace Graphics } // namespace Scoreboard #endif diff --git a/src/menu.cpp b/src/menu.cpp index 2a2cfe1c..9dc7c989 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -7,6 +7,7 @@ #include "scores-graphics.hpp" #include "scores.hpp" #include "statistics.hpp" +#include #include #include #include @@ -35,10 +36,38 @@ void continueGame() { Game::continueGame(); } +Scoreboard::Graphics::scoreboard_display_data_list_t +make_scoreboard_display_data_list() { + using namespace Scoreboard::Graphics; + auto scoreList = Scoreboard::Scoreboard_t{}; + // bool loaded_scorelist; + // Warning: Does not care if file exists or not! + std::tie(std::ignore, scoreList) = + Scoreboard::loadFromFileScore("../data/scores.txt"); + + auto counter{1}; + const auto convert_to_display_list_t = [&counter](const Scoreboard::Score s) { + const auto data_stats = std::make_tuple( + std::to_string(counter), s.name, std::to_string(s.score), + s.win ? "Yes" : "No", std::to_string(s.moveCount), + std::to_string(s.largestTile), secondsFormat(s.duration)); + counter++; + return data_stats; + }; + + auto scoreboard_display_list = scoreboard_display_data_list_t{}; + std::transform(std::begin(scoreList), std::end(scoreList), + std::back_inserter(scoreboard_display_list), + convert_to_display_list_t); + return scoreboard_display_list; +}; + void showScores() { + const auto sbddl = make_scoreboard_display_data_list(); clearScreen(); DrawAlways(std::cout, Game::Graphics::AsciiArt2048); - DrawAlways(std::cout, Scoreboard::Graphics::ScoreboardOverlay); + DrawAlways(std::cout, + DataSuppliment(sbddl, Scoreboard::Graphics::ScoreboardOverlay)); Statistics::prettyPrintStats(std::cout); std::cout << std::flush; pause_for_keypress(); diff --git a/src/scores-graphics.cpp b/src/scores-graphics.cpp index 27bcc9a2..3d4f07cc 100644 --- a/src/scores-graphics.cpp +++ b/src/scores-graphics.cpp @@ -1,14 +1,12 @@ #include "scores-graphics.hpp" #include "color.hpp" -#include "scores.hpp" #include #include -#include namespace Scoreboard { namespace Graphics { -std::string ScoreboardOverlay() { +std::string ScoreboardOverlay(scoreboard_display_data_list_t sbddl) { constexpr auto no_save_text = "No saved scores."; const auto score_attributes_text = { "No.", "Name", "Score", "Won?", "Moves", "Largest Tile", "Duration"}; @@ -25,16 +23,11 @@ std::string ScoreboardOverlay() { std::ostringstream str_os; - std::vector scoreList{}; - // bool loaded_scorelist; - // Warning: Does not care if file exists or not! - std::tie(std::ignore, scoreList) = loadFromFileScore("../data/scores.txt"); - str_os << green << bold_on << sp << score_title_text << bold_off << def << "\n"; str_os << green << bold_on << sp << divider_text << bold_off << def << "\n"; - const auto number_of_scores = scoreList.size(); + const auto number_of_scores = sbddl.size(); if (number_of_scores) { str_os << sp << header_border_text << "\n"; str_os << std::left; @@ -52,25 +45,17 @@ std::string ScoreboardOverlay() { str_os << std::right; str_os << sp << mid_border_text << "\n"; - auto counter{1}; - const auto print_score_stat = [&counter, &str_os](const Score i) { - constexpr auto number_of_fields = 7; - auto data_stats = std::array{}; - data_stats = {std::to_string(counter), i.name, - std::to_string(i.score), i.win ? "Yes" : "No", - std::to_string(i.moveCount), std::to_string(i.largestTile), - secondsFormat(i.duration)}; - str_os << sp << "│ " << std::setw(2) << data_stats[0] << ". │ " - << std::left << std::setw(18) << data_stats[1] << std::right - << " │ " << std::setw(8) << data_stats[2] << " │ " << std::setw(4) - << data_stats[3] << " │ " << std::setw(5) << data_stats[4] << " │ " - << std::setw(12) << data_stats[5] << " │ " << std::setw(12) - << data_stats[6] << " │" + const auto print_score_stat = [&str_os](const scoreboard_display_data_t i) { + str_os << sp << "│ " << std::setw(2) << std::get<0>(i) << ". │ " + << std::left << std::setw(18) << std::get<1>(i) << std::right + << " │ " << std::setw(8) << std::get<2>(i) << " │ " << std::setw(4) + << std::get<3>(i) << " │ " << std::setw(5) << std::get<4>(i) + << " │ " << std::setw(12) << std::get<5>(i) << " │ " + << std::setw(12) << std::get<6>(i) << " │" << "\n"; - counter++; }; - for (const auto s : scoreList) { + for (const auto s : sbddl) { print_score_stat(s); } str_os << sp << bottom_border_text << "\n"; From c2998199074594c14e62c81841bcf169113e0980 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Tue, 8 Oct 2019 21:08:41 +0200 Subject: [PATCH 22/34] added: statistics-graphics TU --- CMakeLists.txt | 2 +- meson.build | 2 +- src/headers/statistics-graphics.hpp | 4 ++++ src/statistics-graphics.cpp | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 src/headers/statistics-graphics.hpp create mode 100644 src/statistics-graphics.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 88de88b8..710cba9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.7) # 3.7: CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT, 3.3: CXX_STANDARD project(2048 CXX) -set(SOURCES src/2048.cpp src/gameboard.cpp src/game.cpp src/game-graphics.cpp src/game-input.cpp src/game-pregamemenu.cpp src/global.cpp src/loadresource.cpp src/menu.cpp src/menu-graphics.cpp src/saveresource.cpp src/scores.cpp src/scores-graphics.cpp src/statistics.cpp src/tile.cpp) +set(SOURCES src/2048.cpp src/gameboard.cpp src/game.cpp src/game-graphics.cpp src/game-input.cpp src/game-pregamemenu.cpp src/global.cpp src/loadresource.cpp src/menu.cpp src/menu-graphics.cpp src/saveresource.cpp src/scores.cpp src/scores-graphics.cpp src/statistics.cpp src/statistics-graphics.cpp src/tile.cpp) if(CMAKE_CXX_COMPILER_ID STREQUAL GNU) list(APPEND FLAGS -Wall) diff --git a/meson.build b/meson.build index 82ade976..fc21563e 100644 --- a/meson.build +++ b/meson.build @@ -10,7 +10,7 @@ elif cxx.get_id() == 'intel' endif main_target_name = '2048' -sources = ['src/2048.cpp', 'src/gameboard.cpp', 'src/game.cpp', 'src/game-input.cpp', 'src/game-graphics.cpp', 'src/game-pregamemenu.cpp', 'src/global.cpp', 'src/loadresource.cpp', 'src/menu.cpp', 'src/menu-graphics.cpp', 'src/saveresource.cpp', 'src/scores.cpp', 'src/scores-graphics.cpp', 'src/statistics.cpp', 'src/tile.cpp'] +sources = ['src/2048.cpp', 'src/gameboard.cpp', 'src/game.cpp', 'src/game-input.cpp', 'src/game-graphics.cpp', 'src/game-pregamemenu.cpp', 'src/global.cpp', 'src/loadresource.cpp', 'src/menu.cpp', 'src/menu-graphics.cpp', 'src/saveresource.cpp', 'src/scores.cpp', 'src/scores-graphics.cpp', 'src/statistics.cpp', 'src/statistics-graphics.cpp', 'src/tile.cpp'] hdrs = include_directories('src/headers') executable(main_target_name, sources, diff --git a/src/headers/statistics-graphics.hpp b/src/headers/statistics-graphics.hpp new file mode 100644 index 00000000..65ce242c --- /dev/null +++ b/src/headers/statistics-graphics.hpp @@ -0,0 +1,4 @@ +#ifndef STATISTICSGRAPHICS_H +#define STATISTICSGRAPHICS_H + +#endif diff --git a/src/statistics-graphics.cpp b/src/statistics-graphics.cpp new file mode 100644 index 00000000..b4562777 --- /dev/null +++ b/src/statistics-graphics.cpp @@ -0,0 +1 @@ +#include "statistics-graphics.hpp" From 11eadf37cb24097c0aaa329a38c7ed35ccaf41e4 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Tue, 8 Oct 2019 21:21:04 +0200 Subject: [PATCH 23/34] fix: Statistics: prettyPrintStats renamed and moved to statistics- graphics TU - prettyPrintStats() now named TotalStatisticsOverlay(). --- src/headers/statistics-graphics.hpp | 6 +++ src/headers/statistics.hpp | 1 - src/menu.cpp | 3 +- src/statistics-graphics.cpp | 69 +++++++++++++++++++++++++++++ src/statistics.cpp | 59 ------------------------ 5 files changed, 77 insertions(+), 61 deletions(-) diff --git a/src/headers/statistics-graphics.hpp b/src/headers/statistics-graphics.hpp index 65ce242c..86b81330 100644 --- a/src/headers/statistics-graphics.hpp +++ b/src/headers/statistics-graphics.hpp @@ -1,4 +1,10 @@ #ifndef STATISTICSGRAPHICS_H #define STATISTICSGRAPHICS_H +#include + +namespace Statistics { +std::string TotalStatisticsOverlay(); +} + #endif diff --git a/src/headers/statistics.hpp b/src/headers/statistics.hpp index a1e6eb40..3f0fb208 100644 --- a/src/headers/statistics.hpp +++ b/src/headers/statistics.hpp @@ -19,7 +19,6 @@ using load_stats_status_t = std::tuple; load_stats_status_t loadFromFileStatistics(std::string filename); bool saveToFileEndGameStatistics(std::string filename, total_game_stats_t s); -void prettyPrintStats(std::ostream &os); } // namespace Statistics std::istream &operator>>(std::istream &is, Statistics::total_game_stats_t &s); diff --git a/src/menu.cpp b/src/menu.cpp index 9dc7c989..f9a02966 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -6,6 +6,7 @@ #include "menu-graphics.hpp" #include "scores-graphics.hpp" #include "scores.hpp" +#include "statistics-graphics.hpp" #include "statistics.hpp" #include #include @@ -68,7 +69,7 @@ void showScores() { DrawAlways(std::cout, Game::Graphics::AsciiArt2048); DrawAlways(std::cout, DataSuppliment(sbddl, Scoreboard::Graphics::ScoreboardOverlay)); - Statistics::prettyPrintStats(std::cout); + DrawAlways(std::cout, Statistics::TotalStatisticsOverlay); std::cout << std::flush; pause_for_keypress(); Menu::startMenu(); diff --git a/src/statistics-graphics.cpp b/src/statistics-graphics.cpp index b4562777..8625f10f 100644 --- a/src/statistics-graphics.cpp +++ b/src/statistics-graphics.cpp @@ -1 +1,70 @@ #include "statistics-graphics.hpp" +#include "color.hpp" +#include "statistics.hpp" +#include +#include +#include + +namespace Statistics { + +std::string TotalStatisticsOverlay() { + constexpr auto stats_title_text = "STATISTICS"; + constexpr auto divider_text = "──────────"; + constexpr auto header_border_text = "┌────────────────────┬─────────────┐"; + constexpr auto footer_border_text = "└────────────────────┴─────────────┘"; + const auto stats_attributes_text = {"Best Score", "Game Count", + "Number of Wins", "Total Moves Played", + "Total Duration"}; + constexpr auto no_save_text = "No saved statistics."; + constexpr auto any_key_exit_text = + "Press any key to return to the main menu... "; + constexpr auto sp = " "; + + std::ostringstream stats_richtext; + + total_game_stats_t stats; + bool stats_file_loaded{}; + std::tie(stats_file_loaded, stats) = + loadFromFileStatistics("../data/statistics.txt"); + if (stats_file_loaded) { + constexpr auto num_of_stats_attributes_text = 5; + auto data_stats = std::array{}; + data_stats = { + std::to_string(stats.bestScore), std::to_string(stats.gameCount), + std::to_string(stats.winCount), std::to_string(stats.totalMoveCount), + secondsFormat(stats.totalDuration)}; + + auto counter{0}; + const auto populate_stats_info = [data_stats, stats_attributes_text, + &counter, + &stats_richtext](const std::string) { + stats_richtext << sp << "│ " << bold_on << std::left << std::setw(18) + << std::begin(stats_attributes_text)[counter] << bold_off + << " │ " << std::right << std::setw(11) + << data_stats[counter] << " │" + << "\n"; + counter++; + }; + + stats_richtext << green << bold_on << sp << stats_title_text << bold_off + << def << "\n"; + stats_richtext << green << bold_on << sp << divider_text << bold_off << def + << "\n"; + stats_richtext << sp << header_border_text << "\n"; + + for (const auto s : stats_attributes_text) { + populate_stats_info(s); + } + stats_richtext << sp << footer_border_text << "\n"; + + } else { + stats_richtext << sp << no_save_text << "\n"; + } + + stats_richtext << "\n\n\n"; + stats_richtext << sp << any_key_exit_text; + + return stats_richtext.str(); +} + +} // namespace Statistics diff --git a/src/statistics.cpp b/src/statistics.cpp index 93bcee69..85a4098d 100644 --- a/src/statistics.cpp +++ b/src/statistics.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include namespace Statistics { @@ -37,64 +36,6 @@ bool saveToFileEndGameStatistics(std::string filename, total_game_stats_t s) { return generateFilefromStatsData(filedata, s); } -void prettyPrintStats(std::ostream &os) { - constexpr auto stats_title_text = "STATISTICS"; - constexpr auto divider_text = "──────────"; - constexpr auto header_border_text = "┌────────────────────┬─────────────┐"; - constexpr auto footer_border_text = "└────────────────────┴─────────────┘"; - const auto stats_attributes_text = {"Best Score", "Game Count", - "Number of Wins", "Total Moves Played", - "Total Duration"}; - constexpr auto no_save_text = "No saved statistics."; - constexpr auto any_key_exit_text = - "Press any key to return to the main menu... "; - constexpr auto sp = " "; - - std::ostringstream stats_richtext; - - total_game_stats_t stats; - bool stats_file_loaded{}; - std::tie(stats_file_loaded, stats) = - loadFromFileStatistics("../data/statistics.txt"); - if (stats_file_loaded) { - constexpr auto num_of_stats_attributes_text = 5; - auto data_stats = std::array{}; - data_stats = { - std::to_string(stats.bestScore), std::to_string(stats.gameCount), - std::to_string(stats.winCount), std::to_string(stats.totalMoveCount), - secondsFormat(stats.totalDuration)}; - - auto counter{0}; - const auto populate_stats_info = [data_stats, stats_attributes_text, - &counter, - &stats_richtext](const std::string) { - stats_richtext << sp << "│ " << bold_on << std::left << std::setw(18) - << std::begin(stats_attributes_text)[counter] << bold_off - << " │ " << std::right << std::setw(11) - << data_stats[counter] << " │" - << "\n"; - counter++; - }; - - stats_richtext << green << bold_on << sp << stats_title_text << bold_off - << def << "\n"; - stats_richtext << green << bold_on << sp << divider_text << bold_off << def - << "\n"; - stats_richtext << sp << header_border_text << "\n"; - std::for_each(std::begin(stats_attributes_text), - std::end(stats_attributes_text), populate_stats_info); - stats_richtext << sp << footer_border_text << "\n"; - - } else { - stats_richtext << sp << no_save_text << "\n"; - } - - stats_richtext << "\n\n\n"; - stats_richtext << sp << any_key_exit_text; - - os << stats_richtext.str(); -} - } // namespace Statistics using namespace Statistics; From 748c056aa6f2aed2b426691972e888537f18c3e6 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Tue, 8 Oct 2019 22:32:46 +0200 Subject: [PATCH 24/34] fix: Statistics-Graphics: TotalStatisticsOverlay accepts custom tuple as parameter - TotalStatisticsOverlay() uses total_stats_display_data_t as parameter. --- src/headers/statistics-graphics.hpp | 8 ++++++-- src/menu.cpp | 19 ++++++++++++++++++- src/statistics-graphics.cpp | 27 +++++++++++++++++---------- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/headers/statistics-graphics.hpp b/src/headers/statistics-graphics.hpp index 86b81330..0363188d 100644 --- a/src/headers/statistics-graphics.hpp +++ b/src/headers/statistics-graphics.hpp @@ -2,9 +2,13 @@ #define STATISTICSGRAPHICS_H #include +#include namespace Statistics { -std::string TotalStatisticsOverlay(); -} +using total_stats_display_data_t = + std::tuple; +std::string TotalStatisticsOverlay(total_stats_display_data_t tsdd); +} // namespace Statistics #endif diff --git a/src/menu.cpp b/src/menu.cpp index f9a02966..f4c9ce1a 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -63,13 +63,30 @@ make_scoreboard_display_data_list() { return scoreboard_display_list; }; +Statistics::total_stats_display_data_t make_total_stats_display_data() { + Statistics::total_game_stats_t stats; + bool stats_file_loaded{}; + std::tie(stats_file_loaded, stats) = + Statistics::loadFromFileStatistics("../data/statistics.txt"); + + const auto tsdd = std::make_tuple( + stats_file_loaded, std::to_string(stats.bestScore), + std::to_string(stats.gameCount), std::to_string(stats.winCount), + std::to_string(stats.totalMoveCount), secondsFormat(stats.totalDuration)); + return tsdd; +}; + void showScores() { const auto sbddl = make_scoreboard_display_data_list(); + const auto tsdd = make_total_stats_display_data(); + clearScreen(); DrawAlways(std::cout, Game::Graphics::AsciiArt2048); DrawAlways(std::cout, DataSuppliment(sbddl, Scoreboard::Graphics::ScoreboardOverlay)); - DrawAlways(std::cout, Statistics::TotalStatisticsOverlay); + + DrawAlways(std::cout, + DataSuppliment(tsdd, Statistics::TotalStatisticsOverlay)); std::cout << std::flush; pause_for_keypress(); Menu::startMenu(); diff --git a/src/statistics-graphics.cpp b/src/statistics-graphics.cpp index 8625f10f..7a08f3a7 100644 --- a/src/statistics-graphics.cpp +++ b/src/statistics-graphics.cpp @@ -1,13 +1,12 @@ #include "statistics-graphics.hpp" #include "color.hpp" -#include "statistics.hpp" #include #include #include namespace Statistics { -std::string TotalStatisticsOverlay() { +std::string TotalStatisticsOverlay(total_stats_display_data_t tsdd) { constexpr auto stats_title_text = "STATISTICS"; constexpr auto divider_text = "──────────"; constexpr auto header_border_text = "┌────────────────────┬─────────────┐"; @@ -20,19 +19,27 @@ std::string TotalStatisticsOverlay() { "Press any key to return to the main menu... "; constexpr auto sp = " "; + enum TotalStatsDisplayDataFields { + IDX_DATA_AVAILABLE, + IDX_BEST_SCORE, + IDX_GAME_COUNT, + IDX_GAME_WIN_COUNT, + IDX_TOTAL_MOVE_COUNT, + IDX_TOTAL_DURATION, + MAX_TOTALSTATSDISPLAYDATA_INDEXES + }; + std::ostringstream stats_richtext; - total_game_stats_t stats; - bool stats_file_loaded{}; - std::tie(stats_file_loaded, stats) = - loadFromFileStatistics("../data/statistics.txt"); + const auto stats_file_loaded = std::get(tsdd); if (stats_file_loaded) { constexpr auto num_of_stats_attributes_text = 5; auto data_stats = std::array{}; - data_stats = { - std::to_string(stats.bestScore), std::to_string(stats.gameCount), - std::to_string(stats.winCount), std::to_string(stats.totalMoveCount), - secondsFormat(stats.totalDuration)}; + data_stats = {std::get(tsdd), + std::get(tsdd), + std::get(tsdd), + std::get(tsdd), + std::get(tsdd)}; auto counter{0}; const auto populate_stats_info = [data_stats, stats_attributes_text, From b4b9b20f6ff00a4e510f3849248dd3a9c618dfd8 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Tue, 8 Oct 2019 22:58:13 +0200 Subject: [PATCH 25/34] fix: Menu: fix namespacing --- src/headers/statistics-graphics.hpp | 2 ++ src/menu.cpp | 17 +++++++++-------- src/statistics-graphics.cpp | 2 ++ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/headers/statistics-graphics.hpp b/src/headers/statistics-graphics.hpp index 0363188d..66414323 100644 --- a/src/headers/statistics-graphics.hpp +++ b/src/headers/statistics-graphics.hpp @@ -5,10 +5,12 @@ #include namespace Statistics { +namespace Graphics { using total_stats_display_data_t = std::tuple; std::string TotalStatisticsOverlay(total_stats_display_data_t tsdd); +} // namespace Graphics } // namespace Statistics #endif diff --git a/src/menu.cpp b/src/menu.cpp index f4c9ce1a..eecdab27 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -63,7 +63,8 @@ make_scoreboard_display_data_list() { return scoreboard_display_list; }; -Statistics::total_stats_display_data_t make_total_stats_display_data() { +Statistics::Graphics::total_stats_display_data_t +make_total_stats_display_data() { Statistics::total_game_stats_t stats; bool stats_file_loaded{}; std::tie(stats_file_loaded, stats) = @@ -77,19 +78,19 @@ Statistics::total_stats_display_data_t make_total_stats_display_data() { }; void showScores() { + using namespace Game::Graphics; + using namespace Scoreboard::Graphics; + using namespace Statistics::Graphics; const auto sbddl = make_scoreboard_display_data_list(); const auto tsdd = make_total_stats_display_data(); clearScreen(); - DrawAlways(std::cout, Game::Graphics::AsciiArt2048); - DrawAlways(std::cout, - DataSuppliment(sbddl, Scoreboard::Graphics::ScoreboardOverlay)); - - DrawAlways(std::cout, - DataSuppliment(tsdd, Statistics::TotalStatisticsOverlay)); + DrawAlways(std::cout, AsciiArt2048); + DrawAlways(std::cout, DataSuppliment(sbddl, ScoreboardOverlay)); + DrawAlways(std::cout, DataSuppliment(tsdd, TotalStatisticsOverlay)); std::cout << std::flush; pause_for_keypress(); - Menu::startMenu(); + ::Menu::startMenu(); } void receive_input_flags(std::istream &in_os) { diff --git a/src/statistics-graphics.cpp b/src/statistics-graphics.cpp index 7a08f3a7..8df40dc1 100644 --- a/src/statistics-graphics.cpp +++ b/src/statistics-graphics.cpp @@ -5,6 +5,7 @@ #include namespace Statistics { +namespace Graphics { std::string TotalStatisticsOverlay(total_stats_display_data_t tsdd) { constexpr auto stats_title_text = "STATISTICS"; @@ -74,4 +75,5 @@ std::string TotalStatisticsOverlay(total_stats_display_data_t tsdd) { return stats_richtext.str(); } +} // namespace Graphics } // namespace Statistics From 15731f54d0ee916520654cd608f099f5204cd9ea Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Wed, 9 Oct 2019 21:05:59 +0200 Subject: [PATCH 26/34] added: gameboard-graphics TU --- CMakeLists.txt | 2 +- meson.build | 2 +- src/gameboard-graphics.cpp | 1 + src/headers/gameboard-graphics.hpp | 4 ++++ 4 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 src/gameboard-graphics.cpp create mode 100644 src/headers/gameboard-graphics.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 710cba9c..23103098 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.7) # 3.7: CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT, 3.3: CXX_STANDARD project(2048 CXX) -set(SOURCES src/2048.cpp src/gameboard.cpp src/game.cpp src/game-graphics.cpp src/game-input.cpp src/game-pregamemenu.cpp src/global.cpp src/loadresource.cpp src/menu.cpp src/menu-graphics.cpp src/saveresource.cpp src/scores.cpp src/scores-graphics.cpp src/statistics.cpp src/statistics-graphics.cpp src/tile.cpp) +set(SOURCES src/2048.cpp src/gameboard.cpp src/gameboard-graphics.cpp src/game.cpp src/game-graphics.cpp src/game-input.cpp src/game-pregamemenu.cpp src/global.cpp src/loadresource.cpp src/menu.cpp src/menu-graphics.cpp src/saveresource.cpp src/scores.cpp src/scores-graphics.cpp src/statistics.cpp src/statistics-graphics.cpp src/tile.cpp) if(CMAKE_CXX_COMPILER_ID STREQUAL GNU) list(APPEND FLAGS -Wall) diff --git a/meson.build b/meson.build index fc21563e..48668e5d 100644 --- a/meson.build +++ b/meson.build @@ -10,7 +10,7 @@ elif cxx.get_id() == 'intel' endif main_target_name = '2048' -sources = ['src/2048.cpp', 'src/gameboard.cpp', 'src/game.cpp', 'src/game-input.cpp', 'src/game-graphics.cpp', 'src/game-pregamemenu.cpp', 'src/global.cpp', 'src/loadresource.cpp', 'src/menu.cpp', 'src/menu-graphics.cpp', 'src/saveresource.cpp', 'src/scores.cpp', 'src/scores-graphics.cpp', 'src/statistics.cpp', 'src/statistics-graphics.cpp', 'src/tile.cpp'] +sources = ['src/2048.cpp', 'src/gameboard.cpp', 'src/gameboard-graphics.cpp', 'src/game.cpp', 'src/game-input.cpp', 'src/game-graphics.cpp', 'src/game-pregamemenu.cpp', 'src/global.cpp', 'src/loadresource.cpp', 'src/menu.cpp', 'src/menu-graphics.cpp', 'src/saveresource.cpp', 'src/scores.cpp', 'src/scores-graphics.cpp', 'src/statistics.cpp', 'src/statistics-graphics.cpp', 'src/tile.cpp'] hdrs = include_directories('src/headers') executable(main_target_name, sources, diff --git a/src/gameboard-graphics.cpp b/src/gameboard-graphics.cpp new file mode 100644 index 00000000..030dc244 --- /dev/null +++ b/src/gameboard-graphics.cpp @@ -0,0 +1 @@ +#include "gameboard-graphics.hpp" diff --git a/src/headers/gameboard-graphics.hpp b/src/headers/gameboard-graphics.hpp new file mode 100644 index 00000000..67962abd --- /dev/null +++ b/src/headers/gameboard-graphics.hpp @@ -0,0 +1,4 @@ +#ifndef GAMEBOARDGRAPHICS_H +#define GAMEBOARDGRAPHICS_H + +#endif From d620e380aacbe81c4a7f22a1e7b6be58f361a969 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Wed, 9 Oct 2019 21:29:38 +0200 Subject: [PATCH 27/34] fix: Gameboard: drawSelf moved to gameboard-graphics TU - getPlaySizeOfGameboardDataArray and getTileOnGameboardDataArray now have public TU linkage visibility. --- src/gameboard-graphics.cpp | 65 +++++++++++++++++++++++++++ src/gameboard.cpp | 71 ++++-------------------------- src/headers/gameboard-graphics.hpp | 10 +++++ src/headers/gameboard.hpp | 6 +++ 4 files changed, 90 insertions(+), 62 deletions(-) diff --git a/src/gameboard-graphics.cpp b/src/gameboard-graphics.cpp index 030dc244..83919d60 100644 --- a/src/gameboard-graphics.cpp +++ b/src/gameboard-graphics.cpp @@ -1 +1,66 @@ #include "gameboard-graphics.hpp" +#include "point2d.hpp" +#include +#include +#include + +namespace Game { +namespace { + +template +std::array make_patterned_bars(int playsize) { + auto temp_bars = std::array{}; + using bar_pattern_t = std::tuple; + + const auto bar_pattern_list = {std::make_tuple("┌", "┬", "┐"), + std::make_tuple("├", "┼", "┤"), + std::make_tuple("└", "┴", "┘")}; + + // generate types of horizontal bars... + const auto generate_x_bar_pattern = [playsize](const bar_pattern_t t) { + enum { PATTERN_HEAD, PATTERN_MID, PATTERN_TAIL }; + constexpr auto sp = " "; + constexpr auto separator = "──────"; + std::ostringstream temp_richtext; + temp_richtext << sp << std::get(t); + for (auto i = 0; i < playsize; i++) { + const auto is_not_last_column = (i < playsize - 1); + temp_richtext << separator + << (is_not_last_column ? std::get(t) : + std::get(t)); + } + temp_richtext << "\n"; + return temp_richtext.str(); + }; + std::transform(std::begin(bar_pattern_list), std::end(bar_pattern_list), + std::begin(temp_bars), generate_x_bar_pattern); + return temp_bars; +} + +} // namespace + +std::string drawSelf(GameBoard::gameboard_data_array_t gbda) { + enum { TOP_BAR, XN_BAR, BASE_BAR, MAX_TYPES_OF_BARS }; + const int playsize = getPlaySizeOfGameboardDataArray(gbda); + const auto vertibar = make_patterned_bars(playsize); + std::ostringstream str_os; + for (auto y = 0; y < playsize; y++) { + const auto is_first_row = (y == 0); + str_os << (is_first_row ? std::get(vertibar) : + std::get(vertibar)); + for (auto x = 0; x < playsize; x++) { + const auto is_first_col = (x == 0); + const auto sp = (is_first_col ? " " : " "); + str_os << sp; + str_os << "│ "; + str_os << getTileOnGameboardDataArray(gbda, point2D_t{x, y}); + } + str_os << " │"; + str_os << "\n"; + } + str_os << std::get(vertibar); + str_os << "\n"; + return str_os.str(); +} + +} // namespace Game diff --git a/src/gameboard.cpp b/src/gameboard.cpp index 206866de..721882ce 100644 --- a/src/gameboard.cpp +++ b/src/gameboard.cpp @@ -1,4 +1,5 @@ #include "gameboard.hpp" +#include "gameboard-graphics.hpp" #include "point2d.hpp" #include #include @@ -31,10 +32,6 @@ class RandInt { using gameboard_data_array_t = GameBoard::gameboard_data_array_t; enum gameboard_data_array_fields { IDX_PLAYSIZE, IDX_BOARD, MAX_NO_INDEXES }; -size_t getPlaySizeOfGameboardDataArray(gameboard_data_array_t gbda) { - return std::get(gbda); -} - struct gameboard_data_point_t { static int point2D_to_1D_index(gameboard_data_array_t gbda, point2D_t pt) { int x, y; @@ -50,10 +47,6 @@ struct gameboard_data_point_t { } }; -tile_t getTileOnGameboardDataArray(gameboard_data_array_t gbda, point2D_t pt) { - return gameboard_data_point_t{}(gbda, pt); -} - void setTileOnGameboardDataArray(gameboard_data_array_t &gbda, point2D_t pt, tile_t tile) { gameboard_data_point_t{}(gbda, pt) = tile; @@ -74,60 +67,6 @@ bool getTileBlockedOnGameboardDataArray(gameboard_data_array_t gbda, return gameboard_data_point_t{}(gbda, pt).blocked; } -template -std::array make_patterned_bars(int playsize) { - auto temp_bars = std::array{}; - using bar_pattern_t = std::tuple; - - const auto bar_pattern_list = {std::make_tuple("┌", "┬", "┐"), - std::make_tuple("├", "┼", "┤"), - std::make_tuple("└", "┴", "┘")}; - - // generate types of horizontal bars... - const auto generate_x_bar_pattern = [playsize](const bar_pattern_t t) { - enum { PATTERN_HEAD, PATTERN_MID, PATTERN_TAIL }; - constexpr auto sp = " "; - constexpr auto separator = "──────"; - std::ostringstream temp_richtext; - temp_richtext << sp << std::get(t); - for (auto i = 0; i < playsize; i++) { - const auto is_not_last_column = (i < playsize - 1); - temp_richtext << separator - << (is_not_last_column ? std::get(t) : - std::get(t)); - } - temp_richtext << "\n"; - return temp_richtext.str(); - }; - std::transform(std::begin(bar_pattern_list), std::end(bar_pattern_list), - std::begin(temp_bars), generate_x_bar_pattern); - return temp_bars; -} - -std::string drawSelf(gameboard_data_array_t gbda) { - enum { TOP_BAR, XN_BAR, BASE_BAR, MAX_TYPES_OF_BARS }; - const int playsize = getPlaySizeOfGameboardDataArray(gbda); - const auto vertibar = make_patterned_bars(playsize); - std::ostringstream str_os; - for (auto y = 0; y < playsize; y++) { - const auto is_first_row = (y == 0); - str_os << (is_first_row ? std::get(vertibar) : - std::get(vertibar)); - for (auto x = 0; x < playsize; x++) { - const auto is_first_col = (x == 0); - const auto sp = (is_first_col ? " " : " "); - str_os << sp; - str_os << "│ "; - str_os << getTileOnGameboardDataArray(gbda, point2D_t{x, y}); - } - str_os << " │"; - str_os << "\n"; - } - str_os << std::get(vertibar); - str_os << "\n"; - return str_os.str(); -} - std::string printStateOfGameBoardDataArray(gameboard_data_array_t gbda) { const int playsize = getPlaySizeOfGameboardDataArray(gbda); std::ostringstream os; @@ -420,6 +359,14 @@ GameBoard::GameBoard(ull playsize, tile_data_array_t prempt_board) : gbda{playsize, prempt_board} { } +size_t getPlaySizeOfGameboardDataArray(gameboard_data_array_t gbda) { + return std::get(gbda); +} + +tile_t getTileOnGameboardDataArray(gameboard_data_array_t gbda, point2D_t pt) { + return gameboard_data_point_t{}(gbda, pt); +} + bool hasWonOnGameboard(GameBoard gb) { return gb.win; } diff --git a/src/headers/gameboard-graphics.hpp b/src/headers/gameboard-graphics.hpp index 67962abd..02a3e616 100644 --- a/src/headers/gameboard-graphics.hpp +++ b/src/headers/gameboard-graphics.hpp @@ -1,4 +1,14 @@ #ifndef GAMEBOARDGRAPHICS_H #define GAMEBOARDGRAPHICS_H +#include "gameboard.hpp" +#include +#include + +namespace Game { + +std::string drawSelf(GameBoard::gameboard_data_array_t gbda); + +} + #endif diff --git a/src/headers/gameboard.hpp b/src/headers/gameboard.hpp index 108c896e..23bc38ea 100644 --- a/src/headers/gameboard.hpp +++ b/src/headers/gameboard.hpp @@ -5,6 +5,8 @@ #include #include +struct point2D_t; + namespace Game { struct GameBoard { @@ -23,6 +25,10 @@ struct GameBoard { explicit GameBoard(ull playsize, tile_data_array_t prempt_board); }; +size_t getPlaySizeOfGameboardDataArray(GameBoard::gameboard_data_array_t gbda); +tile_t getTileOnGameboardDataArray(GameBoard::gameboard_data_array_t gbda, + point2D_t pt); + bool hasWonOnGameboard(GameBoard gb); long long MoveCountOnGameBoard(GameBoard gb); From 822bb58ef64d4197a20156a948f96e515017cd52 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Fri, 11 Oct 2019 15:10:04 +0200 Subject: [PATCH 28/34] fix: Gameboard-Graphics: added drawGameBoard helper function --- src/game.cpp | 5 +++-- src/gameboard-graphics.cpp | 9 +++++++-- src/gameboard.cpp | 6 ------ src/headers/gameboard-graphics.hpp | 9 +++------ src/headers/gameboard.hpp | 2 -- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 11a82476..35b65440 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2,6 +2,7 @@ #include "game-graphics.hpp" #include "game-input.hpp" #include "game-pregamemenu.hpp" +#include "gameboard-graphics.hpp" #include "gameboard.hpp" #include "global.hpp" #include "loadresource.hpp" @@ -95,7 +96,7 @@ gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { const auto scdd = make_scoreboard_display_data(); clearScreen(); DrawAlways(os, DataSuppliment(scdd, Graphics::GameScoreBoardOverlay)); - os << gamePlayBoard; + DrawAlways(os, DataSuppliment(gamePlayBoard, drawGameBoard)); DrawAsOneTimeFlag(os, gamestatus[FLAG_SAVED_GAME], Graphics::GameStateNowSavedPrompt); DrawOnlyWhen(os, gamestatus[FLAG_QUESTION_STAY_OR_QUIT], @@ -273,7 +274,7 @@ void endlessGameLoop() { const auto scdd = make_scoreboard_display_data(); clearScreen(); DrawAlways(std::cout, DataSuppliment(scdd, Graphics::GameScoreBoardOverlay)); - std::cout << gamePlayBoard; + DrawAlways(std::cout, DataSuppliment(gamePlayBoard, drawGameBoard)); const auto esdd = make_end_screen_display_data(world_gamestatus); DrawAlways(std::cout, DataSuppliment(esdd, Graphics::GameEndScreenOverlay)); } diff --git a/src/gameboard-graphics.cpp b/src/gameboard-graphics.cpp index 83919d60..62ceb3eb 100644 --- a/src/gameboard-graphics.cpp +++ b/src/gameboard-graphics.cpp @@ -1,4 +1,5 @@ #include "gameboard-graphics.hpp" +#include "gameboard.hpp" #include "point2d.hpp" #include #include @@ -37,8 +38,6 @@ std::array make_patterned_bars(int playsize) { return temp_bars; } -} // namespace - std::string drawSelf(GameBoard::gameboard_data_array_t gbda) { enum { TOP_BAR, XN_BAR, BASE_BAR, MAX_TYPES_OF_BARS }; const int playsize = getPlaySizeOfGameboardDataArray(gbda); @@ -63,4 +62,10 @@ std::string drawSelf(GameBoard::gameboard_data_array_t gbda) { return str_os.str(); } +} // namespace + +std::string drawGameBoard(GameBoard gb) { + return drawSelf(gb.gbda); +} + } // namespace Game diff --git a/src/gameboard.cpp b/src/gameboard.cpp index 721882ce..757e2e28 100644 --- a/src/gameboard.cpp +++ b/src/gameboard.cpp @@ -413,9 +413,3 @@ std::string printStateOfGameBoard(GameBoard gb) { } } // namespace Game - -using namespace Game; - -std::ostream &operator<<(std::ostream &os, const GameBoard &gb) { - return os << drawSelf(gb.gbda); -} diff --git a/src/headers/gameboard-graphics.hpp b/src/headers/gameboard-graphics.hpp index 02a3e616..e6cb7168 100644 --- a/src/headers/gameboard-graphics.hpp +++ b/src/headers/gameboard-graphics.hpp @@ -1,14 +1,11 @@ #ifndef GAMEBOARDGRAPHICS_H #define GAMEBOARDGRAPHICS_H -#include "gameboard.hpp" #include -#include namespace Game { - -std::string drawSelf(GameBoard::gameboard_data_array_t gbda); - -} +struct GameBoard; +std::string drawGameBoard(GameBoard gb); +} // namespace Game #endif diff --git a/src/headers/gameboard.hpp b/src/headers/gameboard.hpp index 23bc38ea..4f60a718 100644 --- a/src/headers/gameboard.hpp +++ b/src/headers/gameboard.hpp @@ -46,6 +46,4 @@ std::string printStateOfGameBoard(GameBoard gb); } // namespace Game -std::ostream &operator<<(std::ostream &os, const Game::GameBoard &gb); - #endif From f2883973903250e673ae2c4ac07713e07cef7b49 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Fri, 11 Oct 2019 19:17:39 +0200 Subject: [PATCH 29/34] added: tile-graphics TU --- CMakeLists.txt | 2 +- meson.build | 2 +- src/headers/tile-graphics.hpp | 4 ++++ src/tile-graphics.cpp | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 src/headers/tile-graphics.hpp create mode 100644 src/tile-graphics.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 23103098..3eb3a7b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.7) # 3.7: CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT, 3.3: CXX_STANDARD project(2048 CXX) -set(SOURCES src/2048.cpp src/gameboard.cpp src/gameboard-graphics.cpp src/game.cpp src/game-graphics.cpp src/game-input.cpp src/game-pregamemenu.cpp src/global.cpp src/loadresource.cpp src/menu.cpp src/menu-graphics.cpp src/saveresource.cpp src/scores.cpp src/scores-graphics.cpp src/statistics.cpp src/statistics-graphics.cpp src/tile.cpp) +set(SOURCES src/2048.cpp src/gameboard.cpp src/gameboard-graphics.cpp src/game.cpp src/game-graphics.cpp src/game-input.cpp src/game-pregamemenu.cpp src/global.cpp src/loadresource.cpp src/menu.cpp src/menu-graphics.cpp src/saveresource.cpp src/scores.cpp src/scores-graphics.cpp src/statistics.cpp src/statistics-graphics.cpp src/tile.cpp src/tile-graphics.cpp) if(CMAKE_CXX_COMPILER_ID STREQUAL GNU) list(APPEND FLAGS -Wall) diff --git a/meson.build b/meson.build index 48668e5d..e71b824c 100644 --- a/meson.build +++ b/meson.build @@ -10,7 +10,7 @@ elif cxx.get_id() == 'intel' endif main_target_name = '2048' -sources = ['src/2048.cpp', 'src/gameboard.cpp', 'src/gameboard-graphics.cpp', 'src/game.cpp', 'src/game-input.cpp', 'src/game-graphics.cpp', 'src/game-pregamemenu.cpp', 'src/global.cpp', 'src/loadresource.cpp', 'src/menu.cpp', 'src/menu-graphics.cpp', 'src/saveresource.cpp', 'src/scores.cpp', 'src/scores-graphics.cpp', 'src/statistics.cpp', 'src/statistics-graphics.cpp', 'src/tile.cpp'] +sources = ['src/2048.cpp', 'src/gameboard.cpp', 'src/gameboard-graphics.cpp', 'src/game.cpp', 'src/game-input.cpp', 'src/game-graphics.cpp', 'src/game-pregamemenu.cpp', 'src/global.cpp', 'src/loadresource.cpp', 'src/menu.cpp', 'src/menu-graphics.cpp', 'src/saveresource.cpp', 'src/scores.cpp', 'src/scores-graphics.cpp', 'src/statistics.cpp', 'src/statistics-graphics.cpp', 'src/tile.cpp', 'src/tile-graphics.cpp'] hdrs = include_directories('src/headers') executable(main_target_name, sources, diff --git a/src/headers/tile-graphics.hpp b/src/headers/tile-graphics.hpp new file mode 100644 index 00000000..f7b85299 --- /dev/null +++ b/src/headers/tile-graphics.hpp @@ -0,0 +1,4 @@ +#ifndef TILEGRAPHICS_H +#define TILEGRAPHICS_H + +#endif diff --git a/src/tile-graphics.cpp b/src/tile-graphics.cpp new file mode 100644 index 00000000..3ba0831a --- /dev/null +++ b/src/tile-graphics.cpp @@ -0,0 +1 @@ +#include "tile-graphics.hpp" From 2015c081df82f0ef0f7898fb579c95a8e2dadb01 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Fri, 11 Oct 2019 19:40:58 +0200 Subject: [PATCH 30/34] fix: Tile: drawTileString moved to tile-graphics TU - drawTileString() and tileColor() moved to tile-graphics TU. --- src/gameboard-graphics.cpp | 5 ++++- src/headers/tile-graphics.hpp | 7 +++++++ src/headers/tile.hpp | 4 ---- src/tile-graphics.cpp | 31 ++++++++++++++++++++++++++++ src/tile.cpp | 39 +---------------------------------- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/gameboard-graphics.cpp b/src/gameboard-graphics.cpp index 62ceb3eb..ba1ee38e 100644 --- a/src/gameboard-graphics.cpp +++ b/src/gameboard-graphics.cpp @@ -1,6 +1,8 @@ #include "gameboard-graphics.hpp" #include "gameboard.hpp" #include "point2d.hpp" +#include "tile-graphics.hpp" +#include "tile.hpp" #include #include #include @@ -50,9 +52,10 @@ std::string drawSelf(GameBoard::gameboard_data_array_t gbda) { for (auto x = 0; x < playsize; x++) { const auto is_first_col = (x == 0); const auto sp = (is_first_col ? " " : " "); + const auto tile = getTileOnGameboardDataArray(gbda, point2D_t{x, y}); str_os << sp; str_os << "│ "; - str_os << getTileOnGameboardDataArray(gbda, point2D_t{x, y}); + str_os << drawTileString(tile); } str_os << " │"; str_os << "\n"; diff --git a/src/headers/tile-graphics.hpp b/src/headers/tile-graphics.hpp index f7b85299..134ac4f5 100644 --- a/src/headers/tile-graphics.hpp +++ b/src/headers/tile-graphics.hpp @@ -1,4 +1,11 @@ #ifndef TILEGRAPHICS_H #define TILEGRAPHICS_H +#include + +namespace Game { +struct tile_t; +std::string drawTileString(tile_t currentTile); +} // namespace Game + #endif diff --git a/src/headers/tile.hpp b/src/headers/tile.hpp index 6229a811..0a15b302 100644 --- a/src/headers/tile.hpp +++ b/src/headers/tile.hpp @@ -4,14 +4,10 @@ #include "global.hpp" namespace Game { - struct tile_t { ull value{}; bool blocked{}; }; - } // namespace Game -std::ostream &operator<<(std::ostream &os, const Game::tile_t &t); - #endif diff --git a/src/tile-graphics.cpp b/src/tile-graphics.cpp index 3ba0831a..b91bfe4f 100644 --- a/src/tile-graphics.cpp +++ b/src/tile-graphics.cpp @@ -1 +1,32 @@ #include "tile-graphics.hpp" +#include "color.hpp" +#include "tile.hpp" +#include +#include +#include +#include + +namespace Game { +namespace { + +Color::Modifier tileColor(ull value) { + std::vector colors{red, yellow, magenta, blue, cyan, yellow, + red, yellow, magenta, blue, green}; + int log = log2(value); + int index = log < 12 ? log - 1 : 10; + + return colors[index]; +} +} // namespace + +std::string drawTileString(tile_t currentTile) { + std::ostringstream tile_richtext; + if (!currentTile.value) { + tile_richtext << " "; + } else { + tile_richtext << tileColor(currentTile.value) << bold_on << std::setw(4) + << currentTile.value << bold_off << def; + } + return tile_richtext.str(); +} +} // namespace Game diff --git a/src/tile.cpp b/src/tile.cpp index d448a5d9..47442cdc 100644 --- a/src/tile.cpp +++ b/src/tile.cpp @@ -1,40 +1,3 @@ #include "tile.hpp" -#include "color.hpp" -#include -#include -#include -#include -namespace Game { - -namespace { - -Color::Modifier tileColor(ull value) { - std::vector colors{red, yellow, magenta, blue, cyan, yellow, - red, yellow, magenta, blue, green}; - int log = log2(value); - int index = log < 12 ? log - 1 : 10; - - return colors[index]; -} - -std::string drawTileString(tile_t currentTile) { - std::ostringstream tile_richtext; - if (!currentTile.value) { - tile_richtext << " "; - } else { - tile_richtext << tileColor(currentTile.value) << bold_on << std::setw(4) - << currentTile.value << bold_off << def; - } - return tile_richtext.str(); -} - -} // namespace - -} // namespace Game - -using namespace Game; - -std::ostream &operator<<(std::ostream &os, const tile_t &t) { - return os << drawTileString(t); -} +namespace Game {} // namespace Game From fb007254e7c8d58bbb77bcb5fdd3d3f746598358 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Sat, 12 Oct 2019 10:37:06 +0200 Subject: [PATCH 31/34] fix: Gameboard-Graphics: drawGameBoard renamed; wrapped in new namespace - drawGameBoard() now renamed to GameBoardTextOutput(). - GameBoardTextOutput() wrapped in Gameboard::Graphics namespace. --- src/game.cpp | 23 +++++++++++++---------- src/gameboard-graphics.cpp | 7 +++++-- src/headers/gameboard-graphics.hpp | 7 ++++++- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 35b65440..afe56480 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -93,20 +93,21 @@ make_input_controls_display_data(gamestatus_t gamestatus) { }; gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { + using namespace Graphics; + using namespace Gameboard::Graphics; const auto scdd = make_scoreboard_display_data(); clearScreen(); - DrawAlways(os, DataSuppliment(scdd, Graphics::GameScoreBoardOverlay)); - DrawAlways(os, DataSuppliment(gamePlayBoard, drawGameBoard)); - DrawAsOneTimeFlag(os, gamestatus[FLAG_SAVED_GAME], - Graphics::GameStateNowSavedPrompt); + DrawAlways(os, DataSuppliment(scdd, GameScoreBoardOverlay)); + DrawAlways(os, DataSuppliment(gamePlayBoard, GameBoardTextOutput)); + DrawAsOneTimeFlag(os, gamestatus[FLAG_SAVED_GAME], GameStateNowSavedPrompt); DrawOnlyWhen(os, gamestatus[FLAG_QUESTION_STAY_OR_QUIT], - Graphics::QuestionEndOfWinningGamePrompt); + QuestionEndOfWinningGamePrompt); const auto input_controls_display_data = make_input_controls_display_data(gamestatus); DrawAlways(os, DataSuppliment(input_controls_display_data, - Graphics::GameInputControlsOverlay)); + GameInputControlsOverlay)); DrawAsOneTimeFlag(os, gamestatus[FLAG_INPUT_ERROR], - Graphics::InvalidInputGameBoardErrorPrompt); + InvalidInputGameBoardErrorPrompt); return gamestatus; } @@ -264,6 +265,8 @@ make_end_screen_display_data(gamestatus_t world_gamestatus) { }; void endlessGameLoop() { + using namespace Graphics; + using namespace Gameboard::Graphics; auto loop_again{true}; gamestatus_t world_gamestatus{}; @@ -273,10 +276,10 @@ void endlessGameLoop() { const auto scdd = make_scoreboard_display_data(); clearScreen(); - DrawAlways(std::cout, DataSuppliment(scdd, Graphics::GameScoreBoardOverlay)); - DrawAlways(std::cout, DataSuppliment(gamePlayBoard, drawGameBoard)); + DrawAlways(std::cout, DataSuppliment(scdd, GameScoreBoardOverlay)); + DrawAlways(std::cout, DataSuppliment(gamePlayBoard, GameBoardTextOutput)); const auto esdd = make_end_screen_display_data(world_gamestatus); - DrawAlways(std::cout, DataSuppliment(esdd, Graphics::GameEndScreenOverlay)); + DrawAlways(std::cout, DataSuppliment(esdd, GameEndScreenOverlay)); } void saveEndGameStats(Scoreboard::Score finalscore) { diff --git a/src/gameboard-graphics.cpp b/src/gameboard-graphics.cpp index ba1ee38e..d737edf8 100644 --- a/src/gameboard-graphics.cpp +++ b/src/gameboard-graphics.cpp @@ -8,6 +8,8 @@ #include namespace Game { +namespace Gameboard { +namespace Graphics { namespace { template @@ -67,8 +69,9 @@ std::string drawSelf(GameBoard::gameboard_data_array_t gbda) { } // namespace -std::string drawGameBoard(GameBoard gb) { +std::string GameBoardTextOutput(GameBoard gb) { return drawSelf(gb.gbda); } - +} // namespace Graphics +} // namespace Gameboard } // namespace Game diff --git a/src/headers/gameboard-graphics.hpp b/src/headers/gameboard-graphics.hpp index e6cb7168..5959dc80 100644 --- a/src/headers/gameboard-graphics.hpp +++ b/src/headers/gameboard-graphics.hpp @@ -5,7 +5,12 @@ namespace Game { struct GameBoard; -std::string drawGameBoard(GameBoard gb); +namespace Gameboard { +namespace Graphics { +std::string GameBoardTextOutput(GameBoard gb); + +} +} // namespace Gameboard } // namespace Game #endif From 1b41435bdd76b993b8eb634a3742b474c02e1ba2 Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Sat, 12 Oct 2019 21:08:50 +0200 Subject: [PATCH 32/34] fix: Game: added comments for drawGraphics - Moved AsciiArt2048() call from Game-Graphics TU's GameScoreBoardOverlay() into Game TU's drawGraphics(). --- src/game-graphics.cpp | 1 - src/game.cpp | 22 +++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/game-graphics.cpp b/src/game-graphics.cpp index 64d6d086..5495fa67 100644 --- a/src/game-graphics.cpp +++ b/src/game-graphics.cpp @@ -289,7 +289,6 @@ std::string GameScoreBoardBox(scoreboard_display_data_t scdd) { std::string GameScoreBoardOverlay(scoreboard_display_data_t scdd) { std::ostringstream str_os; - DrawAlways(str_os, AsciiArt2048); DrawAlways(str_os, DataSuppliment(scdd, GameScoreBoardBox)); return str_os.str(); } diff --git a/src/game.cpp b/src/game.cpp index afe56480..27129f6a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -93,21 +93,40 @@ make_input_controls_display_data(gamestatus_t gamestatus) { }; gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { + // Graphical Output has a specific ordering... using namespace Graphics; using namespace Gameboard::Graphics; - const auto scdd = make_scoreboard_display_data(); + // 1. Clear screen clearScreen(); + + // 2. Draw Game Title Art + DrawAlways(os, AsciiArt2048); + + // 3. Draw Scoreboard of current game session + const auto scdd = make_scoreboard_display_data(); DrawAlways(os, DataSuppliment(scdd, GameScoreBoardOverlay)); + + // 4 Draw current 2048 game active gameboard DrawAlways(os, DataSuppliment(gamePlayBoard, GameBoardTextOutput)); + + // 5 Draw anyinstant status feedback, like + // "Game saved!" (which disappers after next key input). DrawAsOneTimeFlag(os, gamestatus[FLAG_SAVED_GAME], GameStateNowSavedPrompt); + + // 6. Draw any "questions to the player" (from the game) text output DrawOnlyWhen(os, gamestatus[FLAG_QUESTION_STAY_OR_QUIT], QuestionEndOfWinningGamePrompt); + + // 7. Draw Keyboard / Input Keycodes to the player const auto input_controls_display_data = make_input_controls_display_data(gamestatus); DrawAlways(os, DataSuppliment(input_controls_display_data, GameInputControlsOverlay)); + + // 8. Draw any game error messages to the player (to do with keyboard input) DrawAsOneTimeFlag(os, gamestatus[FLAG_INPUT_ERROR], InvalidInputGameBoardErrorPrompt); + return gamestatus; } @@ -276,6 +295,7 @@ void endlessGameLoop() { const auto scdd = make_scoreboard_display_data(); clearScreen(); + DrawAlways(std::cout, AsciiArt2048); DrawAlways(std::cout, DataSuppliment(scdd, GameScoreBoardOverlay)); DrawAlways(std::cout, DataSuppliment(gamePlayBoard, GameBoardTextOutput)); const auto esdd = make_end_screen_display_data(world_gamestatus); From dd3ebf8da696420d6536fe0baae8571cb54ce81b Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Sun, 13 Oct 2019 07:06:08 +0200 Subject: [PATCH 33/34] fix: Game: added new gamestatus_t flag - FLAG_GAME_IS_ASKING_QUESTION_MODE added to gamestatus_t. - DisplayGameQuestionsToPlayerPrompt() created to hold all text prompt for questions to player. Only active when FLAG_GAME_IS_ASKING_QUESTION_MODE is active. --- src/game.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 27129f6a..737ea76c 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -25,6 +25,7 @@ enum GameStatusFlag { FLAG_SAVED_GAME, FLAG_INPUT_ERROR, FLAG_ENDLESS_MODE, + FLAG_GAME_IS_ASKING_QUESTION_MODE, FLAG_QUESTION_STAY_OR_QUIT, MAX_NO_GAME_STATUS_FLAGS }; @@ -63,6 +64,7 @@ gamestatus_t process_gamelogic(gamestatus_t gamestatus) { if (!gamestatus[FLAG_ENDLESS_MODE]) { if (hasWonOnGameboard(gamePlayBoard)) { gamestatus[FLAG_WIN] = true; + gamestatus[FLAG_GAME_IS_ASKING_QUESTION_MODE] = true; gamestatus[FLAG_QUESTION_STAY_OR_QUIT] = true; } } @@ -92,6 +94,15 @@ make_input_controls_display_data(gamestatus_t gamestatus) { return icdd; }; +std::string DisplayGameQuestionsToPlayerPrompt(gamestatus_t gamestatus) { + using namespace Graphics; + + std::ostringstream str_os; + DrawOnlyWhen(str_os, gamestatus[FLAG_QUESTION_STAY_OR_QUIT], + QuestionEndOfWinningGamePrompt); + return str_os.str(); +} + gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { // Graphical Output has a specific ordering... using namespace Graphics; @@ -114,8 +125,8 @@ gamestatus_t drawGraphics(std::ostream &os, gamestatus_t gamestatus) { DrawAsOneTimeFlag(os, gamestatus[FLAG_SAVED_GAME], GameStateNowSavedPrompt); // 6. Draw any "questions to the player" (from the game) text output - DrawOnlyWhen(os, gamestatus[FLAG_QUESTION_STAY_OR_QUIT], - QuestionEndOfWinningGamePrompt); + DrawOnlyWhen(os, gamestatus[FLAG_GAME_IS_ASKING_QUESTION_MODE], + DataSuppliment(gamestatus, DisplayGameQuestionsToPlayerPrompt)); // 7. Draw Keyboard / Input Keycodes to the player const auto input_controls_display_data = @@ -262,6 +273,9 @@ wrapper_bool_gamestatus_t process_gameStatus(gamestatus_t gamestatus) { if (gamestatus[FLAG_SAVED_GAME]) { saveGamePlayState(); } + + // New loop cycle: reset question asking event trigger + gamestatus[FLAG_GAME_IS_ASKING_QUESTION_MODE] = false; return std::make_tuple(loop_again, gamestatus); } From be5d6593a99e7dafb1d783ef7571d3a28aaeca1c Mon Sep 17 00:00:00 2001 From: tcoyvwac <53616399+tcoyvwac@users.noreply.github.com> Date: Sun, 13 Oct 2019 07:13:51 +0200 Subject: [PATCH 34/34] fix: Game: added drawEndGameLoopGraphics - drawEndGameLoopGraphics() is a helper function that draws items needed for game-over screen. --- src/game.cpp | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 737ea76c..74e2e7ca 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -297,9 +297,32 @@ make_end_screen_display_data(gamestatus_t world_gamestatus) { return esdd; }; -void endlessGameLoop() { +std::string drawEndGameLoopGraphics(gamestatus_t world_gamestatus) { + // Graphical Output has a specific ordering... using namespace Graphics; using namespace Gameboard::Graphics; + std::ostringstream str_os; + // 1. Clear screen + clearScreen(); + + // 2. Draw Game Title Art + DrawAlways(str_os, AsciiArt2048); + + // 3. Draw Scoreboard of ending current game session + const auto scdd = make_scoreboard_display_data(); + DrawAlways(str_os, DataSuppliment(scdd, GameScoreBoardOverlay)); + + // 4 Draw snapshot of ending 2048 session's gameboard + DrawAlways(str_os, DataSuppliment(gamePlayBoard, GameBoardTextOutput)); + + // 5. Draw "You win!" or "You Lose" prompt, only if not in endless mode. + const auto esdd = make_end_screen_display_data(world_gamestatus); + DrawAlways(str_os, DataSuppliment(esdd, GameEndScreenOverlay)); + + return str_os.str(); +} + +void endlessGameLoop() { auto loop_again{true}; gamestatus_t world_gamestatus{}; @@ -307,13 +330,8 @@ void endlessGameLoop() { std::tie(loop_again, world_gamestatus) = soloGameLoop(world_gamestatus); } - const auto scdd = make_scoreboard_display_data(); - clearScreen(); - DrawAlways(std::cout, AsciiArt2048); - DrawAlways(std::cout, DataSuppliment(scdd, GameScoreBoardOverlay)); - DrawAlways(std::cout, DataSuppliment(gamePlayBoard, GameBoardTextOutput)); - const auto esdd = make_end_screen_display_data(world_gamestatus); - DrawAlways(std::cout, DataSuppliment(esdd, GameEndScreenOverlay)); + DrawAlways(std::cout, + DataSuppliment(world_gamestatus, drawEndGameLoopGraphics)); } void saveEndGameStats(Scoreboard::Score finalscore) {