-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.js
194 lines (169 loc) · 4.31 KB
/
chess.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
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
let W = 300;
let H = 500;
const PADDING = 5;
const PADDING_TOP = 60 + PADDING;
const PADDING_BOTTOM = PADDING;
let CELL_SIZE = 20; //px
const COL_CELLS = 8; // count
const ROW_CELLS = 8; // count
const COL_CELLS_AND_BOUNDARY = COL_CELLS;
const ROW_CELLS_AND_BOUNDARY = ROW_CELLS;
let verbose = 0; // 0 - none, 1 - default log, 2 - moves logs
let cnv;
let startScreen;
let game;
let buttons = {};
let useStartScreen = false;
let isGameFinished = false;
let imgSprite;
let imgFigures = [];
let fen_hash = window.location.hash.substring(1).replace(/%20/g, " ");
let computerName;
function createTButton(title, name) {
const newButton = createButton(title, name);
newButton.style("display", "none");
buttons[name] = newButton;
}
function button(name) {
return buttons[name];
}
function enableButton(name, x, y, callback) {
const btn = buttons[name];
btn.position(x, y);
btn.style("display", "block");
btn.mousePressed(() => {
callback.apply();
return false;
});
}
function disableButton(name) {
const btn = buttons[name];
btn.style("display", "none");
btn.mousePressed(() => {});
}
function preload() {
imgSprite = loadImage("images/Chess_Pieces_Sprite-large.png");
}
function resizeIfNeeded() {
W = windowWidth;
H = windowHeight;
let cellSizeW = Math.floor((W - 2 * PADDING) / COL_CELLS_AND_BOUNDARY);
let cellSizeH = Math.floor(
(H - 2 * PADDING - PADDING_BOTTOM - PADDING_TOP) / ROW_CELLS_AND_BOUNDARY
);
if (cellSizeW > cellSizeH) {
W = windowWidth;
H = windowHeight - 150;
cellSizeW = Math.floor((W - 2 * PADDING) / COL_CELLS_AND_BOUNDARY);
cellSizeH = Math.floor(
(H - 2 * PADDING - PADDING_BOTTOM - PADDING_TOP) / ROW_CELLS_AND_BOUNDARY
);
}
CELL_SIZE = Math.min(cellSizeH, cellSizeW);
W = CELL_SIZE * ROW_CELLS + 2 * PADDING;
H = CELL_SIZE * ROW_CELLS + 2 * PADDING + PADDING_BOTTOM + PADDING_TOP;
resizeCanvas(W, H);
}
function resizeFinalize() {
game = new Game(
CELL_SIZE * COL_CELLS_AND_BOUNDARY,
CELL_SIZE * ROW_CELLS_AND_BOUNDARY,
PADDING,
PADDING_TOP,
PADDING_BOTTOM,
fen_hash
);
}
function reset() {
useStartScreen = false;
isGameFinished = false;
}
function clickedInCanvas(event) {
return game.clicked(event.clientY, event.clientX);
}
function undoLastMove() {
game.undoLastMove();
}
function testMoves(callbackStats) {
console.log("Start test move calculation:");
maxDepth = MAX_DEPTH;
for (let depth = 1; depth < maxDepth + 1; depth++) {
const startTime = performance.now();
const numPositions = game.board.data.testMoves(depth);
const diffTime = Math.round(performance.now() - startTime);
console.log(
"Depth: " +
depth +
" ply Result: " +
numPositions +
" Time " +
diffTime +
" ms"
);
callbackStats(depth, numPositions);
}
}
function getElementByValue(tag, value) {
const elems = [].filter.call(
document.getElementsByTagName(tag),
function (input) {
return input.value === value;
}
);
return elems[0];
}
function windowResized() {
fen_hash = window.location.hash.substring(1).replace(/%20/g, " ");
resizeCanvas(windowWidth, windowHeight);
resizeIfNeeded();
resizeFinalize();
}
function uuidv4() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
function setup() {
const piecesInImage = [
Piece.KING,
Piece.QUEEN,
Piece.BISHOP,
Piece.KNIGHT,
Piece.ROOK,
Piece.PAWN,
];
boardSetupStatic();
imgFigures = new Array(12);
const pixels = 45 * 5;
for (let y = 0; y < 2; y++) {
for (let x = 0; x < 6; x++) {
const figure = createImage(pixels, pixels);
// The arguments are: source image, source x, source y, source width, source height, dest x, dest y, dest width, dest height
figure.copy(
imgSprite,
pixels * x,
pixels * y,
pixels,
pixels,
0,
0,
pixels,
pixels
);
const index = 6 * y + piecesInImage[x] - 1;
imgFigures[index] = figure;
}
}
resizeIfNeeded();
cnv = createCanvas(W, H);
cnv.mouseClicked(clickedInCanvas);
resizeFinalize();
}
function draw() {
background("black");
game.draw();
fill("red");
rect(W, H);
}