summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-05-25 11:44:25 -0400
committerAndreas Kling <kling@serenityos.org>2021-05-25 21:20:50 +0200
commit0b30a0febbbab0c29fd609b340927faa522b144d (patch)
tree69d7f3156c4efc3b190f9e5973361dd36c77fa43
parent95401d2ca2dae9d08ead3c8fd17077910c1f8e54 (diff)
downloadserenity-0b30a0febbbab0c29fd609b340927faa522b144d.zip
Solitaire: Only update high score after a victorious game
Doesn't make much sense to update the high score on a lost game.
-rw-r--r--Userland/Games/Solitaire/Game.cpp4
-rw-r--r--Userland/Games/Solitaire/Game.h7
-rw-r--r--Userland/Games/Solitaire/main.cpp12
3 files changed, 14 insertions, 9 deletions
diff --git a/Userland/Games/Solitaire/Game.cpp b/Userland/Games/Solitaire/Game.cpp
index 7aab23456e..40868ec465 100644
--- a/Userland/Games/Solitaire/Game.cpp
+++ b/Userland/Games/Solitaire/Game.cpp
@@ -80,7 +80,7 @@ void Game::start_game_over_animation()
start_timer(s_timer_interval_ms);
if (on_game_end)
- on_game_end();
+ on_game_end(GameOverReason::Victory, m_score);
}
void Game::stop_game_over_animation()
@@ -103,7 +103,7 @@ void Game::setup(Mode mode)
m_mode = mode;
if (on_game_end)
- on_game_end();
+ on_game_end(GameOverReason::NewGame, m_score);
for (auto& stack : m_stacks)
stack.clear();
diff --git a/Userland/Games/Solitaire/Game.h b/Userland/Games/Solitaire/Game.h
index fba4c1a39a..72d1ed78df 100644
--- a/Userland/Games/Solitaire/Game.h
+++ b/Userland/Games/Solitaire/Game.h
@@ -21,6 +21,11 @@ enum class Mode : u8 {
__Count
};
+enum class GameOverReason {
+ Victory,
+ NewGame,
+};
+
class Game final : public GUI::Frame {
C_OBJECT(Game)
public:
@@ -34,7 +39,7 @@ public:
Function<void(uint32_t)> on_score_update;
Function<void()> on_game_start;
- Function<void()> on_game_end;
+ Function<void(GameOverReason, uint32_t)> on_game_end;
private:
Game();
diff --git a/Userland/Games/Solitaire/main.cpp b/Userland/Games/Solitaire/main.cpp
index 75123ec3ca..cd81600549 100644
--- a/Userland/Games/Solitaire/main.cpp
+++ b/Userland/Games/Solitaire/main.cpp
@@ -93,11 +93,6 @@ int main(int argc, char** argv)
game.on_score_update = [&](uint32_t score) {
statusbar.set_text(0, String::formatted("Score: {}", score));
-
- if (score > high_score) {
- update_high_score(score);
- statusbar.set_text(1, String::formatted("High Score: {}", high_score));
- }
};
uint64_t seconds_elapsed = 0;
@@ -116,9 +111,14 @@ int main(int argc, char** argv)
seconds_elapsed = 0;
timer->start();
};
- game.on_game_end = [&]() {
+ game.on_game_end = [&](Solitaire::GameOverReason reason, uint32_t score) {
if (timer->is_active())
timer->stop();
+
+ if ((reason == Solitaire::GameOverReason::Victory) && (score > high_score)) {
+ update_high_score(score);
+ statusbar.set_text(1, String::formatted("High Score: {}", high_score));
+ }
};
GUI::ActionGroup draw_setting_actions;