forked from tony-luisi/minesweeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.js
81 lines (75 loc) · 2.76 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
document.addEventListener('DOMContentLoaded', startGame)
// Define your `board` object here!
var board = {
cells:
[
{row: 0, col: 0, isMine: false, isMarked: false, hidden: true, surroundingMines: 0},
{row: 0, col: 1, isMine: false, isMarked: false, hidden: true, surroundingMines: 0},
{row: 0, col: 2, isMine: false, isMarked: false, hidden: true, surroundingMines: 0},
{row: 1, col: 0, isMine: true, isMarked: false, hidden: true, surroundingMines: 0},
{row: 1, col: 1, isMine: false, isMarked: false, hidden: true, surroundingMines: 0},
{row: 1, col: 2, isMine: false, isMarked: false, hidden: true, surroundingMines: 0},
{row: 2, col: 0, isMine: true, isMarked: false, hidden: true, surroundingMines: 0},
{row: 2, col: 1, isMine: false, isMarked: false, hidden: true, surroundingMines: 0},
{row: 2, col: 2, isMine: true, isMarked: false, hidden: true, surroundingMines: 0}
]
}
// Alternate board definition
// var board = { cells: generateBoard(3)}
// function generateBoard(size) {
// var cells = []
// for (i= 0; i< size; i++) {
// for (j=0; j< size; j++) {
// cell = {
// row: i,
// col: j,
// isMine: Math.random()<0.20,0:1,
// hidden: true,
// isMarked: false,
// }
// cells.push(cell)
// }
// }
// return cells
// }
function startGame () {
for (var i = 0; i < board.cells.length; i++){
board.cells[i].surroundingMines = countSurroundingMines(board.cells[i])
}
function countSurroundingMines (cell) {
var surrounding = lib.getSurroundingCells(cell.row, cell.col)
var count = 0
for (var i = 0; i < surrounding.length; i++){
if (surrounding[i].isMine === true) {
count++
}
else console.log("peanuts?")
}
return count
}
document.addEventListener("click", checkForWin);
document.addEventListener("contextmenu", checkForWin);
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?
function checkForWin () {
for (var i = 0; i < board.cells.length; i++){
var check = board.cells[i]
if(!check.isMine && check.isMarked) {
return
}
// You can use this function call to declare a winner (once you've
// detected that they've won, that is!)
// lib.displayMessage('You win!')
}
// Define this function to count the number of mines around the cell
// (there could be as many as 8). You don't have to get the surrounding
// cells yourself! Just use `lib.getSurroundingCells`:
//
// var surrounding = lib.getSurroundingCells(cell.row, cell.col)
//
// It will return cell objects in an array. You should loop through
// them, counting the number of times `cell.isMine` is true.