-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo.html
152 lines (137 loc) · 4.93 KB
/
demo.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
<!DOCTYPE html>
<html lang="en-GB">
<head>
<meta charset="utf-8" />
<style>
* {
box-sizing: border-box;
}
body {
min-height: 100vh;
max-width: 100vw;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background: #fafafa;
}
</style>
</head>
<body>
<h1>Minesweeper</h1>
<p>
Just play
<a
href="https://www.instructables.com/id/How-to-beat-Minesweeper/"
target="_blank"
title="Explanation"
>minesweeper</a
>. To place a flag press alt while clicking on a field.
</p>
<select name="select-game-mode" id="select-game-mode">
<option value="easy" selected>Easy - 9x9 / 10 Mines</option>
<option value="normal">Normal - 16x16 / 40 Mines</option>
<option value="hard">Hard - 16x30 / 99 Mines</option>
</select>
<p><span id="bomb-counter"></span> Mines</p>
<minesweeper-game
id="minesweeper"
restart-selector="#restart-game-button"
bomb-counter-selector="#bomb-counter"
></minesweeper-game>
<p>
<button id="toggle-flag-mode">Toggle Flag Mode</button>
<button id="restart-game-button">Restart!</button>
<button id="restart-game-button-confirm">Restart with Confirm!</button>
</p>
<h2>Disabled Long Press</h2>
<minesweeper-game rows="5" columns="5" bombs="5" long-press-threshold="0"></minesweeper-game>
<h2>Disabled Question Marks</h2>
<minesweeper-game rows="5" columns="5" bombs="5" disable-question-marks></minesweeper-game>
<h2>Custom tag name</h2>
<custom-minesweeper-game></custom-minesweeper-game>
<script type="module">
import './dist/custom-element.js';
// Replace with: import 'minesweeper-for-web';
// Alternative:
// import 'minesweeper-for-web/custom-element';
// import 'minesweeper-for-web/custom-element.min';
import { MinesweeperGame } from './dist/minesweeper-game.js';
// Replace with: import { MinesweeperGame } from 'minesweeper-for-web/minesweeper-game';
// Alternative:
// import 'minesweeper-for-web/minesweeper-game.min';
window.customElements.define('custom-minesweeper-game', MinesweeperGame);
function getGameModeConfiguration(currentGameMode) {
switch (currentGameMode) {
case 'hard':
return {
columns: 30,
rows: 16,
bombs: 99,
};
case 'normal':
return {
columns: 16,
rows: 16,
bombs: 40,
};
default: // 'easy'
return {
columns: 9,
rows: 9,
bombs: 10,
};
}
}
window.addEventListener('DOMContentLoaded', () => {
/** @type {MinesweeperGame} **/
const minesweeper = document.querySelector('#minesweeper');
minesweeper.addEventListener('minesweeper:game-won', (event) => {
console.log('game-won', event);
// eslint-disable-next-line no-alert
window.alert('You won!');
});
minesweeper.addEventListener('minesweeper:game-lost', (event) => {
console.log('game-lost', event);
// eslint-disable-next-line no-alert
window.alert('You lost!');
});
minesweeper.addEventListener('minesweeper:field-click', (event) => {
console.log('field-click', event);
});
minesweeper.addEventListener('minesweeper:field-long-press', (event) => {
console.log('field-long-press', event);
});
minesweeper.addEventListener('minesweeper:field-interaction', (event) => {
console.log('field-interaction', event);
});
document.querySelector('#toggle-flag-mode').addEventListener('click', (event) => {
event.preventDefault();
console.log('minesweeper', { minesweeper });
console.log('current flag placement mode:', minesweeper.flagPlacementMode);
if (minesweeper.flagPlacementMode) {
minesweeper.removeAttribute('flag-placement-mode');
} else {
minesweeper.setAttribute('flag-placement-mode', '');
}
});
document
.querySelector('#restart-game-button-confirm')
.addEventListener('click', (event) => {
event.preventDefault();
if (
// eslint-disable-next-line no-alert
window.confirm('Are you sure, that you want to restart the game?')
) {
minesweeper.restartGame();
}
});
document.querySelector('#select-game-mode').addEventListener('change', (event) => {
event.preventDefault();
const gameModeConfiguration = getGameModeConfiguration(event.target.value);
minesweeper.setGameModeConfiguration(gameModeConfiguration);
});
});
</script>
</body>
</html>