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

#include <LibGUI/GFrame.h>
#include <LibCore/CTimer.h>

class SquareButton;
class GButton;
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 GFrame {
public:
    Field(GLabel& flag_label, GLabel& time_label, GButton& face_button, 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();
    void win();
    void reveal_mines();

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

    enum class Face { Default, Good, Bad };
    void set_face(Face);

    int m_rows { 9 };
    int m_columns { 9 };
    int m_mine_count { 10 };
    int m_unswept_empties { 0 };
    Vector<Square> m_squares;
    RetainPtr<GraphicsBitmap> m_mine_bitmap;
    RetainPtr<GraphicsBitmap> m_flag_bitmap;
    RetainPtr<GraphicsBitmap> m_number_bitmap[8];
    GButton& m_face_button;
    GLabel& m_flag_label;
    GLabel& m_time_label;
    CTimer m_timer;
    int m_seconds_elapsed { 0 };
    int m_flags_left { 0 };
    Face m_face { Face::Default };
};