FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
minesweeper/src/sweeper.cpp at main · R-Besson/minesweeper · GitHub
R-Besson
/
minesweeper
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
minesweeper
/
src
/
sweeper.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
172 lines (138 loc) · 4.04 KB
Breadcrumbs
minesweeper
/
src
/
sweeper.cpp
Copy path
File metadata and controls
172 lines (138 loc) · 4.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
sweeper.h
"
const
int
xDir[
8
] {
0
,
1
,
0
, -
1
,
1
,
1
, -
1
, -
1
};
const
int
yDir[
8
] {
1
,
0
, -
1
,
0
,
1
, -
1
,
1
, -
1
};
//
Initializes/Resets the Grid
void
Game::initialize
(
int
GRID_WIDTH
,
int
GRID_HEIGHT
,
int
NB_MINES
) {
grid->
clear
();
this
->
GRID_WIDTH
=
GRID_WIDTH
;
this
->
GRID_HEIGHT
=
GRID_HEIGHT
;
this
->
NB_MINES
=
NB_MINES
;
for
(
int
x =
0
; x <
GRID_WIDTH
; x++)
{
grid->
push_back
(
new
CellList);
for
(
int
y =
0
; y <
GRID_HEIGHT
; y++)
{
Cell* cell =
new
Cell;
cell->
x
= x;
cell->
y
= y;
grid->
at
(x)->
push_back
(cell);
}
}
state = GameState::Initiated;
}
//
Add Mines + Calulcate Numbers adjacent to Mines, to the Grid
void
Game::populateGrid
(Cell* &exception)
{
//
Generate mines
int
generated =
0
;
while
(generated !=
NB_MINES
)
{
int
x =
rand
() %
GRID_WIDTH
;
int
y =
rand
() %
GRID_HEIGHT
;
Cell* cell = grid->
at
(x)->
at
(y);
if
(cell->
isMine
)
continue
;
if
(
abs
(exception->
x
- cell->
x
) <=
1
&&
abs
(exception->
y
- cell->
y
) <=
1
)
continue
;
cell->
isMine
=
true
;
generated++;
}
FLAGS_LEFT
= generated;
//
Generate adjacent numbered cells
//
NSWE + Diagonals
for
(
int
x =
0
; x <
GRID_WIDTH
; x++)
{
for
(
int
y =
0
; y <
GRID_HEIGHT
; y++)
{
Cell* cell = grid->
at
(x)->
at
(y);
if
(!(cell->
isMine
))
continue
;
CellList* adjs =
getAdjacentCells
(cell);
for
(
auto
it = adjs->
begin
(); it != adjs->
end
(); it++)
if
(!(*it)->
isMine
) (*it)->
num
++;
}
}
}
//
Gets 8 cells surrounding the root cell
CellList*
Game::getAdjacentCells
(Cell* &root)
{
CellList* adjs =
new
CellList;
//
Up, Down, Left, Right, + Diagonals -> 8 Squares total
for
(
int
d =
0
; d <
8
; d++)
{
int
nX = root->
x
+ xDir[d];
int
nY = root->
y
+ yDir[d];
if
(
nX >=
GRID_WIDTH
||
nX <
0
||
nY >=
GRID_HEIGHT
||
nY <
0
)
continue
;
adjs->
push_back
(grid->
at
(nX)->
at
(nY));
}
return
adjs;
}
//
Flags a cell (False flag is cell isnt a Mine, else is it Valid (Not known to user))
void
Game::flagCell
(Cell* &cell)
{
if
(cell->
isRevealed
)
return
;
cell->
isFlagged
= !(cell->
isFlagged
);
if
(cell->
isFlagged
)
FLAGS_LEFT
--;
else
FLAGS_LEFT
++;
}
//
Recursively reveals an area that is purely mine-free
void
Game::revealCell
(Cell* &cell)
{
if
(!cell->
isRevealed
&& !cell->
isFlagged
&& state == GameState::Initiated) {
cell->
isRevealed
=
true
;
//
End the game if the Cell is a mine
if
(cell->
isMine
) {
state = GameState::Lost;
}
else
if
(cell->
num
==
0
) {
//
If the cell has 0 adjacent mines, we need to recurse to clear out all adjacent 0 cells
CellList* adjs =
getAdjacentCells
(cell);
for
(
auto
it = adjs->
begin
(); it != adjs->
end
(); it++)
if
(!(*it)->
isMine
)
revealCell
(*it);
}
}
}
//
Reveals all cells (End game)
void
Game::revealAll
()
{
for
(
int
x =
0
; x <
GRID_WIDTH
; x++)
{
for
(
int
y =
0
; y <
GRID_HEIGHT
; y++)
{
grid->
at
(x)->
at
(y)->
isRevealed
=
true
;
}
}
}
//
Checks if user has successfully guessed all Mines
bool
Game::checkWin
()
{
if
(
FLAGS_LEFT
!=
0
)
return
false
;
for
(
int
x =
0
; x <
GRID_WIDTH
; x++)
{
for
(
int
y =
0
; y <
GRID_HEIGHT
; y++)
{
Cell* cell = grid->
at
(x)->
at
(y);
if
(cell->
isMine
&& !cell->
isFlagged
)
return
false
;
if
(!cell->
isRevealed
&& !cell->
isMine
)
return
false
;
}
}
return
true
;
}
//
...
void
Game::printGrid
()
{
for
(
int
y =
0
; y <
GRID_WIDTH
; y++)
{
std::cout <<
"
\n
"
;
for
(
int
x =
0
; x <
GRID_HEIGHT
; x++)
{
Cell* cell = grid->
at
(x)->
at
(y);
if
(cell->
isMine
) std::cout <<
"
X
"
;
else
std::cout << cell->
num
<<
"
"
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL