From d609dde7b00d1724035635114a09c6dd8fcb9cbd Mon Sep 17 00:00:00 2001 From: Mohsan Ali Date: Mon, 4 Oct 2021 22:57:13 +0500 Subject: [PATCH] 2048: Let user decide if he wants to continue the game Before game finish when a player has reached target tile, Now player will be able to decide if he wants to continue or not --- Userland/Games/2048/Game.cpp | 2 +- Userland/Games/2048/Game.h | 3 ++- Userland/Games/2048/main.cpp | 21 +++++++++++++++------ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Userland/Games/2048/Game.cpp b/Userland/Games/2048/Game.cpp index 5319ba41aa..1a1552f8f4 100644 --- a/Userland/Games/2048/Game.cpp +++ b/Userland/Games/2048/Game.cpp @@ -232,7 +232,7 @@ Game::MoveOutcome Game::attempt_move(Direction direction) add_tile(); } - if (is_complete(m_board, m_target_tile)) + if (is_complete(m_board, m_target_tile) && !m_want_to_continue) return MoveOutcome::Won; if (m_board.is_stalled()) return MoveOutcome::GameOver; diff --git a/Userland/Games/2048/Game.h b/Userland/Games/2048/Game.h index 92f49ebcc7..8be815e3eb 100644 --- a/Userland/Games/2048/Game.h +++ b/Userland/Games/2048/Game.h @@ -34,7 +34,7 @@ public: size_t turns() const { return m_turns; } u32 target_tile() const { return m_target_tile; } u32 largest_tile() const; - + void set_want_to_continue() { m_want_to_continue = true; } class Board { public: using Row = Vector; @@ -119,6 +119,7 @@ private: u32 m_target_tile { 0 }; bool m_evil_ai { false }; + bool m_want_to_continue { false }; Board m_board; size_t m_score { 0 }; diff --git a/Userland/Games/2048/main.cpp b/Userland/Games/2048/main.cpp index 4565fc5c44..7294f4b353 100644 --- a/Userland/Games/2048/main.cpp +++ b/Userland/Games/2048/main.cpp @@ -151,14 +151,23 @@ int main(int argc, char** argv) case Game::MoveOutcome::InvalidMove: undo_stack.take_last(); break; - case Game::MoveOutcome::Won: + case Game::MoveOutcome::Won: { update(); - GUI::MessageBox::show(window, - String::formatted("You reached {} in {} turns with a score of {}", game.target_tile(), game.turns(), game.score()), - "You won!", - GUI::MessageBox::Type::Information); - start_a_new_game(); + auto message_box = GUI::MessageBox::construct(window, "Congratulations! You won the game, Do you still want to continue?", + "Want to continue?", + GUI::MessageBox::Type::Question, + GUI::MessageBox::InputType::YesNo); + if (message_box->exec() == GUI::MessageBox::ExecYes) + game.set_want_to_continue(); + else { + GUI::MessageBox::show(window, + String::formatted("You reached {} in {} turns with a score of {}", game.largest_tile(), game.turns(), game.score()), + "You won!", + GUI::MessageBox::Type::Information); + start_a_new_game(); + } break; + } case Game::MoveOutcome::GameOver: update(); GUI::MessageBox::show(window,