-
Notifications
You must be signed in to change notification settings - Fork 1
/
minesweep.html
154 lines (128 loc) · 3.01 KB
/
minesweep.html
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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<style>
* {
box-sizing: border-box;
}
.row {
clear: both;
}
.field {
width: 50px;
height: 50px;
float: left;
border: 1px solid black;
line-height: 50px;
}
.board {
text-align: center;
overflow: hidden;
}
.covered {
background-color: gray;
}
.hidden {
display: none;
}
.failure {
color: red;
}
.info {
margin-top: 10px;
}
#newGame {
width: 85px;
height: 25px;
margin-top: 10px;
display: block;
}
#result {
margin-left: 10px;
display: inline-block;
}
</style>
</head>
<body>
<div class="board">
</div>
<div class="info">
<span>Mines Left: 5</span><span id="result"></span>
<button id="newGame">New Game</button>
</div>
<script>
window.onload = function(){
initBoard();
onClickHandler();
document.getElementById('newGame').addEventListener('click', initBoard);
}
function initBoard(){
var board = document.querySelector(".board");
var row = 7, col = 7, numOfBombs = 5, radomNum, bombFields = [], numbers;
var neighbors = [-9, -8, -7, -1, 1, 7, 8, 9];
var numOfCells = row * col;
numbers = new Array(numOfCells);
numbers.fill(0);
var cells = '';
for(var i = 0; i < row; i++){
cells += '<div class="row">'
for(var j = 0; j < col; j++){
cells += '<div class="field covered"></div>';
}
cells += '</div>';
}
board.innerHTML = cells;
var fields = document.querySelectorAll('.field');
// Random put bombs
for(i = 0; i < numOfBombs; i++){
randomNum = Math.floor(Math.random() * numOfCells);
if(bombFields.indexOf(randomNum) === -1){
bombFields.push(randomNum);
fields[randomNum].innerHTML = '<i class="fa fa-bomb hidden"></i>';
fields[randomNum].dataset.marked = 'mine';
}
else{
i--;
}
}
console.log("bombFields " + bombFields);
// Set numbers
bombFields.forEach(function(field){
neighbors.forEach(function(pos){
var neighbor = field + pos;
console.log("neighbor " + neighbor);
if(neighbor >= 0 && neighbor < numOfCells && bombFields.indexOf(neighbor) === -1){ // Valid field
numbers[neighbor]++;
}
});
});
console.log("numbers " + numbers);
numbers.forEach(function(number, index){
if(number !== 0){
fields[index].innerHTML = '<span class="hidden">'+ number + '</span>';
}
});
}
// Once user clicks on a cell, uncover it.
//
function onClickHandler(){
var board = document.querySelector(".board");
board.addEventListener('click', function(event){
let target = event.target;
if(target.classList.contains('field')){
target.classList.remove('covered');
if( target.children.length > 0){
target.children[0].classList.remove('hidden');
}
if(target.dataset.marked === 'mine'){
let result = document.getElementById('result');
result.innerHTML = 'Failure';
result.classList.add('failure');
}
}
})
}
</script>
</body>
</html>