-
Notifications
You must be signed in to change notification settings - Fork 562
/
minesweeper.js
113 lines (99 loc) · 2.39 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
document.addEventListener('DOMContentLoaded', startGame)
var board = {
cells: []
}
function startGame () {
var children = getCells()
for (var i = 0; i < children.length; i++) {
addListeners(children[i])
addCellToBoard(children[i])
}
for (var i = 0; i < board.cells.length; i++) {
board.cells[i].surroundingMines = countMines(board.cells[i])
}
}
function getCells(){
var boardElement = document.getElementsByClassName('board')
return boardElement[0].children
}
function countMines(cell) {
var surroundingCells = getSurroundingCells(cell.row, cell.col)
var mineCount = 0
for (var i = 0; i < surroundingCells.length; i++) {
if (surroundingCells[i].isMine) {
mineCount++
}
}
return mineCount
}
function addCellToBoard(cell) {
var newCell = {
row: getRow(cell),
col: getCol(cell),
isMine: cell.classList.contains('mine')
}
board.cells.push(newCell)
}
function getRow (cell) {
return Number(cell.classList[0].split('-')[1])
}
function getCol (cell) {
return Number(cell.classList[1].split('-')[1])
}
function addListeners(cell) {
cell.addEventListener('click', showCell)
cell.addEventListener('contextmenu', markCell)
}
function checkForWin() {
checkForLoss()
var allMinesMarked = true
var allCellsUnhidden = true
var cells = board.cells
for (var i = 0; i < cells.length; i++) {
if (!cells[i].isMarked && cells[i].isMine) {
allMinesMarked = false
}
}
if (!allMinesMarked) {
return
}
var domCells = getCells()
for (var i = 0; i < domCells.length; i++) {
if (domCells[i].classList.contains('hidden')) {
allCellsUnhidden = false
}
}
if (allCellsUnhidden) {
alert("You won!")
}
}
function checkForLoss(){
}
function showCell(evt) {
evt.target.classList.remove('hidden')
if (evt.target.classList.contains('mine')) {
showAllMines()
}
showSurrounding(evt.target)
checkForWin()
}
function showAllMines(){
var cells = getCells()
for (var i = 0; i < cells.length; i++) {
cells[i].classList.remove('hidden')
}
}
function markCell(evt) {
evt.preventDefault()
evt.target.classList.toggle('marked')
evt.target.classList.toggle('hidden')
var filteredCells = board.cells
.filter(function(cell){
return cell.row === getRow(evt.target)
})
.filter(function(cell){
return cell.col === getCol(evt.target)
})
filteredCells[0].isMarked = true
checkForWin()
}