-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
555 lines (465 loc) · 16.8 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
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
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>chess</title>
<!-- <link rel="stylesheet" type="text/css" href="chess.css">
-->
<style>
.figure_to_html {
cursor: pointer;
}
</style>
<script src="chess.js"></script>
</head>
<body>
<header>
<div 1class="logo">
<h2 class logo>Chess </h2>
</div>
</header>
<div id="board">
</div>
<div id="info">
</div>
<script>
let map = Array();
let inf = Array();
let move_color = "white";
let move_from_x;
let move_from_y;
let pawn_attack_x; // кординаты битого поля
let pawn_attack_y;
let from_figure;
let to_figure;
let possible_moves;
function init_map() { //starting figure position
// map[x][y] R - white . r - black
map = [ // y=0 ; y=1 y=2 ; y=3 y=4 ; y=5 y=6 ; y=7
["R", "P", " ", " ", " ", " ", "p", "r"], //x=0
["N", "P", " ", " ", " ", " ", "p", "n"], //x =1
["B", "P", " ", " ", " ", " ", "p", "b"], //x =2
["Q", "P", " ", " ", " ", " ", "p", "q"], //x =3
["K", "P", " ", " ", " ", " ", "p", "k"], //x =4
["B", "P", " ", " ", " ", " ", "p", "b"], //x =5
["N", "P", " ", " ", " ", " ", "p", "n"], //x =6
["R", "P", " ", " ", " ", " ", "p", "r"] //x =7 WHITE LEFT - BLACK RIGHT
];
}
function init_inf() {
inf = [ // changes by step
[" ", " ", " ", " ", " ", " ", " ", " "], //
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " ", " "]
];
}
function is_correct_move(sx, sy, dx, dy) {
let figure = map[sx][sy];
if (is_king(figure)) {
return is_correct_king_move(sx, sy, dx, dy);
}
if (is_queen(figure)) {
return is_correct_queen_move(sx, sy, dx, dy);
}
if (is_bishop(figure)) {
return is_correct_bishop_move(sx, sy, dx, dy);
}
if (is_knight(figure)) {
return is_correct_knight_move(sx, sy, dx, dy);
}
if (is_rook(figure)) {
return is_correct_rook_move(sx, sy, dx, dy);
}
if (is_pawn(figure)) {
return is_correct_pawn_move(sx, sy, dx, dy);
}
return false;
}
function is_king(figure) {
return figure.toUpperCase() == "K";
}
function is_queen(figure) {
return figure.toUpperCase() == "Q";
}
function is_bishop(figure) {
return figure.toUpperCase() == "B";
}
function is_knight(figure) {
return figure.toUpperCase() == "N";
}
function is_rook(figure) {
return figure.toUpperCase() == "R";
}
function is_pawn(figure) {
return figure.toUpperCase() == "P";
}
function is_correct_line_move(sx, sy, dx, dy, figure) {
let delta_x = Math.sign(dx - sx);
let delta_y = Math.sign(dy - sy);
// if (dx > sx) delta_x = +1;
// if (dx < sx) delta_x = -1;
// if (dy > sy) delta_y = +1;
// if (dy < sy) delta_y = -1;
if (!is_correct_line_delta(delta_x, delta_y, figure)) {
return false;
}
do { // сразу смещаем потом проверяем
sx += delta_x;
sy += delta_y;
if (sx == dx && sy == dy) {
return true;
}
} while (is_empety(sx, sy))
return false;
}
function is_correct_line_delta(delta_x, delta_y, figure) {
if (is_rook(figure))
return is_corect_rook_delta(delta_x, delta_y);
if (is_bishop(figure))
return is_corect_bishop_delta(delta_x, delta_y);
if (is_queen(figure))
return is_corect_queen_delta(delta_x, delta_y);
return false;
}
function is_corect_rook_delta(delta_x, delta_y) {
return Math.abs(delta_x) + Math.abs(delta_y) ==
1; // delta x == 1 delte y == 0 || delta x == 0 delta y =1
}
function is_corect_bishop_delta(delta_x, delta_y) {
return Math.abs(delta_x) + Math.abs(delta_y) == 2;
}
function is_corect_queen_delta(delta_x, delta_y) {
return true;
}
function is_correct_king_move(sx, sy, dx, dy) {
if (Math.abs(dx - sx) <= 1 && Math.abs(dy - sy) <= 1) {
return true;
}
return false;
}
function is_correct_queen_move(sx, sy, dx, dy) {
return is_correct_line_move(sx, sy, dx, dy, "Q")
}
function is_correct_bishop_move(sx, sy, dx, dy) {
return is_correct_line_move(sx, sy, dx, dy, "B")
}
function is_correct_knight_move(sx, sy, dx, dy) // only check thet can move fore other pozition
{
if (Math.abs(dx - sx) == 1 && Math.abs(dy - sy) == 2)
return true;
if (Math.abs(dx - sx) == 2 && Math.abs(dy - sy) == 1)
return true;
return false;
}
function is_correct_rook_move(sx, sy, dx, dy) {
return is_correct_line_move(sx, sy, dx, dy, "R")
}
function is_correct_pawn_move(sx, sy, dx, dy) {
if (sy < 1 || sy > 6) {
return false;
}
if (get_color(sx, sy) == "white") {
return is_correct_sign_pawn_move(sx, sy, dx, dy, +1);
}
if (get_color(sx, sy) == "black") {
return is_correct_sign_pawn_move(sx, sy, dx, dy, -1);
}
return false;
}
function is_correct_sign_pawn_move(sx, sy, dx, dy, sign) {
if (is_pawn_passant(sx, sy, dx, dy, sign)) {
return true;
}
if (!is_empety(dx, dy)) { // это взятие ?
if (Math.abs(dx - sx) != 1) { // 1 шаг влево вправо
return false;
}
return dy - sy == sign;
}
if (dx != sx) {
return false;
}
if (dy - sy == sign) {
return true;
}
if (dy - sy == sign * 2) { // на две клетки ( 1 [ ход])
if (sy != 1 && sy != 6)
return false;
return is_empety(sx, sy + sign);
}
return false;
}
function is_pawn_passant(sx, sy, dx, dy, sign) {
if (!(dx == pawn_attack_x && dy == pawn_attack_y)) {
return false;
}
if (sign == +1 && sy != 4) {
return false;
}
if (sign == -1 && sy != 3) {
return false;
}
if (dy - sy != sign) {
return false;
}
return (Math.abs(dx - sx) == 1);
}
function is_empety(x, y) {
if (!on_map(x, y)) {
return false;
}
return map[x][y] == " ";
}
function on_map(x, y) {
return (x >= 0 && x <= 7 &&
y >= 0 && y <= 7)
}
function can_move(sx, sy, dx, dy) {
if (!can_move_from(sx, sy)) // figure can step
return false;
if (!can_move_to(dx, dy)) // square free ( or can kill)
return false;
if (!is_correct_move(sx, sy, dx, dy)) {
return false;
};
if (!is_check_after_move(sx, sy, dx, dy)) {
return true;
}
return false
}
function is_check_after_move(sx, sy, dx, dy) {
move_figure(sx, sy, dx, dy);
// 2)найти короля
turn_move(); // Передать ход
let check = is_check(move_color);
turn_move(); // Передать ход назад
back_figure(sx, sy, dx, dy);
//5) вернуть ход
return check;
}
function is_check() {
//1) сделать ход белых
king = find_figure(move_color == "white" ? "k" : "K");
// если ход белых ищем белого короля
for (let x = 0; x <= 7; x++) // 3) Перебрать все черные фигуры
for (let y = 0; y <= 7; y++)
if (get_color(x, y) == move_color)
// 3 если ходы белых перебираем беые фигура
if (is_correct_move(x, y, king.x, king.y))
//4) проверить,может ли фигура сьесть короля
return true;
return false;
}
function is_checkmate() {
if (!is_check(move_color)) return false;
return possible_moves == 0;
}
function is_stalemate() {
if (is_check(move_color)) return false;
return possible_moves == 0;
}
function find_figure(figure) {
for (let x = 0; x <= 7; x++)
for (let y = 0; y <= 7; y++)
if (map[x][y] == figure)
return {
x: x,
y: y
}
return {
x: -1,
y: -1
}
}
function mark_moves_from() {
possible_moves = 0;
init_inf();
for (let sx = 0; sx <= 7; sx++)
for (let sy = 0; sy <= 7; sy++)
for (let dx = 0; dx <= 7; dx++)
for (let dy = 0; dy <= 7; dy++)
if (can_move(sx, sy, dx, dy)) {
inf[sx][sy] = 1;
possible_moves++;
}
}
function mark_moves_to() {
init_inf();
for (let x = 0; x <= 7; x++)
for (let y = 0; y <= 7; y++)
if (can_move(move_from_x, move_from_y, x, y))
inf[x][y] = 2;
}
function can_move_from(x, y) {
if (!on_map(x, y)) {
return false;
}
return get_color(x, y) == move_color;
}
function can_move_to(x, y) {
if (!on_map(x, y)) {
return false;
}
if (map[x][y] == " ")
return true;
return get_color(x, y) != move_color; // white cat goes to black
}
function get_color(x, y) {
let figure = map[x][y];
if (figure == " ")
return "";
return (figure.toUpperCase() == figure) ? "white" : "black";
}
function click_box(x, y) {
if (inf[x][y] == "1")
click_box_from(x, y);
if (inf[x][y] == "2")
click_box_to(x, y);
}
function click_box_from(x, y) {
move_from_x = x;
move_from_y = y; // save corinait of figure
mark_moves_to();
show_map();
}
function move_figure(sx, sy, dx, dy) {
from_figure = map[sx][sy];
to_figure = map[dx][dy];
map[dx][dy] = from_figure;
map[sx][sy] = " "; // deleted old figure
}
function back_figure(sx, sy, dx, dy) {
map[sx][sy] = from_figure;
map[dx][dy] = to_figure
}
function click_box_to(to_x, to_y) {
move_figure(move_from_x, move_from_y, to_x, to_y)
pawn_figure = promote_pawn(from_figure, to_x, to_y);
check_pawn_attack(from_figure, to_x, to_y);
turn_move();
mark_moves_from(); //change figue
show_map();
}
function promote_pawn(from_figure, to_x, to_y) {
if (!is_pawn(from_figure)) {
return " ";
}
if (!(to_y == 7 || to_y == 0)) // дошла к концу
return;
do {
figure = prompt("Select figure to promote: Q R B N ", "Q")
} while (!(is_queen(figure) || is_rook(figure) || is_bishop(figure) || is_knight(figure)));
if (move_color == "white")
figure = figure.toUpperCase();
else
figure = figure.toLowerCase();
map[to_x][to_y] = figure;
}
function check_pawn_attack(from_figure, to_x, to_y) {
if (is_pawn(from_figure)) {
if (to_x == pawn_attack_x && to_y == pawn_attack_y)
if (move_color == "white")
map[to_x][to_y - 1] = " "; // white
else
map[to_x][to_y + 1] = " "; // black
}
pawn_attack_x = -1;
pawn_attack_y = -1;
if (is_pawn(from_figure)) {
if (Math.abs(to_y - move_from_y)) {
pawn_attack_x = move_from_x;
pawn_attack_y = (move_from_y + to_y) / 2;
}
}
}
function turn_move() {
if (move_color == "white")
move_color = "black";
else
move_color = "white";
}
function figure_to_html(figure) {
switch (figure) {
case "K":
return "♔";
case "k":
return "♚";
case "Q":
return "♕";
case "q":
return "♛";
case "R":
return "♖";
case "r":
return "♜";
case "B":
return "♗";
case "b":
return "♝";
case "N":
return "♘";
case "n":
return "♞";
case "P":
return "♙";
case "p":
return "♟";
default:
return " ";
}
}
function show_map() {
html = "<table border = '1' cellpadding = '2' cellspacing='0' >";
for (let y = 7; y >= 0; y--) {
html += "<tr>";
html += "<td> " + y + " </td>";
for (let x = 0; x <= 7; x++) {
if (inf[x][y] == " ")
color = (x + y) % 2 ? "white" : "grey";
else
color = inf[x][y] == "1" ? "#aaffaa" : "#FF69B4";
html += "<td style = 'height: 50px; width: 50px; " +
" background-color : " + color + ";" +
" text-align: center ; " +
"font-size:40px" +
"' onclick = 'click_box(" + x + "," + y + ");'>";
html += figure_to_html(map[x][y]);
html += "</td>";
}
html += "</tr>";
}
html += "<tr>";
html += "<td> </td>";
for (let x = 1; x <= 8; x++) {
html += "<td style = 'text=align: center '>" + x + "</td>";
}
document.getElementById("board").innerHTML = html;
show_info();
}
function show_info() {
let html = "Turns:" + move_color;
turn_move();
if (is_checkmate()) {
html += "CHECKMATE"; //шаъ
} else if (is_stalemate()) {
html += "STALEMATE"; // мат
} else
if (is_check())
html += "CHECK";
turn_move();
document.getElementById("info").innerHTML = html;
}
function start() {
init_map();
mark_moves_from();
show_map();
};
start();
</script>
</body>
</html>