|
@@ -24,8 +24,9 @@ public:
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
-Field::Field(GWidget* parent)
|
|
|
|
|
|
+Field::Field(GButton& face_button, GWidget* parent)
|
|
: GFrame(parent)
|
|
: GFrame(parent)
|
|
|
|
+ , m_face_button(face_button)
|
|
{
|
|
{
|
|
set_frame_thickness(2);
|
|
set_frame_thickness(2);
|
|
set_frame_shape(FrameShape::Container);
|
|
set_frame_shape(FrameShape::Container);
|
|
@@ -38,12 +39,30 @@ Field::Field(GWidget* parent)
|
|
set_fill_with_background_color(true);
|
|
set_fill_with_background_color(true);
|
|
set_background_color(Color::LightGray);
|
|
set_background_color(Color::LightGray);
|
|
reset();
|
|
reset();
|
|
|
|
+
|
|
|
|
+ m_face_button.on_click = [this] (auto&) { reset(); };
|
|
|
|
+ set_face(Face::Default);
|
|
}
|
|
}
|
|
|
|
|
|
Field::~Field()
|
|
Field::~Field()
|
|
{
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+void Field::set_face(Face face)
|
|
|
|
+{
|
|
|
|
+ switch (face) {
|
|
|
|
+ case Face::Default:
|
|
|
|
+ m_face_button.set_icon(GraphicsBitmap::load_from_file("/res/icons/minesweeper/face-default.png"));
|
|
|
|
+ break;
|
|
|
|
+ case Face::Good:
|
|
|
|
+ m_face_button.set_icon(GraphicsBitmap::load_from_file("/res/icons/minesweeper/face-good.png"));
|
|
|
|
+ break;
|
|
|
|
+ case Face::Bad:
|
|
|
|
+ m_face_button.set_icon(GraphicsBitmap::load_from_file("/res/icons/minesweeper/face-bad.png"));
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
template<typename Callback>
|
|
template<typename Callback>
|
|
void Field::for_each_neighbor_of(const Square& square, Callback callback)
|
|
void Field::for_each_neighbor_of(const Square& square, Callback callback)
|
|
{
|
|
{
|
|
@@ -70,12 +89,16 @@ void Field::for_each_neighbor_of(const Square& square, Callback callback)
|
|
void Field::reset()
|
|
void Field::reset()
|
|
{
|
|
{
|
|
set_greedy_for_hits(false);
|
|
set_greedy_for_hits(false);
|
|
|
|
+ set_face(Face::Default);
|
|
srand(time(nullptr));
|
|
srand(time(nullptr));
|
|
m_squares.resize(rows() * columns());
|
|
m_squares.resize(rows() * columns());
|
|
|
|
|
|
HashTable<int> mines;
|
|
HashTable<int> mines;
|
|
- for (int i = 0; i < m_mine_count; ++i)
|
|
|
|
- mines.set(rand() % (rows() * columns()));
|
|
|
|
|
|
+ while (mines.size() != m_mine_count) {
|
|
|
|
+ int location = rand() % (rows() * columns());
|
|
|
|
+ if (!mines.contains(location))
|
|
|
|
+ mines.set(location);
|
|
|
|
+ }
|
|
|
|
|
|
int i = 0;
|
|
int i = 0;
|
|
for (int r = 0; r < rows(); ++r) {
|
|
for (int r = 0; r < rows(); ++r) {
|
|
@@ -123,6 +146,8 @@ void Field::reset()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ m_unswept_empties = rows() * columns() - m_mine_count;
|
|
|
|
+
|
|
update();
|
|
update();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -141,15 +166,21 @@ void Field::on_square_clicked(Square& square)
|
|
return;
|
|
return;
|
|
if (square.has_flag)
|
|
if (square.has_flag)
|
|
return;
|
|
return;
|
|
|
|
+ update();
|
|
square.is_swept = true;
|
|
square.is_swept = true;
|
|
square.button->set_visible(false);
|
|
square.button->set_visible(false);
|
|
square.label->set_visible(true);
|
|
square.label->set_visible(true);
|
|
if (square.has_mine) {
|
|
if (square.has_mine) {
|
|
square.label->set_fill_with_background_color(true);
|
|
square.label->set_fill_with_background_color(true);
|
|
game_over();
|
|
game_over();
|
|
- } else if (square.number == 0) {
|
|
|
|
- flood_fill(square);
|
|
|
|
|
|
+ } else {
|
|
|
|
+ --m_unswept_empties;
|
|
|
|
+ if (square.number == 0)
|
|
|
|
+ flood_fill(square);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if (!m_unswept_empties)
|
|
|
|
+ win();
|
|
}
|
|
}
|
|
|
|
|
|
void Field::on_square_right_clicked(Square& square)
|
|
void Field::on_square_right_clicked(Square& square)
|
|
@@ -161,9 +192,22 @@ void Field::on_square_right_clicked(Square& square)
|
|
square.button->update();
|
|
square.button->update();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+void Field::win()
|
|
|
|
+{
|
|
|
|
+ set_greedy_for_hits(true);
|
|
|
|
+ set_face(Face::Good);
|
|
|
|
+ reveal_mines();
|
|
|
|
+}
|
|
|
|
+
|
|
void Field::game_over()
|
|
void Field::game_over()
|
|
{
|
|
{
|
|
set_greedy_for_hits(true);
|
|
set_greedy_for_hits(true);
|
|
|
|
+ set_face(Face::Bad);
|
|
|
|
+ reveal_mines();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Field::reveal_mines()
|
|
|
|
+{
|
|
for (int r = 0; r < rows(); ++r) {
|
|
for (int r = 0; r < rows(); ++r) {
|
|
for (int c = 0; c < columns(); ++c) {
|
|
for (int c = 0; c < columns(); ++c) {
|
|
auto& square = this->square(r, c);
|
|
auto& square = this->square(r, c);
|