# yosua1011/chess-js architecture

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

- Interactive diagram: https://gitdiagram.com/yosua1011/chess-js
- Repository: https://github.com/yosua1011/chess-js

## Overview

##### Main Components
- User Interface (UI) Layer:
- index.html and styles.css form the visual and layout entry point.
- The images folder likely holds chess piece graphics or other UI assets.
- js/gui.js and js/io.js are responsible for handling the graphical representation, user interactions, and possibly input/output with the engine.
- Engine Core / Chess Logic:
- js/board.js: Manages the board representation and game state.
- js/defs.js: Contains definitions, constants, or mappings (for example, piece codes and board configurations).
- js/movegen.js: Implements the move generation rules.
- js/makemove.js (two copies suggesting perhaps an iterative development or different implementations): Handles making moves (updating the board state).
- js/evaluate.js: Contains logic for evaluating board positions.
- js/search.js: Likely implements the search algorithm (e.g., minimax, alpha-beta pruning) to decide on moves.
- js/pvtable.js: Manages the principal variation table which is important in search algorithms.
- js/perft.js: Possibly used for performance testing (calculating the number of legal moves in positions as a check for move generator correctness).
- Application Bootstrapping / Coordination:
- js/main.js: Likely serves as the primary entry point that ties the UI and engine components together.

##### Relationships and Interactions
- The UI layer (index.html, styles.css, images, js/gui.js, js/io.js) is the customer-facing side, taking chess move inputs via the graphical board.
- The main.js acts as the coordinator and orchestrates communication between the UI and the engine core.
- When a user interacts (e.g., clicks on a board position), the IO functions capture the event, which then triggers appropriate functions (from makemove.js and movegen.js) to update the board.
- The board state (managed by board.js) is used along with definitions from defs.js.
- The search.js and evaluate.js work together to analyze moves and decide the best move, possibly with feedback shown back on the board via the GUI.
- Utility files like pvtable.js and perft.js support deeper analysis and performance checking.

##### Architectural Patterns and Design Principles
- Layered Architecture: Clearly separate the presentation (UI) layer from the underlying business logic (engine computations and game rules).
- Modular Design: Each JS file focuses on a specific chess function (e.g., move generation, evaluation), ensuring separation of concerns.
- Data Flow: Inputs from the UI are validated and then passed to the engine core which computes game moves and then updates the UI accordingly.

##### Relevant Technologies and Frameworks
- Pure JavaScript is used as the primary language, with HTML and CSS for the frontend.
- No external libraries or frameworks are directly indicated, which suggests a self-contained architecture.
- The project structure may allow future modular enhancements or integration with more sophisticated frameworks if needed.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Interface Layer"
        UI1["index.html"]
        UI2["styles.css"]
        UI3["images"]
        UI4["gui.js"]
        UI5["io.js"]
    end

    subgraph "Coordinator/Bootstrapping"
        COORD["main.js"]
    end

    subgraph "Engine Core"
        ENG1["board.js"]
        ENG2["defs.js"]
        ENG3["movegen.js"]
        ENG4["makemove.js"]
        ENG5["evaluate.js"]
        ENG6["search.js"]
        ENG7["pvtable.js"]
        ENG8["perft.js"]
    end

    %% Connections from UI to Coordinator
    UI5 -->|"userInput"| COORD
    UI4 -->|"displayRequest"| COORD

    %% Coordinator interactions with Engine Core
    COORD -->|"generateMoves"| ENG3
    COORD -->|"makeMove"| ENG4
    COORD -->|"updateBoard"| ENG1
    COORD -->|"computeBestMove"| ENG6

    %% Engine Core internal dependencies
    ENG2 -->|"provideDefs"| ENG1
    ENG1 -->|"currentState"| ENG5
    ENG5 -->|"analyzeState"| ENG6
    ENG7 -->|"storePV"| ENG6

    %% Feedback to UI after engine processing
    ENG6 -->|"moveDecision"| UI4

    %% Class Definitions for Colouring
    class UI1,UI2,UI3,UI4,UI5 ui;
    class ENG1,ENG2,ENG3,ENG4,ENG5,ENG6,ENG7,ENG8 engine;
    class COORD coord;

    %% Class styles
    classDef ui fill:#f9e79f,stroke:#7d6608,stroke-width:2px,color:#000;
    classDef engine fill:#aed6f1,stroke:#2874a6,stroke-width:2px,color:#000;
    classDef coord fill:#abebc6,stroke:#117a65,stroke-width:2px,color:#000;

    %% Click Events for UI Layer
    click UI1 "https://github.com/yosua1011/chess-js/blob/master/index.html"
    click UI2 "https://github.com/yosua1011/chess-js/blob/master/styles.css"
    click UI3 "https://github.com/yosua1011/chess-js/tree/master/images"
    click UI4 "https://github.com/yosua1011/chess-js/blob/master/js/gui.js"
    click UI5 "https://github.com/yosua1011/chess-js/blob/master/js/io.js"

    %% Click Events for Engine Core
    click ENG1 "https://github.com/yosua1011/chess-js/blob/master/js/board.js"
    click ENG2 "https://github.com/yosua1011/chess-js/blob/master/js/defs.js"
    click ENG3 "https://github.com/yosua1011/chess-js/blob/master/js/movegen.js"
    click ENG4 "https://github.com/yosua1011/chess-js/blob/master/js/makemove.js"
    click ENG5 "https://github.com/yosua1011/chess-js/blob/master/js/evaluate.js"
    click ENG6 "https://github.com/yosua1011/chess-js/blob/master/js/search.js"
    click ENG7 "https://github.com/yosua1011/chess-js/blob/master/js/pvtable.js"
    click ENG8 "https://github.com/yosua1011/chess-js/blob/master/js/perft.js"

    %% Click Event for Coordinator/Bootstrapping
    click COORD "https://github.com/yosua1011/chess-js/blob/master/js/main.js"
```
