# response-r/hexzero-ml-based-connect6-engine architecture

> Architecture diagram of the GitHub repository response-r/hexzero-ml-based-connect6-engine, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/response-r/hexzero-ml-based-connect6-engine
- Repository: https://github.com/response-r/hexzero-ml-based-connect6-engine

## Overview

#### Purpose and Entry Points
Python 3.8+ Connect-6 experiment platform: a 19×19 game engine supporting human/AI play, AI-vs-AI evaluation, replay, and DQN self-play training. Primary GUI entry point is `qt_main.py`; legacy/console execution is rooted in `game/game_main.py`. Batch evaluation and training are implemented under `src/models/run_ai_battles.py` and `src/models/train_self_play.py`.

#### Game Domain and Rule Engine
The game state, legal-move handling, turn progression, and win evaluation are centralized in the legacy game package, chiefly `game/board.py` and `game/engine.py`. The engine enforces Connect-6’s asymmetric opening (one black stone) followed by two-stone turns, providing the state-transition boundary consumed by UIs, search algorithms, and recorders.

#### Interaction Layer
Two presentation modes drive the engine:
- PyQt GUI bootstrap: `qt_main.py`, with UI composition in `src/UI/qt_ui.py` and custom controls in `src/UI/qt_widgets.py`.
- Console mode: `game/console_ui.py`, invoked through `game/game_main.py`.

These layers should remain orchestration/presentation boundaries; game rules and AI move selection belong below them.

#### AI Strategy Implementations
Classic search implementations are separated by algorithm in `src/algorithms/`: Alpha-Beta Minimax in `src/algorithms/minimax_ab_search.py`, plain Monte Carlo tree search in `src/algorithms/mcts_search.py`, and UCB1-guided UCT in `src/algorithms/uct_search.py`. Each consumes board state and returns legal Connect-6 moves, while differing in expansion, simulation, evaluation, and selection policies.

A parallel older AI implementation family exists in `game/ai_mm_AB.py`, `game/ai_mcts.py`, and `game/ai_uct.py`; integration work should establish which family is canonical before cross-cutting changes.

#### DQN Learning Pipeline
DQN inference/search is represented by `src/models/dqn_search.py`; the repository also contains an older counterpart at `game/ai_dqn.py`. Self-play training is orchestrated by `src/models/train_self_play.py`, with repeated/batched execution in `src/models/train_run_batches.py`. Training produces local PyTorch `.pth` weights; inference defaults to random network parameters unless a generated or downloaded checkpoint is configured.

#### Experiment Orchestration and Outputs
`src/models/ai_battle_manager.py` coordinates individual algorithm matchups, while `src/models/run_ai_battles.py` launches automated tournaments. The flow is: choose AI pairings → repeatedly initialize/play engine games → collect outcomes and move histories → export statistics/plots and saved-game artifacts. Plot and model artifacts are generated locally; pretrained weights and larger experiment outputs are distributed externally through Google Drive.

#### Recording and Replay
Match histories are serialized as JSON through `game/recorder.py`. Replay execution is split between `game/replay.py` and `game/replayer.py`, which reconstruct recorded move sequences against game state. This persistence boundary lets GUI, console, and batch experiments consume historical games without rerunning AI decisions.

#### Runtime and Dependencies
The runtime is Python with PyQt for desktop GUI and PyTorch for DQN training/inference. Search workloads can become expensive because normal turns choose pairs of board positions; MCTS/UCT simulation counts are the principal practical compute-control boundary. Audio assets are local (`game/sound/stone.wav`) and playback support is implemented in `game/sound/make_sound.py`.

Main entry points: [`src/models/ai_battle_manager.py`](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/ai_battle_manager.py) (Battle manager), [`game/engine.py`](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/engine.py) (Connect-6 engine), [`src/models/train_self_play.py`](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/train_self_play.py) (Self-play training), [`src/models/dqn_search.py`](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/dqn_search.py) (DQN search)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_presentation["Presentation"]
  node_qt_entry["Qt entry point<br/>Python bootstrap<br/>[qt_main.py]"]
  node_qt_ui["Qt UI composition<br/>PyQt UI<br/>[qt_ui.py]"]
  node_qt_widgets["Custom Qt controls<br/>PyQt widgets<br/>[qt_widgets.py]"]
  node_console_entry["Console entry point<br/>Python bootstrap<br/>[game_main.py]"]
  node_console_ui["Console UI<br/>terminal presentation<br/>[console_ui.py]"]
end

subgraph group_game["Game Core"]
  node_board["Board state<br/>game state<br/>[board.py]"]
  node_engine{{"Connect-6 engine<br/>rule engine<br/>[engine.py]"}}
end

subgraph group_ai["AI Strategies"]
  node_classic_search["Modern classic search<br/>search algorithms"]
  node_dqn_search["DQN search<br/>DQN inference<br/>[dqn_search.py]"]
  node_legacy_ai["Legacy AI family<br/>older strategies<br/>[ai_mm_AB.py]"]
end

subgraph group_experiments["Experiments &amp; Learning"]
  node_battle_manager["Battle manager<br/>match coordinator"]
  node_battle_runner["Tournament runner<br/>batch evaluation<br/>[run_ai_battles.py]"]
  node_self_play["Self-play training<br/>DQN trainer<br/>[train_self_play.py]"]
  node_batch_training["Training batch runner<br/>batch orchestration"]
  node_model_weights[("PyTorch weights<br/>model artifact")]
  node_experiment_outputs["Results and plots<br/>experiment artifacts"]
end

subgraph group_persistence["Recording &amp; Replay"]
  node_recorder["Game recorder<br/>JSON persistence<br/>[recorder.py]"]
  node_replay["Replay executor<br/>history playback<br/>[replay.py]"]
