-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
153 lines (123 loc) · 3.63 KB
/
board.cpp
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
#include <cstdlib>
#include <ctime>
#include <queue>
#include <unordered_set>
#include "board.h"
#include "gameover.h"
using std::pair;
using std::make_pair;
using std::queue;
using std::unordered_set;
board_t::board_t(const int W, const int H) {
width = W;
height = H;
cells = new cell_t **[H];
for (int i = 0; i < height; ++i) {
cells[i] = new cell_t *[W];
for (int j = 0; j < width; ++j) {
cells[i][j] = new cell_t();
}
}
}
board_t::~board_t() {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
delete cells[i][j];
}
delete[] cells[i];
}
delete[] cells;
}
void board_t::reset() {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
cells[i][j]->reset();
}
}
}
void board_t::place_mines(const int count) {
int i = 0;
srand(time(nullptr));
unordered_set<int> positions;
while (i < count) {
int pos = rand() % (width * height);
if (positions.count(pos) > 0)
continue;
int row = pos / width;
int col = pos % width;
positions.insert(pos);
place_mine(row, col);
++i;
}
}
void board_t::place_mine(unsigned int row, unsigned int col) {
auto cell = cells[row][col];
cell->place_mine();
auto neighbours = get_adjacent_positions(row, col);
for (auto pos : neighbours) {
cells[pos.first][pos.second]->increment_adjacent_mines();
}
}
vector<pair<int, int>> board_t::get_adjacent_positions(unsigned int row, unsigned int col) {
vector<pair<int, int>> neighbours;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue;
if (row + i >= 0 and row + i < height and col + j >= 0 and col + j < width)
neighbours.emplace_back(row + i, col + j);
}
}
return neighbours;
}
void board_t::reveal_cell(const unsigned int row, const unsigned int col) {
if (row >= height || col >= width)
return;
auto cell = cells[row][col];
bool mine_revealed = cell->reveal();
if (mine_revealed)
throw GameOver();
queue<pair<int, int>> q;
unordered_set<cell_t *> visited;
q.push(make_pair(row, col));
while (!q.empty()) {
auto pos = q.front();
q.pop();
cell = cells[pos.first][pos.second];
if (visited.count(cell) > 0) {
continue;
}
visited.insert(cell);
if (!cell->auto_reveal())
continue;
cell->reveal();
if (cell->auto_reveal_neighbors()) {
auto neighbors = get_adjacent_positions(pos.first, pos.second);
for (auto neighbor : neighbors)
q.push(neighbor);
}
}
}
void board_t::reveal_all_cells() {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
cells[i][j]->unflag();
cells[i][j]->reveal();
}
}
}
void board_t::toggle_flag_cell(const unsigned int row, const unsigned int col) {
if (row < height && col < width)
cells[row][col]->toggle_flag();
}
void board_t::draw(sf::RenderWindow* window, TextureManager *textureManager, int cell_size) {
window->clear();
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
cell_t* cell = cells[i][j];
sf::Texture texture = textureManager->retrieve(cell->get_display_value());
cell->sprite->setTexture(texture);
cell->sprite->setPosition(i * cell_size, j * cell_size);
window->draw(*cell->sprite);
}
}
}