forked from richard-graham/richard-graham.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.js
107 lines (83 loc) · 2.61 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
document.addEventListener('DOMContentLoaded', startGame);
document.addEventListener('click', checkForWin);
document.addEventListener('contextmenu', checkForWin);
function runAudio() {
let audio = new Audio('./audio/background.wav');
audio.play();
}
function createBoard(row, col) {
var board = {};
board.cells = makeCells(row, col);
//createMines(row, col);
return board;
};
function makeCells(rows, columns) {
var cellIndex = 0;
let cells = []
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
cells[cellIndex] = {row: i, col: j, isMarked: false, hidden: true}
cells[cellIndex].isMine = createMines();
cellIndex++;
}
}
return cells
};
function createMines () {
let randomise = Math.floor((Math.random() * 10) + 1)
if (randomise > 7) {
return true;
} else {
return false;
}
};
var board = createBoard(5, 5);
function startGame () {
// Don't remove this function call: it makes the game work!
for (i = 0; i < board.cells.length; i++) {
board.cells[i].surroundingMines = countSurroundingMines(board.cells[i]);
//console.log(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?
function checkForWin () {
let counter = 0;
for (i = 0; i < board.cells.length; i++) {
if ((board.cells[i].isMine === false && board.cells[i].hidden === false) || (board.cells[i].isMine === true && board.cells[i].isMarked === true)) {
counter++;
}
}
console.log(counter)
if (counter === board.cells.length) {
lib.displayMessage('You won! But you failed to find Jesus :(.')
}
runAudio();
// 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.
function countSurroundingMines (cell) {
var surroundingCells = lib.getSurroundingCells(cell.row, cell.col);
//console.log(surroundingCells);
var counter = 0;
for (j = 0; j < surroundingCells.length; j++) {
//console.log(surroundingCells[j].isMine);
if (surroundingCells[j].isMine === true) {
counter++;
}
}
//console.log(counter);
return counter;
}