mangala-game 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mangala_game-0.1.0/.gitignore +34 -0
- mangala_game-0.1.0/CHANGELOG.md +55 -0
- mangala_game-0.1.0/LICENSE +21 -0
- mangala_game-0.1.0/PKG-INFO +304 -0
- mangala_game-0.1.0/README.md +266 -0
- mangala_game-0.1.0/docs/architecture.md +105 -0
- mangala_game-0.1.0/docs/assets/banner.svg +72 -0
- mangala_game-0.1.0/docs/devlog.md +310 -0
- mangala_game-0.1.0/docs/rules.md +98 -0
- mangala_game-0.1.0/pyproject.toml +119 -0
- mangala_game-0.1.0/src/mangala/__init__.py +32 -0
- mangala_game-0.1.0/src/mangala/__main__.py +5 -0
- mangala_game-0.1.0/src/mangala/ai.py +630 -0
- mangala_game-0.1.0/src/mangala/board.py +69 -0
- mangala_game-0.1.0/src/mangala/book.py +77 -0
- mangala_game-0.1.0/src/mangala/cli.py +339 -0
- mangala_game-0.1.0/src/mangala/engine.py +262 -0
- mangala_game-0.1.0/src/mangala/gui_pygame.py +844 -0
- mangala_game-0.1.0/src/mangala/py.typed +0 -0
- mangala_game-0.1.0/src/mangala/server.py +293 -0
- mangala_game-0.1.0/src/mangala/web/ai.js +276 -0
- mangala_game-0.1.0/src/mangala/web/book.js +74 -0
- mangala_game-0.1.0/src/mangala/web/engine.js +169 -0
- mangala_game-0.1.0/src/mangala/web/index.html +2198 -0
- mangala_game-0.1.0/src/mangala/web/manifest.json +17 -0
- mangala_game-0.1.0/src/mangala/web/sw.js +34 -0
- mangala_game-0.1.0/tests/fixtures/engine_parity_fixtures.json +39377 -0
- mangala_game-0.1.0/tests/test_ai.py +344 -0
- mangala_game-0.1.0/tests/test_board.py +40 -0
- mangala_game-0.1.0/tests/test_cli.py +263 -0
- mangala_game-0.1.0/tests/test_engine.py +312 -0
- mangala_game-0.1.0/tests/test_fixtures_parity.py +44 -0
- mangala_game-0.1.0/tests/test_gui.py +42 -0
- mangala_game-0.1.0/tests/test_parity.mjs +79 -0
- mangala_game-0.1.0/tests/test_properties.py +66 -0
- mangala_game-0.1.0/tests/test_selfplay.py +52 -0
- mangala_game-0.1.0/tests/test_server.py +344 -0
- mangala_game-0.1.0/tools/generate_parity_fixtures.py +117 -0
- mangala_game-0.1.0/tools/selfplay.py +129 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.egg-info/
|
|
5
|
+
build/
|
|
6
|
+
dist/
|
|
7
|
+
.eggs/
|
|
8
|
+
|
|
9
|
+
# Virtual environments
|
|
10
|
+
.venv/
|
|
11
|
+
venv/
|
|
12
|
+
env/
|
|
13
|
+
|
|
14
|
+
# Tooling caches / reports
|
|
15
|
+
.pytest_cache/
|
|
16
|
+
.ruff_cache/
|
|
17
|
+
.mypy_cache/
|
|
18
|
+
.hypothesis/
|
|
19
|
+
.coverage
|
|
20
|
+
.coverage.*
|
|
21
|
+
coverage.xml
|
|
22
|
+
htmlcov/
|
|
23
|
+
|
|
24
|
+
# Editors / OS
|
|
25
|
+
.vscode/
|
|
26
|
+
.idea/
|
|
27
|
+
*.swp
|
|
28
|
+
.DS_Store
|
|
29
|
+
Thumbs.db
|
|
30
|
+
|
|
31
|
+
# Local AI-assistant settings
|
|
32
|
+
.claude/
|
|
33
|
+
.gemini/
|
|
34
|
+
GEMINI.md
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## [0.1.0] - 2026-11-02
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- **Core Engine (`mangala.engine`)**:
|
|
14
|
+
- Pure, functional rules engine with frozen `GameState` dataclass.
|
|
15
|
+
- Strict input validation via `validate_state()`.
|
|
16
|
+
- Configurable end-game sweep rules (`SWEEP_EMPTIED_SIDE` default, `SWEEP_LAST_MOVER` variant).
|
|
17
|
+
- Complete rule implementation: leave-one-behind, single stone pickup, opponent treasury skipping, extra turns, even captures, and empty-hole captures.
|
|
18
|
+
- **AI Search Engine (`mangala.ai`)**:
|
|
19
|
+
- Thread-safe `Searcher` class with isolated transposition table, deadline, and node telemetry.
|
|
20
|
+
- Symmetrical Minimax with Alpha-Beta pruning handling extra-turn branches without negamax inversions.
|
|
21
|
+
- Quiescence search extending captures and extra turns up to 4 plies.
|
|
22
|
+
- Iterative deepening with anytime time budget management (`time_limit`).
|
|
23
|
+
- Endgame exact solver for positions with $\le 12$ stones remaining on the board.
|
|
24
|
+
- Difficulty presets: `easy`, `medium`, `hard`, and `expert`.
|
|
25
|
+
- Comprehensive position analysis API (`analyse()` returning best move, score, depth, node count, and principal variation).
|
|
26
|
+
- **Web Front-End & HTTP Backend (`mangala.server`, `src/mangala/web/index.html`)**:
|
|
27
|
+
- Stateless JSON REST API (`GET /api/new`, `POST /api/move`, `POST /api/ai`).
|
|
28
|
+
- Single-file modern web UI featuring felt/wood aesthetics, animated stone sowing, move log, analysis tools (undo, redo, pass, AI hint), and 180ยฐ board flip.
|
|
29
|
+
- Difficulty selector pills (Easy, Medium, Hard, Expert) and animation speed controls (โก 1x, 2x, Instant).
|
|
30
|
+
- Keyboard navigation for rapid play (keys `1`โ`6` for pits, `F` to flip board).
|
|
31
|
+
- Dual-mode operation: seamless automatic fallback to client-side JavaScript engine for offline play and static GitHub Pages hosting.
|
|
32
|
+
- Request body size limits ($4096$ bytes), time limit clamping ($[0.05, 5.0]$ seconds), request body draining, and concurrent search throttling.
|
|
33
|
+
- Strict security headers (CSP, X-Frame-Options, X-Content-Type-Options) and path traversal protections.
|
|
34
|
+
- **Cross-Language Engine & Parity Testing (`src/mangala/web/engine.js`, `ai.js`, `tests/test_parity.mjs`)**:
|
|
35
|
+
- Client-side JavaScript rules and minimax AI engines matching Python implementation.
|
|
36
|
+
- Fixture generator (`tools/generate_parity_fixtures.py`) creating 629 corner-case and playout state transitions.
|
|
37
|
+
- Cross-language parity runner (`node --test tests/test_parity.mjs`) validating 100% equivalence in under 10ms.
|
|
38
|
+
- **Terminal CLI (`mangala.cli`)**:
|
|
39
|
+
- ASCII board visualizer with active player indicators and ANSI color support.
|
|
40
|
+
- Interactive Human vs Human, Human vs AI, and AI vs AI modes with selectable difficulty levels.
|
|
41
|
+
- Safe UTF-8 console reconfiguring for Windows without destroying stream descriptors.
|
|
42
|
+
- **Pygame Desktop GUI (`mangala.gui_pygame`)**:
|
|
43
|
+
- Desktop interface with smooth frame animation, capture banners, move log, and analysis mode.
|
|
44
|
+
- Headless test support with dummy video driver fallback.
|
|
45
|
+
- **Development & Packaging**:
|
|
46
|
+
- PEP 621 compliant `pyproject.toml` with `hatchling` build backend.
|
|
47
|
+
- PEP 561 `py.typed` marker file for library type checker compliance.
|
|
48
|
+
- Comprehensive 78-test suite with 97% overall code coverage.
|
|
49
|
+
- AI-vs-AI tournament benchmark arena tool (`tools/selfplay.py`).
|
|
50
|
+
- GitHub Actions CI matrix across Linux & Windows on Python 3.10 through 3.13 and Node.js parity testing.
|
|
51
|
+
|
|
52
|
+
### Fixed
|
|
53
|
+
- Fixed race condition where concurrent AI calls shared a module-level deadline and cut off longer searches prematurely.
|
|
54
|
+
- Fixed server input validation allowing malformed states and unbound time limits.
|
|
55
|
+
- Corrected outdated rule descriptions in CLI and docstrings regarding chain captures and sweep directions.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mammadow
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: mangala-game
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Turkish Mangala (mancala) with a strong alpha-beta AI, a web UI, a terminal UI and a Pygame UI.
|
|
5
|
+
Project-URL: Homepage, https://github.com/mammadow/mangala
|
|
6
|
+
Project-URL: Issues, https://github.com/mammadow/mangala/issues
|
|
7
|
+
Project-URL: Changelog, https://github.com/mammadow/mangala/blob/main/CHANGELOG.md
|
|
8
|
+
Author: mammadow
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: alpha-beta,board-game,game-ai,mancala,mangala,minimax
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Environment :: Web Environment
|
|
15
|
+
Classifier: Intended Audience :: Education
|
|
16
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
25
|
+
Classifier: Topic :: Games/Entertainment :: Board Games
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Python: >=3.10
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: hypothesis>=6.100; extra == 'dev'
|
|
30
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
31
|
+
Requires-Dist: pre-commit>=3.7; extra == 'dev'
|
|
32
|
+
Requires-Dist: pytest-cov>=5; extra == 'dev'
|
|
33
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
34
|
+
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
35
|
+
Provides-Extra: gui
|
|
36
|
+
Requires-Dist: pygame>=2.5; extra == 'gui'
|
|
37
|
+
Description-Content-Type: text/markdown
|
|
38
|
+
|
|
39
|
+
<div align="center">
|
|
40
|
+
|
|
41
|
+
# ๐บ Turkish Mangala (Mancala)
|
|
42
|
+
|
|
43
|
+
[](https://mammadow.github.io/mangala/)
|
|
44
|
+
|
|
45
|
+
### Authentic Game Engine โข Tournament AI โข Responsive Web โข Terminal CLI โข Desktop Arcade
|
|
46
|
+
|
|
47
|
+
[](https://github.com/mammadow/mangala/actions/workflows/ci.yml)
|
|
48
|
+
[](https://github.com/mammadow/mangala/releases)
|
|
49
|
+
[](https://github.com/mammadow/mangala)
|
|
50
|
+
[](https://github.com/mammadow/mangala)
|
|
51
|
+
[](https://github.com/mammadow/mangala)
|
|
52
|
+
[](LICENSE)
|
|
53
|
+
[](https://github.com/astral-sh/ruff)
|
|
54
|
+
|
|
55
|
+
<p align="center">
|
|
56
|
+
<a href="https://mammadow.github.io/mangala/"><strong>๐ฎ Play Live in Browser</strong></a> โข
|
|
57
|
+
<a href="#-quick-start"><strong>๐ Quick Start</strong></a> โข
|
|
58
|
+
<a href="#-three-ways-to-play"><strong>๐น๏ธ Interfaces</strong></a> โข
|
|
59
|
+
<a href="#-turkish-mangala-rules"><strong>๐ Rules</strong></a> โข
|
|
60
|
+
<a href="#-ai-architecture"><strong>๐ง AI Engine</strong></a> โข
|
|
61
|
+
<a href="docs/architecture.md"><strong>๐ Architecture</strong></a>
|
|
62
|
+
</p>
|
|
63
|
+
|
|
64
|
+
</div>
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## ๐ Highlights
|
|
69
|
+
|
|
70
|
+
| Capability | Specification |
|
|
71
|
+
| :--- | :--- |
|
|
72
|
+
| **Pure Standard Library Engine** | Zero runtime dependencies (`dependencies = []`). Immutable `GameState` verified by property-based Hypothesis fuzzing. |
|
|
73
|
+
| **Tournament-Grade AI** | Alpha-Beta minimax with symmetrical extra-turn handling, 64-bit Transposition Table, PV move ordering, Quiescence search, and an endgame exact solver for $\le 12$ stones. |
|
|
74
|
+
| **Dual-Mode Web UI** | Modern wood & felt responsive interface. Seamlessly runs against a Python REST API or directly offline in the browser via client-side JavaScript engine. |
|
|
75
|
+
| **Cross-Language Parity** | 100% rules parity between Python and JavaScript engines validated across 629 corner-case and simulated playout test fixtures. |
|
|
76
|
+
| **Multi-Platform Support** | Run anywhere: Web browser, terminal (ANSI/ASCII), or desktop arcade window (Pygame). |
|
|
77
|
+
| **Thread-Safe Architecture** | Concurrency-isolated `Searcher` contexts allow simultaneous background AI analysis, hint generation, and multi-user web serving without race conditions. |
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## ๐ Quick Start
|
|
82
|
+
|
|
83
|
+
### Installation
|
|
84
|
+
|
|
85
|
+
Install via `pip`:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
# Standard installation (Web GUI + CLI, zero external dependencies)
|
|
89
|
+
pip install mangala-game
|
|
90
|
+
|
|
91
|
+
# Optional Pygame desktop GUI
|
|
92
|
+
pip install "mangala-game[gui]"
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Or clone the repository to run from source:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
git clone https://github.com/mammadow/mangala.git
|
|
99
|
+
cd mangala
|
|
100
|
+
python -m venv .venv
|
|
101
|
+
source .venv/bin/activate # Windows: .venv\Scripts\activate
|
|
102
|
+
pip install -e ".[dev,gui]"
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
---
|
|
106
|
+
|
|
107
|
+
## ๐น๏ธ Three Ways to Play
|
|
108
|
+
|
|
109
|
+
### 1. ๐ Modern Web GUI
|
|
110
|
+
Launch the local web server (automatically opens in your browser at `http://localhost:8000`):
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
mangala
|
|
114
|
+
# or: python -m mangala.server
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
- **Features**: Animated stone sowing, difficulty pills (`Easy`, `Medium`, `Hard`, `Expert`), speed toggle (`โก 1x`, `2x`, `Instant`), 180ยฐ board rotation (`F` key), keyboard pit sowing (keys `1`โ`6`), live move history, undo/redo, and interactive AI coach hints.
|
|
118
|
+
- **Offline / Static**: You can also open [`src/mangala/web/index.html`](src/mangala/web/index.html) directly in any modern browser without Python running!
|
|
119
|
+
|
|
120
|
+
### 2. ๐ป Terminal CLI
|
|
121
|
+
Play an authentic text-based game directly inside your terminal with active player indicators and ANSI color highlights:
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
mangala-cli
|
|
125
|
+
# or: python -m mangala.cli
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
- **Modes**:
|
|
129
|
+
- `1` : Human vs Human (hotseat)
|
|
130
|
+
- `2` : Human vs AI (choose your side and difficulty)
|
|
131
|
+
- `3` : AI vs AI (spectate the minimax engine analyzing in real time)
|
|
132
|
+
|
|
133
|
+
### 3. ๐ฎ Pygame Desktop GUI
|
|
134
|
+
For an arcade-style experience with desktop window physics, stone drop sounds, and capture banners:
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
mangala-gui
|
|
138
|
+
# or: python -m mangala.gui_pygame
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
## ๐ Turkish Mangala Rules
|
|
144
|
+
|
|
145
|
+
Turkish Mangala is played on a board with **12 pits** (6 per player) and **2 treasuries** (*Hazine*). Total of **48 stones** (4 in each pit initially).
|
|
146
|
+
|
|
147
|
+
```text
|
|
148
|
+
Player 2 Pits (top row, counterclockwise)
|
|
149
|
+
12 11 10 9 8 7
|
|
150
|
+
P2 [ 0] [ 4] [ 4] [ 4] [ 4] [ 4] [ 4] [ 0] P1
|
|
151
|
+
[ 4] [ 4] [ 4] [ 4] [ 4] [ 4]
|
|
152
|
+
0 1 2 3 4 5
|
|
153
|
+
Player 1 Pits (bottom row, counterclockwise)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### The 4 Core Rules
|
|
157
|
+
|
|
158
|
+
1. **๐ฐ Sowing**: Pick up all stones from one of your pits:
|
|
159
|
+
- If the pit has **$> 1$ stone**: Leave **1 stone behind** in the origin pit, then sow the remaining stones counterclockwise one-by-one into consecutive pits and your own treasury.
|
|
160
|
+
- If the pit has **exactly $1$ stone**: Lift it and place it into the **next pit** to the right (the origin pit becomes empty).
|
|
161
|
+
- **Opponent's Treasury is always skipped**.
|
|
162
|
+
2. **โก Extra Turn**: If the last sown stone lands in your own treasury (*Hazine*), you retain your turn and play again immediately.
|
|
163
|
+
3. **๐ฏ Even Capture**: If the last sown stone lands in an opponent's pit and makes that pit's total count **even** ($2, 4, 6, \dots$), you capture **all stones from that pit** and deposit them into your treasury.
|
|
164
|
+
4. **๐ Empty-Hole Capture**: If the last sown stone lands in one of your own pits that was previously empty (now has 1 stone), and the opposite opponent pit (`12 - i`) contains stones, you capture your stone **plus all stones from the opposite pit**.
|
|
165
|
+
5. **๐ Endgame Sweep**: When either player's 6 pits are completely emptied, the game ends immediately. The player whose side emptied captures **all remaining stones on the opponent's side**. The player with the highest score wins!
|
|
166
|
+
|
|
167
|
+
> [!TIP]
|
|
168
|
+
> Comprehensive rule variations, official tournament rules, and tactical strategy guides are available in [docs/rules.md](docs/rules.md).
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## ๐ง AI Architecture
|
|
173
|
+
|
|
174
|
+
The minimax search engine is located in [`src/mangala/ai.py`](src/mangala/ai.py).
|
|
175
|
+
|
|
176
|
+
```mermaid
|
|
177
|
+
flowchart TD
|
|
178
|
+
Start["Position & Time Budget"] --> ID["Iterative Deepening (d = 1..64)"]
|
|
179
|
+
ID --> Order["Move Ordering: PV Move -> Extra Turns -> Captures -> Quiet"]
|
|
180
|
+
Order --> Minimax["Alpha-Beta Minimax (Symmetrical Extra-Turn Evaluation)"]
|
|
181
|
+
Minimax --> Horizon{"Horizon Reached?"}
|
|
182
|
+
Horizon -- "Yes" --> QSearch["Quiescence Search (Tactical & Extra-Turn Extensions)"]
|
|
183
|
+
Horizon -- "No" --> TT["Transposition Table Probe & Cutoffs"]
|
|
184
|
+
QSearch --> TT
|
|
185
|
+
TT --> Solved{"Forced Win or Time Expired?"}
|
|
186
|
+
Solved -- "No" --> ID
|
|
187
|
+
Solved -- "Yes" --> Best["Best Move + Evaluation + PV Line"]
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Key AI Features
|
|
191
|
+
|
|
192
|
+
- **Symmetrical Root Alpha-Beta**: Turkish Mangala grants extra turns on treasury finishes, breaking standard negamax sign inversions. The engine uses explicit alternating min/max routines to evaluate branch chains correctly.
|
|
193
|
+
- **Principal Variation (PV) Ordering**: Move ordering checks the transposition table first, evaluating high-probability cutoffs first to maximize alpha-beta pruning.
|
|
194
|
+
- **Quiescence Search**: Extends search horizons up to 4 extra plies for tactical captures and extra turns to eliminate horizon effect blunders.
|
|
195
|
+
- **Endgame Exact Solver**: Automatically transitions to a full retrograde solver whenever $\le 12$ stones remain on the board, calculating forced wins up to depth 64.
|
|
196
|
+
- **Anytime Budgeting**: Iterative deepening with clock checks every 128 nodes ensures strict adherence to time constraints.
|
|
197
|
+
|
|
198
|
+
### Difficulty Presets
|
|
199
|
+
|
|
200
|
+
| Preset | Search Method | Typical Depth | Target Audience |
|
|
201
|
+
| :--- | :--- | :--- | :--- |
|
|
202
|
+
| **`easy`** | Fixed-depth minimax | Depth 2 | Beginners & learning the rules |
|
|
203
|
+
| **`medium`** | Fixed-depth with Quiescence | Depth 5 | Casual play |
|
|
204
|
+
| **`hard`** | Iterative Deepening | ~8โ12 plies (1.0s budget) | Experienced players |
|
|
205
|
+
| **`expert`** | Tournament Iterative Deepening | ~12โ18 plies (2.5s budget) | Master / benchmark level |
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## ๐ Project Structure
|
|
210
|
+
|
|
211
|
+
```text
|
|
212
|
+
mangala/
|
|
213
|
+
โโโ .github/workflows/
|
|
214
|
+
โ โโโ ci.yml # Multi-OS CI (Python 3.10-3.13 on Ubuntu & Windows)
|
|
215
|
+
โ โโโ pages.yml # GitHub Pages automated deployment
|
|
216
|
+
โโโ docs/
|
|
217
|
+
โ โโโ assets/banner.svg # Vector art board illustration
|
|
218
|
+
โ โโโ rules.md # Formal rules & tournament specifications
|
|
219
|
+
โ โโโ architecture.md # Engine design, AI algorithms, and REST API
|
|
220
|
+
โ โโโ devlog.md # Development history & optimization log
|
|
221
|
+
โโโ src/mangala/
|
|
222
|
+
โ โโโ __init__.py # Package version & public exports
|
|
223
|
+
โ โโโ __main__.py # CLI runner (python -m mangala)
|
|
224
|
+
โ โโโ engine.py # Pure rules engine & immutable GameState
|
|
225
|
+
โ โโโ ai.py # Thread-safe Minimax / TT / Quiescence search
|
|
226
|
+
โ โโโ server.py # Hardened stdlib HTTP server & REST API
|
|
227
|
+
โ โโโ cli.py # Terminal front-end with ANSI visualizer
|
|
228
|
+
โ โโโ board.py # ASCII board formatter
|
|
229
|
+
โ โโโ gui_pygame.py # Pygame desktop arcade GUI
|
|
230
|
+
โ โโโ py.typed # PEP 561 static type checking marker
|
|
231
|
+
โ โโโ web/
|
|
232
|
+
โ โโโ index.html # Responsive web UI (single-file HTML/CSS/JS)
|
|
233
|
+
โ โโโ engine.js # Client-side JavaScript rules engine
|
|
234
|
+
โ โโโ ai.js # Client-side JavaScript minimax AI
|
|
235
|
+
โโโ tests/
|
|
236
|
+
โ โโโ fixtures/
|
|
237
|
+
โ โ โโโ engine_parity_fixtures.json # 629 cross-language test fixtures
|
|
238
|
+
โ โโโ test_engine.py # Engine rule tests & edge-case sweeps
|
|
239
|
+
โ โโโ test_ai.py # Minimax, quiescence, and thread safety tests
|
|
240
|
+
โ โโโ test_server.py # HTTP REST API & security header tests
|
|
241
|
+
โ โโโ test_cli.py # Terminal interactive game loop tests
|
|
242
|
+
โ โโโ test_board.py # Board formatting tests
|
|
243
|
+
โ โโโ test_gui.py # Headless Pygame desktop tests
|
|
244
|
+
โ โโโ test_selfplay.py # Selfplay tournament runner tests
|
|
245
|
+
โ โโโ test_fixtures_parity.py # Python parity validation against fixtures
|
|
246
|
+
โ โโโ test_parity.mjs # Node.js cross-language parity runner
|
|
247
|
+
โ โโโ test_properties.py # Hypothesis property fuzzing (48-stone conservation)
|
|
248
|
+
โโโ tools/
|
|
249
|
+
โ โโโ selfplay.py # AI benchmarking & Elo rating arena
|
|
250
|
+
โ โโโ generate_parity_fixtures.py# Parity fixture generator
|
|
251
|
+
โโโ pyproject.toml # PEP 621 package & tool configurations
|
|
252
|
+
โโโ LICENSE # MIT License
|
|
253
|
+
โโโ README.md
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
## ๐งช Testing & Verification
|
|
259
|
+
|
|
260
|
+
Run the comprehensive test suite locally:
|
|
261
|
+
|
|
262
|
+
```bash
|
|
263
|
+
# 1. Run all Python unit and property tests with 98% coverage
|
|
264
|
+
pytest --cov=mangala tests/
|
|
265
|
+
|
|
266
|
+
# 2. Run cross-language Python <-> JavaScript parity suite (629 test cases)
|
|
267
|
+
node --test tests/test_parity.mjs
|
|
268
|
+
|
|
269
|
+
# 3. Lint with Ruff (0 warnings, strict formatting)
|
|
270
|
+
ruff check .
|
|
271
|
+
|
|
272
|
+
# 4. Type check with Mypy
|
|
273
|
+
mypy
|
|
274
|
+
|
|
275
|
+
# 5. Build distribution packages (wheel and sdist)
|
|
276
|
+
python -m build
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
---
|
|
280
|
+
|
|
281
|
+
## โ๏ธ AI Tournament Arena
|
|
282
|
+
|
|
283
|
+
Benchmark two AI configurations or measure Elo differences:
|
|
284
|
+
|
|
285
|
+
```bash
|
|
286
|
+
# Run a 20-game tournament between Hard and Medium AI (alternating starters)
|
|
287
|
+
python tools/selfplay.py --games 20 --p1 hard --p2 medium
|
|
288
|
+
|
|
289
|
+
# Run an Expert vs Hard tournament with custom time limits
|
|
290
|
+
python tools/selfplay.py --games 50 --p1 expert --p2 hard
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
---
|
|
294
|
+
|
|
295
|
+
## ๐ค Contributing
|
|
296
|
+
|
|
297
|
+
Contributions, bug reports, and rule suggestions are warmly welcomed! Please read [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on code style, linting, and PR submissions.
|
|
298
|
+
|
|
299
|
+
---
|
|
300
|
+
|
|
301
|
+
## ๐ License
|
|
302
|
+
|
|
303
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
304
|
+
Distributed freely for education, competition, and enjoyment.
|