mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:27:35 +00:00
Minesweeper: Flag unflagged mines on win.
This commit is contained in:
parent
3d61c8ec09
commit
ac67a2ed5e
2 changed files with 21 additions and 8 deletions
|
@ -344,15 +344,24 @@ void Field::on_square_right_clicked(Square& square)
|
||||||
if (!square.has_flag && !m_flags_left)
|
if (!square.has_flag && !m_flags_left)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
set_flag(square, !square.has_flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Field::set_flag(Square& square, bool flag)
|
||||||
|
{
|
||||||
|
ASSERT(!square.is_swept);
|
||||||
|
if (square.has_flag == flag)
|
||||||
|
return;
|
||||||
square.is_considering = false;
|
square.is_considering = false;
|
||||||
|
|
||||||
if (!square.has_flag) {
|
if (!flag) {
|
||||||
--m_flags_left;
|
|
||||||
square.has_flag = true;
|
|
||||||
} else {
|
|
||||||
++m_flags_left;
|
++m_flags_left;
|
||||||
square.has_flag = false;
|
} else {
|
||||||
|
|
||||||
|
ASSERT(m_flags_left);
|
||||||
|
--m_flags_left;
|
||||||
}
|
}
|
||||||
|
square.has_flag = flag;
|
||||||
|
|
||||||
m_flag_label.set_text(String::format("%u", m_flags_left));
|
m_flag_label.set_text(String::format("%u", m_flags_left));
|
||||||
square.button->set_icon(square.has_flag ? m_flag_bitmap : nullptr);
|
square.button->set_icon(square.has_flag ? m_flag_bitmap : nullptr);
|
||||||
|
@ -378,6 +387,10 @@ void Field::win()
|
||||||
m_timer.stop();
|
m_timer.stop();
|
||||||
set_greedy_for_hits(true);
|
set_greedy_for_hits(true);
|
||||||
set_face(Face::Good);
|
set_face(Face::Good);
|
||||||
|
for_each_square([&] (auto& square) {
|
||||||
|
if (!square.has_flag && square.has_mine)
|
||||||
|
set_flag(square, true);
|
||||||
|
});
|
||||||
reveal_mines();
|
reveal_mines();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -448,7 +461,6 @@ Square::~Square()
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
void Field::for_each_square(Callback callback)
|
void Field::for_each_square(Callback callback)
|
||||||
{
|
{
|
||||||
for (auto& square : m_squares) {
|
for (int i = 0; i < rows() * columns(); ++i)
|
||||||
callback(*square);
|
callback(*m_squares[i]);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,6 +59,7 @@ private:
|
||||||
void win();
|
void win();
|
||||||
void reveal_mines();
|
void reveal_mines();
|
||||||
void set_chord_preview(Square&, bool);
|
void set_chord_preview(Square&, bool);
|
||||||
|
void set_flag(Square&, bool);
|
||||||
|
|
||||||
Square& square(int row, int column) { return *m_squares[row * columns() + column]; }
|
Square& square(int row, int column) { return *m_squares[row * columns() + column]; }
|
||||||
const Square& square(int row, int column) const { return *m_squares[row * columns() + column]; }
|
const Square& square(int row, int column) const { return *m_squares[row * columns() + column]; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue