From 2336c6290188df573ac2a90cb9fde29d0a5d80b0 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Fri, 14 Aug 2020 16:20:13 +0430 Subject: [PATCH] 2048: Move out the 'undo' action to the app menu/action --- Games/2048/2048.cpp | 17 +++++++++-------- Games/2048/2048.h | 1 + Games/2048/main.cpp | 3 +++ 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Games/2048/2048.cpp b/Games/2048/2048.cpp index e29498b05d..7d2e518dd4 100644 --- a/Games/2048/2048.cpp +++ b/Games/2048/2048.cpp @@ -235,14 +235,6 @@ void TwentyFortyEightGame::keydown_event(GUI::KeyEvent& event) case KeyCode::Key_Down: new_state.board = reverse(slide_left(reverse(previous_state.board), successful_merge_score)); break; - case KeyCode::Key_U: - case KeyCode::Key_Backspace: - if (m_states.size() > 1) { - m_states.take_last(); - update(); - } else { - return; - } default: return; } @@ -349,3 +341,12 @@ int TwentyFortyEightGame::score() const { return m_states.last().score; } + +void TwentyFortyEightGame::undo() +{ + if (m_states.size() > 1) { + m_states.take_last(); + --m_current_turn; + update(); + } +} diff --git a/Games/2048/2048.h b/Games/2048/2048.h index 7f619e871f..2b84f22da1 100644 --- a/Games/2048/2048.h +++ b/Games/2048/2048.h @@ -36,6 +36,7 @@ public: void reset(); int score() const; + void undo(); struct State { Vector> board; diff --git a/Games/2048/main.cpp b/Games/2048/main.cpp index 085f397648..6e4c9a52ba 100644 --- a/Games/2048/main.cpp +++ b/Games/2048/main.cpp @@ -75,6 +75,9 @@ int main(int argc, char** argv) app_menu.add_action(GUI::Action::create("New game", { Mod_None, Key_F2 }, [&](auto&) { game.reset(); })); + app_menu.add_action(GUI::CommonActions::make_undo_action([&](auto&) { + game.undo(); + })); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { GUI::Application::the()->quit(); }));