-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
174 lines (146 loc) · 3.8 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <vector>
#include "board.hpp"
#include "cell.hpp"
using namespace std;
Board::Board(unsigned width, unsigned height, unsigned mines) {
// for setting cells
unsigned i;
unsigned j;
// for setting mines
unsigned x;
unsigned y;
this->width = width;
this->height = height;
// initialize random number sequence
srand(time(NULL));
// initialize cells
cells = new Cell** [height];
for (i=0; i < height; i++) {
cells[i] = new Cell* [width];
for (j=0; j < width; j++)
cells[i][j] = new Cell(j, i, CELL_TYPE_FREE);
}
// add mines at random on the map
for (i=0; i < mines; i++) {
x = rand() % width;
y = rand() % height;
if (cells[y][x]->getType() == CELL_TYPE_MINE)
continue;
cells[y][x]->setType(CELL_TYPE_MINE);
for (Cell* cellAround : this->getCellsAround(x, y))
cellAround->minesAround++;
}
}
vector<Cell*> Board::getCellsAround(unsigned x, unsigned y) {
vector<Cell*> cellsAround;
// SIDES
// left
if (x > 0) cellsAround.push_back(this->cells[y][x - 1]);
// right
if (x < this->width - 1) cellsAround.push_back(this->cells[y][x + 1]);
// top
if (y > 0) cellsAround.push_back(this->cells[y - 1][x]);
// bottom
if (y < this->width - 1) cellsAround.push_back(this->cells[y + 1][x]);
// CORNERS
// top left
if (x > 0 && y > 0) cellsAround.push_back(this->cells[y - 1][x - 1]);
// bottom left
if (x > 0 && y < height - 1) cellsAround.push_back(this->cells[y + 1][x - 1]);
// top right
if (x < width - 1 && y > 0) cellsAround.push_back(this->cells[y - 1][x + 1]);
// bottom right
if (x < width - 1 && y < height - 1) cellsAround.push_back(this->cells[y + 1][x + 1]);
return cellsAround;
}
/* Board::show
* Prints board to cout
*
* IDEA: here I could add an "output" parameter
* if output is stream, Board::print there.
* if output is render target, Board::draw there.
* Board could be a base class that could be extended
* to BoardTexture or TextBoard or whatever
*/
void Board::show(void) {
// iteraton over board
unsigned i;
unsigned j;
cout << " ";
for (i=0; i < this->width; i++)
cout << i + 1;
cout << endl;
// print board upper border
cout << " ┏";
for (i=0; i < this->width; i++)
cout << "━";
cout << "┓" << endl;
// print board
for (i=0; i < this->height; i++){
cout << i + 1 << "┃";
for (j=0; j< this->width; j++) {
switch (cells[i][j]->getType()) {
case CELL_TYPE_FREE:
if (cells[i][j]->isRevealed()) {
if (cells[i][j]->minesAround)
cout << cells[i][j]->minesAround;
else cout << "░";
} else {
cout << "▓";
}
break;
case CELL_TYPE_MINE:
if (cells[i][j]->isRevealed())
cout << "*";
else
cout << "▓";
break;
}
}
cout << "┃" << endl;
}
// print board lower border
cout << " ┗";
for (i=0; i < this->width; i++)
cout << "━";
cout << "┛" << endl;
}
// Here is where most of the magic happens
Cell* Board::revealCellAt(unsigned x, unsigned y) {
this->cells[y][x]->reveal();
// game over, buddy
if (cells[y][x]->getType() == CELL_TYPE_MINE)
return this->cells[y][x];
// if there are no mines around this cell
if (this->cells[y][x]->minesAround == 0) {
for (Cell* cell : this->getCellsAround(x, y)) {
if (!cell->isRevealed())
this->revealCellAt(cell->getX(), cell->getY());
}
}
return this->cells[y][x];
}
bool Board::isSolved(void) {
unsigned i;
unsigned j;
for (i=0; i < this->height; i++)
for (j=0; j < this->width; j++)
if (cells[i][j]->getType() == CELL_TYPE_FREE && !cells[i][j]->isRevealed())
return false;
return true;
}
void Board::revealAllCells(void) {
unsigned i;
unsigned j;
for (i=0; i < this->height; i++)
for (j=0; j < this->width; j++)
cells[i][j]->reveal();
}
Board::~Board() {
unsigned i;
unsigned j;
for (i=0; i < this->height; i++)
for (j=0; j < this->width; j++)
delete cells[i][j];
}