forked from tony-luisi/minesweeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.js
265 lines (211 loc) · 5.08 KB
/
minesweeper.js
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
document.addEventListener('DOMContentLoaded', startGame)
// Define your `board` object here!
var board = {
cells: generateCells(6)
}
// {
// row: 0,
// col: 0,
// isMine: false,
// hidden: true,
// },
// {
// row: 1,
// col: 0,
// isMine: false,
// hidden: true,
// },
// {
// row: 0,
// col: 1,
// isMine: false,
// hidden: true,
// },
// {
// row: 1,
// col: 1,
// isMine: false,
// hidden: true,
// },
// ]
function generateCells (size) {
// create a cells array
// make a loop that automatically generate generateCells
// fill the cells aray with the generated cells
//return a cells array
var cells = []
for (i = 0; i < size; i++ ) {
for (j = 0; j < size; j++){
cell = {
row: i,
col: j,
isMine: mineRandom(),
isMarked: false,
hidden: true,
}
cells.push(cell)
}
}
return cells
}
function mineRandom () {
cellChance = Math.round(Math.random() * 99);
if (cellChance < 20) {
return true
} else {
return false
}
}
function startGame () {
// Don't remove this function call: it makes the game work!
// board.cells[4].isMine = true
for (i = 0; i < board.cells.length; i++) {
board.cells[i].surroundingMines = countSurroundingMines(board.cells[i])
}
lib.initBoard()
}
// Define this function to look for a win condition:
//
// 1. Are all of the cells that are NOT mines visible?
// 2. Are all of the mines marked?
document.addEventListener("click", checkForWin);
document.addEventListener("contextmenu", checkForWin);
function checkForWin (){
for (var i = 0; i < board.cells.length; i++){
var check = board.cells[i];
if(!check.isMine && check.hidden){
return false;
} else if (check.isMine && !check.isMarked) {
return false;
}else if (!check.hasOwnProperty("isMarked"))
return false;
}
lib.displayMessage('You win!')
}
// else if( countVisibleNonMines = !visibleNonMines ) {
// console.log(" Try Again ")
// }
// }
//
//
// // Once [ c ] is equal to visibleNonMines the user wins.
// // Create an if statement for if they are equal console log this, if this is not true console.log try again.
//
//
// if (countVisibleNonMines = visibleNonMines ) {
// console.log (lib.displayMessage('You win!’))
// }
// if else ( countVisibleNonMines = !visibleNonMines ) {
// console.log(“ Try Again ”)
// }
// }
//
//
// // Count through the array and count all of the mines and mark it [ a ]
// // Then, [ a ] Mines - all cells on the board = visibleNonMines
// if nonMinesCount = nonMinesVisible then you win
// how many aren't mines
function nonMinesCount (cell) {
var mine = 0
for (i = 0; i < board.cells.length; i++) {
if (board.cells[i].isMine === true) {
mine++
}
}
console.log(board.cells.length - mine)
return board.cells.length - mine
}
// how many non mines are showing
function nonMinesVisible (cell) {
var count = 0
for (i = 0; i < board.cells.length; i++) {
if (board.cells[i].isMine === false && board.cells[i].hidden === false) {
count ++
}
}
console.log(count)
return count
}
function mineMarked (cell) {
var count = 0
for (i = 0; i < board.cells.length; i++) {
if(board.cells[i].isMine === true && board.cells[i].isMarked === true) {
count ++
}
}
return count
}
function cellsShown (cell) {
for (i = 0; i < board.cells.length; i++) {
if (board.cells[i].hidden === true) {
return false
} else {
return true
}
}
}
// function visibleNonMines (cell) {
// var mine = 0
// for (a=0; a < board.cells.length; a++) {
// if (cell[a].isMine){
// mine ++
// }
// }
// return mine - cells
// }
// Function visibleNonMines (cell) {
//
// Var mine = 0
//
// For (a =0; a < board.cells.length; a++){
// If ( cell[a].isMine ){
// mine ++
// }
// }
//
// return mine - cells
//
// }
//
//
//
// Count through the array again and count all the cells that are not mines and mark it [ b]
// Then count through all the nonMines [ b ] and for every one that has the value hidden = false increase a number called [ c ].
function countVisibleNonMines (cell) {
var count = 0
for (b = 0; b < board.cells.length; b++) {
if (board.cells.hidden === false) {
count++
}
}
return count
}
//
//
// Function countVisibleNonMines (cell) {
//
// Var count = 0
//
// For (b =0; b < board.cells.length; b++){
// if ( cell[b].!hidden .!isMine){
// count++
// }
// }
//
// Return count
// }
function countSurroundingMines (cell) {
// make a surround array variable using the lib function
// make a count variable that starts at 0
// loop through all the surrounding cells, and if they are a mine, add to the count.
// return the counts
//
var surrounding = lib.getSurroundingCells(cell.row, cell.col)
var count = 0
for (j = 0; j < surrounding.length; j++) {
if (surrounding[j].isMine){
count++
}
}
return count
}