mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-01 20:10:28 +00:00
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.
This commit is contained in:
parent
5d4cca7e0c
commit
a428812ba2
Notes:
sideshowbarker
2024-07-18 17:23:52 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/a428812ba26 Pull-request: https://github.com/SerenityOS/serenity/pull/7464
1 changed files with 27 additions and 6 deletions
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue