How response-r/hexzero-ml-based-connect6-engine works
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: ai_battle_manager.py Battle manager · engine.py Connect-6 engine · train_self_play.py Self-play training · dqn_search.py DQN search
Components 18
Presentation
- Qt entry point
qt_main.py - Qt UI composition
src/UI/qt_ui.py - Custom Qt controls
src/UI/qt_widgets.py - Console entry point
game/game_main.py - Console UI
game/console_ui.py
Game Core
- Board state · Owns the 19×19 board and legal-move state.
game/board.py - Connect-6 engine · Applies asymmetric opening and two-stone turn transitions.
game/engine.py
AI Strategies
- Modern classic search · Alpha-beta, MCTS, and UCT return legal Connect-6 moves.
- DQN search
src/models/dqn_search.py - Legacy AI family · Older minimax, MCTS, UCT, and DQN implementations await canonicalization.
game/ai_mm_AB.py
Experiments & Learning
- Battle manager
src/models/ai_battle_manager.py - Tournament runner
src/models/run_ai_battles.py - Self-play training
src/models/train_self_play.py - Training batch runner
src/models/train_run_batches.py - PyTorch weights · Generated locally as .pth checkpoints.
- Results and plots · Statistics, plots, and saved-game artifacts are produced locally.
Recording & Replay
- Game recorder
game/recorder.py - Replay executor · Reconstructs recorded move sequences against game state.
game/replay.py
Connections 22
- 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