end

node_qt_entry -->|"boots"| node_qt_ui
node_qt_ui -->|"composes"| node_qt_widgets
node_qt_ui -->|"drives games"| node_engine
node_console_entry -->|"launches"| node_console_ui
node_console_ui -->|"drives games"| node_engine
node_engine -->|"updates state"| node_board
node_classic_search -->|"reads legal state"| node_board
node_dqn_search -->|"reads state"| node_board
node_dqn_search -.->|"loads checkpoint"| node_model_weights
node_legacy_ai -->|"uses state"| node_board
node_battle_runner -->|"launches matchups"| node_battle_manager
node_battle_manager -->|"plays games"| node_engine
node_battle_manager -->|"selects strategies"| node_classic_search
node_battle_manager -->|"selects strategy"| node_dqn_search
node_battle_manager -->|"records histories"| node_recorder
node_battle_manager -->|"exports results"| node_experiment_outputs
node_batch_training -->|"repeats runs"| node_self_play
node_self_play -->|"generates self-play"| node_engine
node_self_play -->|"trains policy"| node_dqn_search
node_self_play -->|"writes checkpoints"| node_model_weights
node_recorder -->|"provides JSON histories"| node_replay
node_replay -->|"reconstructs games"| node_engine

click node_qt_entry "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/qt_main.py"
click node_qt_ui "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/UI/qt_ui.py"
click node_qt_widgets "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/UI/qt_widgets.py"
click node_console_entry "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/game_main.py"
click node_console_ui "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/console_ui.py"
click node_board "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/board.py"
click node_engine "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/engine.py"
click node_dqn_search "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/dqn_search.py"
click node_legacy_ai "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/ai_mm_AB.py"
click node_battle_manager "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/ai_battle_manager.py"
click node_battle_runner "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/run_ai_battles.py"
click node_self_play "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/train_self_play.py"
click node_batch_training "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/train_run_batches.py"
click node_recorder "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/recorder.py"
click node_replay "https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/replay.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_qt_entry,node_qt_ui,node_qt_widgets,node_console_entry,node_console_ui toneBlue
class node_board,node_engine toneAmber
class node_classic_search,node_dqn_search,node_legacy_ai toneMint
class node_battle_manager,node_battle_runner,node_self_play,node_batch_training,node_model_weights,node_experiment_outputs toneRose
class node_recorder,node_replay toneIndigo
```

## Components

### Presentation

- [Qt entry point](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/qt_main.py): Python bootstrap · `qt_main.py`
- [Qt UI composition](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/UI/qt_ui.py): PyQt UI · `src/UI/qt_ui.py`
- [Custom Qt controls](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/UI/qt_widgets.py): PyQt widgets · `src/UI/qt_widgets.py`
- [Console entry point](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/game_main.py): Python bootstrap · `game/game_main.py`
- [Console UI](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/console_ui.py): terminal presentation · `game/console_ui.py`

### Game Core

- [Board state](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/board.py): game state · Owns the 19×19 board and legal-move state. · `game/board.py`
- [Connect-6 engine](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/engine.py): rule engine · Applies asymmetric opening and two-stone turn transitions. · `game/engine.py`

### AI Strategies

- **Modern classic search**: search algorithms · Alpha-beta, MCTS, and UCT return legal Connect-6 moves.
- [DQN search](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/dqn_search.py): DQN inference · `src/models/dqn_search.py`
- [Legacy AI family](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/ai_mm_AB.py): older strategies · Older minimax, MCTS, UCT, and DQN implementations await canonicalization. · `game/ai_mm_AB.py`

### Experiments & Learning

- [Battle manager](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/ai_battle_manager.py): match coordinator · `src/models/ai_battle_manager.py`
- [Tournament runner](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/run_ai_battles.py): batch evaluation · `src/models/run_ai_battles.py`
- [Self-play training](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/train_self_play.py): DQN trainer · `src/models/train_self_play.py`
- [Training batch runner](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/src/models/train_run_batches.py): batch orchestration · `src/models/train_run_batches.py`
- **PyTorch weights**: model artifact · Generated locally as .pth checkpoints.
- **Results and plots**: experiment artifacts · Statistics, plots, and saved-game artifacts are produced locally.

### Recording & Replay

- [Game recorder](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/recorder.py): JSON persistence · `game/recorder.py`
- [Replay executor](https://github.com/response-r/hexzero-ml-based-connect6-engine/blob/main/game/replay.py): history playback · Reconstructs recorded move sequences against game state. · `game/replay.py`

## Connections

- Qt entry point → Qt UI composition: boots
- Qt UI composition → Custom Qt controls: composes
- Qt UI composition → Connect-6 engine: drives games
- Console entry point → Console UI: launches
- Console UI → Connect-6 engine: drives games
- Connect-6 engine → Board state: updates state
- Modern classic search → Board state: reads legal state
- DQN search → Board state: reads state
- DQN search → PyTorch weights: loads checkpoint
- Legacy AI family → Board state: uses state
- Tournament runner → Battle manager: launches matchups
- Battle manager → Connect-6 engine: plays games
- Battle manager → Modern classic search: selects strategies
- Battle manager → DQN search: selects strategy
- Battle manager → Game recorder: records histories
- Battle manager → Results and plots: exports results
- Training batch runner → Self-play training: repeats runs
- Self-play training → Connect-6 engine: generates self-play
- Self-play training → DQN search: trains policy
- Self-play training → PyTorch weights: writes checkpoints
- Game recorder → Replay executor: provides JSON histories
- Replay executor → Connect-6 engine: reconstructs games
