# uiri/html5chess architecture

> Architecture diagram of the GitHub repository uiri/html5chess, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/uiri/html5chess
- Repository: https://github.com/uiri/html5chess

## Overview

##### Project type and purpose
- This is a single‐page, client-only HTML5 chess application.
- It offers three modes: user vs. user (local), user vs. AI, and user vs. user (networked).

##### File structure
- index.html : HTML shell, loads the canvas and JS modules.
- chess.js    : core chess “engine” (board representation, legal‐move checking, game state).
- ai.js       : simple AI module that queries chess.js for moves.
- multiplayer.js : networking layer for peer‐to‐peer or server‐based play.
- No server code included, so networking must rely on a third-party signaling server or built-in browser peers (WebRTC/WebSocket).

##### README for additional insights
- Confirms HTML5 canvas rendering.
- Notes “dumb AI,” “local and networked multiplayer.”

a. Main components
- Browser (single page application)
- Canvas UI layer
- Chess Engine module (chess.js)
- AI Worker module (ai.js)
- Networking module (multiplayer.js)
- (Optional) External signaling/WebSocket server or peer endpoint

b. Relationships & interactions
1. User Input → Canvas UI
2. Canvas UI → Chess Engine (moves, state queries)
3. Chess Engine → Canvas UI (render updates)
4. On AI turn: Chess Engine → AI module → chosen move → Chess Engine
5. On networked turn: Chess Engine ↔ Multiplayer module ↔ Remote peer (via signaling server if needed)

c. Architectural patterns & principles
- Modular separation of concerns:
- Game logic (chess.js)
- Rendering/UI (index.html + Canvas)
- AI decision making (ai.js)
- Networking (multiplayer.js)
- Event-driven flow: user/multiplayer/AI triggers moves, engine emits state changes.

d. Key technologies
- HTML5 <canvas> for board rendering
- JavaScript modules (ES5 or ES6)
- WebSocket or WebRTC (multiplayer signaling)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Nodes
    User(("User")):::external
    subgraph "Browser SPA"
        UI["UI/Canvas"]:::ui
        ChessEngine["Chess Engine"]:::module
        AI["AI Worker"]:::module
        Multiplayer["Networking Module"]:::module
    end
    Signaling["Signaling Server / Peer"]:::external

    %% Flows
    User -->|"User Input"| UI
    UI -->|"if local mode"| ChessEngine
    UI -->|"if AI mode"| ChessEngine
    UI -->|"if network mode"| ChessEngine
    ChessEngine -->|"Render Update"| UI
    ChessEngine -->|"if AI mode"| AI
    AI -->|"chosen move"| ChessEngine
    ChessEngine -->|"if network mode"| Multiplayer
    Multiplayer -->|"signal"| Signaling
    Signaling -->|"signal"| Multiplayer
    Multiplayer -->|"remote move"| ChessEngine

    %% Click Events
    click UI "https://github.com/uiri/html5chess/blob/master/index.html"
    click ChessEngine "https://github.com/uiri/html5chess/blob/master/chess.js"
    click AI "https://github.com/uiri/html5chess/blob/master/ai.js"
    click Multiplayer "https://github.com/uiri/html5chess/blob/master/multiplayer.js"

    %% Styles
    classDef ui fill:#c2f0c2,stroke:#333,stroke-width:1px
    classDef module fill:#c2d9f7,stroke:#333,stroke-width:1px
    classDef external fill:#e0e0e0,stroke:#333,stroke-width:1px
```
