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

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

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

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. You can switch language from the main menu (option 6).

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

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
โ”‚   โ”‚   โ”œโ”€โ”€ shanten.py          # Shanten calculation
โ”‚   โ”‚   โ”œโ”€โ”€ fu.py               # Fu calculation
โ”‚   โ”‚   โ”œโ”€โ”€ yaku.py             # Yaku detection (30+ types)
โ”‚   โ”‚   โ”œโ”€โ”€ scoring.py          # Score calculation
โ”‚   โ”‚   โ”œโ”€โ”€ furiten.py          # Furiten detection
โ”‚   โ”‚   โ””โ”€โ”€ sanma_rules.py      # 3-player special rules
โ”‚   โ”œโ”€โ”€ 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
โ”‚   โ”œโ”€โ”€ player/                 # Player abstraction & AI
โ”‚   โ”‚   โ”œโ”€โ”€ base.py             # Player base class + GameView
โ”‚   โ”‚   โ”œโ”€โ”€ human.py            # Human player
โ”‚   โ”‚   โ””โ”€โ”€ greedy_ai.py        # Greedy AI
โ”‚   โ””โ”€โ”€ ui/                     # Terminal UI
โ”‚       โ”œโ”€โ”€ renderer.py         # Rich rendering engine
โ”‚       โ”œโ”€โ”€ tile_display.py     # Tile display formatting
โ”‚       โ”œโ”€โ”€ board_layout.py     # Board layout rendering
โ”‚       โ”œโ”€โ”€ input_handler.py    # User input handling
โ”‚       โ”œโ”€โ”€ i18n.py             # Internationalization (zh/ja/en)
โ”‚       โ””โ”€โ”€ locales/            # Translation files
โ”‚           โ”œโ”€โ”€ zh.py           # Chinese translations
โ”‚           โ”œโ”€โ”€ ja.py           # Japanese translations
โ”‚           โ””โ”€โ”€ en.py           # English translations
โ”œโ”€โ”€ tests/                      # Unit tests (120 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
  • EventBus Decoupling - Engine notifies UI via events for extensibility
  • Swappable AI - Standard interface allows future AI model integration
  • i18n Architecture - t() translation function with locale dictionaries, yaku names used as stable keys

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

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.1.2.tar.gz (65.2 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.1.2-py3-none-any.whl (65.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: riichi_mahjong_cli-1.1.2.tar.gz
  • Upload date:
  • Size: 65.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.18

File hashes

Hashes for riichi_mahjong_cli-1.1.2.tar.gz
Algorithm Hash digest
SHA256 e95b87b14f9598b47796eb2d698fc773b7fcb8f7b7ce6f29324dadb545ba0d35
MD5 e6d5f57b16ae0c7e0eb6e83e064de7eb
BLAKE2b-256 216e7a7b6f3b8c7786b2b0c8952257d5fc0a082bed0629d50a73643d0ad0b446

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for riichi_mahjong_cli-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ef32c980a066d72b6b6655962c6def5db99071bebe09488c883f64b70493917e
MD5 b8239b15a5d63076b0006b8da9b1c706
BLAKE2b-256 3a2c0bcbbd62682492afa38bf8ee98bf23aa8620ee38e60127ecfddc34ffacfc

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