# lk16/swap architecture

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

- Interactive diagram: https://gitdiagram.com/lk16/swap
- Repository: https://github.com/lk16/swap

## Overview

##### Main Components

a) Frontend Layer:
- Web interface (assets/index.html, assets/app.js, assets/styles.css)
- WebSocket client connection for real-time game updates

b) Backend Server:
- Rust-based server implementation
- WebSocket server component (src/frontend/websocket.rs)
- Application logic handler (src/frontend/app.rs)

c) Game Engine Core:
- Othello game logic (src/othello/)
- Board representation
- Move generation and validation
- Evaluation

d) AI Systems:
1. Edax Bot (src/bot/edax/)
2. Squared Bot (src/bot/squared/)
- Random Bot as a simple alternative

##### Component Relationships

- Frontend ↔ Backend Server (WebSocket connection)
- Backend Server → Game Engine (game state management)
- Game Engine → AI Systems (position evaluation requests)
- AI Systems → Game Engine (move responses)

##### Detailed Component Breakdown

Game Engine (src/othello/):
**┌─────────────────────────┐**
- Game Engine        │
- ┤
- Board Representation  │
- Move Generation       │
- Position Evaluation   │
- Game State Management │
- ┘

AI Systems (src/bot/):
**┌─────────────────────┐    ┌─────────────────────┐**
- Edax Bot       │    │     Squared Bot     │
- ┤    ├─────────────────────┤
- Midgame Logic     │    │ - Midgame Logic     │
- Evaluation        │    │ - Endgame Logic     │
- Weights           │    └─────────────────────┘
- ┘

##### Implementation Details to Highlight

- Multiple move generation strategies:
- Edax bitscan
- Simple implementation
- Edax slow
- Separation between midgame and endgame logic in the AI systems
- Weight-based evaluation system in the Edax bot

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef backend fill:#2ecc71,stroke:#27ae60,color:white
    classDef engine fill:#e67e22,stroke:#d35400,color:white
    classDef ai fill:#9b59b6,stroke:#8e44ad,color:white
    classDef websocket stroke:#e74c3c,color:black
    
    %% Frontend Layer
    subgraph Frontend
        UI["Web Interface"]:::frontend
        ClientApp["Client Application"]:::frontend
        Styles["Styling"]:::frontend
    end
    
    %% Backend Server
    subgraph Server
        WS["WebSocket Server"]:::backend
        AppLogic["Application Logic"]:::backend
        ServerEntry["Server Entry Point"]:::backend
    end
    
    %% Game Engine
    subgraph GameEngine
        Board["Board Representation"]:::engine
        GameState["Game State Management"]:::engine
        Position["Position Management"]:::engine
        MoveGen["Move Generation"]:::engine
        
        subgraph MoveGenStrategies
            EdaxBitscan["Edax Bitscan"]:::engine
            Simple["Simple Implementation"]:::engine
            EdaxSlow["Edax Slow"]:::engine
        end
        
        Squares["Square Definitions"]:::engine
        FFO["FFO Problems Handler"]:::engine
    end
    
    %% AI Systems
    subgraph AISystems
        subgraph EdaxBot
            EdaxMain["Edax Main"]:::ai
            EdaxEval["Evaluation Logic"]:::ai
            EdaxMid["Midgame Strategy"]:::ai
            EdaxWeights["Weights System"]:::ai
        end
        
        subgraph SquaredBot
            SquaredMain["Squared Main"]:::ai
            SquaredEnd["Endgame Strategy"]:::ai
            SquaredMid["Midgame Strategy"]:::ai
        end
        
        RandomBot["Random Bot"]:::ai
    end
    
    %% Relationships
    UI <--> |WebSocket|WS:::websocket
    ClientApp --> WS
    WS --> AppLogic
    AppLogic --> GameState
    GameState --> Board
    Board --> Position
    Position --> MoveGen
    MoveGen --> MoveGenStrategies
    GameState --> AISystems
    AISystems --> GameState
    
    %% Click Events
    click UI "https://github.com/lk16/swap/blob/main/assets/index.html"
    click ClientApp "https://github.com/lk16/swap/blob/main/assets/app.js"
    click Styles "https://github.com/lk16/swap/blob/main/assets/styles.css"
    click WS "https://github.com/lk16/swap/blob/main/src/frontend/websocket.rs"
    click AppLogic "https://github.com/lk16/swap/blob/main/src/frontend/app.rs"
    click ServerEntry "https://github.com/lk16/swap/blob/main/src/main.rs"
    click Board "https://github.com/lk16/swap/blob/main/src/othello/board.rs"
    click GameState "https://github.com/lk16/swap/blob/main/src/othello/game.rs"
    click Position "https://github.com/lk16/swap/blob/main/src/othello/position.rs"
    click MoveGen "https://github.com/lk16/swap/blob/main/src/othello/get_moves.rs"
    click MoveGenStrategies "https://github.com/lk16/swap/tree/main/src/othello/do_move/"
    click Squares "https://github.com/lk16/swap/blob/main/src/othello/squares.rs"
    click FFO "https://github.com/lk16/swap/blob/main/src/othello/ffo_problems.rs"
    click EdaxMain "https://github.com/lk16/swap/blob/main/src/bot/edax/bot.rs"
    click EdaxEval "https://github.com/lk16/swap/blob/main/src/bot/edax/eval.rs"
    click EdaxMid "https://github.com/lk16/swap/blob/main/src/bot/edax/midgame.rs"
    click EdaxWeights "https://github.com/lk16/swap/blob/main/src/bot/edax/weights.rs"
    click SquaredMain "https://github.com/lk16/swap/blob/main/src/bot/squared/bot.rs"
    click SquaredEnd "https://github.com/lk16/swap/blob/main/src/bot/squared/endgame.rs"
    click SquaredMid "https://github.com/lk16/swap/blob/main/src/bot/squared/midgame.rs"
    click RandomBot "https://github.com/lk16/swap/blob/main/src/bot/random.rs"
    click EdaxBitscan "https://github.com/lk16/swap/blob/main/src/othello/do_move/edax_bitscan.rs"
    click Simple "https://github.com/lk16/swap/blob/main/src/othello/do_move/simple.rs"
    click EdaxSlow "https://github.com/lk16/swap/blob/main/src/othello/do_move/edax_slow.rs"
```
