-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlchesslines.html
More file actions
621 lines (512 loc) · 16.7 KB
/
Copy pathhtmlchesslines.html
File metadata and controls
621 lines (512 loc) · 16.7 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="description"
content="An html file that can beat you at chess"
/>
<meta name="keywords" content="Chess, HTML, JavaScript, Stockfish" />
<meta name="author" content="David Ryan" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>htmlchess</title>
<!-- load libraries-->
<!-- load jquery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- load chessboard.js library source code-->
<script
src="https://unpkg.com/@chrisoakman/chessboardjs@1.0.0/dist/chessboard-1.0.0.min.js"
integrity="sha384-8Vi8VHwn3vjQ9eUHUxex3JSN/NFqUg3QbPyX8kWyb93+8AC/pPWTzj+nHtbC5bxD"
crossorigin="anonymous"
></script>
<!-- load chess.js library-->
<script src="https://cdn.jsdelivr.net/npm/chess.js@0.11.0/chess.min.js"></script>
<!-- load stockfish library-->
<script src="https://cdn.jsdelivr.net/npm/stockfish@11.0.0/src/stockfish.asm.min.js"></script>
<style>
/* Row column html styling */
.column {
float: left;
width: 33.33%;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
.button {
display: inline-block;
padding: 10px 20px;
font-size: 15px;
cursor: pointer;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: #33694c;
border: none;
border-radius: 15px;
box-shadow: 0 9px #999;
}
.button:hover {
background-color: #4a986e;
}
.button:active {
background-color: #4a986e;
box-shadow: 0 5px #666;
transform: translateY(4px);
}
.disabled {
opacity: 0.6;
cursor: not-allowed;
}
.clearfix-7da63 {
clear: both;
}
.board-b72b1 {
border: 5px solid #404040;
box-sizing: content-box;
}
.square-55d63 {
float: left;
position: relative;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.white-1e1d7 {
background-color: #eeeeeb;
color: #33694c;
}
.black-3c85d {
background-color: #33694c;
color: #eeeeeb;
}
.highlight1-32417,
.highlight2-9c5d2 {
box-shadow: inset 0 0 3px 3px #ff0;
}
.notation-322f9 {
cursor: default;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
position: absolute;
}
.alpha-d2270 {
bottom: 1px;
right: 3px;
}
.numeric-fc462 {
top: 2px;
left: 2px;
}
.highlight-white {
box-shadow: inset 0 0 3px 3px yellow;
}
.highlight-black {
box-shadow: inset 0 0 3px 3px blue;
}
#myBoard {
width: 500px;
margin: auto;
}
#log-container {
overflow: auto;
height: 150px;
}
.log-warn {
color: orange;
}
.log-error {
color: red;
}
.log-info {
color: skyblue;
}
.log-log {
color: silver;
}
.log-warn,
.log-error {
font-weight: bold;
}
</style>
</head>
<body>
<h1>htmlchess</h1>
<div class="row">
<div class="column">
<button class="button" id="setBtn">Set Board</button>
<button class="button" id="clearBtn">Clear Board</button>
<button class="button" id="startBtn">Start Game</button>
<button class="button" id="flipOrientationBtn">Flip orientation</button>
<br />
<br />
<button class="button" id="goBackBtn">Back</button>
<button class="button disabled" id="goFowardBtn">Forward</button>
<br />
<br />
<br />
<p id="gameEnd"></p>
<p id="output1"></p>
<p id="output2"></p>
<p id="output3"></p>
</div>
<div class="column">
<div id="myBoard"></div>
</div>
<div class="column"></div>
</div>
<div id="log-container">
<pre id="log"></pre>
</div>
<script>
// difficulty level
// TODO be able to change mode and level
var mode = "level";
var level = "10";
var board = null;
var $board = $("#myBoard");
var game = new Chess();
var squareClass = "square-55d63";
var squareToHighlight = null;
var colorToHighlight = null;
var whiteSquareGrey = "#a9a9a9";
var blackSquareGrey = "#696969";
var moveList = [];
var index = 0;
var userColor = "white";
var configStart = {
pieceTheme: pieceTheme,
draggable: true,
dropOffBoard: "trash",
sparePieces: true,
onDragStart: onDragStart,
onDrop: onDrop,
onMouseoutSquare: onMouseoutSquare,
onMouseoverSquare: onMouseoverSquare,
//onSnapEnd: onSnapEnd
};
var configFree = {
pieceTheme: pieceTheme,
draggable: true,
dropOffBoard: "trash",
sparePieces: true,
};
function initializePage() {
// TODO initialize page
}
function removeHighlights(color) {
$board.find("." + squareClass).removeClass("highlight-" + color);
}
function addHighlights(move) {
if (game.turn() === "b") {
removeHighlights("white");
$board.find(".square-" + move.from).addClass("highlight-white");
$board.find(".square-" + move.to).addClass("highlight-white");
} else if (game.turn() === "w") {
removeHighlights("black");
$board.find(".square-" + move.from).addClass("highlight-black");
$board.find(".square-" + move.to).addClass("highlight-black");
}
}
function removeGreySquares() {
$("#myBoard .square-55d63").css("background", "");
}
function greySquare(square) {
var $square = $("#myBoard .square-" + square);
var background = whiteSquareGrey;
if ($square.hasClass("black-3c85d")) {
background = blackSquareGrey;
}
$square.css("background", background);
}
function onDragStart(source, piece) {
// do not pick up pieces if the game is over
if (game.game_over()) return false;
// or if it's not that side's turn
if (
(game.turn() === "w" && piece.search(/^b/) !== -1) ||
(game.turn() === "b" && piece.search(/^w/) !== -1)
) {
return false;
}
}
function onDrop(source, target) {
removeGreySquares();
// see if the move is legal
var move = game.move({
from: source,
to: target,
promotion: "q", // NOTE: always promote to a queen for example simplicity
});
// illegal move
if (move === null) return "snapback";
addHighlights(move);
checkGameEnd();
document.getElementById("output2").innerHTML = game.fen();
moveList.push(game.fen());
index++;
stockfishMove();
}
function onMouseoverSquare(square, piece) {
// get list of possible moves for this square
var moves = game.moves({
square: square,
verbose: true,
});
// exit if there are no moves available for this square
if (moves.length === 0) return;
// highlight the square they moused over
greySquare(square);
// highlight the possible squares for this piece
for (var i = 0; i < moves.length; i++) {
greySquare(moves[i].to);
}
}
function onMouseoutSquare(square, piece) {
removeGreySquares();
}
function onSnapEnd() {
board.position(game.fen());
}
function onMoveEnd() {
$board
.find(".square-" + squareToHighlight)
.addClass("highlight-" + colorToHighlight);
}
function clearBoard() {
removeHighlights("white");
removeHighlights("black");
board.destroy();
board = Chessboard("myBoard", configFree);
game = null;
}
function setBoard() {
clearBoard();
board.start();
}
function startGame() {
game = new Chess();
game.load(board.fen() + " w KQkq - 0 1");
board = Chessboard("myBoard", configStart);
board.position(game.fen());
moveList.push(game.fen());
index = 0;
if (userColor == "black") {
stockfishMove();
}
}
function flipBoard() {
board.flip();
if (userColor === "white") {
userColor = "black";
} else userColor = "white";
console.log(userColor);
}
function gameEnd() {
// TODO write game end function
}
// TODO highlight in check square
function checkCheck() {
var check = game.in_check();
}
function takenPieces() {}
function checkGameEnd() {
var winner = "White";
if (game.turn() == "w") {
winner = "Black";
}
if (game.in_checkmate()) {
document.getElementById("gameEnd").innerHTML =
"Game has ended by Checkmate! " + winner + " has won!";
} else if (game.in_draw()) {
document.getElementById("gameEnd").innerHTML =
"Game has ended in a draw!" + winner + " has won!";
} else if (game.in_stalemate()) {
document.getElementById("gameEnd").innerHTML =
"Game has ended in a stalemate!" + winner + " has won!";
} else if (game.in_threefold_repetition()) {
document.getElementById("gameEnd").innerHTML =
"Game has ended by threefold repetition!" + winner + " has won!";
} else if (game.insufficient_material()) {
document.getElementById("gameEnd").innerHTML =
"Game has ended by insufficient material!" + winner + " has won!";
}
gameEnd();
}
function stepFoward() {
// TODO write step forward function
}
function stepBack() {
game.undo();
game.undo();
board.position(game.fen());
// if (index > 1) {
// console.log("current: " + index + " | " + moveList[index])
// index = index - 2
// console.log("back: " + + index + " | " + moveList[index])
// game.load(moveList[index])
// board.position(moveList[index])
// } else {
// // TODO send error message
// }
}
function displayError(error) {
if ((error = 201)) {
console.log(" ");
}
}
function stockfishMove() {
var message = "position fen " + game.fen();
var goMessage = null;
if ((mode = "time")) {
goMessage = "go movetime " + level;
} else if ((mode = "level")) {
goMessage = "go depth " + level;
}
var bestmove = null;
var move = null;
console.log(message);
stockfish.postMessage(message);
stockfish.postMessage(goMessage);
stockfish.onmessage = function (event) {
//NOTE: Web Workers wrap the response in an object.
console.log(event.data ? event.data : event);
// find bestmove substring
if (event.includes("bestmove")) {
bestmove = event.substr(9, 4);
move = game.move(bestmove, { sloppy: true });
addHighlights(move);
board.position(game.fen());
checkGameEnd();
document.getElementById("output2").innerHTML = game.fen();
moveList.push(game.fen());
index++;
} else {
document.getElementById("output1").innerHTML = event;
}
};
}
rewireLoggingToElement(
() => document.getElementById("log"),
() => document.getElementById("log-container"),
true
);
function rewireLoggingToElement(
eleLocator,
eleOverflowLocator,
autoScroll
) {
fixLoggingFunc("log");
fixLoggingFunc("debug");
fixLoggingFunc("warn");
fixLoggingFunc("error");
fixLoggingFunc("info");
function fixLoggingFunc(name) {
console["old" + name] = console[name];
console[name] = function (...arguments) {
const output = produceOutput(name, arguments);
const eleLog = eleLocator();
if (autoScroll) {
const eleContainerLog = eleOverflowLocator();
const isScrolledToBottom =
eleContainerLog.scrollHeight - eleContainerLog.clientHeight <=
eleContainerLog.scrollTop + 1;
eleLog.innerHTML += output + "<br>";
if (isScrolledToBottom) {
eleContainerLog.scrollTop =
eleContainerLog.scrollHeight - eleContainerLog.clientHeight;
}
} else {
eleLog.innerHTML += output + "<br>";
}
console["old" + name].apply(undefined, arguments);
};
}
function produceOutput(name, args) {
return args.reduce((output, arg) => {
return (
output +
'<span class="log-' +
typeof arg +
" log-" +
name +
'">' +
(typeof arg === "object" && (JSON || {}).stringify
? JSON.stringify(arg)
: arg) +
"</span> "
);
}, "");
}
}
function pieceTheme(piece) {
// pull svg piece images from wikipedia
if (piece == "bB") {
return "https://upload.wikimedia.org/wikipedia/commons/9/98/Chess_bdt45.svg";
} else if (piece == "bK") {
return "https://upload.wikimedia.org/wikipedia/commons/f/f0/Chess_kdt45.svg";
} else if (piece == "bN") {
return "https://upload.wikimedia.org/wikipedia/commons/e/ef/Chess_ndt45.svg";
} else if (piece == "bP") {
return "https://upload.wikimedia.org/wikipedia/commons/c/c7/Chess_pdt45.svg";
} else if (piece == "bQ") {
return "https://upload.wikimedia.org/wikipedia/commons/4/47/Chess_qdt45.svg";
} else if (piece == "bR") {
return "https://upload.wikimedia.org/wikipedia/commons/f/ff/Chess_rdt45.svg";
} else if (piece == "wB") {
return "https://upload.wikimedia.org/wikipedia/commons/b/b1/Chess_blt45.svg";
} else if (piece == "wK") {
return "https://upload.wikimedia.org/wikipedia/commons/4/42/Chess_klt45.svg";
} else if (piece == "wN") {
return "https://upload.wikimedia.org/wikipedia/commons/7/70/Chess_nlt45.svg";
} else if (piece == "wP") {
return "https://upload.wikimedia.org/wikipedia/commons/4/45/Chess_plt45.svg";
} else if (piece == "wQ") {
return "https://upload.wikimedia.org/wikipedia/commons/1/15/Chess_qlt45.svg";
} else if (piece == "wR") {
return "https://upload.wikimedia.org/wikipedia/commons/7/72/Chess_rlt45.svg";
}
}
var board = Chessboard("myBoard", configFree);
var stockfish = STOCKFISH();
stockfish.onmessage = function (event) {
//NOTE: Web Workers wrap the response in an object.
console.log(event.data ? event.data : event);
// find bestmove substring
if (event.includes("bestmove")) {
bestmove = event.substr(9, 4);
move = game.move(bestmove, { sloppy: true });
addHighlights(move);
board.position(game.fen());
checkGameEnd();
document.getElementById("output2").innerHTML = game.fen();
moveList.push(game.fen());
index++;
} else {
document.getElementById("output1").innerHTML = event;
}
};
// var settings = "setoption name MultiPV value 3"
// console.log(settings)
// stockfish.postMessage(settings)
// settings = "debug on"
// console.log(settings)
// stockfish.postMessage(settings)
// settings = "setoption UCI_ShowWDL"
// console.log(settings)
// stockfish.postMessage(settings)
$("#setBtn").on("click", setBoard);
$("#clearBtn").on("click", clearBoard);
$("#startBtn").on("click", startGame);
$("#goBackBtn").on("click", stepBack);
$("#goFowardBtn").on("click", stepFoward);
$("#flipOrientationBtn").on("click", flipBoard);
</script>
</body>
</html>