#riichi #mahjong #game #tenhou #majsoul #lookup-tables

bin+lib riichi-decomp

Japanese Riichi Mahjong Waiting Hand Decomposition

1 unstable release

0.1.0 Oct 8, 2022

#1450 in Game dev


Used in riichi

MIT license

155KB
3K SLoC

Japanese Riichi Mahjong Waiting Hand Decomposition

Crates.io docs.rs

In Japanese Riichi Mahjong, a closed hand with 3N+1 tiles is considered "waiting" (1 tile away from winning) if it matches any of the following:

This crate can be used to analyze a 3N+1 closed hand and determine all possible waiting patterns for it. For each pattern, the waiting tile, pattern kind, and details of decomposition (for regular patterns) are calculated.

Important note: The algorithm depends on lookup tables that must be generated before a hand can be analyzed. If feature static-lut is enabled, this will be done during compile time using build.rs. Otherwise, the tables will be lazily generated when a Decomposer is first instantiated during runtime.

Example

The included CLI (src/main.rs) prints all possible waiting patterns for the hands supplied through command-line arguments or the standard input. It can be invoked using cargo run.

It basically does the following (using the classic Pure Nine Gates example):

use riichi_decomp::*;
use riichi_elements::prelude::*;

let mut decomposer = Decomposer::new();
let tile_set = TileSet34::from_iter(tiles_from_str("1112345678999m"));
let result = WaitSet::from_tile_set(&mut decomposer, &tile_set);

assert_eq!(result.waiting_tiles.0, 0b111111111);
assert_eq!(result.regular.len(), 15);
assert_eq!(result.irregular, None);

Note that a Decomposer instance can be reused to analyze different hands.

Optional Features

serde (default: enabled)

Provides serialization for all results. Deserialization is not implemented as these are intended to be generated internally only.

static-lut (default: enabled)

Generates lookup tables required for computations during compile time (instead of lazily during runtime).

Dependencies

~1.5–2.4MB
~46K SLoC