Skip to main content

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.

PyPI GitHub MahjongCLI DeepWiki

ไธญๆ–‡ๆ–‡ๆกฃ

Preview

Game Board Winning Screen
Game Board Winning Screen

Discard Action

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 with colored tile display
  • 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

  1. Parse Tenhou mjlog XML replay files into structured event streams (draws, discards, melds, riichi, agari, ryuukyoku)
  2. Reconstruct the exact tile wall order from the replay data (initial hands, draw sequence, dead wall)
  3. Replay each round step-by-step through our engine, feeding the same actions from the replay
  4. 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_action protocol) 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), and the step-by-step game replay feature.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

riichi_mahjong_cli-1.2.0.tar.gz (85.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

riichi_mahjong_cli-1.2.0-py3-none-any.whl (79.3 kB view details)

Uploaded Python 3

File details

Details for the file riichi_mahjong_cli-1.2.0.tar.gz.

File metadata

  • Download URL: riichi_mahjong_cli-1.2.0.tar.gz
  • Upload date:
  • Size: 85.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for riichi_mahjong_cli-1.2.0.tar.gz
Algorithm Hash digest
SHA256 198546482c68ac9edf9fda170594399cc62b7a05758d347b1e8c3de4cec7a3e9
MD5 9c005039a28357f2debc7e94b5e74797
BLAKE2b-256 e1d91d846ef31a2b4896d6c3c283f71d8922158715f95694b47108df9bc91fee

See more details on using hashes here.

File details

Details for the file riichi_mahjong_cli-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for riichi_mahjong_cli-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e7ac55d92c9a3ff951b86bedf4def593fa409f97148f9054b932a91bcdc379f
MD5 5d536253d5c0157ea4b9b508f1801c04
BLAKE2b-256 f56c33c789e35e513603a9ff9330ef9c921eab3559640cd3dae3f6c8f3a5e8e9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page