# empa2/py-chess architecture

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

- Interactive diagram: https://gitdiagram.com/empa2/py-chess
- Repository: https://github.com/empa2/py-chess

## Overview

PyChess is a UCI chess engine. Its principal workflow is a GUI or other UCI client sending position and search commands, the engine searching the position, and returning a best move. Internally, search relies on board representation, move generation and make/unmake, hashing, draw rules, move ordering, and NNUE evaluation. The README documents these capabilities; edges into internals are limited to relationships supported by the excerpts or source index, so some implementation details remain unconnected.

Main entry points: [`engine/search.py`](https://github.com/empa2/py-chess/blob/main/engine/search.py) (Iterative search), [`engine/movegen.py`](https://github.com/empa2/py-chess/blob/main/engine/movegen.py) (Move generation), [`engine/see.py`](https://github.com/empa2/py-chess/blob/main/engine/see.py) (Exchange evaluation), [`engine/nnue.py`](https://github.com/empa2/py-chess/blob/main/engine/nnue.py) (NNUE evaluator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interface["UCI interface"]
  node_uci["UCI protocol<br/>[uci.py]"]
end

subgraph group_position["Position and moves"]
  node_board["Board and FEN<br/>[board.py]"]
  node_bitboard["Bitboard utilities<br/>[bitboard.py]"]
  node_attacks["Attack tables<br/>[attacks.py]"]
  node_magic["Magic attacks<br/>[magic.py]"]
  node_move["Move encoding<br/>[move.py]"]
  node_movegen["Move generation<br/>[movegen.py]"]
  node_make["Make and unmake<br/>[make_move.py]"]
end

subgraph group_search["Search and evaluation"]
  node_searchcore["Iterative search<br/>[search.py]"]
  node_nnue["NNUE evaluator<br/>[nnue.py]"]
  node_accumulator["NNUE accumulator"]
  node_network["NNUE network<br/>[nnue_128_v1.bin]"]
  node_rules["Draw rules<br/>[rules.py]"]
end

subgraph group_support["Search support"]
  node_see["Exchange evaluation<br/>[see.py]"]
  node_hashing["Zobrist hashing<br/>[zobrist.py]"]
  node_tt[("Transposition table<br/>[tt.py]")]
  node_stats["Search statistics<br/>[search_stats.py]"]
end

node_client(("UCI client"))

node_client -->|"sends commands"| node_uci
node_uci -->|"starts search"| node_searchcore
node_searchcore -->|"uses position"| node_board
node_searchcore -->|"generates moves"| node_movegen
node_searchcore -->|"plays moves"| node_make
node_searchcore -->|"evaluates positions"| node_nnue
node_searchcore -->|"updates evaluation"| node_accumulator
node_searchcore -->|"checks draws"| node_rules
node_searchcore -->|"orders captures"| node_see
node_searchcore -->|"tracks positions"| node_hashing
node_searchcore -->|"probes table"| node_tt
node_searchcore -->|"records metrics"| node_stats
node_nnue -.->|"loads weights"| node_network
node_accumulator -->|"uses evaluator"| node_nnue
node_movegen -->|"encodes moves"| node_move
node_movegen -->|"uses attack tables"| node_attacks
node_movegen -->|"queries sliders"| node_magic
node_see -->|"reads attacks"| node_attacks
node_see -->|"queries sliders"| node_magic
node_board -.->|"uses bitboards"| node_bitboard

click node_uci "https://github.com/empa2/py-chess/blob/main/uci.py"
click node_board "https://github.com/empa2/py-chess/blob/main/engine/board.py"
click node_bitboard "https://github.com/empa2/py-chess/blob/main/engine/bitboard.py"
click node_attacks "https://github.com/empa2/py-chess/blob/main/engine/attacks.py"
click node_magic "https://github.com/empa2/py-chess/blob/main/engine/magic.py"
click node_move "https://github.com/empa2/py-chess/blob/main/engine/move.py"
click node_movegen "https://github.com/empa2/py-chess/blob/main/engine/movegen.py"
click node_make "https://github.com/empa2/py-chess/blob/main/engine/make_move.py"
click node_searchcore "https://github.com/empa2/py-chess/blob/main/engine/search.py"
click node_nnue "https://github.com/empa2/py-chess/blob/main/engine/nnue.py"
click node_accumulator "https://github.com/empa2/py-chess/blob/main/engine/nnue_accumulator.py"
click node_network "https://github.com/empa2/py-chess/blob/main/nets/nnue_128_v1.bin"
click node_rules "https://github.com/empa2/py-chess/blob/main/engine/rules.py"
click node_see "https://github.com/empa2/py-chess/blob/main/engine/see.py"
click node_hashing "https://github.com/empa2/py-chess/blob/main/engine/zobrist.py"
click node_tt "https://github.com/empa2/py-chess/blob/main/engine/tt.py"
click node_stats "https://github.com/empa2/py-chess/blob/main/engine/search_stats.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_uci,node_client toneBlue
class node_board,node_bitboard,node_attacks,node_magic,node_move,node_movegen,node_make toneAmber
class node_searchcore,node_nnue,node_accumulator,node_network,node_rules toneMint
class node_see,node_hashing,node_tt,node_stats toneRose
```

## Components

### UCI interface

- [UCI protocol](https://github.com/empa2/py-chess/blob/main/uci.py): `uci.py`

### Position and moves

- [Board and FEN](https://github.com/empa2/py-chess/blob/main/engine/board.py): `engine/board.py`
- [Bitboard utilities](https://github.com/empa2/py-chess/blob/main/engine/bitboard.py): `engine/bitboard.py`
- [Attack tables](https://github.com/empa2/py-chess/blob/main/engine/attacks.py): `engine/attacks.py`
- [Magic attacks](https://github.com/empa2/py-chess/blob/main/engine/magic.py): `engine/magic.py`
- [Move encoding](https://github.com/empa2/py-chess/blob/main/engine/move.py): `engine/move.py`
- [Move generation](https://github.com/empa2/py-chess/blob/main/engine/movegen.py): `engine/movegen.py`
- [Make and unmake](https://github.com/empa2/py-chess/blob/main/engine/make_move.py): `engine/make_move.py`

### Search and evaluation

- [Iterative search](https://github.com/empa2/py-chess/blob/main/engine/search.py): `engine/search.py`
- [NNUE evaluator](https://github.com/empa2/py-chess/blob/main/engine/nnue.py): `engine/nnue.py`
- [NNUE accumulator](https://github.com/empa2/py-chess/blob/main/engine/nnue_accumulator.py): `engine/nnue_accumulator.py`
- [NNUE network](https://github.com/empa2/py-chess/blob/main/nets/nnue_128_v1.bin): `nets/nnue_128_v1.bin`
- [Draw rules](https://github.com/empa2/py-chess/blob/main/engine/rules.py): `engine/rules.py`

### Search support

- [Exchange evaluation](https://github.com/empa2/py-chess/blob/main/engine/see.py): `engine/see.py`
- [Zobrist hashing](https://github.com/empa2/py-chess/blob/main/engine/zobrist.py): `engine/zobrist.py`
- [Transposition table](https://github.com/empa2/py-chess/blob/main/engine/tt.py): `engine/tt.py`
- [Search statistics](https://github.com/empa2/py-chess/blob/main/engine/search_stats.py): `engine/search_stats.py`

### Other components

- **UCI client**

## Connections

- UCI client → UCI protocol: sends commands (evidence: `README.md`)
- UCI protocol → Iterative search: starts search (evidence: `README.md`)
- Iterative search → Board and FEN: uses position (evidence: `engine/search.py`)
- Iterative search → Move generation: generates moves (evidence: `engine/search.py`)
- Iterative search → Make and unmake: plays moves (evidence: `engine/search.py`)
- Iterative search → NNUE evaluator: evaluates positions (evidence: `engine/search.py`)
- Iterative search → NNUE accumulator: updates evaluation (evidence: `engine/search.py`)
- Iterative search → Draw rules: checks draws (evidence: `engine/search.py`)
- Iterative search → Exchange evaluation: orders captures (evidence: `engine/search.py`)
- Iterative search → Zobrist hashing: tracks positions (evidence: `engine/search.py`)
- Iterative search → Transposition table: probes table (evidence: `engine/search.py`)
- Iterative search → Search statistics: records metrics (evidence: `engine/search.py`)
- NNUE evaluator → NNUE network: loads weights
- NNUE accumulator → NNUE evaluator: uses evaluator (evidence: `engine/nnue_accumulator.py`)
- Move generation → Move encoding: encodes moves (evidence: `engine/movegen.py`)
- Move generation → Attack tables: uses attack tables (evidence: `engine/movegen.py`)
- Move generation → Magic attacks: queries sliders (evidence: `engine/movegen.py`)
- Exchange evaluation → Attack tables: reads attacks (evidence: `engine/see.py`)
- Exchange evaluation → Magic attacks: queries sliders (evidence: `engine/see.py`)
- Board and FEN → Bitboard utilities: uses bitboards
