Terminal CLI Japanese Riichi Mahjong game
Project description
๐๏ธ Japanese Riichi Mahjong - Terminal CLI
A fully-featured Japanese Riichi Mahjong terminal CLI game supporting 4-player (yonma) and 3-player (sanma) modes, with multilingual interface (Chinese/Japanese/English), built with Python + Rich.
Preview
| Game Board | Winning Screen |
|---|---|
Features
- 4-Player Mahjong - Hanchan (half game) / Tonpuusen (east-only)
- 3-Player Mahjong - Hanchan / Tonpuusen (no 2m-8m, no chi, kita)
- Complete Rule Engine - 30+ yaku detection, fu calculation, scoring
- Greedy AI Opponents - Shanten-based AI with basic defense
- Spectator Mode - AI vs AI auto-play
- Multilingual - Chinese, Japanese, and English interface
- Colored Tiles - Rich terminal rendering; red fives shown in orange, draw tile and call/ron target tile highlighted
- A+B Time Control - Base + bank seconds per action, real-time countdown with per-second refresh (unlimited by default)
- Game Replay - Browse finished games and step through every action in god view (menu option 7)
- Game Logs - Every finished game is recorded as JSON under
logs/(full wall, all actions, results) - Adjustable AI Speed - 1s / 3s / 5s / random delay presets in Settings
Install
pipx install riichi-mahjong-cli
Or with pip:
pip install riichi-mahjong-cli
Quick Start
riichi
Or run from source:
python main.py
The game starts in Chinese by default. Language, time control, and AI speed can be changed in Settings (main menu option 6). Replay (option 7) lets you review any finished game step by step.
Run Tests
pytest tests/
Controls
| Key | Action |
|---|---|
| 1-14 | Select tile to discard |
t |
Tsumo (self-draw win) |
h |
Ron (win off discard) |
r |
Declare Riichi |
p |
Pon |
c |
Chi |
k |
Kan (concealed/added/open) |
n |
Kita (3-player only) |
9 |
Nine-tile draw |
s |
Skip |
Replay Controls
| Key | Action |
|---|---|
Enter |
Next step |
b |
Previous step |
| number | Jump to step |
q |
Back |
Project Structure
game/
โโโ main.py # Entry point (backward compatible)
โโโ mahjong/
โ โโโ cli.py # CLI entry point (riichi command)
โ โโโ core/ # Core data models
โ โ โโโ tile.py # Tile definitions (136/34 dual encoding, red dora)
โ โ โโโ meld.py # Meld data structures
โ โ โโโ hand.py # Hand management
โ โ โโโ wall.py # Wall and dead wall
โ โ โโโ player_state.py # Player state tracking
โ โโโ rules/ # Rule engine (pure functions, stateless)
โ โ โโโ agari.py # Win detection (LRU cached)
โ โ โโโ shanten.py # Shanten calculation (LRU cached)
โ โ โโโ fu.py # Fu calculation
โ โ โโโ yaku.py # Yaku detection (30+ types)
โ โ โโโ scoring.py # Score calculation
โ โ โโโ furiten.py # Furiten detection
โ โโโ engine/ # Game engine
โ โ โโโ game.py # Hanchan/Tonpuusen management
โ โ โโโ round.py # Single round flow control
โ โ โโโ action.py # Action definitions
โ โ โโโ event.py # Event bus
โ โ โโโ game_logger.py # Game logging
โ โ โโโ time_control.py # A+B time control presets
โ โ โโโ ai_delay.py # AI action speed presets
โ โโโ player/ # Player abstraction & AI
โ โ โโโ base.py # Player base class + GameView
โ โ โโโ human.py # Human player + timing logic
โ โ โโโ greedy_ai.py # Greedy AI
โ โโโ replay/ # Game replay
โ โ โโโ loader.py # Log scanning & loading
โ โ โโโ state.py # Step-by-step state reconstruction
โ โโโ ui/ # Terminal UI
โ โโโ renderer.py # Rendering facade
โ โโโ tile_display.py # Tile display formatting
โ โโโ board_layout.py # Board layout rendering
โ โโโ replay_screen.py # Replay browser (menu option 7)
โ โโโ input_handler.py # User input handling
โ โโโ timeout_input.py # Timed input + live countdown (ANSI)
โ โโโ i18n.py # Internationalization (zh/ja/en)
โ โโโ labels.py # Localized label construction
โ โโโ locales/ # Translation files
โ โโโ zh.py # Chinese translations
โ โโโ ja.py # Japanese translations
โ โโโ en.py # English translations
โโโ tests/ # Unit tests (168 cases)
โโโ data/
โโโ scoring_table.json # Han/fu โ points lookup table
Supported Yaku
1 Han
Riichi, Menzen Tsumo, Tanyao, Pinfu, Iipeikou, Yakuhai (round/seat wind, dragons), Ippatsu, Haitei, Houtei, Rinshan Kaihou, Chankan
2 Han
Double Riichi, Chanta, Ittsu, Sanshoku Doujun, Sanshoku Doukou, Toitoi, San Ankou, Honroutou, Shousangen, Chiitoitsu
3 Han
Honitsu, Junchan, Ryanpeikou
6 Han
Chinitsu
Yakuman
Kokushi Musou, Suu Ankou, Daisangen, Shousuushii, Daisuushii, Tsuuiisou, Chinroutou, Ryuuiisou, Chuuren Poutou, Suukantsu, Tenhou, Chiihou
Rule Engine Verification
The rule engine has been verified against 1,000 real games from Tenhou (the most popular online Japanese Mahjong platform). All 1,000 replays pass with 100% consistency.
Verification Process
- Parse Tenhou mjlog XML replay files into structured event streams (draws, discards, melds, riichi, agari, ryuukyoku)
- Reconstruct the exact tile wall order from the replay data (initial hands, draw sequence, dead wall)
- Replay each round step-by-step through our engine, feeding the same actions from the replay
- Validate at every step:
- Each draw, discard, meld, and riichi is legal according to the engine's rule checks
- Ron/tsumo legality (furiten, valid yaku, score calculation)
- Final scoring matches Tenhou's results (fu, han, points, payments)
- Ryuukyoku (draw) tenpai status and point transfers match
# Run Tenhou replay verification
pytest tests/tenhou_replay/ -v
tests/tenhou_replay/test_tenhou_replay.py - 1000 passed
What This Proves
- Yaku detection, fu calculation, and scoring are correct for real-world game scenarios
- Furiten rules (discard furiten, temporary furiten, riichi furiten) behave correctly
- Meld legality (chi, pon, kan) matches Tenhou's rule interpretation
- Edge cases (haitei, houtei, rinshan, chankan, double riichi) are handled correctly
Design Highlights
- Dual Encoding - 136-encoding tracks unique tile identity, 34-encoding for efficient algorithms
- GameView Barrier - AI and human use the same interface, ensuring fairness
- Strict Layering - core/engine/rules never import the UI layer; translations live entirely in
ui/ - EventBus Logging - the engine emits events consumed by the game logger; replays are rebuilt from these logs
- Swappable AI - Standard interface (a single
choose_actionprotocol) allows future AI model integration - i18n Architecture -
t()translation function with locale dictionaries, yaku names used as stable keys; locale consistency is enforced by tests - Invariant Tests - AI self-play smoke tests assert point conservation after every round
Dependencies
- Python >= 3.10
- rich >= 13.0.0
- pytest >= 7.0.0 (development)
About This Project
This project was fully implemented by Claude Code from scratch. Humans only provided requirement documents โ all code, tests, and documentation were generated by AI.
Implementation Info
| Item | Details |
|---|---|
| AI Tool | Claude Code (Anthropic CLI) |
| Model | Claude Opus 4.6 (claude-opus-4-6) |
| Process | Complete code writing and debugging in a single session |
| Scale | ~30 source files, 120 unit tests |
| Tokens | ~200K+ tokens (planning, code generation, test fixing) |
| Date | 2025-02-12 |
Later Iterations
All subsequent versions were also implemented by Claude Code, including: multilingual interface and PyPI release (v1.1.0), the A+B time control system and settings menu (v1.1.x), rule-engine verification against 1,000 real Tenhou games, a full engine audit in 2026-06 (fixed tenhou/chankan/kita-dora/riichi-stick-conservation bugs, removed dead code, enforced layering, added LRU caching), the step-by-step game replay feature, and UI improvements (AI riichi tile marker fix, orange red-five tiles, call/ron target highlighting, restructured result screen).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file riichi_mahjong_cli-1.2.2.tar.gz.
File metadata
- Download URL: riichi_mahjong_cli-1.2.2.tar.gz
- Upload date:
- Size: 86.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
143b75712a83e494ec4c80e59a89400fb8b4b0b53f5a4164f4f5e87f2c4418dd
|
|
| MD5 |
283db2ec2b0188dd543fdbd0ae621e8c
|
|
| BLAKE2b-256 |
3432f965335269c4ab4ef2522cedad095fe57c0ee9d8600202ffef5026153e02
|
File details
Details for the file riichi_mahjong_cli-1.2.2-py3-none-any.whl.
File metadata
- Download URL: riichi_mahjong_cli-1.2.2-py3-none-any.whl
- Upload date:
- Size: 79.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22b6aa9a96cb987a7aed70021f997fad42a7284b9c2f8fab76bb6ae5b7de816d
|
|
| MD5 |
50be400cf79695d8b0b1428c9b7f3b34
|
|
| BLAKE2b-256 |
5c425a0b65d925dccfb5509fb41c5d89b2e2b08062c3ef76941df23f937becb3
|