1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:07:36 +00:00

Minesweeper: Create field from Difficulty enum

This change makes it easier to generate a new field. Instead of using
hard-coded values everywhere, we now just need to keep track of
the Difficulty enum value.
This commit is contained in:
Pedro Pereira 2021-11-14 11:53:56 +00:00 committed by Linus Groh
parent 1c29633110
commit 7b4b060b9c
4 changed files with 91 additions and 14 deletions

View file

@ -45,13 +45,62 @@ class Field final : public GUI::Frame {
public:
virtual ~Field() override;
enum class Difficulty {
Beginner,
Intermediate,
Expert,
Madwoman,
Custom
};
StringView difficulty_to_string(Difficulty difficulty) const
{
switch (difficulty) {
case Difficulty::Beginner:
return "beginner"sv;
case Difficulty::Intermediate:
return "intermediate"sv;
case Difficulty::Expert:
return "expert"sv;
case Difficulty::Madwoman:
return "madwoman"sv;
case Difficulty::Custom:
return "custom"sv;
default:
VERIFY_NOT_REACHED();
}
}
Optional<Difficulty> difficulty_from_string(StringView difficulty_string) const
{
if (difficulty_string.matches("beginner"))
return Difficulty::Beginner;
if (difficulty_string.equals_ignoring_case("intermediate"))
return Difficulty::Intermediate;
if (difficulty_string.equals_ignoring_case("expert"))
return Difficulty::Expert;
if (difficulty_string.equals_ignoring_case("madwoman"))
return Difficulty::Madwoman;
if (difficulty_string.equals_ignoring_case("custom"))
return Difficulty::Custom;
return {};
}
Difficulty difficulty() const { return m_difficulty; }
size_t rows() const { return m_rows; }
size_t columns() const { return m_columns; }
size_t mine_count() const { return m_mine_count; }
int square_size() const { return 15; }
bool is_single_chording() const { return m_single_chording; }
void set_field_size(size_t rows, size_t columns, size_t mine_count);
void set_field_difficulty(Difficulty difficulty);
void set_field_size(Difficulty difficulty, size_t rows, size_t columns, size_t mine_count);
void set_single_chording(bool new_val);
void reset();
@ -87,6 +136,7 @@ private:
};
void set_face(Face);
Difficulty m_difficulty { Difficulty::Beginner };
size_t m_rows { 0 };
size_t m_columns { 0 };
size_t m_mine_count { 0 };