# tirth-nagar/chess-bot architecture

> Architecture diagram of the GitHub repository tirth-nagar/chess-bot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tirth-nagar/chess-bot
- Repository: https://github.com/tirth-nagar/chess-bot

## Overview

##### Input Layer
- Chess.com interface where the bot captures the game state
- Screen capture mechanism

##### Computer Vision Pipeline
- Image Processing Module
- Chess Piece Classifier (CNN Model)
- Board State Analyzer
- Input image (400x400 pixels)
- CNN Architecture layers (as specified in README)
- Output classification (13 classes: 6 black pieces, 6 white pieces, empty square)

##### Game Logic Layer
- FEN String Generator
- Validator
- Game State Manager

##### Decision Engine
- Stockfish Integration
- Move Calculator
- Best Move Selector

##### Output Layer
- Move Execution System
- Interface with chess.com

##### Flow Direction
- Main flow should be top to bottom
- Data processing flow should be left to right
- Feedback loops where applicable

Specific Implementation Details:

##### CNN Model Section
- Input Layer (50x50 image patches)
- Conv2D (32 filters) + ReLU
- MaxPooling2D
- Conv2D (64 filters) + ReLU
- MaxPooling2D
- Flatten
- Dense (64 units)
- Output (13 classes)

##### Data Processing Pipeline
Flow from:
- Raw board image
- Image splitting (64 squares)
- Classification
- FEN string generation

##### Integration Points
- Chess.com interface
- Stockfish engine connection
- Current position storage

Key Files to Reference:
- main.py: Central orchestration
- classifier_model.py: CNN implementation
- data_processing.py: Image processing
- chess_piece_classifier.h5: Trained model
- stockfish-windows-x86-64-avx2.exe: Chess engine

##### External dependencies

Level of Detail:
- Keep high-level flow clear and visible
- Configuration parameters
- Error handling paths

## Diagram (Mermaid)

```mermaid
%%{init: {
  'theme': 'base',
  'themeVariables': {
    'primaryColor': '#2b2b2b',
    'primaryTextColor': '#fff',
    'primaryBorderColor': '#7C0000',
    'lineColor': '#F8B229',
    'secondaryColor': '#006100',
    'tertiaryColor': '#0000ff'
  }
}}%%

flowchart TB
    classDef input fill:#4287f5,stroke:#333,stroke-width:2px
    classDef process fill:#50C878,stroke:#333,stroke-width:2px
    classDef ai fill:#FFD700,stroke:#333,stroke-width:2px
    classDef external fill:#A9A9A9,stroke:#333,stroke-width:2px
    classDef storage fill:#DEB887,stroke:#333,stroke-width:2px

    subgraph Input["Input Layer"]
        ChessCom["Chess.com Interface"]:::input
        Screen["Screen Capture"]:::input
    end

    subgraph Vision["Computer Vision Pipeline"]
        ImgProc["Image Processing"]:::process
        CNN["Chess Piece Classifier (CNN)"]:::ai
        BoardAnalyzer["Board State Analyzer"]:::process
    end

    subgraph GameLogic["Game Logic Layer"]
        FEN["FEN String Generator"]:::process
        Validator["Position Validator"]:::process
        StateManager["Game State Manager"]:::process
    end

    subgraph Decision["Decision Engine"]
        Stockfish["Stockfish Engine"]:::external
        MoveCalc["Move Calculator"]:::process
        BestMove["Best Move Selector"]:::process
    end

    subgraph Output["Output Layer"]
        MoveExec["Move Execution System"]:::process
        Interface["Chess.com Interface"]:::input
    end

    subgraph CNNArch["CNN Architecture"]
        Input50["Input (50x50)"]:::ai
        Conv1["Conv2D (32) + ReLU"]:::ai
        Pool1["MaxPooling2D"]:::ai
        Conv2["Conv2D (64) + ReLU"]:::ai
        Pool2["MaxPooling2D"]:::ai
        Flatten["Flatten"]:::ai
        Dense["Dense (64)"]:::ai
        Output13["Output (13 classes)"]:::ai
    end

    Storage[("Game State Storage")]:::storage

    ChessCom --> Screen
    Screen --> ImgProc
    ImgProc --> CNN
    CNN --> BoardAnalyzer
    BoardAnalyzer --> FEN
    FEN --> Validator
    Validator --> StateManager
    StateManager --> Storage
    Storage --> MoveCalc
    Stockfish --> MoveCalc
    MoveCalc --> BestMove
    BestMove --> MoveExec
    MoveExec --> Interface

    Input50 --> Conv1
    Conv1 --> Pool1
    Pool1 --> Conv2
    Conv2 --> Pool2
    Pool2 --> Flatten
    Flatten --> Dense
    Dense --> Output13

    %% Click events for component mapping
    click Vision "https://github.com/Tirth-Nagar/Chess-Bot/blob/main/Chess-Bot/data_processing.py"
    click CNN "https://github.com/Tirth-Nagar/Chess-Bot/blob/main/Chess-Bot/classifer_model.py"
    click Storage "https://github.com/Tirth-Nagar/Chess-Bot/tree/main/Chess-Bot/current_positions/"
    click Stockfish "https://github.com/Tirth-Nagar/Chess-Bot/blob/main/Chess-Bot/stockfish-windows-x86-64-avx2.exe"
    click GameLogic "https://github.com/Tirth-Nagar/Chess-Bot/blob/main/Chess-Bot/main.py"

    %% Legend
    subgraph Legend
        L1["Input/Output"]:::input
        L2["Processing"]:::process
        L3["AI/ML Components"]:::ai
        L4["External Services"]:::external
        L5["Storage"]:::storage
    end
```
