Skip to content

Commit

Permalink
Solitaire: Track and persist high score
Browse files Browse the repository at this point in the history
  • Loading branch information
trflynn89 authored and awesomekling committed May 18, 2021
1 parent 7d062d0 commit 3d3a75f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Userland/Games/Solitaire/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ 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;
Expand All @@ -58,6 +59,13 @@ int main(int argc, char** argv)
GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error);
};

auto update_high_score = [&](u32 new_high_score) {
high_score = new_high_score;
config->write_num_entry("Game", "HighScore", static_cast<int>(high_score));
if (!config->sync())
GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error);
};

if (mode >= Solitaire::Mode::__Count)
update_mode(Solitaire::Mode::SingleCardDraw);

Expand All @@ -84,6 +92,9 @@ 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);
};

uint64_t seconds_elapsed = 0;
Expand Down

0 comments on commit 3d3a75f

Please sign in to comment.