# 2040pi/chess architecture

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

- Interactive diagram: https://gitdiagram.com/2040pi/chess
- Repository: https://github.com/2040pi/chess

## Overview

This repository is a chess engine with terminal, graphical, UCI-protocol, and self-play entry points. In the main workflow, a player or chess GUI supplies a position, an interface delegates computer moves to the engine, and the engine searches legal moves using evaluation and move ordering. Evaluation combines piece-square and material values with attacked-square scoring. The terminal and graphical interfaces, and the self-play runner, also write PGN output. The README gives no further capability detail; relationships shown here are supported by the sampled files and README evidence.

Main entry points: [`engine.py`](https://github.com/2040pi/chess/blob/main/engine.py) (Move selection), [`cli.py`](https://github.com/2040pi/chess/blob/main/cli.py) (Terminal game), [`gui.py`](https://github.com/2040pi/chess/blob/main/gui.py) (Graphical game), [`minimax.py`](https://github.com/2040pi/chess/blob/main/minimax.py) (Minimax search)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["Game interfaces"]
  node_cli["Terminal game<br/>[cli.py]"]
  node_gui["Graphical game<br/>[gui.py]"]
  node_uci["UCI protocol<br/>[uci.py]"]
  node_selfplay["Self-play runner<br/>[main.py]"]
end

subgraph group_search["Move search"]
  node_engine["Move selection<br/>[engine.py]"]
  node_minimax["Minimax search<br/>[minimax.py]"]
  node_move_ordering["Move ordering<br/>[preorderMoves.py]"]
end

subgraph group_evaluation["Position evaluation"]
  node_evaluation["Position scoring<br/>[eval.py]"]
  node_attack_scoring["Attacked-square scoring<br/>[attackedSquares.py]"]
  node_tables["Chess value tables<br/>[tables.py]"]
end

subgraph group_output["Game output"]
  node_pgn["PGN game file"]
end

node_terminal_player(("Terminal player"))
node_chess_gui(("Chess GUI"))
node_uci_client(("UCI client"))

node_terminal_player -->|"enters moves"| node_cli
node_cli -->|"requests moves"| node_engine
node_chess_gui -->|"enters moves"| node_gui
node_gui -->|"requests moves"| node_engine
node_uci_client -->|"sends commands"| node_uci
node_uci -->|"starts search"| node_engine
node_selfplay -->|"requests moves"| node_engine
node_engine -->|"selects move"| node_minimax
node_minimax -->|"orders moves"| node_move_ordering
node_minimax -->|"scores leaves"| node_evaluation
node_evaluation -->|"reads values"| node_tables
node_evaluation -->|"scores attacks"| node_attack_scoring
node_attack_scoring -->|"reads weights"| node_tables
node_move_ordering -->|"reads values"| node_tables
node_cli -->|"writes PGN"| node_pgn
node_gui -->|"writes PGN"| node_pgn
node_selfplay -->|"writes PGN"| node_pgn

click node_cli "https://github.com/2040pi/chess/blob/main/cli.py"
click node_gui "https://github.com/2040pi/chess/blob/main/gui.py"
click node_uci "https://github.com/2040pi/chess/blob/main/uci.py"
click node_selfplay "https://github.com/2040pi/chess/blob/main/main.py"
click node_engine "https://github.com/2040pi/chess/blob/main/engine.py"
click node_minimax "https://github.com/2040pi/chess/blob/main/minimax.py"
click node_move_ordering "https://github.com/2040pi/chess/blob/main/preorderMoves.py"
click node_evaluation "https://github.com/2040pi/chess/blob/main/eval.py"
click node_attack_scoring "https://github.com/2040pi/chess/blob/main/attackedSquares.py"
click node_tables "https://github.com/2040pi/chess/blob/main/tables.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_gui,node_uci,node_selfplay,node_chess_gui,node_uci_client toneBlue
class node_engine,node_minimax,node_move_ordering toneAmber
class node_evaluation,node_attack_scoring,node_tables toneMint
class node_pgn toneRose
class node_terminal_player toneIndigo
```

## Components

### Game interfaces

- [Terminal game](https://github.com/2040pi/chess/blob/main/cli.py): `cli.py`
- [Graphical game](https://github.com/2040pi/chess/blob/main/gui.py): `gui.py`
- [UCI protocol](https://github.com/2040pi/chess/blob/main/uci.py): `uci.py`
- [Self-play runner](https://github.com/2040pi/chess/blob/main/main.py): `main.py`

### Move search

- [Move selection](https://github.com/2040pi/chess/blob/main/engine.py): `engine.py`
- [Minimax search](https://github.com/2040pi/chess/blob/main/minimax.py): `minimax.py`
- [Move ordering](https://github.com/2040pi/chess/blob/main/preorderMoves.py): `preorderMoves.py`

### Position evaluation

- [Position scoring](https://github.com/2040pi/chess/blob/main/eval.py): `eval.py`
- [Attacked-square scoring](https://github.com/2040pi/chess/blob/main/attackedSquares.py): `attackedSquares.py`
- [Chess value tables](https://github.com/2040pi/chess/blob/main/tables.py): `tables.py`

### Game output

- **PGN game file**

### Other components

- **Terminal player**
- **Chess GUI**
- **UCI client**

## Connections

- Terminal player → Terminal game: enters moves (evidence: `cli.py`)
- Terminal game → Move selection: requests moves (evidence: `cli.py`)
- Chess GUI → Graphical game: enters moves (evidence: `gui.py`)
- Graphical game → Move selection: requests moves (evidence: `gui.py`)
- UCI client → UCI protocol: sends commands (evidence: `uci.py`)
- UCI protocol → Move selection: starts search (evidence: `uci.py`)
- Self-play runner → Move selection: requests moves (evidence: `main.py`)
- Move selection → Minimax search: selects move (evidence: `engine.py`)
- Minimax search → Move ordering: orders moves (evidence: `minimax.py`)
- Minimax search → Position scoring: scores leaves (evidence: `minimax.py`)
- Position scoring → Chess value tables: reads values (evidence: `eval.py`)
- Position scoring → Attacked-square scoring: scores attacks (evidence: `eval.py`)
- Attacked-square scoring → Chess value tables: reads weights (evidence: `attackedSquares.py`)
- Move ordering → Chess value tables: reads values (evidence: `preorderMoves.py`)
- Terminal game → PGN game file: writes PGN (evidence: `cli.py`)
- Graphical game → PGN game file: writes PGN (evidence: `gui.py`)
- Self-play runner → PGN game file: writes PGN (evidence: `main.py`)
