diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-05-25 12:59:27 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-25 21:20:50 +0200 |
commit | a428812ba26ad3f3903354c3552365ac25cb6dcc (patch) | |
tree | f45a8b7484e51f6add38c382ccd65f00fa1e3f7a /Userland/Games/Solitaire/main.cpp | |
parent | 5d4cca7e0c737626876648cdeabf6242f64037fa (diff) | |
download | serenity-a428812ba26ad3f3903354c3552365ac25cb6dcc.zip |
Solitaire: Persist high score separately per game mode
With different scoring rules for one-card vs. three-card draw mode, it
makes more sense to separately track their high scores.
Diffstat (limited to 'Userland/Games/Solitaire/main.cpp')
-rw-r--r-- | Userland/Games/Solitaire/main.cpp | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/Userland/Games/Solitaire/main.cpp b/Userland/Games/Solitaire/main.cpp index 736efc6cc1..cb6c6dd9fd 100644 --- a/Userland/Games/Solitaire/main.cpp +++ b/Userland/Games/Solitaire/main.cpp @@ -50,7 +50,6 @@ int main(int argc, char** argv) window->set_title("Solitaire"); auto mode = static_cast<Solitaire::Mode>(config->read_num_entry("Settings", "Mode", static_cast<int>(Solitaire::Mode::SingleCardDraw))); - auto high_score = static_cast<u32>(config->read_num_entry("Game", "HighScore", 0)); auto update_mode = [&](Solitaire::Mode new_mode) { mode = new_mode; @@ -59,9 +58,29 @@ int main(int argc, char** argv) GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error); }; + auto high_score = [&]() { + switch (mode) { + case Solitaire::Mode::SingleCardDraw: + return static_cast<u32>(config->read_num_entry("HighScores", "SingleCardDraw", 0)); + case Solitaire::Mode::ThreeCardDraw: + return static_cast<u32>(config->read_num_entry("HighScores", "ThreeCardDraw", 0)); + default: + VERIFY_NOT_REACHED(); + } + }; + auto update_high_score = [&](u32 new_high_score) { - high_score = new_high_score; - config->write_num_entry("Game", "HighScore", static_cast<int>(high_score)); + switch (mode) { + case Solitaire::Mode::SingleCardDraw: + config->write_num_entry("HighScores", "SingleCardDraw", static_cast<int>(new_high_score)); + break; + case Solitaire::Mode::ThreeCardDraw: + config->write_num_entry("HighScores", "ThreeCardDraw", static_cast<int>(new_high_score)); + break; + default: + VERIFY_NOT_REACHED(); + } + if (!config->sync()) GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error); }; @@ -77,7 +96,7 @@ int main(int argc, char** argv) auto& statusbar = *widget.find_descendant_of_type_named<GUI::Statusbar>("statusbar"); statusbar.set_text(0, "Score: 0"); - statusbar.set_text(1, String::formatted("High Score: {}", high_score)); + statusbar.set_text(1, String::formatted("High Score: {}", high_score())); statusbar.set_text(2, "Time: 00:00:00"); app->on_action_enter = [&](GUI::Action& action) { @@ -122,9 +141,9 @@ int main(int argc, char** argv) score += bonus; } - if (score > high_score) { + if (score > high_score()) { update_high_score(score); - statusbar.set_text(1, String::formatted("High Score: {}", high_score)); + statusbar.set_text(1, String::formatted("High Score: {}", score)); } } }; @@ -134,6 +153,7 @@ int main(int argc, char** argv) auto single_card_draw_action = GUI::Action::create_checkable("&Single Card Draw", [&](auto&) { update_mode(Solitaire::Mode::SingleCardDraw); + statusbar.set_text(1, String::formatted("High Score: {}", high_score())); game.setup(mode); }); single_card_draw_action->set_checked(mode == Solitaire::Mode::SingleCardDraw); @@ -142,6 +162,7 @@ int main(int argc, char** argv) auto three_card_draw_action = GUI::Action::create_checkable("&Three Card Draw", [&](auto&) { update_mode(Solitaire::Mode::ThreeCardDraw); + statusbar.set_text(1, String::formatted("High Score: {}", high_score())); game.setup(mode); }); three_card_draw_action->set_checked(mode == Solitaire::Mode::ThreeCardDraw); |