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 PyPI Downloads 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.1.tar.gz (85.8 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.1-py3-none-any.whl (79.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: riichi_mahjong_cli-1.2.1.tar.gz
  • Upload date:
  • Size: 85.8 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.1.tar.gz
Algorithm Hash digest
SHA256 43ef0d662a151445cc7b378a3993d7b72ffbbe17fcf05e997687369a10a24e33
MD5 78b90266d79a6f27e8547b337fd872ef
BLAKE2b-256 898b736fbb4061c79adb8bfe9a81e4dce3909ff6f7a6928a441389de1dd79c7a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for riichi_mahjong_cli-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 876b278f956b225934c257eb675860cf179650b12881dad4608ff91f96dfce05
MD5 32d98497288e44a160bda0a1437a6580
BLAKE2b-256 4d8f79b6c60f18ce1e8b87b9f1af93cc95930fbedaebc72353efa61395d07c33

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