aboutsummaryrefslogtreecommitdiff
path: root/src/games/minesweeper/Minesweeper.hpp
blob: b90f4a326a4ba030b6e22fd6e1b20ba483483d10 (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
#pragma once

#include <games/Game.hpp>
#include <basic/math.hpp>
#include <common/Font.hpp>

#include <array>


namespace std {
    template <>
    struct hash<V2ST> {
        size_t operator()(const V2ST& v) const {
            size_t h1 = hash<size_t>{}(v.x);
            size_t h2 = hash<size_t>{}(v.y);
            return h1 ^ (h2 << 1);
        }
    };
}



class Minesweeper : public Game {
public:
    enum RunState {
        start_menu,
        pause,
        resume,
        game_over,
        exit
    };

    enum Difficulty {
        beginner,
        intermediate,
        expert
    };

public:
    Minesweeper();
    ~Minesweeper() = default;

    bool Update(std::vector<SDL_Event>& events) override;

    void ProcessEventDuringPause(SDL_Event& event);
    void ProcessEventDuringResume(SDL_Event& event);


private:
    void Reset(Difficulty Difficulty);
    void InitIsMineBitmap(int32_t mine_count);
    void InitAdjacentMineCounters();

    void UncoverMines();
    void Uncover(int32_t x, int32_t y);
    void ToggleFlag(int32_t x, int32_t y);

    bool IsCovered(int32_t x, int32_t y);
    bool IsFlagged(int32_t x, int32_t y);
    bool IsMine(int32_t x, int32_t y);


    V2F32 ScreenPosToViewPos(V2F32 screen_pos);


private:
    void DrawBoard();
    void DrawStartMenu();
    void DrawPauseMenu();
    void DrawGameOverMenu();


private:
    static constexpr int32_t MAX_MAP_HEIGHT = 32;
    static constexpr int32_t MAX_MAP_WIDTH = 32;
    static constexpr std::string_view s_FontFilepath = "./fonts/dejavu_ttf/DejaVuSans.ttf";


private:
    RunState m_run_state = start_menu;
    Difficulty m_difficulty = beginner;

    float m_world_width = 4.0f;
    float m_world_height = 3.0f;

    int32_t m_grid_width;
    int32_t m_grid_height;

    V2F32 m_grid_pos;
    V2F32 m_cell_outer_size;
    V2F32 m_cell_inner_size;

    uint32_t m_is_covered_bitmap[MAX_MAP_HEIGHT] {};
    uint32_t m_is_flagged_bitmap[MAX_MAP_HEIGHT] {};
    uint32_t m_is_mine_bitmap[MAX_MAP_HEIGHT] {};
    int32_t m_adjacent_mine_counts[MAX_MAP_WIDTH * MAX_MAP_HEIGHT] {};

    Font m_font;
    std::array<Glyph, 9> m_digit_glyphs;
};