Welcome. This is the comprehensive, deep-dive architectural map of XChess. Use this document as your source of truth for debugging, extending features, or understanding state flow.
XChess uses a strict State vs. Presentation separation.
- State is exclusively owned by
ChessEngine.js(a pure JS class). It holds the logical 8x8 grid. - Presentation is owned by Phaser 3 (
GameScene.jsand its managers). It maintains a parallel 8x8 grid of graphical sprites (pieceSprites). - Communication: The UI/Input triggers methods on
ChessEngine.GameScenethen reads the result and orchestrates the visual updates (animations, FX, piece destruction).
main.js: The absolute entry point. Initializes theChessEnginesingleton and the Phaser Game instance (new Phaser.Game(config)). Passes the engine toGameScenevia thescene.startdata payload. Also manages the global light/dark theme toggle (theme-toggleDOM element).style.css: The sole stylesheet controlling all HTML UI overlays, the promotion modal, HUD, and the dark/light mode CSS variables.
ChessEngine.js:- State: Maintains
this.board(8x8 array of{type, color, hasMoved, isGhost, originalPiece}),this.turn('white'|'black'),this.moveHistory(for undos), andthis.clocks. - Responsibilities: Validates moves (
isValidMove), calculates check/checkmate (isInCheck,isCheckmate), executes moves (movePiece,undoMove), and manages time (updateClock). - Dependencies: Imports and utilizes
XtraRuleRegistryto evaluate non-standard moves (Xtra rules).
- State: Maintains
Implements a Strategy Pattern. Each rule evaluates if a special move is valid and computes collateral damage.
XtraRuleRegistry.js: Master registry. Toggled via the UI. WhenChessEnginevalidates a move, it queries the registry to see if any active rule allows it.KnightTrampleRule.js: Allows Knights to jump over an enemy and kill it en route to the destination.RookExplosiveLandingRule.js: Rook destroys all pieces (except kings/itself) in a 3x3 radius upon landing.QueenSirensCallRule.js: Queen pulls an enemy piece into an adjacent square and destroys it without moving.BishopRicochetRule.js: Bishop bounces off the board edges to change trajectory.KingSwapperRule.js: King swaps places with an allied piece instead of moving normally.PawnStraightShooterRule.js: Pawn captures by shooting straight ahead (2 tiles) instead of diagonally.
GameScene.js: The Controller. At~250 lines, it avoids being a "God Object".- Flow: Listens to input -> queries
engine.isValidMove-> moves the physical sprite -> checks for captures -> plays animations -> updates turns. - Delegation: Instantiates and delegates strictly to
renderer,indicator,fx,checkVfx,ui, andpromo.
- Flow: Listens to input -> queries
BoardRenderer.js:- State: Maintains
this.pieceSprites(8x8 array of Phaser Graphics). - Responsibilities: Draws the dark/light checkerboard tiles. Handles pointer events (click to select, click to move) and dispatches them back to
GameScene.
- State: Maintains
MoveIndicator.js: Given a selected piece, queries the engine for valid moves and draws graphical overlays (dots for empty squares, red corners for captures, AoE boxes, ricochet path lines).FXManager.js: The VFX system. Contains reusable particle emitters (purpleBurst,redBurst,cyanSpark,goldFlare). Also houses complex asynchronous cinematic functions (playExplosiveFX,playTrampleFX,playSirensCallFX,playSwapFX) utilizing timelines and tweens. Note: Camera shakes are limited ONLY toplayExplosiveFX.CheckVisuals.js: Draws the pulsing red tile under a King in check, and the dashed red threat-line from the attacking piece to the King.
PieceFactory.js: Procedurally generates the vector shapes for pieces (Pawn, Rook, Knight, Bishop, Queen, King) using Phaser primitive paths (no PNGs/SVGs used). Returns a nested Container with an infinite floating/hovering tween.CaptureAnimations.js: Defines the specific death animations when pieces are killed normally (e.g., swords slashing for Knight, lightning for Queen, crumbling for Rook).Pieces.js: Stores static constants like premium adult dark/light mode hex colors and stroke widths.
UIManager.js: Bridges Phaser and the HTML DOM.- Generates the Xtra Rules toggle cards (
#rules-container). - Updates the turn text and timer clocks.
- Manages the Undo button (only visible if Bot ELO < 500).
- Handles the Music toggle button (mutes both HTML Audio and Phaser Web Audio).
- Displays the Game Over toast.
- Generates the Xtra Rules toggle cards (
PromotionModal.js: Controls the hidden HTML modal#promotion-modal. When a pawn reaches the back rank, this pauses the game, awaits a user click (Q, R, B, N), and resolves the promotion in both theChessEngineand the PhaserBoardRenderer.
SoundFX.js: A procedural Web Audio API synthesizer. Generates dynamic sound waves for moves (thud), captures (clash), checks (alarm), and errors (buzz), ensuring zero external asset loading time.
Bot.js: Evaluates board state. Implements Minimax with Alpha-Beta pruning. Reads the#elo-selectDOM element to determine search depth (intelligence) and randomness (blunders).
- Coordinates: The board uses
(row, col)formatting universally. Row 0 is the top (Black's back rank), Row 7 is the bottom (White's back rank). - DOM IDs: Never change HTML IDs without updating the corresponding class (e.g.,
UIManagerrelies on#music-toggle,#undo-btn,#elo-select). - Dependencies: Never introduce circular dependencies. Managers (
FXManager,BoardRenderer, etc.) should only communicate up toGameScene, never to each other directly.GameScenepasses required references (likepieceSprites) as arguments.