summaryrefslogtreecommitdiff
path: root/Games/Minesweeper/Field.cpp
diff options
context:
space:
mode:
authorDrew Stratford <drewstratford@outlook.com>2019-07-30 03:46:21 +1200
committerAndreas Kling <awesomekling@gmail.com>2019-07-29 19:34:31 +0200
commit80cb833594587750b78e449de461d84961ed7b13 (patch)
treebe349d6fe8481ca15f0ff4e3fac5cd905852b434 /Games/Minesweeper/Field.cpp
parent6b757a152a4fd1e59472cc2ea154fdf92d3b3e59 (diff)
downloadserenity-80cb833594587750b78e449de461d84961ed7b13.zip
Minesweeper: refactor on_square_clicked and flood_mark
Both on_square_clicked and flood_mark were very similar so I've introduced the on_square_clicked_impl function which is now called by on_square_clicked and flood_fill.
Diffstat (limited to 'Games/Minesweeper/Field.cpp')
-rw-r--r--Games/Minesweeper/Field.cpp36
1 files changed, 11 insertions, 25 deletions
diff --git a/Games/Minesweeper/Field.cpp b/Games/Minesweeper/Field.cpp
index fd8b7ddc3e..13c7ded0ce 100644
--- a/Games/Minesweeper/Field.cpp
+++ b/Games/Minesweeper/Field.cpp
@@ -272,39 +272,20 @@ void Field::reset()
set_updates_enabled(true);
}
-void Field::flood_mark(Square& square)
-{
- if (square.is_swept)
- return;
- if (square.has_flag)
- return;
- if (square.is_considering)
- return;
- update();
- square.is_swept = true;
- square.button->set_visible(false);
- square.label->set_visible(true);
-
- --m_unswept_empties;
-
- if (!m_unswept_empties)
- win();
-}
-
void Field::flood_fill(Square& square)
{
Queue<Square*> queue;
queue.enqueue(&square);
- while(!queue.is_empty()) {
- Square *s = queue.dequeue();
+ while (!queue.is_empty()) {
+ Square* s = queue.dequeue();
s->for_each_neighbor([this, &queue](Square& neighbor) {
if (!neighbor.is_swept && !neighbor.has_mine && neighbor.number == 0) {
- flood_mark(neighbor);
+ on_square_clicked_impl(neighbor, false);
queue.enqueue(&neighbor);
}
if (!neighbor.has_mine && neighbor.number)
- flood_mark(neighbor);
+ on_square_clicked_impl(neighbor, false);
});
}
}
@@ -330,7 +311,7 @@ void Field::paint_event(GPaintEvent& event)
}
}
-void Field::on_square_clicked(Square& square)
+void Field::on_square_clicked_impl(Square& square, bool should_flood_fill)
{
if (m_first_click) {
while (square.has_mine) {
@@ -358,13 +339,18 @@ void Field::on_square_clicked(Square& square)
}
--m_unswept_empties;
- if (square.number == 0)
+ if (should_flood_fill && square.number == 0)
flood_fill(square);
if (!m_unswept_empties)
win();
}
+void Field::on_square_clicked(Square& square)
+{
+ on_square_clicked_impl(square, true);
+}
+
void Field::on_square_chorded(Square& square)
{
if (!square.is_swept)