Spaces:
Running
Running
File size: 35,969 Bytes
841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea 841b352 8a3c3ea | 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 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Chess Game</title>
<link href="https://cdn.tailwindcss.com" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cinzel:wght@400;700;900&family=Inter:wght@300;400;500;600&display=swap" rel="stylesheet">
<style>
:root {
--bg: #0a0a0f;
--fg: #e8e6e3;
--muted: #6b7280;
--accent: #c9a227;
--accent-glow: #ffd700;
--card: #141419;
--border: #2a2a35;
--light-square: #e8d4a8;
--dark-square: #8b6914;
--white-piece: #f5f5f5;
--black-piece: #1a1a1a;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Inter', sans-serif;
background: var(--bg);
color: var(--fg);
min-height: 100vh;
overflow-x: hidden;
}
.font-display {
font-family: 'Cinzel', serif;
}
/* Background atmosphere */
.bg-atmosphere {
position: fixed;
inset: 0;
background:
radial-gradient(ellipse 80% 50% at 50% -20%, rgba(201, 162, 39, 0.15) 0%, transparent 50%),
radial-gradient(ellipse 60% 40% at 80% 100%, rgba(139, 105, 20, 0.1) 0%, transparent 50%),
radial-gradient(ellipse 40% 30% at 20% 80%, rgba(201, 162, 39, 0.08) 0%, transparent 50%);
pointer-events: none;
z-index: 0;
}
/* Noise texture overlay */
.noise-overlay {
position: fixed;
inset: 0;
opacity: 0.03;
background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
pointer-events: none;
z-index: 1;
}
.game-container {
position: relative;
z-index: 10;
}
/* 3D Board Container */
.board-scene {
perspective: 1200px;
perspective-origin: 50% 40%;
}
.board-3d {
transform-style: preserve-3d;
transform: rotateX(55deg) rotateZ(-45deg);
transition: transform 0.8s cubic-bezier(0.4, 0, 0.2, 1);
}
.board-3d:hover {
transform: rotateX(50deg) rotateZ(-45deg);
}
@media (max-width: 768px) {
.board-scene {
perspective: 800px;
}
.board-3d {
transform: rotateX(60deg) rotateZ(-45deg) scale(0.7);
}
.board-3d:hover {
transform: rotateX(55deg) rotateZ(-45deg) scale(0.7);
}
}
/* Chess Board */
.chess-board {
display: grid;
grid-template-columns: repeat(8, 1fr);
width: min(70vmin, 560px);
height: min(70vmin, 560px);
border: 4px solid var(--accent);
box-shadow:
0 20px 60px rgba(0, 0, 0, 0.8),
0 0 100px rgba(201, 162, 39, 0.2),
inset 0 0 30px rgba(0, 0, 0, 0.3);
transform-style: preserve-3d;
}
.square {
position: relative;
display: flex;
align-items: center;
justify-content: center;
aspect-ratio: 1;
transform-style: preserve-3d;
cursor: pointer;
transition: all 0.2s ease;
}
.square::before {
content: '';
position: absolute;
inset: 0;
transform: translateZ(-20px);
filter: brightness(0.3);
}
.square.light {
background: linear-gradient(135deg, #f0e6c8 0%, #d4c4a0 100%);
}
.square.dark {
background: linear-gradient(135deg, #b8963c 0%, #8b6914 100%);
}
.square.selected {
box-shadow: inset 0 0 0 4px var(--accent-glow);
background: linear-gradient(135deg, rgba(255, 215, 0, 0.4), rgba(201, 162, 39, 0.4)) !important;
}
.square.valid-move::after {
content: '';
position: absolute;
width: 30%;
height: 30%;
background: radial-gradient(circle, rgba(0, 255, 100, 0.8) 0%, rgba(0, 200, 80, 0.6) 100%);
border-radius: 50%;
transform: translateZ(5px);
animation: pulse 1.5s ease-in-out infinite;
}
.square.valid-capture {
box-shadow: inset 0 0 0 4px rgba(255, 50, 50, 0.8);
}
.square.last-move {
background: linear-gradient(135deg, rgba(100, 200, 255, 0.3), rgba(50, 150, 200, 0.3)) !important;
}
.square.check {
animation: checkPulse 0.8s ease-in-out infinite;
box-shadow: inset 0 0 20px rgba(255, 0, 0, 0.6);
}
@keyframes pulse {
0%, 100% { transform: translateZ(5px) scale(1); opacity: 0.8; }
50% { transform: translateZ(5px) scale(1.2); opacity: 1; }
}
@keyframes checkPulse {
0%, 100% { box-shadow: inset 0 0 20px rgba(255, 0, 0, 0.6); }
50% { box-shadow: inset 0 0 30px rgba(255, 0, 0, 0.9); }
}
/* Chess Pieces */
.piece {
position: absolute;
width: 80%;
height: 80%;
display: flex;
align-items: center;
justify-content: center;
transform-style: preserve-3d;
transform: translateZ(15px);
transition: transform 0.15s ease;
cursor: grab;
filter: drop-shadow(2px 4px 6px rgba(0, 0, 0, 0.5));
}
.piece:hover {
transform: translateZ(25px) scale(1.1);
}
.piece.dragging {
cursor: grabbing;
transform: translateZ(40px) scale(1.2);
z-index: 100;
}
.piece.white {
color: var(--white-piece);
}
.piece.black {
color: var(--black-piece);
}
.piece svg {
width: 100%;
height: 100%;
filter: drop-shadow(1px 2px 3px rgba(0, 0, 0, 0.4));
}
.piece.white svg {
fill: #f5f5f5;
stroke: #333;
stroke-width: 8;
}
.piece.black svg {
fill: #2a2a2a;
stroke: #000;
stroke-width: 8;
}
/* Board Labels */
.board-labels {
position: absolute;
font-family: 'Cinzel', serif;
font-size: 12px;
color: var(--accent);
font-weight: 600;
}
/* Info Panel */
.info-panel {
background: linear-gradient(145deg, var(--card) 0%, rgba(20, 20, 25, 0.9) 100%);
border: 1px solid var(--border);
border-radius: 12px;
backdrop-filter: blur(10px);
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
}
.turn-indicator {
display: flex;
align-items: center;
gap: 12px;
}
.turn-dot {
width: 16px;
height: 16px;
border-radius: 50%;
animation: turnGlow 2s ease-in-out infinite;
}
.turn-dot.white {
background: linear-gradient(135deg, #fff 0%, #ccc 100%);
box-shadow: 0 0 20px rgba(255, 255, 255, 0.5);
}
.turn-dot.black {
background: linear-gradient(135deg, #444 0%, #111 100%);
box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
}
@keyframes turnGlow {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.2); }
}
/* Captured Pieces */
.captured-pieces {
display: flex;
flex-wrap: wrap;
gap: 4px;
min-height: 40px;
}
.captured-piece {
width: 28px;
height: 28px;
opacity: 0.7;
animation: captureIn 0.3s ease-out;
}
@keyframes captureIn {
from { transform: scale(0) rotate(-180deg); opacity: 0; }
to { transform: scale(1) rotate(0); opacity: 0.7; }
}
/* Move History */
.move-history {
max-height: 200px;
overflow-y: auto;
scrollbar-width: thin;
scrollbar-color: var(--accent) var(--card);
}
.move-history::-webkit-scrollbar {
width: 6px;
}
.move-history::-webkit-scrollbar-track {
background: var(--card);
}
.move-history::-webkit-scrollbar-thumb {
background: var(--accent);
border-radius: 3px;
}
.move-entry {
display: grid;
grid-template-columns: 30px 1fr 1fr;
gap: 8px;
padding: 4px 0;
font-size: 13px;
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}
/* Buttons */
.btn {
padding: 10px 20px;
border-radius: 8px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s ease;
border: none;
font-family: 'Inter', sans-serif;
}
.btn-primary {
background: linear-gradient(135deg, var(--accent) 0%, #a88520 100%);
color: var(--bg);
box-shadow: 0 4px 15px rgba(201, 162, 39, 0.3);
}
.btn-primary:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(201, 162, 39, 0.5);
}
.btn-secondary {
background: transparent;
color: var(--fg);
border: 1px solid var(--border);
}
.btn-secondary:hover {
border-color: var(--accent);
color: var(--accent);
}
/* Modal */
.modal-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.8);
backdrop-filter: blur(5px);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
}
.modal-overlay.active {
opacity: 1;
visibility: visible;
}
.modal-content {
background: var(--card);
border: 1px solid var(--accent);
border-radius: 16px;
padding: 32px;
max-width: 400px;
text-align: center;
transform: scale(0.9);
transition: transform 0.3s ease;
}
.modal-overlay.active .modal-content {
transform: scale(1);
}
/* Entrance animations */
.fade-in {
animation: fadeIn 0.6s ease-out forwards;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.stagger-1 { animation-delay: 0.1s; }
.stagger-2 { animation-delay: 0.2s; }
.stagger-3 { animation-delay: 0.3s; }
/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<div class="bg-atmosphere"></div>
<div class="noise-overlay"></div>
<div class="game-container min-h-screen py-6 px-4">
<!-- Header -->
<header class="max-w-6xl mx-auto mb-8 fade-in">
<div class="flex items-center justify-between flex-wrap gap-4">
<div>
<h1 class="font-display text-3xl md:text-4xl font-bold tracking-wider" style="color: var(--accent);">
3D CHESS
</h1>
<p class="text-sm mt-1" style="color: var(--muted);">Strategic Warfare in Three Dimensions</p>
</div>
<div class="flex items-center gap-4">
<a href="https://huggingface.co/spaces/akhaliq/anycoder"
target="_blank"
class="text-sm hover:text-[var(--accent)] transition-colors"
style="color: var(--muted);">
Built with anycoder
</a>
</div>
</div>
</header>
<!-- Main Game Area -->
<main class="max-w-6xl mx-auto">
<div class="grid lg:grid-cols-[1fr_320px] gap-8 items-start">
<!-- Board Section -->
<div class="fade-in stagger-1">
<div class="board-scene flex items-center justify-center py-8">
<div class="board-3d">
<div class="chess-board" id="chessBoard">
<!-- Board squares will be generated by JS -->
</div>
</div>
</div>
</div>
<!-- Info Panel -->
<div class="space-y-4 fade-in stagger-2">
<!-- Turn Indicator -->
<div class="info-panel p-5">
<h3 class="font-display text-lg mb-3" style="color: var(--accent);">Current Turn</h3>
<div class="turn-indicator">
<div class="turn-dot white" id="turnDot"></div>
<span class="text-xl font-semibold" id="turnText">White to move</span>
</div>
</div>
<!-- Game Status -->
<div class="info-panel p-5">
<h3 class="font-display text-lg mb-3" style="color: var(--accent);">Game Status</h3>
<p id="gameStatus" class="text-sm" style="color: var(--muted);">Game in progress</p>
</div>
<!-- Captured Pieces -->
<div class="info-panel p-5">
<h3 class="font-display text-lg mb-3" style="color: var(--accent);">Captured Pieces</h3>
<div class="space-y-3">
<div>
<p class="text-xs mb-1" style="color: var(--muted);">White captured:</p>
<div class="captured-pieces" id="whiteCaptured"></div>
</div>
<div>
<p class="text-xs mb-1" style="color: var(--muted);">Black captured:</p>
<div class="captured-pieces" id="blackCaptured"></div>
</div>
</div>
</div>
<!-- Move History -->
<div class="info-panel p-5">
<h3 class="font-display text-lg mb-3" style="color: var(--accent);">Move History</h3>
<div class="move-history" id="moveHistory">
<p class="text-sm" style="color: var(--muted);">No moves yet</p>
</div>
</div>
<!-- Controls -->
<div class="flex gap-3">
<button class="btn btn-primary flex-1" onclick="resetGame()">New Game</button>
<button class="btn btn-secondary flex-1" onclick="undoMove()">Undo</button>
</div>
</div>
</div>
</main>
</div>
<!-- Game Over Modal -->
<div class="modal-overlay" id="gameOverModal">
<div class="modal-content">
<h2 class="font-display text-2xl mb-4" style="color: var(--accent);" id="modalTitle">Game Over</h2>
<p class="mb-6" id="modalMessage">White wins by checkmate!</p>
<button class="btn btn-primary" onclick="resetGame()">Play Again</button>
</div>
</div>
<script>
// Chess piece SVG paths
const pieceSVGs = {
king: `<svg viewBox="0 0 45 45"><path d="M22.5 11.63V6M20 8h5" stroke-linejoin="round"/><path d="M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5" fill="currentColor" stroke-linecap="butt"/><path d="M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-2.5-7.5-12-10.5-16-4-3 6 5 10 5 10V37z" fill="currentColor" stroke-linecap="butt"/></svg>`,
queen: `<svg viewBox="0 0 45 45"><path d="M8 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM24.5 7.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM41 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM16 9a2 2 0 1 1-4 0 2 2 0 1 1 4 0zM33 9a2 2 0 1 1-4 0 2 2 0 1 1 4 0z" fill="currentColor"/><path d="M9 26c8.5-1.5 21-1.5 27 0l2-12-7 11V11l-5.5 13.5-3-15-3 15-5.5-14V25L6 14l3 12z" fill="currentColor" stroke-linecap="butt"/><path d="M9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z" fill="currentColor" stroke-linecap="butt"/><path d="M11.5 30c3.5-1 18.5-1 22 0M12 33.5c6-1 15-1 21 0" fill="none" stroke-linecap="butt"/></svg>`,
rook: `<svg viewBox="0 0 45 45"><path d="M9 39h27v-3H9v3zM12 36v-4h21v4H12zM11 14V9h4v2h5V9h5v2h5V9h4v5" stroke-linecap="butt"/><path d="M34 14l-3 3H14l-3-3" stroke-linecap="butt"/><path d="M31 17v12.5H14V17" stroke-linecap="butt" stroke-linejoin="miter"/><path d="M31 29.5l1.5 2.5h-20l1.5-2.5" stroke-linecap="butt" stroke-linejoin="miter"/><path d="M11 14h23" fill="none" stroke-linejoin="miter"/></svg>`,
bishop: `<svg viewBox="0 0 45 45"><path d="M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.354.49-2.323.47-3-.5 1.354-1.46 3-2 3-2z" fill="currentColor"/><path d="M15 32c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2z" fill="currentColor"/><path d="M25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z" fill="currentColor"/></svg>`,
knight: `<svg viewBox="0 0 45 45"><path d="M22 10c10.5 1 16.5 8 16 29H15c0-9 10-6.5 8-21" fill="currentColor"/><path d="M24 18c.38 2.91-5.55 7.37-8 9-3 2-2.82 4.34-5 4-1.042-.94 1.41-3.04 0-3-1 0 .19 1.23-1 2-1 0-4.003 1-4-4 0-2 6-12 6-12s1.89-1.9 2-3.5c-.73-.994-.5-2-.5-3 1-1 3 2.5 3 2.5h2s.78-1.992 2.5-3c1 0 1 3 1 3" fill="currentColor"/><path d="M9.5 25.5a.5.5 0 1 1-1 0 .5.5 0 1 1 1 0zM14.933 15.75a.5 1.5 30 1 1-.866-.5.5 1.5 30 1 1 .866.5z" fill="currentColor" stroke="currentColor"/></svg>`,
pawn: `<svg viewBox="0 0 45 45"><path d="M25.5 14a4.5 4.5 0 1 1-9 0 4.5 4.5 0 1 1 9 0z" fill="currentColor"/><path d="M21 21c-4 0-8 3-8 8v3h24v-3c0-5-4-8-8-8h-8z" fill="currentColor"/></svg>`
};
// Game state
let gameState = {
board: [],
currentTurn: 'white',
selectedSquare: null,
validMoves: [],
moveHistory: [],
capturedPieces: { white: [], black: [] },
lastMove: null,
isCheck: false,
isCheckmate: false,
isStalemate: false,
enPassantTarget: null,
castlingRights: {
white: { kingSide: true, queenSide: true },
black: { kingSide: true, queenSide: true }
}
};
// Initialize the board
function initBoard() {
const board = document.getElementById('chessBoard');
board.innerHTML = '';
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
const square = document.createElement('div');
square.className = `square ${(row + col) % 2 === 0 ? 'light' : 'dark'}`;
square.dataset.row = row;
square.dataset.col = col;
square.addEventListener('click', () => handleSquareClick(row, col));
board.appendChild(square);
}
}
// Initialize game state board
gameState.board = [
['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'],
['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
[null, null, null, null, null, null, null, null],
['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'],
['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']
];
renderPieces();
}
// Get piece info
function getPieceInfo(piece) {
if (!piece) return null;
const isWhite = piece === piece.toUpperCase();
const type = piece.toLowerCase();
const names = { 'k': 'king', 'q': 'queen', 'r': 'rook', 'b': 'bishop', 'n': 'knight', 'p': 'pawn' };
return { isWhite, type: names[type], symbol: piece };
}
// Render pieces on board
function renderPieces() {
const squares = document.querySelectorAll('.square');
squares.forEach(square => {
const existingPiece = square.querySelector('.piece');
if (existingPiece) existingPiece.remove();
});
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 8; col++) {
const piece = gameState.board[row][col];
if (piece) {
const info = getPieceInfo(piece);
const square = document.querySelector(`[data-row="${row}"][data-col="${col}"]`);
const pieceEl = document.createElement('div');
pieceEl.className = `piece ${info.isWhite ? 'white' : 'black'}`;
pieceEl.innerHTML = pieceSVGs[info.type];
square.appendChild(pieceEl);
}
}
}
}
// Handle square click
function handleSquareClick(row, col) {
if (gameState.isCheckmate || gameState.isStalemate) return;
const piece = gameState.board[row][col];
const pieceInfo = getPieceInfo(piece);
// If a piece is already selected
if (gameState.selectedSquare) {
const [selRow, selCol] = gameState.selectedSquare;
// Check if clicking on a valid move
const isValidMove = gameState.validMoves.some(m => m.row === row && m.col === col);
if (isValidMove) {
makeMove(selRow, selCol, row, col);
clearSelection();
return;
}
// If clicking on own piece, select it instead
if (pieceInfo && pieceInfo.isWhite === (gameState.currentTurn === 'white')) {
selectSquare(row, col);
return;
}
clearSelection();
return;
}
// Select piece if it belongs to current player
if (pieceInfo && pieceInfo.isWhite === (gameState.currentTurn === 'white')) {
selectSquare(row, col);
}
}
// Select a square
function selectSquare(row, col) {
clearSelection();
gameState.selectedSquare = [row, col];
gameState.validMoves = getValidMoves(row, col);
const square = document.querySelector(`[data-row="${row}"][data-col="${col}"]`);
square.classList.add('selected');
gameState.validMoves.forEach(move => {
const targetSquare = document.querySelector(`[data-row="${move.row}"][data-col="${move.col}"]`);
if (gameState.board[move.row][move.col]) {
targetSquare.classList.add('valid-capture');
} else {
targetSquare.classList.add('valid-move');
}
});
}
// Clear selection
function clearSelection() {
gameState.selectedSquare = null;
gameState.validMoves = [];
document.querySelectorAll('.square').forEach(sq => {
sq.classList.remove('selected', 'valid-move', 'valid-capture');
});
}
// Get valid moves for a piece
function getValidMoves(row, col, checkKingSafety = true) {
const piece = gameState.board[row][col];
if (!piece) return [];
const info = getPieceInfo(piece);
let moves = [];
switch (info.type) {
case 'pawn':
moves = getPawnMoves(row, col, info.isWhite);
break;
case 'rook':
moves = getRookMoves(row, col, info.isWhite);
break;
case 'knight':
moves = getKnightMoves(row, col, info.isWhite);
break;
case 'bishop':
moves = getBishopMoves(row, col, info.isWhite);
break;
case 'queen':
moves = [...getRookMoves(row, col, info.isWhite), ...getBishopMoves(row, col, info.isWhite)];
break;
case 'king':
moves = getKingMoves(row, col, info.isWhite);
break;
}
// Filter moves that would leave king in check
if (checkKingSafety) {
moves = moves.filter(move => !wouldLeaveKingInCheck(row, col, move.row, move.col, info.isWhite));
}
return moves;
}
// Pawn moves
function getPawnMoves(row, col, isWhite) {
const moves = [];
const direction = isWhite ? -1 : 1;
const startRow = isWhite ? 6 : 1;
// Forward move
if (isInBounds(row + direction, col) && !gameState.board[row + direction][col]) {
moves.push({ row: row + direction, col });
// Double move from start
if (row === startRow && !gameState.board[row + 2 * direction][col]) {
moves.push({ row: row + 2 * direction, col });
}
}
// Captures
[-1, 1].forEach(dc => {
const newRow = row + direction;
const newCol = col + dc;
if (isInBounds(newRow, newCol)) {
const target = gameState.board[newRow][newCol];
if (target && getPieceInfo(target).isWhite !== isWhite) {
moves.push({ row: newRow, col: newCol });
}
// En passant
if (gameState.enPassantTarget &&
gameState.enPassantTarget.row === newRow &&
gameState.enPassantTarget.col === newCol) {
moves.push({ row: newRow, col: newCol, enPassant: true });
}
}
});
return moves;
}
// Rook moves
function getRookMoves(row, col, isWhite) {
return getSlidingMoves(row, col, isWhite, [[0, 1], [0, -1], [1, 0], [-1, 0]]);
}
// Bishop moves
function getBishopMoves(row, col, isWhite) {
return getSlidingMoves(row, col, isWhite, [[1, 1], [1, -1], [-1, 1], [-1, -1]]);
}
// Sliding piece moves
function getSlidingMoves(row, col, isWhite, directions) {
const moves = [];
directions.forEach(([dr, dc]) => {
let r = row + dr;
let c = col + dc;
while (isInBounds(r, c)) {
const target = gameState.board[r][c];
if (!target) {
moves.push({ row: r, col: c });
} else {
if (getPieceInfo(target).isWhite !== isWhite) {
moves.push({ row: r, col: c });
}
break;
}
r += dr;
c += dc;
}
});
return moves;
}
// Knight moves
function getKnightMoves(row, col, isWhite) {
const moves = [];
const offsets = [[-2, -1], [-2, 1], [-1, -2], [-1, 2], [1, -2], [1, 2], [2, -1], [2, 1]];
offsets.forEach(([dr, dc]) => {
const r = row + dr;
const c = col + dc;
if (isInBounds(r, c)) {
const target = gameState.board[r][c];
if (!target || getPieceInfo(target).isWhite !== isWhite) {
moves.push({ row: r, col: c });
}
}
});
return moves;
}
// King moves
function getKingMoves(row, col, isWhite) {
const moves = [];
const offsets = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]];
offsets.forEach(([dr, dc]) => {
const r = row + dr;
const c = col + dc;
if (isInBounds(r, c)) {
const target = gameState.board[r][c];
if (!target || getPieceInfo(target).isWhite !== isWhite) {
moves.push({ row: r, col: c });
}
}
});
// Castling
const rights = gameState.castlingRights[isWhite ? 'white' : 'black'];
const kingRow = isWhite ? 7 : 0;
if (row === kingRow && col === 4 && !isKingInCheck(isWhite)) {
// King side
if (rights.kingSide &&
!gameState.board[kingRow][5] &&
!gameState.board[kingRow][6] &&
!isSquareAttacked(kingRow, 5, !isWhite) &&
!isSquareAttacked(kingRow, 6, !isWhite)) {
moves.push({ row: kingRow, col: 6, castling: 'kingSide' });
}
// Queen side
if (rights.queenSide &&
!gameState.board[kingRow][1] &&
!gameState.board[kingRow][2] &&
!gameState.board[kingRow][3] &&
!isSquareAttacked(kingRow, 2, !isWhite) &&
!isSquareAttacked(kingRow, 3, !isWhite)) {
moves.push({ row: kingRow, col: 2, castling: 'queenSide' });
}
}
return moves;
}
// Check if position is in bounds
function isInBounds(row, col) {
return row >= 0 && row < 8 && col >= 0 && col < 8;
}
// Find king position
function findKing(isWhite) {
const king = isWhite ? 'K' : 'k';
for (let r = 0; r < 8; r++) {
for (let c = 0; c < 8; c++) {
if (gameState.board[r][c] === king) {
return { row: r, col: c };
}
}
}
return null;
}
// Check if king is in check
function isKingInCheck(isWhite) {
const kingPos = findKing(isWhite);
if (!kingPos) return false;
return isSquareAttacked(kingPos.row, kingPos.col, !isWhite);
}
// Check if square is attacked
function isSquareAttacked(row, col, byWhite) {
for (let r = 0; r < 8; r++) {
for (let c = 0; c < 8; c++) {
const piece = gameState.board[r][c];
if (piece) {
const info = getPieceInfo(piece);
if (info.isWhite === byWhite) {
const moves = getValidMoves(r, c, false);
if (moves.some(m => m.row === row && m.col === col)) {
return true;
}
}
}
}
}
return false;
}
// Check if move would leave king in check
function wouldLeaveKingInCheck(fromRow, fromCol, toRow, toCol, isWhite) {
// Make temporary move
const originalPiece = gameState.board[toRow][toCol];
const movingPiece = gameState.board[fromRow][fromCol];
gameState.board[toRow][toCol] = movingPiece;
gameState.board[fromRow][fromCol] = null;
const inCheck = isKingInCheck(isWhite);
// Restore board
gameState.board[fromRow][fromCol] = movingPiece;
gameState.board[toRow][toCol] = originalPiece;
return inCheck;
}
// Make a move
function makeMove(fromRow, fromCol, toRow, toCol) {
const piece = gameState.board[fromRow][fromCol];
const info = getPieceInfo(piece);
const capturedPiece = gameState.board[toRow][toCol];
const move = gameState.validMoves.find(m => m.row === toRow && m.col === toCol);
// Store move for history
const moveRecord = {
from: { row: fromRow, col: fromCol },
to: { row: toRow, col: toCol },
piece: piece,
captured: capturedPiece,
castling: move?.castling,
enPassant: move?.enPassant
};
// Handle en passant capture
if (move?.enPassant) {
const capturedRow = info.isWhite ? toRow + 1 : toRow - 1;
const epCaptured = gameState.board[capturedRow][toCol];
gameState.capturedPieces[info.isWhite ? 'white' : 'black'].push(epCaptured);
gameState.board[capturedRow][toCol] = null;
moveRecord.epCapture = { row: capturedRow, col: toCol, piece: epCaptured };
}
// Handle castling
if (move?.castling) {
const rookCol = move.castling === 'kingSide' ? 7 : 0;
const newRookCol = move.castling === 'kingSide' ? 5 : 3;
gameState.board[toRow][newRookCol] = gameState.board[toRow][rookCol];
gameState.board[toRow][rookCol] = null;
}
// Handle capture
if (capturedPiece) {
gameState.capturedPieces[info.isWhite ? 'white' : 'black'].push(capturedPiece);
}
// Make the move
gameState.board[toRow][toCol] = piece;
gameState.board[fromRow][fromCol] = null;
// Pawn promotion (auto-promote to queen)
if (info.type === 'pawn' && (toRow === 0 || toRow === 7)) {
gameState.board[toRow][ |