Spaces:
Running
Running
Download index.html from sergiomcfly/anycoder-9854f2ac: direct link, hf CLI and curl.
- Browser
- Download file 36 kB
-
https://huggingface.co/spaces/sergiomcfly/anycoder-9854f2ac/resolve/main/index.html
- Command line
-
hf download hf://spaces/sergiomcfly/anycoder-9854f2ac/index.html
-
curl -L -o index.html https://huggingface.co/spaces/sergiomcfly/anycoder-9854f2ac/resolve/main/index.html
36 kB
| <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)) ; | |
| } | |
| .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)) ; | |
| } | |
| .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 ; | |
| animation-iteration-count: 1 ; | |
| transition-duration: 0.01ms ; | |
| } | |
| } | |
| </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][ |