-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
289 lines (237 loc) · 8.02 KB
/
index.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CRN - Chess Random Ness</title>
<link rel="icon"
href="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'><text x='0' y='14'>♟️</text></svg>">
<link rel="stylesheet" href="https://unpkg.com/@chrisoakman/[email protected]/dist/chessboard-1.0.0.min.css">
<link href="https://fonts.googleapis.com/css2?family=Fira+Code&family=Roboto&family=Press+Start+2P&family=Ubuntu+Mono&display=swap" rel="stylesheet">
<style>
html, body {
align-items: center;
display: flex;
flex-direction: column;
height: 100%;
justify-content: center;
margin: 0;
padding: 0;
}
table th {
font-family: 'Ubuntu Mono', monospace;
text-align: center;
width: min-content;
}
table td {
font-family: 'Press Start 2P', cursive;
text-align: right;
}
th a {
color: black;
font-size: 80%;
}
th, td {
border-bottom: 1px solid;
border-right: 1px solid;
}
th:first-child, td:first-child {
border-left: 1px solid;
}
</style>
</head>
<body>
<form onsubmit="return false">
<button data-on-true="⏸" data-on-false="▶" name="play" title="Play ▶ or Pause ⏸ game">️</button>
<button data-on-true="🙈" data-on-false="🐵" name="show-board" title="Show 🐵 or hide 🙈 the chess board"></button>
<button data-on-true="🔁" data-on-false="🏆" name="stop-on-win"
title="Keep playing 🔁 or stop when a player wins 🏆"></button>
</form>
<div id="myBoard" style="width: min(75vw, 75vh)"></div>
<table>
<thead>
<tr>
<th colspan="2">Wins</th>
<th colspan="4">Draws</th>
<th colspan="3">Games</th>
</tr>
<tr>
<th>♚</th>
<th>♔</th>
<th><a href="https://en.wikipedia.org/wiki/Fifty-move_rule">Fifty-move Rule</a></th>
<th><a href="https://en.wikipedia.org/wiki/Glossary_of_chess#insufficient_material">Insufficient Material</a></th>
<th><a href="https://en.wikipedia.org/wiki/Stalemate">Stalemate</a></th>
<th><a href="https://en.wikipedia.org/wiki/Threefold_repetition">Threefold Repetition</a></th>
<th>Total</th>
<th>Moves</th>
<th>Average</th>
</tr>
</thead>
<tbody>
<tr>
<td class="win-black"></td>
<td class="win-white"></td>
<td class="draw-fifty-move-rule"></td>
<td class="draw-insufficient-material"></td>
<td class="draw-stalemate"></td>
<td class="draw-threefold-repetition"></td>
<td class="games-total"></td>
<td class="games-moves"></td>
<td class="games-average"></td>
</tr>
</tbody>
</table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://unpkg.com/@chrisoakman/[email protected]/dist/chessboard-1.0.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js"></script>
<script>
const config = {
"board-speed": 50,
"play": true,
"show-board": true,
"speed": -1,
"stop-on-win": false,
}
const board = Chessboard('myBoard', {
"appearSpeed": config["board-speed"],
"moveSpeed": config["board-speed"],
"pieceTheme": 'https://cdn.jsdelivr.net/gh/oakmac/chessboardjs/website/img/chesspieces/alpha/{piece}.png',
"position": 'start',
"snapbackSpeed": config["board-speed"],
"snapSpeed": config["board-speed"],
"trashSpeed": config["board-speed"],
})
const elements = {
"games-total": document.querySelector('.games-total'),
"games-moves": document.querySelector('.games-moves'),
"games-average": document.querySelector('.games-average'),
"win-black": document.querySelector('.win-black'),
"win-white": document.querySelector('.win-white'),
"draw-fifty-move-rule": document.querySelector('.draw-fifty-move-rule'),
"draw-insufficient-material": document.querySelector('.draw-insufficient-material'),
"draw-stalemate": document.querySelector('.draw-stalemate'),
"draw-threefold-repetition": document.querySelector('.draw-threefold-repetition'),
}
const chess = new Chess()
const stats = {
draw: {
"fifty-move-rule": 0,
"insufficient-material": 0,
"stalemate": 0,
"threefold-repetition": 0,
},
games: [],
win: {black: 0, white: 0}
}
function handleButtonToggle(event) {
const input = event.target
const value = JSON.parse(input.value)
input.value = ! value
config[input.name] = ! value
if (input.name === 'show-board') {
const board = document.querySelector('#myBoard');
const display = board.style.display
board.style.display = display === 'none'
? 'block'
: 'none'
}
setButtonText(input)
}
function handleGameOver() {
stats.games.push(chess.history().length)
if (chess.in_draw()) {
let reason
if (chess.in_stalemate()) {
reason = 'stalemate'
} else if (chess.in_threefold_repetition()) {
reason = 'threefold-repetition'
} else if (chess.insufficient_material()) {
reason = 'insufficient-material'
} else {
reason = 'fifty-move-rule'
}
stats.draw[reason]++
} else if (chess.in_checkmate()) {
if (chess.turn() === 'b') {
stats.win.white++
} else {
stats.win.black++
}
} else {
throw new Error('Game over, but neither checkmate nor draw')
}
updateStatsTable()
}
function getRandomMove(length) {
let randomMove
/*/ @TODO: Getting randomness from a back-end goes here!
*
* As it would be to load-heavy to call the backend for every move, it should
* return a list of random numbers. [how many? 100 / 500 / 1000 / ?]
* the list will be used in order, once the list is exhausted, a new list
* will be retrieved from the backend.
*
/*/
randomMove = Math.floor(Math.random() * length)
return randomMove;
}
function makeRandomMove() {
// Handle end of game
if (chess.game_over()) {
handleGameOver();
if (config["stop-on-win"] && chess.in_checkmate()) {
board.position(chess.fen())
// @TODO: Stop gameplay, toggle play/pause button
return
}
chess.clear()
chess.reset()
}
// Make a random move
const possibleMoves = chess.moves()
const randomIdx = getRandomMove(possibleMoves.length);
chess.move(possibleMoves[randomIdx])
// Update the board
if (config["show-board"]) {
board.position(chess.fen())
}
// Wait for the next move
let timeout;
if(config.speed === -1) {
timeout = possibleMoves.length
} else if (config["show-board"] === false) {
timeout = 1
} else {
timeout = config.speed;
}
window.setTimeout(makeRandomMove, timeout)
}
function updateStatsTable() {
const moves = stats.games.reduce((a, b) => a + b, 0);
elements['games-total'].innerText = stats.games.length
elements['games-moves'].innerText = moves
elements['games-average'].innerText = Math.round(moves / stats.games.length) || 0
elements['win-black'].innerText = stats.win.black
elements['win-white'].innerText = stats.win.white
elements['draw-fifty-move-rule'].innerText = stats.draw["fifty-move-rule"]
elements['draw-insufficient-material'].innerText = stats.draw["insufficient-material"]
elements['draw-stalemate'].innerText = stats.draw.stalemate
elements['draw-threefold-repetition'].innerText = stats.draw["threefold-repetition"]
console.log(stats)
}
function setButtonText(input) {
input.innerText = input.dataset['on' + input.value.charAt(0).toUpperCase() + input.value.slice(1)]
}
Object.entries(config).forEach(([name, value]) => {
const input = document.querySelector(`[name=${name}]`)
if (input) {
input.value = JSON.stringify(value)
setButtonText(input);
input.addEventListener('click', handleButtonToggle)
}
})
window.addEventListener('resize', board.resize);
updateStatsTable()
makeRandomMove()
</script>
</body>
</html>