-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChess.html
More file actions
1569 lines (1384 loc) · 53.9 KB
/
Chess.html
File metadata and controls
1569 lines (1384 loc) · 53.9 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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
(function() {
var ua = navigator.userAgent;
var isPhone = /iPhone|iPad|iPod|Android/i.test(ua) || window.innerWidth < 768;
if (isPhone) {
var meta = document.querySelector('meta[name="viewport"]');
if (meta) {
var targetWidth = 780;
var scale = window.innerWidth / targetWidth;
meta.setAttribute('content', 'width=' + targetWidth + ', initial-scale=' + scale + ', maximum-scale=3.0, user-scalable=yes');
}
}
})();
</script>
<meta name="theme-color" content="#0c1016">
<title>Chess — Free Online Chess Game | Board Gaming Hub</title>
<meta name="description" content="Play classic Chess online for free. Single-player vs AI, two-player local, all standard rules including castling and en passant. No signup, mobile-friendly.">
<link rel="canonical" href="https://boardgaminghub.com/Chess.html">
<meta property="og:type" content="website">
<meta property="og:url" content="https://boardgaminghub.com/Chess.html">
<meta property="og:title" content="Chess — Free Online Chess">
<meta property="og:description" content="Free in-browser Chess. Single-player vs AI or 2-player local. No signup.">
<meta property="og:image" content="https://boardgaminghub.com/og-image.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://boardgaminghub.com/og-image.png">
<script type="application/ld+json">
{ "@context": "https://schema.org", "@type": "VideoGame", "name": "Chess",
"description": "The royal game. Two-player or vs AI, with all standard rules.",
"url": "https://boardgaminghub.com/Chess.html", "genre": "Board Game",
"applicationCategory": "Game", "operatingSystem": "Any",
"offers": { "@type": "Offer", "price": "0", "priceCurrency": "USD" } }
</script>
<script type="application/ld+json">{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Board Gaming Hub","item":"https://boardgaminghub.com/"},{"@type":"ListItem","position":2,"name":"Puzzles & Classics","item":"https://boardgaminghub.com/#puzzles"},{"@type":"ListItem","position":3,"name":"Chess","item":"https://boardgaminghub.com/Chess.html"}]}</script>
<script type="application/ld+json">{"@context":"https://schema.org","@type":"FAQPage","mainEntity":[{"@type":"Question","name":"How does each chess piece move?","acceptedAnswer":{"@type":"Answer","text":"King: 1 square in any direction (also castling). Queen: any distance along ranks, files, or diagonals. Rook: any distance along ranks or files. Bishop: any distance along diagonals. Knight: L-shape (2+1), the only piece that jumps over others. Pawn: forward 1 square (or 2 from its starting rank), captures diagonally, promotes on reaching rank 8."}},{"@type":"Question","name":"What is castling and when can I do it?","acceptedAnswer":{"@type":"Answer","text":"Castling is a special two-piece king-and-rook move performed in one turn. Conditions: the king and the chosen rook have not yet moved; no pieces sit between them; the king is not currently in check; and the king does not pass through or land on a square attacked by an enemy piece. Kingside castling places the king on g-file, rook on f-file. Queenside places the king on c-file, rook on d-file."}},{"@type":"Question","name":"What is en passant?","acceptedAnswer":{"@type":"Answer","text":"When an enemy pawn advances two squares from its starting rank and ends beside one of your pawns, you may capture it on your very next move as if it had moved only one square. The capturing pawn moves diagonally to the square the enemy pawn passed over. Must be played on the immediate next turn or the right is lost."}},{"@type":"Question","name":"What's the difference between checkmate and stalemate?","acceptedAnswer":{"@type":"Answer","text":"Checkmate: the side to move is in check with no legal escape — game ends, checkmating side wins. Stalemate: the side to move is NOT in check but has no legal moves at all — game ends in a draw. Stalemating a winning position is a common way for beginners to throw away material advantage."}},{"@type":"Question","name":"What's a good chess opening for beginners?","acceptedAnswer":{"@type":"Answer","text":"For White, 1.e4 leads to open tactical games — try the Italian Game (1.e4 e5 2.Nf3 Nc6 3.Bc4) or the Ruy Lopez (3.Bb5). For Black against 1.e4, the Caro-Kann (1...c6) and French (1...e6) are solid. Against 1.d4, the Slav (1...d5 2.c4 c6) and King's Indian (1...Nf6 2.c4 g6) are widely played. The principles — control the center, develop minor pieces before queens or rooks, castle early, connect rooks — matter more than memorized lines below ~1500 Elo."}},{"@type":"Question","name":"What is the 50-move rule?","acceptedAnswer":{"@type":"Answer","text":"If 50 consecutive moves are made by both sides with no capture and no pawn move, either player may claim a draw. The counter resets on every capture or pawn push. The threefold-repetition rule is similar: if the same position (with the same player to move and same castling/en-passant rights) occurs three times, either side may claim a draw."}}]}</script>
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>♟️</text></svg>">
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Georgia', serif;
background:
radial-gradient(ellipse at top, #2a1a0e 0%, transparent 65%),
radial-gradient(ellipse at bottom, #100a06 0%, transparent 60%),
#1A1410;
color: #D8C8B0;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
min-height: 100vh;
}
h1 {
background: linear-gradient(180deg, #E8C078 0%, #D4A860 50%, #B88840 100%);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
filter: drop-shadow(0 1px 2px rgba(0,0,0,0.6));
}
h1 {
font-size: 1.7em;
color: #D4A860;
margin-bottom: 4px;
text-shadow: 1px 1px 3px rgba(0,0,0,0.5);
letter-spacing: 0.15em;
}
.subtitle {
font-size: 0.8em;
color: #8B7355;
margin-bottom: 14px;
font-style: italic;
}
.mode-selector {
display: flex;
margin-bottom: 14px;
border-radius: 6px;
overflow: hidden;
border: 2px solid #8B7355;
}
.mode-btn {
padding: 6px 18px;
font-size: 0.95em;
font-family: 'Georgia', serif;
cursor: pointer;
border: none;
background: #2A1E14;
color: #8B7355;
transition: background 0.2s, color 0.2s;
}
.mode-btn:not(:last-child) { border-right: 1px solid #8B7355; }
.mode-btn.active { background: #6B4E37; color: #D8C8B0; cursor: default; }
.mode-btn:hover:not(.active) { background: #3A2A1C; color: #D4A860; }
.game-info { text-align: center; margin-bottom: 10px; }
#turn-indicator { font-size: 1.15em; font-weight: bold; margin-bottom: 6px; }
#message { font-size: 1em; color: #D4A860; min-height: 1.4em; margin-bottom: 8px; }
.buttons { margin-bottom: 15px; }
button {
padding: 8px 22px;
font-size: 1em;
font-family: 'Georgia', serif;
cursor: pointer;
margin: 0 5px;
border: 2px solid #8B7355;
border-radius: 6px;
background: #2A1E14;
color: #D8C8B0;
transition: background 0.2s;
}
button:hover:not(:disabled) { background: #4A3828; }
button:disabled { opacity: 0.4; cursor: not-allowed; }
.container {
display: flex;
align-items: flex-start;
gap: 20px;
}
.side-panel {
display: flex;
flex-direction: column;
align-items: center;
min-width: 80px;
padding-top: 20px;
}
.side-panel h3 {
font-size: 0.85em;
margin-bottom: 8px;
color: #8B7355;
}
.captured-area {
display: flex;
flex-wrap: wrap;
gap: 1px;
justify-content: center;
max-width: 110px;
min-height: 64px;
background:
linear-gradient(135deg, rgba(60, 30, 18, 0.55), rgba(30, 18, 10, 0.85)),
repeating-linear-gradient(45deg, transparent 0 4px, rgba(0,0,0,0.08) 4px 6px);
border: 1px solid #4A3828;
border-radius: 4px;
padding: 6px 4px;
box-shadow: inset 0 1px 3px rgba(0,0,0,0.5);
}
.captured-piece {
font-size: 1.25em;
opacity: 0.85;
line-height: 1.0;
filter: drop-shadow(0 1px 1px rgba(0,0,0,0.5));
}
.board-wrapper {
position: relative;
padding: 22px;
}
.coord-top, .coord-bottom {
display: flex;
position: absolute;
left: 22px;
right: 22px;
}
.coord-top { top: 4px; }
.coord-bottom { bottom: 4px; }
.coord-top span, .coord-bottom span {
flex: 1;
text-align: center;
font-size: 0.72em;
color: #B89868;
letter-spacing: 0.1em;
font-weight: 700;
text-transform: uppercase;
}
.coord-left, .coord-right {
display: flex;
flex-direction: column;
position: absolute;
top: 22px;
bottom: 22px;
}
.coord-left { left: 4px; }
.coord-right { right: 4px; }
.coord-left span, .coord-right span {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.72em;
color: #B89868;
font-weight: 700;
}
.board {
display: grid;
grid-template-columns: repeat(8, 64px);
grid-template-rows: repeat(8, 64px);
border: 6px solid;
border-image: linear-gradient(135deg, #6B4E37, #B88840 30%, #6B4E37 60%, #4A3322) 1;
border-radius: 3px;
box-shadow:
0 0 0 1px rgba(0,0,0,0.6),
0 6px 18px rgba(0,0,0,0.55),
inset 0 0 1px rgba(255,220,160,0.18);
}
.cell {
width: 64px;
height: 64px;
display: flex;
align-items: center;
justify-content: center;
position: relative;
cursor: pointer;
font-size: 2.4em;
user-select: none;
-webkit-user-select: none;
transition: filter 0.15s, box-shadow 0.15s;
}
/* Wood-grain board: a base color + faint repeating linear-gradient mimics
the vertical streak of finished hardwood. Both colors and the noise
pattern are paired to the square shade. */
.cell.light {
background:
repeating-linear-gradient(
92deg,
rgba(110, 70, 35, 0.0) 0px,
rgba(110, 70, 35, 0.10) 2px,
rgba(110, 70, 35, 0.0) 4px,
rgba(60, 35, 18, 0.06) 7px,
rgba(110, 70, 35, 0.0) 11px),
linear-gradient(135deg, #D4B68A 0%, #C8A878 50%, #B89860 100%);
}
.cell.dark {
background:
repeating-linear-gradient(
88deg,
rgba(0, 0, 0, 0.0) 0px,
rgba(0, 0, 0, 0.10) 2px,
rgba(0, 0, 0, 0.0) 4px,
rgba(255, 220, 170, 0.04) 7px,
rgba(0, 0, 0, 0.0) 11px),
linear-gradient(135deg, #7A5A3A 0%, #6B4E37 50%, #543C28 100%);
}
.cell:hover:not(.selected) {
filter: brightness(1.12);
}
.cell.selected {
animation: goldenPulse 1.2s ease-in-out infinite;
z-index: 2;
}
.cell.check-highlight {
box-shadow: inset 0 0 18px 2px rgba(220, 50, 50, 0.85);
animation: checkPulse 0.9s ease-in-out infinite;
}
@keyframes checkPulse {
0%, 100% { box-shadow: inset 0 0 18px 2px rgba(220, 50, 50, 0.85); }
50% { box-shadow: inset 0 0 28px 4px rgba(255, 80, 80, 1.0); }
}
/* Last-move highlight: subtle warm overlay + golden inset border. Stays
visible against the wood grain without changing the underlying color. */
.cell.last-move-from::after,
.cell.last-move-to::after {
content: "";
position: absolute;
inset: 0;
pointer-events: none;
background: rgba(212, 168, 96, 0.18);
box-shadow: inset 0 0 0 2px rgba(212, 168, 96, 0.55);
}
@keyframes goldenPulse {
0%, 100% { box-shadow: inset 0 0 12px #D4A860, 0 0 10px #D4A860; }
50% { box-shadow: inset 0 0 22px #F0C880, 0 0 18px #F0C880; }
}
.move-dot {
position: absolute;
width: 18px;
height: 18px;
border-radius: 50%;
background: radial-gradient(circle, rgba(240, 200, 120, 0.7) 0%, rgba(212, 168, 96, 0.35) 70%);
box-shadow: 0 0 6px rgba(240, 200, 120, 0.45);
pointer-events: none;
z-index: 3;
}
.move-dot.capture-ring {
width: 56px;
height: 56px;
background: transparent;
border: 3px solid rgba(240, 200, 120, 0.6);
box-shadow: 0 0 8px rgba(240, 200, 120, 0.4), inset 0 0 8px rgba(240, 200, 120, 0.25);
border-radius: 50%;
}
/* Pieces. Subtle drop-shadow gives lift off the board. White uses a warm
ivory; black has a thin ivory rim so it reads on dark squares. */
.piece-white {
color: #FBF3DC;
text-shadow:
0 1px 0 rgba(0,0,0,0.5),
0 2px 4px rgba(0,0,0,0.55),
0 0 1px rgba(120, 80, 40, 0.4);
}
.piece-black {
color: #15110A;
text-shadow:
-0.6px 0 0.5px rgba(255,248,232,0.55),
0.6px 0 0.5px rgba(255,248,232,0.55),
0 -0.6px 0.5px rgba(255,248,232,0.55),
0 0.6px 0.5px rgba(255,248,232,0.55),
0 2px 4px rgba(0,0,0,0.55);
}
.game-over-overlay {
display: none;
position: fixed;
top: 0; left: 0; right: 0; bottom: 0;
background: rgba(10,8,6,0.75);
z-index: 100;
align-items: center;
justify-content: center;
}
.game-over-overlay.show {
display: flex;
}
.game-over-box {
background: #2A1E14;
border: 3px solid #D4A860;
border-radius: 10px;
padding: 30px 50px;
text-align: center;
box-shadow: 0 0 30px rgba(212,168,96,0.3);
}
.game-over-box h2 {
font-size: 1.6em;
color: #D4A860;
margin-bottom: 10px;
}
.game-over-box p {
font-size: 1.1em;
margin-bottom: 20px;
}
/* Mobile game nav */
</style>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2835365593874464"
crossorigin="anonymous"></script>
</head>
<body>
<style id="siteNav-css">
#siteNav { position: sticky; top: 0; z-index: 99999; background: rgba(16,20,28,0.96); border-bottom: 1px solid #2a3540; font-family: Georgia, "Times New Roman", serif; box-shadow: 0 2px 10px rgba(0,0,0,0.6); color: #d8d0c0; }
#siteNav * { box-sizing: border-box; }
#siteNav .nav-bar { display: flex; justify-content: space-between; align-items: center; padding: 8px 16px; max-width: 1400px; margin: 0 auto; gap: 12px; }
#siteNav .nav-home { color: #d8d0c0; text-decoration: none; font-size: 0.82em; letter-spacing: 2px; padding: 5px 12px; border: 1px solid #3a5060; border-radius: 3px; white-space: nowrap; }
#siteNav .nav-home:hover { background: #1e2838; color: #f0e0b0; }
#siteNav .nav-title { color: #f0d89c; font-size: 0.95em; letter-spacing: 3px; font-weight: bold; text-align: center; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#siteNav .nav-toggle { background: none; border: 1px solid #3a5060; color: #d8d0c0; font-size: 0.85em; letter-spacing: 2px; padding: 5px 12px; border-radius: 3px; cursor: pointer; font-family: inherit; white-space: nowrap; }
#siteNav .nav-toggle:hover { background: #1e2838; color: #f0e0b0; }
#siteNav .nav-menu { display: none; background: #0f141c; border-top: 1px solid #2a4050; padding: 14px 16px; max-width: 1400px; margin: 0 auto; }
#siteNav .nav-menu.open { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
#siteNav .nav-cat { min-width: 0; }
#siteNav .nav-cat-label { color: #8098a8; font-size: 0.72em; letter-spacing: 4px; padding: 4px 0 8px 0; border-bottom: 1px solid #2a3540; margin-bottom: 6px; }
#siteNav .nav-cat a { display: block; padding: 6px 10px; color: #a8b0c0; text-decoration: none; font-size: 0.88em; border-radius: 3px; }
#siteNav .nav-cat a:hover { background: #1e2838; color: #f0e0b0; }
#siteNav .nav-cat a.current { color: #f0d89c; font-weight: bold; background: #1e2838; }
@media (max-width: 640px) {
#siteNav .nav-menu.open { grid-template-columns: 1fr; gap: 14px; }
#siteNav .nav-title { font-size: 0.82em; letter-spacing: 2px; }
#siteNav .nav-home,
#siteNav .nav-toggle { font-size: 0.74em; padding: 4px 8px; }
}
</style>
<div id="siteNav">
<div class="nav-bar">
<a href="index.html" class="nav-home">HOME</a>
<span class="nav-title">Chess</span>
<button class="nav-toggle" onclick="document.querySelector('#siteNav .nav-menu').classList.toggle('open')">GAMES ☰</button>
</div>
<div class="nav-menu">
<div class="nav-cat">
<div class="nav-cat-label">BOARD GAMES</div>
<a href="Agora.html">Agora</a>
<a href="Aresia.html">Aresia</a>
<a href="Backgammon.html">Backgammon</a>
<a href="Bisque.html">Bisque</a>
<a href="Chess.html" class="current">Chess</a>
<a href="Convergence.html">Convergence</a>
<a href="Go.html">Go</a>
<a href="Mancala.html">Mancala</a>
<a href="Odyssey.html">Odyssey</a>
<a href="Othello.html">Othello</a>
<a href="PenteGrammai.html">Pente Grammai</a>
<a href="Senet.html">Senet</a>
<a href="Tidelands.html">Tidelands</a>
<a href="Ur.html">Ur</a>
</div>
<div class="nav-cat">
<div class="nav-cat-label">SIMULATIONS</div>
<a href="Apoapsis.html">Apoapsis</a>
<a href="BiosphereBlue.html">Biosphere Blue</a>
<a href="BonnevilleSpillwayOperator.html">Bonneville Spillway</a>
<a href="Cliffwalkers.html">Cliffwalkers</a>
<a href="Doctrine.html">Doctrine</a>
<a href="EclipsePredictor.html">Eclipse Predictor</a>
<a href="Floodline.html">Floodline</a>
<a href="Metropolis2K.html">Metropolis 2K</a>
<a href="Tower.html">Tower</a>
</div>
</div>
</div>
<h1>CHESS</h1>
<div class="subtitle">The Royal Game</div>
<div class="mode-selector">
<button class="mode-btn active" id="btn-1p" onclick="setMode(1)">1 Player</button>
<button class="mode-btn" id="btn-2p" onclick="setMode(2)">2 Players</button>
</div>
<div class="game-info">
<div id="turn-indicator">White's Turn</div>
<div id="message"></div>
</div>
<div class="buttons">
<button onclick="resetGame()">New Game</button>
</div>
<div class="container">
<div class="side-panel">
<h3 id="label-black">Black (AI)</h3>
<div class="captured-area" id="captured-white"></div>
</div>
<div class="board-wrapper">
<div class="coord-top" id="coord-top"></div>
<div class="coord-bottom" id="coord-bottom"></div>
<div class="coord-left" id="coord-left"></div>
<div class="coord-right" id="coord-right"></div>
<div class="board" id="board"></div>
</div>
<div class="side-panel">
<h3>White (P1)</h3>
<div class="captured-area" id="captured-black"></div>
</div>
</div>
<div class="game-over-overlay" id="game-over-overlay">
<div class="game-over-box">
<h2 id="game-over-title">Game Over</h2>
<p id="game-over-text"></p>
<button onclick="resetGame()">New Game</button>
</div>
</div>
<script>
(function() {
/* ===== CONSTANTS ===== */
var W = 'w', B = 'b';
var PAWN = 'P', KNIGHT = 'N', BISHOP = 'B', ROOK = 'R', QUEEN = 'Q', KING = 'K';
var PIECE_UNICODE = {};
PIECE_UNICODE['wK'] = '\u2654';
PIECE_UNICODE['wQ'] = '\u2655';
PIECE_UNICODE['wR'] = '\u2656';
PIECE_UNICODE['wB'] = '\u2657';
PIECE_UNICODE['wN'] = '\u2658';
PIECE_UNICODE['wP'] = '\u2659';
PIECE_UNICODE['bK'] = '\u265A';
PIECE_UNICODE['bQ'] = '\u265B';
PIECE_UNICODE['bR'] = '\u265C';
PIECE_UNICODE['bB'] = '\u265D';
PIECE_UNICODE['bN'] = '\u265E';
PIECE_UNICODE['bP'] = '\u265F';
var PIECE_VALUES = {};
PIECE_VALUES[PAWN] = 100;
PIECE_VALUES[KNIGHT] = 320;
PIECE_VALUES[BISHOP] = 330;
PIECE_VALUES[ROOK] = 500;
PIECE_VALUES[QUEEN] = 900;
PIECE_VALUES[KING] = 20000;
/* Piece-square tables (from white's perspective; flip for black) */
var PST = {};
PST[PAWN] = [
0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, -5,-10, 0, 0,-10, -5, 5,
5, 10, 10,-20,-20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
];
PST[KNIGHT] = [
-50,-40,-30,-30,-30,-30,-40,-50,
-40,-20, 0, 0, 0, 0,-20,-40,
-30, 0, 10, 15, 15, 10, 0,-30,
-30, 5, 15, 20, 20, 15, 5,-30,
-30, 0, 15, 20, 20, 15, 0,-30,
-30, 5, 10, 15, 15, 10, 5,-30,
-40,-20, 0, 5, 5, 0,-20,-40,
-50,-40,-30,-30,-30,-30,-40,-50
];
PST[BISHOP] = [
-20,-10,-10,-10,-10,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 10, 10, 10, 10, 0,-10,
-10, 5, 5, 10, 10, 5, 5,-10,
-10, 0, 10, 10, 10, 10, 0,-10,
-10, 10, 10, 10, 10, 10, 10,-10,
-10, 5, 0, 0, 0, 0, 5,-10,
-20,-10,-10,-10,-10,-10,-10,-20
];
PST[ROOK] = [
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, 10, 10, 10, 10, 5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
0, 0, 0, 5, 5, 0, 0, 0
];
PST[QUEEN] = [
-20,-10,-10, -5, -5,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 5, 5, 5, 0,-10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0,-10,
-10, 0, 5, 0, 0, 0, 0,-10,
-20,-10,-10, -5, -5,-10,-10,-20
];
PST[KING] = [
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-20,-30,-30,-40,-40,-30,-30,-20,
-10,-20,-20,-20,-20,-20,-20,-10,
20, 20, 0, 0, 0, 0, 20, 20,
20, 30, 10, 0, 0, 10, 30, 20
];
var FILES = ['a','b','c','d','e','f','g','h'];
/* ===== GAME STATE ===== */
var board; /* 8x8 array, board[row][col], row 0 = rank 8 (top), row 7 = rank 1 (bottom) */
var turn; /* 'w' or 'b' */
var phase; /* 'play', 'gameover' */
var mode; /* 1 or 2 */
var selectedSq; /* {r, c} or null */
var validMoves; /* array of {r, c, special} for selected piece */
var capturedWhite; /* pieces captured from white */
var capturedBlack; /* pieces captured from black */
var castleRights; /* {wK: bool, wQ: bool, bK: bool, bQ: bool} */
var enPassantTarget; /* {r, c} or null */
var lastMove; /* {fromR, fromC, toR, toC} or null */
var aiTimeout;
var inCheck;
var hasCastled; /* {w: bool, b: bool} */
/* ===== INITIALIZATION ===== */
function initBoard() {
board = [];
var backRank = [ROOK, KNIGHT, BISHOP, QUEEN, KING, BISHOP, KNIGHT, ROOK];
var r, c;
for (r = 0; r < 8; r++) {
board[r] = [];
for (c = 0; c < 8; c++) {
board[r][c] = null;
}
}
/* Black pieces (top) */
for (c = 0; c < 8; c++) {
board[0][c] = { color: B, type: backRank[c] };
board[1][c] = { color: B, type: PAWN };
}
/* White pieces (bottom) */
for (c = 0; c < 8; c++) {
board[7][c] = { color: W, type: backRank[c] };
board[6][c] = { color: W, type: PAWN };
}
}
function resetGame() {
if (aiTimeout) { clearTimeout(aiTimeout); aiTimeout = null; }
initBoard();
turn = W;
phase = 'play';
selectedSq = null;
validMoves = [];
capturedWhite = [];
capturedBlack = [];
castleRights = { wK: true, wQ: true, bK: true, bQ: true };
enPassantTarget = null;
lastMove = null;
inCheck = false;
hasCastled = { w: false, b: false };
setMessage('');
document.getElementById('game-over-overlay').className = 'game-over-overlay';
render();
}
/* ===== MODE ===== */
function setMode(m) {
mode = m;
document.getElementById('btn-1p').className = m === 1 ? 'mode-btn active' : 'mode-btn';
document.getElementById('btn-2p').className = m === 2 ? 'mode-btn active' : 'mode-btn';
document.getElementById('label-black').textContent = m === 1 ? 'Black (AI)' : 'Black (P2)';
resetGame();
}
/* ===== UTILITY ===== */
function setMessage(msg) {
document.getElementById('message').textContent = msg;
}
function inBounds(r, c) {
return r >= 0 && r < 8 && c >= 0 && c < 8;
}
function opponent(color) {
return color === W ? B : W;
}
function copyBoard(b) {
var nb = [];
for (var r = 0; r < 8; r++) {
nb[r] = [];
for (var c = 0; c < 8; c++) {
nb[r][c] = b[r][c] ? { color: b[r][c].color, type: b[r][c].type } : null;
}
}
return nb;
}
/* ===== FIND KING ===== */
function findKing(b, color) {
for (var r = 0; r < 8; r++) {
for (var c = 0; c < 8; c++) {
if (b[r][c] && b[r][c].color === color && b[r][c].type === KING) {
return { r: r, c: c };
}
}
}
return null;
}
/* ===== ATTACK DETECTION ===== */
function isSquareAttacked(b, r, c, byColor) {
var dr, dc, nr, nc, p, i;
/* Knight attacks */
var knightMoves = [[-2,-1],[-2,1],[-1,-2],[-1,2],[1,-2],[1,2],[2,-1],[2,1]];
for (i = 0; i < knightMoves.length; i++) {
nr = r + knightMoves[i][0];
nc = c + knightMoves[i][1];
if (inBounds(nr, nc) && b[nr][nc] && b[nr][nc].color === byColor && b[nr][nc].type === KNIGHT) {
return true;
}
}
/* Pawn attacks */
var pawnDir = byColor === W ? 1 : -1; /* direction pawns of byColor attack FROM */
/* A pawn at (r+pawnDir, c+-1) attacks square (r,c) */
if (inBounds(r + pawnDir, c - 1)) {
p = b[r + pawnDir][c - 1];
if (p && p.color === byColor && p.type === PAWN) return true;
}
if (inBounds(r + pawnDir, c + 1)) {
p = b[r + pawnDir][c + 1];
if (p && p.color === byColor && p.type === PAWN) return true;
}
/* King attacks */
for (dr = -1; dr <= 1; dr++) {
for (dc = -1; dc <= 1; dc++) {
if (dr === 0 && dc === 0) continue;
nr = r + dr; nc = c + dc;
if (inBounds(nr, nc) && b[nr][nc] && b[nr][nc].color === byColor && b[nr][nc].type === KING) {
return true;
}
}
}
/* Sliding: rook/queen (straight) */
var straights = [[0,1],[0,-1],[1,0],[-1,0]];
for (i = 0; i < straights.length; i++) {
dr = straights[i][0]; dc = straights[i][1];
nr = r + dr; nc = c + dc;
while (inBounds(nr, nc)) {
if (b[nr][nc]) {
if (b[nr][nc].color === byColor && (b[nr][nc].type === ROOK || b[nr][nc].type === QUEEN)) return true;
break;
}
nr += dr; nc += dc;
}
}
/* Sliding: bishop/queen (diagonal) */
var diags = [[1,1],[1,-1],[-1,1],[-1,-1]];
for (i = 0; i < diags.length; i++) {
dr = diags[i][0]; dc = diags[i][1];
nr = r + dr; nc = c + dc;
while (inBounds(nr, nc)) {
if (b[nr][nc]) {
if (b[nr][nc].color === byColor && (b[nr][nc].type === BISHOP || b[nr][nc].type === QUEEN)) return true;
break;
}
nr += dr; nc += dc;
}
}
return false;
}
function isInCheck(b, color) {
var king = findKing(b, color);
if (!king) return false;
return isSquareAttacked(b, king.r, king.c, opponent(color));
}
/* ===== PSEUDO-LEGAL MOVE GENERATION ===== */
function getPseudoMoves(b, r, c, cRights, epTarget) {
var piece = b[r][c];
if (!piece) return [];
var moves = [];
var color = piece.color;
var type = piece.type;
var nr, nc, dr, dc, i, target;
if (type === PAWN) {
var dir = color === W ? -1 : 1;
var startRow = color === W ? 6 : 1;
var promoRow = color === W ? 0 : 7;
/* Forward 1 */
nr = r + dir;
if (inBounds(nr, c) && !b[nr][c]) {
if (nr === promoRow) {
moves.push({ r: nr, c: c, special: 'promote' });
} else {
moves.push({ r: nr, c: c });
}
/* Forward 2 from start */
if (r === startRow) {
nr = r + dir * 2;
if (!b[nr][c]) {
moves.push({ r: nr, c: c, special: 'pawnDouble' });
}
}
}
/* Diagonal captures */
var capCols = [c - 1, c + 1];
for (i = 0; i < capCols.length; i++) {
nc = capCols[i];
nr = r + dir;
if (!inBounds(nr, nc)) continue;
target = b[nr][nc];
if (target && target.color !== color) {
if (nr === promoRow) {
moves.push({ r: nr, c: nc, special: 'promote' });
} else {
moves.push({ r: nr, c: nc });
}
}
/* En passant */
if (epTarget && epTarget.r === nr && epTarget.c === nc) {
moves.push({ r: nr, c: nc, special: 'enpassant' });
}
}
}
if (type === KNIGHT) {
var knightDeltas = [[-2,-1],[-2,1],[-1,-2],[-1,2],[1,-2],[1,2],[2,-1],[2,1]];
for (i = 0; i < knightDeltas.length; i++) {
nr = r + knightDeltas[i][0]; nc = c + knightDeltas[i][1];
if (inBounds(nr, nc) && (!b[nr][nc] || b[nr][nc].color !== color)) {
moves.push({ r: nr, c: nc });
}
}
}
if (type === BISHOP || type === QUEEN) {
var diagDirs = [[1,1],[1,-1],[-1,1],[-1,-1]];
for (i = 0; i < diagDirs.length; i++) {
dr = diagDirs[i][0]; dc = diagDirs[i][1];
nr = r + dr; nc = c + dc;
while (inBounds(nr, nc)) {
if (b[nr][nc]) {
if (b[nr][nc].color !== color) moves.push({ r: nr, c: nc });
break;
}
moves.push({ r: nr, c: nc });
nr += dr; nc += dc;
}
}
}
if (type === ROOK || type === QUEEN) {
var straightDirs = [[0,1],[0,-1],[1,0],[-1,0]];
for (i = 0; i < straightDirs.length; i++) {
dr = straightDirs[i][0]; dc = straightDirs[i][1];
nr = r + dr; nc = c + dc;
while (inBounds(nr, nc)) {
if (b[nr][nc]) {
if (b[nr][nc].color !== color) moves.push({ r: nr, c: nc });
break;
}
moves.push({ r: nr, c: nc });
nr += dr; nc += dc;
}
}
}
if (type === KING) {
for (dr = -1; dr <= 1; dr++) {
for (dc = -1; dc <= 1; dc++) {
if (dr === 0 && dc === 0) continue;
nr = r + dr; nc = c + dc;
if (inBounds(nr, nc) && (!b[nr][nc] || b[nr][nc].color !== color)) {
moves.push({ r: nr, c: nc });
}
}
}
/* Castling */
var row = color === W ? 7 : 0;
if (r === row && c === 4) {
/* Kingside */
var ksKey = color + 'K';
if (cRights[ksKey] && !b[row][5] && !b[row][6] && b[row][7] && b[row][7].type === ROOK && b[row][7].color === color) {
if (!isSquareAttacked(b, row, 4, opponent(color)) &&
!isSquareAttacked(b, row, 5, opponent(color)) &&
!isSquareAttacked(b, row, 6, opponent(color))) {
moves.push({ r: row, c: 6, special: 'castleK' });
}
}
/* Queenside */
var qsKey = color + 'Q';
if (cRights[qsKey] && !b[row][3] && !b[row][2] && !b[row][1] && b[row][0] && b[row][0].type === ROOK && b[row][0].color === color) {
if (!isSquareAttacked(b, row, 4, opponent(color)) &&
!isSquareAttacked(b, row, 3, opponent(color)) &&
!isSquareAttacked(b, row, 2, opponent(color))) {
moves.push({ r: row, c: 2, special: 'castleQ' });
}
}
}
}
return moves;
}
/* ===== LEGAL MOVE GENERATION (filters moves that leave king in check) ===== */
function getLegalMoves(b, r, c, cRights, epTarget) {
var pseudo = getPseudoMoves(b, r, c, cRights, epTarget);
var legal = [];
var piece = b[r][c];
if (!piece) return legal;
var color = piece.color;
for (var i = 0; i < pseudo.length; i++) {
var m = pseudo[i];
var nb = copyBoard(b);
/* Apply move on copy */
nb[m.r][m.c] = nb[r][c];
nb[r][c] = null;
/* Handle en passant capture */
if (m.special === 'enpassant') {
var capturedRow = color === W ? m.r + 1 : m.r - 1;
nb[capturedRow][m.c] = null;
}
/* Handle promotion */
if (m.special === 'promote') {
nb[m.r][m.c] = { color: color, type: QUEEN };
}
/* Handle castling - move the rook too */
if (m.special === 'castleK') {
var ckRow = color === W ? 7 : 0;
nb[ckRow][5] = nb[ckRow][7];
nb[ckRow][7] = null;
}
if (m.special === 'castleQ') {
var cqRow = color === W ? 7 : 0;
nb[cqRow][3] = nb[cqRow][0];
nb[cqRow][0] = null;
}
/* Only legal if own king not in check */
if (!isInCheck(nb, color)) {
legal.push(m);
}
}
return legal;
}
/* All legal moves for a color */
function getAllLegalMoves(b, color, cRights, epTarget) {
var allMoves = [];
for (var r = 0; r < 8; r++) {
for (var c = 0; c < 8; c++) {
if (b[r][c] && b[r][c].color === color) {
var moves = getLegalMoves(b, r, c, cRights, epTarget);
for (var i = 0; i < moves.length; i++) {
allMoves.push({ fromR: r, fromC: c, toR: moves[i].r, toC: moves[i].c, special: moves[i].special });
}
}
}
}
return allMoves;
}
/* ===== MAKE MOVE ===== */
function makeMove(fromR, fromC, toR, toC, special) {
var piece = board[fromR][fromC];
var captured = board[toR][toC];
var color = piece.color;
/* Handle en passant capture */
if (special === 'enpassant') {
var epCapRow = color === W ? toR + 1 : toR - 1;
captured = board[epCapRow][toC];
board[epCapRow][toC] = null;
}
/* Record capture */
if (captured) {
if (captured.color === W) {
capturedWhite.push(captured);
} else {
capturedBlack.push(captured);
}
}
/* Move piece */
board[toR][toC] = piece;
board[fromR][fromC] = null;
/* Handle promotion */
if (special === 'promote') {
board[toR][toC] = { color: color, type: QUEEN };
}
/* Handle castling - move rook */
if (special === 'castleK') {
var ckRow = color === W ? 7 : 0;
board[ckRow][5] = board[ckRow][7];