1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:47:35 +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

@ -140,12 +140,16 @@ Field::Field(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_b
int mine_count = Config::read_i32("Minesweeper", "Game", "MineCount", 10);
int rows = Config::read_i32("Minesweeper", "Game", "Rows", 9);
int columns = Config::read_i32("Minesweeper", "Game", "Columns", 9);
auto difficulty_string = Config::read_string("Minesweeper", "Game", "Difficulty", "beginner");
auto difficulty = difficulty_from_string(difficulty_string);
// Do a quick sanity check to make sure the user hasn't tried anything crazy
if (mine_count > rows * columns || rows <= 0 || columns <= 0 || mine_count <= 0)
set_field_size(9, 9, 10);
if (!difficulty.has_value() || mine_count > rows * columns || rows <= 0 || columns <= 0 || mine_count <= 0)
set_field_difficulty(Difficulty::Beginner);
else if (difficulty.value() == Difficulty::Custom)
set_field_size(Difficulty::Custom, rows, columns, mine_count);
else
set_field_size(rows, columns, mine_count);
set_field_difficulty(difficulty.value());
set_single_chording(single_chording);
}
@ -481,7 +485,27 @@ void Field::set_chord_preview(Square& square, bool chord_preview)
});
}
void Field::set_field_size(size_t rows, size_t columns, size_t mine_count)
void Field::set_field_difficulty(Difficulty difficulty)
{
switch (difficulty) {
case Difficulty::Beginner:
set_field_size(difficulty, 9, 9, 10);
break;
case Difficulty::Intermediate:
set_field_size(difficulty, 16, 16, 40);
break;
case Difficulty::Expert:
set_field_size(difficulty, 16, 30, 99);
break;
case Difficulty::Madwoman:
set_field_size(difficulty, 32, 60, 350);
break;
default:
VERIFY_NOT_REACHED();
}
}
void Field::set_field_size(Difficulty difficulty, size_t rows, size_t columns, size_t mine_count)
{
if (m_rows == rows && m_columns == columns && m_mine_count == mine_count)
return;
@ -489,7 +513,9 @@ void Field::set_field_size(size_t rows, size_t columns, size_t mine_count)
Config::write_i32("Minesweeper", "Game", "MineCount", mine_count);
Config::write_i32("Minesweeper", "Game", "Rows", rows);
Config::write_i32("Minesweeper", "Game", "Columns", columns);
Config::write_string("Minesweeper", "Game", "Difficulty", difficulty_to_string(difficulty));
}
m_difficulty = difficulty;
m_rows = rows;
m_columns = columns;
m_mine_count = mine_count;