summaryrefslogtreecommitdiff
path: root/Games/Minesweeper/Field.cpp
blob: 92714174d4097b77667eda7eb2718657eb8f48b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "Field.h"
#include <LibGUI/GButton.h>
#include <LibGUI/GLabel.h>
#include <AK/HashTable.h>
#include <unistd.h>
#include <time.h>

class SquareButton final : public GButton {
public:
    SquareButton(GWidget* parent)
        : GButton(parent)
    {
    }

    Function<void()> on_right_click;

    virtual void mousedown_event(GMouseEvent& event) override
    {
        if (event.button() == GMouseButton::Right) {
            if (on_right_click)
                on_right_click();
        }
        GButton::mousedown_event(event);
    }
};

Field::Field(GWidget* parent)
    : GFrame(parent)
{
    set_frame_thickness(2);
    set_frame_shape(FrameShape::Container);
    set_frame_shadow(FrameShadow::Sunken);
    m_mine_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/mine.png");
    m_flag_bitmap = GraphicsBitmap::load_from_file("/res/icons/minesweeper/flag.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);
    reset();
}

Field::~Field()
{
}

template<typename Callback>
void Field::for_each_neighbor_of(const Square& square, Callback callback)
{
    int r = square.row;
    int c = square.column;
    if (r > 0) // Up
        callback(this->square(r - 1, c));
    if (c > 0) // Left
        callback(this->square(r, c - 1));
    if (r < (m_rows - 1)) // Down
        callback(this->square(r + 1, c));
    if (c < (m_columns - 1)) // 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 - 1)) // UpRight
        callback(this->square(r - 1, c + 1));
    if (r < (m_rows - 1) && c > 0) // DownLeft
        callback(this->square(r + 1, c - 1));
    if (r < (m_rows - 1) && c < (m_columns - 1)) // DownRight
        callback(this->square(r + 1, c + 1));
}

void Field::reset()
{
    set_greedy_for_hits(false);
    srand(time(nullptr));
    m_squares.resize(rows() * columns());

    HashTable<int> mines;
    for (int i = 0; i < m_mine_count; ++i)
        mines.set(rand() % (rows() * columns()));

    int i = 0;
    for (int r = 0; r < rows(); ++r) {
        for (int c = 0; c < columns(); ++c) {
            Rect rect = { frame_thickness() + c * square_size(), frame_thickness() + r * square_size(), square_size(), square_size() };
            auto& square = this->square(r, c);
            square.row = r;
            square.column = c;
            square.has_mine = mines.contains(i);
            square.has_flag = false;
            square.is_swept = false;
            if (!square.label)
                square.label = new GLabel(this);
            square.label->set_relative_rect(rect);
            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&) {
                on_square_clicked(square);
            };
            square.button->on_right_click = [this, &square] {
                on_square_right_clicked(square);
            };
            ++i;
        }
    }
    for (int r = 0; r < rows(); ++r) {
        for (int c = 0; c < columns(); ++c) {
            auto& square = this->square(r, c);
            int number = 0;
            for_each_neighbor_of(square, [&number] (auto& neighbor) {
                number += neighbor.has_mine;
            });
            square.number = number;
            if (square.has_mine)
                continue;
            if (square.number)
                square.label->set_icon(m_number_bitmap[square.number - 1].copy_ref());
        }
    }

    update();
}

void Field::flood_fill(Square& square)
{
    on_square_clicked(square);
    for_each_neighbor_of(square, [this] (auto& neighbor) {
        if (!neighbor.is_swept && !neighbor.has_mine && neighbor.number == 0)
            flood_fill(neighbor);
    });
}

void Field::on_square_clicked(Square& square)
{
    if (square.is_swept)
        return;
    if (square.has_flag)
        return;
    square.is_swept = true;
    square.button->set_visible(false);
    square.label->set_visible(true);
    if (square.has_mine) {
        square.label->set_fill_with_background_color(true);
        game_over();
    } else if (square.number == 0) {
        flood_fill(square);
    }
}

void Field::on_square_right_clicked(Square& square)
{
    if (square.is_swept)
        return;
    square.has_flag = !square.has_flag;
    square.button->set_icon(square.has_flag ? m_flag_bitmap : nullptr);
    square.button->update();
}

void Field::game_over()
{
    set_greedy_for_hits(true);
    for (int r = 0; r < rows(); ++r) {
        for (int c = 0; c < columns(); ++c) {
            auto& square = this->square(r, c);
            if (square.has_mine) {
                square.button->set_visible(false);
                square.label->set_visible(true);
            }
        }
    }
    update();
}