summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-08-14 16:20:13 +0430
committerAndreas Kling <kling@serenityos.org>2020-08-14 15:10:31 +0200
commit2336c6290188df573ac2a90cb9fde29d0a5d80b0 (patch)
tree981de9b639335b3760fc773bc2b2f7d3f3ebbfff
parentd97025567a0b4e8b423c41b9af7f2eab7a260d52 (diff)
downloadserenity-2336c6290188df573ac2a90cb9fde29d0a5d80b0.zip
2048: Move out the 'undo' action to the app menu/action
-rw-r--r--Games/2048/2048.cpp17
-rw-r--r--Games/2048/2048.h1
-rw-r--r--Games/2048/main.cpp3
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<Vector<u32>> 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();
}));