-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cpp
More file actions
338 lines (310 loc) · 10 KB
/
Grid.cpp
File metadata and controls
338 lines (310 loc) · 10 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "Grid.h"
#include "GameManager.h"
#include <ctime>
#include <random>
using namespace std;
using namespace sf;
Grid::Grid(RenderWindow & w_ref,SoundManager & _soundManager,GameManager & _gameManager_ref,int width,int height,int bombNumber)
: window_ref(w_ref) , soundManager(_soundManager),gameManager_ref(_gameManager_ref)
{
bombs=bombNumber;
state=GridState::Playing;
firstClick=true;
topMargin=48;
float widthRatio=w_ref.getSize().x/(float)width;
float heightRatio=(w_ref.getSize().y-topMargin)/(float)(height);
maxSize = widthRatio < heightRatio ? widthRatio : heightRatio ;
topLeftCorner.x=(w_ref.getSize().x-maxSize*width)/2;
topLeftCorner.y=(w_ref.getSize().y-topMargin-maxSize*height)/2 + topMargin;
this->width=width;
this->height=height;
cells = vector<Cell>();
// bomb based on a fixed amount of bombs and a random index
for(int j=0;j<height;j++)
for(int i=0;i<width;i++)
cells.push_back(Cell(i,j,maxSize,topLeftCorner,indexConverter(i,j)));
int bombsPlaced=0;
while(bombsPlaced!=bombNumber){
int index=indexConverter(rand()%width,rand()%height);
if( !cells[index].getValue() ){
cells[index].setValue(-1);
bombsPlaced++;
}
}
}
Grid::Grid(RenderWindow& window_ref, SoundManager& soundManager,GameManager & _gameManager_ref)
:window_ref(window_ref)
,soundManager(soundManager)
,gameManager_ref(_gameManager_ref)
{
}
void Grid::update(){
window_ref.clear(Color(200,200,200));
drawTop();
draw();
}
void Grid::setupGrid(int width, int height, int bombNumber)
{
bombs=bombNumber;
state = GridState::Playing;
firstClick = true;
topMargin = 48;
float widthRatio = window_ref.getSize().x / (float)width;
float heightRatio = (window_ref.getSize().y - topMargin) / (float)(height);
maxSize = widthRatio < heightRatio ? widthRatio : heightRatio;
topLeftCorner.x = (window_ref.getSize().x - maxSize * width) / 2;
topLeftCorner.y = (window_ref.getSize().y - topMargin - maxSize * height) / 2 + topMargin;
this->width = width;
this->height = height;
cells = vector<Cell>();
// bomb based on a fixed amount of bombs and a random index
for (int j = 0; j < height; j++)
for (int i = 0; i < width; i++)
cells.push_back(Cell(i, j, maxSize, topLeftCorner));
int bombsPlaced = 0;
while (bombsPlaced != bombNumber) {
int index = indexConverter( rand() % width,rand() % height );
if (!cells[index].getValue()) {
cells[index].setValue(-1);
bombsPlaced++;
}
}
}
void Grid::draw(){
for(int i=0;i<cells.size();i++){
cells[i].update();
window_ref.draw(cells[i]);
}
}
void Grid::drawTop(){
RectangleShape shape(Vector2f(window_ref.getSize().x,topMargin));
shape.setFillColor(Color(150,150,150));
window_ref.draw(shape);
}
void Grid::manageInput(Mouse::Button button){
if(state!=GridState::Playing)
return;
if(button == Mouse::Right){
//flag cells
Vector2i pos=mousePos();
if(isMouseOnGrid(pos)){
Vector2i index2D=mousePosToIndex(pos);
int index=indexConverter(index2D);
if(cells[index].flag()){
if(cells[index].getState() == Cell::Flagged)
soundManager.play(SoundManager::FlagOn);
else
soundManager.play(SoundManager::FlagOff);
}
}
}
else if(button == Mouse::Left){
//revealing cells;
Vector2i pos=mousePos();
if(isMouseOnGrid(pos)){
Vector2i index2D=mousePosToIndex(pos);
int index=indexConverter(index2D);
if(firstClick){
firstClickCheck(index);
gameManager_ref.startTimer();
firstClick=false;
}
if(cells[index].getState() == Cell::CellState::Hidden){
cells[index].reveal();
if(cells[index].getValue()==0){
revealNeighbors(index2D);
}
if(cells[index].getValue()==-1){
gameover();
}
else
soundManager.play(SoundManager::Reveal);
checkGame();
}
}
}
else if(button == Mouse::Middle){
Vector2i pos=mousePos();
if(isMouseOnGrid(pos)){
Vector2i index2D=mousePosToIndex(pos);
if(middleClickCheck(index2D)){
soundManager.play(SoundManager::Reveal);
revealMiddleClick(index2D);
}
checkGame();
}
}
}
Vector2i Grid::mousePos(){
return Mouse::getPosition(window_ref);
}
bool Grid::isMouseOnGrid(Vector2i & pos){
if(pos.x>=topLeftCorner.x && pos.x<=topLeftCorner.x + width*maxSize)
if(pos.y>=topLeftCorner.y && pos.y<=topLeftCorner.y + height*maxSize)
return true;
return false;
}
Vector2i Grid::mousePosToIndex(Vector2i & pos){
Vector2i result;
Vector2i posOnGrid=pos-static_cast<Vector2i>(topLeftCorner);
result.x=posOnGrid.x/maxSize;
result.y=posOnGrid.y/maxSize;
return result;
}
void Grid::calculateValue(){
if(bombs*2>=width*height)
for(int j=0;j<height;j++){
for(int i=0;i<width;i++){
int index=indexConverter(i,j);
if(cells[index].getValue()==-1)
continue;
int neighborBombs=0;
for(int x=j-1;x<=j+1;x++){
for(int k=i-1;k<=i+1;k++){
if( !(k==i && x==j) && x>=0 && x < height && k>=0 && k < width){
int index2=indexConverter(k,x);
if(cells[index2].getValue()==-1)
neighborBombs++;
}
}
}
cells[index].setValue(neighborBombs);
}
}
else
for(int j=0;j<height;j++){
for(int i=0;i<width;i++){
int index=indexConverter(i,j);
if(cells[index].getValue()!=-1)
continue;
for(int x=j-1;x<=j+1;x++){
for(int k=i-1;k<=i+1;k++){
if( !(k==i && x==j) && x>=0 && x < height && k>=0 && k < width){
int index2=indexConverter(k,x);
if(cells[index2].getValue()!=-1)
cells[index2].setValue(cells[index2].getValue()+1);
}
}
}
}
}
}
int Grid::indexConverter(Vector2i index){
return width*index.y+index.x;
}
int Grid::indexConverter(int x,int y){
return width*y+x;
}
void Grid::firstClickCheck(int index){
if(cells[index].getValue()==-1){
for(int i=0;i<width*height;i++){
if(cells[i].getValue()!=-1){
cells[i].setValue(-1);
cells[index].setValue(0);
break;
}
}
}
calculateValue();
}
void Grid::revealNeighbors(Vector2i pos){
int j=pos.y;
int i=pos.x;
for(int x=j-1;x<=j+1;x++){
for(int k=i-1;k<=i+1;k++){
if( !(k==i && x==j) && x>=0 && x < height && k>=0 && k < width){
int index=indexConverter(k,x);
if(cells[index].getState() == Cell::CellState::Hidden || cells[index].getState() == Cell::CellState::Flagged){
cells[index].setState(Cell::Hidden);
cells[index].reveal();
if(cells[index].getValue()==0){
revealNeighbors({k,x});
}
}
}
}
}
}
void Grid::revealMiddleClick(Vector2i pos){
int j=pos.y;
int i=pos.x;
bool bomb=false;
for(int x=j-1;x<=j+1;x++){
for(int k=i-1;k<=i+1;k++){
if( !(k==i && x==j) && x>=0 && x < height && k>=0 && k < width){
int index=indexConverter(k,x);
if(cells[index].getValue()==-1 && cells[index].getState() == Cell::Hidden ){
bomb=true;
}
if(cells[index].getState() == Cell::CellState::Hidden && cells[index].getValue() != -1){
cells[index].setState(Cell::Hidden);
cells[index].reveal();
if(cells[index].getValue()==0){
revealNeighbors({k,x});
}
}
}
}
}
if(bomb)
gameover();
}
bool Grid::middleClickCheck(Vector2i pos){
int indexCell=indexConverter(pos);
if(cells[indexCell].getState() != Cell::Revealed || cells[indexCell].getValue() == 0)
return false;
int j=pos.y;
int i=pos.x;
int bombs=cells[indexCell].getValue();
for(int x=j-1;x<=j+1;x++){
for(int k=i-1;k<=i+1;k++){
if( !(k==i && x==j) && x>=0 && x < height && k>=0 && k < width){
int index=indexConverter(k,x);
if(cells[index].getState() == Cell::CellState::Flagged){
bombs--;
}
}
}
}
if(!bombs)
return true;
return false;
}
void Grid::checkGame(){
bool won=true;
for(auto cell : cells){
if( cell.getValue() >=0 ){
if(cell.getState() != Cell::CellState::Revealed){
won = false;
}
}
}
if(won){
state=GridState::Won;
soundManager.play(SoundManager::Victory);
gameManager_ref.stopTimer();
//you won =D
}
}
void Grid::gameover(){
//you lost =(
gameManager_ref.stopTimer();
state=GridState::Lost;
soundManager.play(SoundManager::Explosion);
for(int i=0;i<cells.size();i++){
if(cells[i].getValue()==-1){
cells[i].setState(Cell::Revealed);
}
}
}
int Grid::getBombCount(){
return bombs;
}
int Grid::getFlagCount(){
int result=0;
for(Cell cell : cells){
if(cell.getState()==Cell::Flagged)
result++;
}
return result;
}