-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
131 lines (111 loc) · 3.5 KB
/
script.js
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
import Chessboard from "chessboardjs";
import { Chess } from "./chess";
console.log(`
|\___ ___\\ \|\ \|\ __ \|\ __ \|\ ____\|\ \|\ \|\ __ \|\ _ \ _ \|\ __ \
\|___ \ \_\ \ \\\ \ \ \|\ \ \ \|\ \ \ \___|\ \ \\\ \ \ \|\ \ \ \\\__\ \ \ \ \|\ \
\ \ \ \ \ \\\ \ \ _ _\ \ \\\ \ \ \ \ \ __ \ \ __ \ \ \\|__| \ \ \ ____\
\ \ \ \ \ \\\ \ \ \\ \\ \ \\\ \ \ \____\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \___|
\ \__\ \ \_______\ \__\\ _\\ \_______\ \_______\ \__\ \__\ \__\ \__\ \__\ \ \__\ \__\
\|__| \|_______|\|__|\|__|\|_______|\|_______|\|__|\|__|\|__|\|__|\|__| \|__|\|__|
`);
const board = Chessboard("board1", {
draggable: true,
position: "start",
onDragStart: onDragStart,
onDrop: onDrop,
onSnapEnd: onSnapEnd,
pieceTheme: '/{piece}.png',
});
board.flip();
board.start();
const game = new Chess();
const worker = new Worker('./worker.js');
let isWhite = true;
const thinking = document.querySelector('.thinking');
const scorecard = document.querySelector('.scorecard');
const statusDisplay = document.querySelector('.status');
let moveNumber = 1;
let newdiv = document.createElement('div');
newdiv.id = "move-1";
scorecard.append(newdiv);
let moveString = moveNumber + ". ";
game.header('White', 'Turochamp', 'Black', 'Player');
document.getElementById('download-pgn').addEventListener('click', downloadPGN);
function downloadPGN() {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(game.pgn()));
element.setAttribute('download', 'turochamp-game-'+Date.now()+'.pgn');
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function onDragStart(source, piece, position, orientation) {
if(isWhite) {return false;}
if (game.game_over()) return false;
if (
(game.turn() === "w") ||
(game.turn() === "b" && piece.search(/^w/) !== -1)
) {
return false;
}
}
function onDrop(source, target) {
// see if the move is legal
var move = game.move({
from: source,
to: target,
promotion: "q",
});
// illegal move
if (move === null) return "snapback";
moveString += " " + move.san;
document.getElementById('move-'+moveNumber).innerHTML = moveString;
moveNumber++;
moveString = moveNumber + ". ";
newdiv = document.createElement('div');
newdiv.id = "move-"+moveNumber;
scorecard.append(newdiv);
updateStatus(move);
}
function updateStatus(move) {
var status = "";
var moveColor = "White";
if (game.turn() === "b") {
moveColor = "Black";
}
if (game.in_checkmate()) {
status = moveColor + " is checkmated.";
}
else if (game.in_draw()) {
status = "Game over, drawn position";
}
else {
status = moveColor + " to move";
if (game.in_check()) {
status += ". Check";
}
}
statusDisplay.innerHTML = status;
if(!game.game_over()){
worker.postMessage(game.fen());
isWhite = true;
thinking.innerHTML = " is thinking ...";
worker.onmessage = e =>{
const nextMove = JSON.parse(e.data);
if(nextMove != null){
game.move(nextMove);
board.position(game.fen());
moveString += " " + nextMove.san;
document.getElementById('move-'+moveNumber).innerHTML = moveString;
updateStatus(nextMove);
}
isWhite = false;
thinking.innerHTML = "";
}
}
}
function onSnapEnd () {
board.position(game.fen())
}
updateStatus();