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:
Timothy Flynn 2021-05-25 12:59:27 -04:00 committed by Andreas Kling
parent 5d4cca7e0c
commit a428812ba2
Notes: sideshowbarker 2024-07-18 17:23:52 +09:00

View file

@ -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);