-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.cpp
More file actions
180 lines (157 loc) · 5.26 KB
/
Grid.cpp
File metadata and controls
180 lines (157 loc) · 5.26 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
#include "Grid.h"
#include <windows.h>
#include <time.h>
#include <iostream>
Grid::Grid(int sizeX, int sizeY) : sizeX(sizeX), sizeY(sizeY) {
this->grid = new Cell**[this->sizeY];
bool switcher = true;
for(int i = 0; i < this->sizeY; i++){
this->grid[i] = new Cell*[this->sizeX];
for(int j = 0; j < this->sizeX; j++){
this->grid[i][j] = new Cell((j % 2 == switcher) ? sf::Color(0, 176, 47) : sf::Color(0, 150, 30), i, j, 50, false);
}
switcher = !switcher;
}
this->firstClick = true;
this->openFieldCounter = 0;
this->numberOfMines = 50;
this->numberOfFlags = 0;
this->gameRun = true;
}
Grid::~Grid() {
for(int i = 0; i < this->sizeY; i++){
for(int j = 0; j < this->sizeX; j++){
delete this->grid[i][j];
}
delete[] this->grid[i];
}
delete [] this->grid;
}
void Grid::checkField(int x, int y){
if(!this->gameRun) return;
sf::Vector2i pos = this->getContain(x, y);
if(pos.x == -1 && pos.y == -1) return;
if(this->grid[pos.y][pos.x]->isFlag()) return;
if(this->firstClick){
this->setRandomBombs(pos.x, pos.y);
this->setNumbers();
this->firstClick = false;
}
if(this->grid[pos.y][pos.x]->isMine()){
this->gameRun = false;
this->openBombs();
return;
}
this->openFields(pos.x, pos.y);
}
void Grid::switchFlag(int x, int y){
if(!this->gameRun) return;
sf::Vector2i pos = this->getContain(x, y);
if((pos.x == -1 && pos.y == -1)|| this->grid[pos.y][pos.x]->isVisible()) return;
if(this->numberOfFlags < this->numberOfMines){
if(!this->grid[pos.y][pos.x]->isFlag()){
this->numberOfFlags++;
this->grid[pos.y][pos.x]->setFlag(true);
}else{
this->numberOfFlags--;
this->grid[pos.y][pos.x]->setFlag(false);
}
}else{
if(this->grid[pos.y][pos.x]->isFlag()){
this->numberOfFlags--;
this->grid[pos.y][pos.x]->setFlag(false);
}
}
}
void Grid::render(sf::RenderWindow &window) const {
for(int i = 0; i < this->sizeY; i++){
for(int j = 0; j < this->sizeX; j++){
this->grid[i][j]->draw(window);
}
}
}
sf::Vector2i Grid::getContain(int x, int y){
for(int i = 0; i < this->sizeY; i++){
for(int j = 0; j < this->sizeX; j++){
if(this->grid[i][j]->contains(x, y))
return sf::Vector2i(j, i);
}
}
return sf::Vector2i(-1, -1);
}
void Grid::setRandomBombs(int x, int y){
srand(time(NULL));
int randY, randX;
for(int i = 0; i < this->numberOfMines; i++){
randX = rand() % this->sizeX;
randY = rand() % this->sizeY;
if(this->grid[y][x]->isMine() || (x -1 == randX && y - 1 == randY)
|| (x == randX && y - 1 == randY)
|| (x + 1 == randX && y - 1 == randY)
|| (x - 1 == randX && y == randY)
|| (x == randX && y == randY)
|| (x + 1 == randX && y == randY)
|| (x -1 == randX && y + 1 == randY)
|| (x == randX && y + 1 == randY)
|| (x + 1 == randX && y + 1 == randY)){
i--;
}else{
this->grid[randY][randX]->setMine(true);
}
}
}
void Grid::openFields(int x, int y){
this->grid[y][x]->setVisible(true);
this->grid[y][x]->setColor(( x % 2 == (y % 2 == 0)) ? sf::Color(150, 150, 150) : sf::Color(120, 120, 120));
this->openFieldCounter++;
if(this->grid[y][x]->getNumber() == 0){
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
if(this->inRange(j + x, i + y) && this->grid[y + i][x + j]->canOpen()) this->openFields(j + x, i + y);
}
}
}
}
bool Grid::inRange(int x, int y) const{
if(x > this->sizeX - 1 || y > this->sizeY - 1 || x < 0 || y < 0) return false;
return true;
}
void Grid::setNumbers(){
for(int i = 0; i < this->sizeY; i++){
for(int j = 0; j < this->sizeX; j++){
if(!this->grid[i][j]->isMine()){
int bombs = this->countBombs(j, i);
if(bombs >= 0){
this->grid[i][j]->setNumber(bombs);
}
}
}
}
}
int Grid::countBombs(int x, int y) const{
int counter = 0;
if(this->grid[y][x]->isMine()){
return -1;
}
for(int i = -1; i <= 1; i++){
for(int j = -1; j <= 1; j++){
if(this->inRange(x + j, y + i) && this->grid[y + i][x + j]->isMine()) counter++;
}
}
return counter;
}
void Grid::openBombs() {
for(int i = 0; i < this->sizeY; i++){
for(int j = 0; j < this->sizeX; j++){
if(this->grid[i][j]->isMine() && !this->grid[i][j]->isFlag()){
this->grid[i][j]->setVisible(true);
}
if(this->grid[i][j]->isFlag() && !this->grid[i][j]->isMine()){
this->grid[i][j]->setWrongFlag(true);
}
}
}
}
bool Grid::checkWin() const{
return (this->openFieldCounter == (this->sizeX * this->sizeY) - this->numberOfMines);
}