-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.html
107 lines (91 loc) · 2.78 KB
/
minesweeper.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Minesweeper!</title>
<style>
html, body {
background-color: #444;
margin: 5px;
padding: 0px;
color: white;
font-family: monospace;
}
#grid {
display: flex;
flex-direction: column;
}
#grid > div.row {
display: flex;
flex-direction: row;
}
#grid > div.row > div {
cursor: default;
margin: 0px;
width: 25px;
height: 25px;
text-align: center;
border: 1px solid #666;
}
#grid > div.row > div.covered {
cursor: pointer;
background-color: #111;
}
#grid > div.row > div.mined {
background-color: red;
}
#grid > div.row > div.flagged {
background-color: blue;
}
</style>
</head>
<body>
<div>
On Click: <i id="action">uncover</i> (toggle via spacebar) <button id="start-solver">Start Solver</button>
</div>
<div id="grid">
</div>
<script src="minefield.js"></script>
<script src="minesweeper-solver.js"></script>
<script src="sudoku.js"></script>
<script>
const $field = document.querySelector('#grid')
const $action = document.querySelector('#action')
const field = new Minefield($field, 25, 40, 0.15)
field.initGame()
const solver = new MinesweeperSolver(field)
let action = 'uncover';
$field.addEventListener('click', event => {
let i = Number.parseInt(event.target.dataset.i),
j = Number.parseInt(event.target.dataset.j)
if (Number.isNaN(i) || Number.isNaN(j))
return
let cell = field.grid[i][j]
if (action == 'uncover')
field.uncover(cell)
else if (action == 'flag')
field.flag(cell)
else if (action == 'debug')
console.log(cell)
})
document.addEventListener('keydown', event => {
if (event.key == ' ') {
if (action == 'uncover')
action = 'flag'
else if (action == 'flag')
action = 'debug'
else if (action == 'debug')
action = 'uncover'
$action.innerText = action
}
})
document.querySelector('#start-solver').addEventListener('click', event => {
setInterval(() => {
let changes = solver.step(true)
if (changes == 0)
event.target.innerText = 'Solver Stuck :('
}, 150)
})
</script>
</body>
</html>