Minesweeper: More implementation work.

This commit is contained in:
Andreas Kling 2019-04-13 03:57:55 +02:00
parent a90e218c71
commit 4df360be8c
Notes: sideshowbarker 2024-07-19 14:44:07 +09:00
7 changed files with 42 additions and 27 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 143 B

View file

@ -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)

View file

@ -40,14 +40,11 @@ private:
template<typename Callback> 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<Square> m_squares;
RetainPtr<GraphicsBitmap> m_mine_bitmap;
RetainPtr<GraphicsBitmap> m_flag_bitmap;
RetainPtr<GraphicsBitmap> m_one_bitmap;
RetainPtr<GraphicsBitmap> m_two_bitmap;
RetainPtr<GraphicsBitmap> m_three_bitmap;
RetainPtr<GraphicsBitmap> m_four_bitmap;
RetainPtr<GraphicsBitmap> m_number_bitmap[8];
};

View file

@ -1,6 +1,8 @@
#include "Field.h"
#include <LibGUI/GApplication.h>
#include <LibGUI/GWindow.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GButton.h>
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<GBoxLayout>(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<GBoxLayout>(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();