diff --git a/Base/res/icons/minesweeper/5.png b/Base/res/icons/minesweeper/5.png new file mode 100644 index 00000000000..d64259f64b2 Binary files /dev/null and b/Base/res/icons/minesweeper/5.png differ diff --git a/Base/res/icons/minesweeper/6.png b/Base/res/icons/minesweeper/6.png new file mode 100644 index 00000000000..f16154b8bb3 Binary files /dev/null and b/Base/res/icons/minesweeper/6.png differ diff --git a/Base/res/icons/minesweeper/7.png b/Base/res/icons/minesweeper/7.png new file mode 100644 index 00000000000..df149914e1c Binary files /dev/null and b/Base/res/icons/minesweeper/7.png differ diff --git a/Base/res/icons/minesweeper/8.png b/Base/res/icons/minesweeper/8.png new file mode 100644 index 00000000000..d0c7427e3e8 Binary files /dev/null and b/Base/res/icons/minesweeper/8.png differ diff --git a/Games/Minesweeper/Field.cpp b/Games/Minesweeper/Field.cpp index af14e09f27d..89f93fa716b 100644 --- a/Games/Minesweeper/Field.cpp +++ b/Games/Minesweeper/Field.cpp @@ -29,10 +29,8 @@ Field::Field(GWidget* parent) { m_mine_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/mine.png"); m_flag_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/flag.png"); - m_one_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/1.png"); - m_two_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/2.png"); - m_three_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/3.png"); - m_four_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/4.png"); + for (int i = 0; i < 8; ++i) + m_number_bitmap[i] = GraphicsBitmap::load_from_file(String::format("/res/icons/minesweeper/%u.png", i + 1)); set_fill_with_background_color(true); set_background_color(Color::LightGray); @@ -56,6 +54,14 @@ void Field::for_each_neighbor_of(const Square& square, Callback callback) callback(this->square(r + 1, c)); if (c < (m_columns - 2)) // Right callback(this->square(r, c + 1)); + if (r > 0 && c > 0) // UpLeft + callback(this->square(r - 1, c - 1)); + if (r > 0 && c < (m_columns - 2)) // UpRight + callback(this->square(r - 1, c + 1)); + if (r < (m_rows - 2) && c > 0) // DownLeft + callback(this->square(r + 1, c - 1)); + if (r < (m_rows - 2) && c < (m_columns - 2)) // DownRight + callback(this->square(r + 1, c + 1)); } void Field::reset() @@ -84,8 +90,10 @@ void Field::reset() square.label->set_visible(false); square.label->set_icon(square.has_mine ? m_mine_bitmap : nullptr); square.label->set_background_color(Color::from_rgb(0xff4040)); + square.label->set_fill_with_background_color(false); if (!square.button) square.button = new SquareButton(this); + square.button->set_icon(nullptr); square.button->set_relative_rect(rect); square.button->set_visible(true); square.button->on_click = [this, &square] (GButton&) { @@ -107,22 +115,12 @@ void Field::reset() square.number = number; if (square.has_mine) continue; - switch (number) { - case 1: - square.label->set_icon(m_one_bitmap.copy_ref()); - break; - case 2: - square.label->set_icon(m_two_bitmap.copy_ref()); - break; - case 3: - square.label->set_icon(m_three_bitmap.copy_ref()); - break; - case 4: - square.label->set_icon(m_four_bitmap.copy_ref()); - break; - } + if (square.number) + square.label->set_icon(m_number_bitmap[square.number - 1].copy_ref()); } } + + update(); } void Field::flood_fill(Square& square) diff --git a/Games/Minesweeper/Field.h b/Games/Minesweeper/Field.h index 2798624f26e..9395bd12870 100644 --- a/Games/Minesweeper/Field.h +++ b/Games/Minesweeper/Field.h @@ -40,14 +40,11 @@ private: template void for_each_neighbor_of(const Square&, Callback); - int m_rows { 10 }; - int m_columns { 10 }; + int m_rows { 9 }; + int m_columns { 9 }; int m_mine_count { 10 }; Vector m_squares; RetainPtr m_mine_bitmap; RetainPtr m_flag_bitmap; - RetainPtr m_one_bitmap; - RetainPtr m_two_bitmap; - RetainPtr m_three_bitmap; - RetainPtr m_four_bitmap; + RetainPtr m_number_bitmap[8]; }; diff --git a/Games/Minesweeper/main.cpp b/Games/Minesweeper/main.cpp index 45a4da77105..3b9dcb790ac 100644 --- a/Games/Minesweeper/main.cpp +++ b/Games/Minesweeper/main.cpp @@ -1,6 +1,8 @@ #include "Field.h" #include #include +#include +#include int main(int argc, char** argv) { @@ -8,9 +10,27 @@ int main(int argc, char** argv) auto* window = new GWindow; window->set_title("Minesweeper"); - window->set_rect(100, 100, 200, 300); - auto* field = new Field(nullptr); - window->set_main_widget(field); + window->set_rect(100, 100, 135, 171); + + auto* widget = new GWidget; + window->set_main_widget(widget); + widget->set_layout(make(Orientation::Vertical)); + + auto* container = new GWidget(widget); + container->set_fill_with_background_color(true); + container->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed); + container->set_preferred_size({ 0, 36 }); + container->set_layout(make(Orientation::Horizontal)); + auto* face_button = new GButton(container); + face_button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed); + face_button->set_preferred_size({ 32, 32 }); + face_button->set_icon(GraphicsBitmap::load_from_file("/res/icons/minesweeper/face-default.png")); + + auto* field = new Field(widget); + + face_button->on_click = [field] (auto&) { + field->reset(); + }; window->show();