HighScoreManager.hxx 906 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. #ifndef SNAKE_HIGHSCOREMANAGER_HXX
  3. #define SNAKE_HIGHSCOREMANAGER_HXX
  4. #include "../NonCopyable.hxx"
  5. #include <optional>
  6. #include <string>
  7. #include <vector>
  8. struct Score final {
  9. std::string player_name_;
  10. unsigned points_;
  11. };
  12. struct HighScores final {
  13. std::vector<Score> scores_;
  14. };
  15. class HighScoreManager final : private NonCopyable {
  16. public:
  17. static int constexpr MAX_SCORES = 10;
  18. void set_new_score(unsigned score);
  19. [[nodiscard]] bool has_new_score() const;
  20. [[nodiscard]] unsigned get_new_score() const;
  21. void provide_name_for_new_score(std::string const& name);
  22. static HighScoreManager& instance();
  23. [[nodiscard]] std::vector<Score> const& get_scores() const;
  24. private:
  25. HighScoreManager();
  26. void load();
  27. void save() const;
  28. std::string data_dir_;
  29. HighScores high_score_;
  30. std::optional<unsigned> new_score_;
  31. };
  32. #endif // SNAKE_HIGHSCOREMANAGER_HXX