summaryrefslogtreecommitdiff
path: root/Games/Minesweeper/Field.h
blob: 9395bd128708b49ea3a9369003fe02bc7a27e7ec (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
#pragma once

#include <LibGUI/GWidget.h>

class SquareButton;
class GLabel;

struct Square {
    bool is_swept { false };
    bool has_mine { false };
    bool has_flag { false };
    int row { 0 };
    int column { 0 };
    int number { 0 };
    SquareButton* button { nullptr };
    GLabel* label { nullptr };
};

class Field final : public GWidget {
public:
    explicit Field(GWidget* parent);
    virtual ~Field() override;

    int rows() const { return m_rows; }
    int columns() const { return m_columns; }
    int mine_count() const { return m_mine_count; }
    int square_size() const { return 15; }

    void reset();

private:
    void on_square_clicked(Square&);
    void on_square_right_clicked(Square&);
    void game_over();

    Square& square(int row, int column) { return m_squares[row * columns() + column]; }
    const Square& square(int row, int column) const { return m_squares[row * columns() + column]; }

    void flood_fill(Square&);

    template<typename Callback> void for_each_neighbor_of(const Square&, Callback);

    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_number_bitmap[8];
};