ritchie/types

Public data types for ritchie.

Types

The reason why the round has ended without a win.

pub type AbortReason {
  NineKinds
  WallExhausted
  NagashiMangan
  FourKan
  FourWind
  FourRiichi
  DoubleRon
  TripleRon
}

Constructors

  • NineKinds

    Nine kinds of terminals has been declared by a player. No reactions are possible. The round is terminated.

  • WallExhausted

    No more tiles can be drawn from the wall. Penalties payments may apply. Can be preempted by ron and all other aborts.

  • NagashiMangan

    Special case of WallExhausted. Treated as penalties payments.

  • FourKan

    Four kans. Either:

    • Player attempted to call the 5th kan, and all 5 are by the same player.
    • Player attempted to call the 4th kan, and all 4 are NOT by the same player. Can be preempted by ron. Kakan and ankan can also be preempted because of chankan.
  • FourWind

    Four consecutive discards of the same wind since the start of the round. Cannot be preempted.

  • FourRiichi

    All four players are under active riichi. Can be preempted by ron.

  • DoubleRon

    Two players declared ron on the same tile. Can be triggered as a (rare) rule variation of TripleRon, see Ruleset.

  • TripleRon

    Three players declared ron on the same tile. Preempts all other abort reasons.

Action by the in-turn player.

pub type Action {
  ActionDiscard(Discard)
  ActionAnkan(Tile)
  ActionKakan(Tile)
  ActionTsumoAgari(Tile)
  ActionAbortNineKinds
}

Constructors

  • ActionDiscard(Discard)

    Discard a tile, see Discard. The called_by field is implied and can be safely ignored here.

  • ActionAnkan(Tile)

    Declare an Ankan (4 tiles in closed hand meld)

  • ActionKakan(Tile)

    Declare a Kakan (extends a pon with drawn tile)

  • ActionTsumoAgari(Tile)

    Win by self-draw.

  • ActionAbortNineKinds

    Abort by nine kinds of terminals.

Reasons an action could be rejected.

pub type ActionError {
  TsumogiriMismatch(Tile, option.Option(Tile))
  DiscardClosedHandUnderRiichi
  NoSwapCalling(Tile, Meld)
  TileNotExist(Tile)
  DeclareRiichiAgain
  DeclareRiichiWithoutPoints
  DeclareRiichiWithOpenMeld
  DeclareRiichiWhileNotReady
  DiscardOnlyAfterChiiPon
  CannotKanOnLastDraw
  InvalidAnkanUnderRiichi(Tile)
  NotEnoughForAnkan(Tile)
  NoPonForKakan(Tile)
  NotEnoughKindsForNineKinds(Int)
  NotInitAbortable
  MustDeclareTsumoAgariOnDraw
  CannotTsumoAgari
}

Constructors

  • TsumogiriMismatch(Tile, option.Option(Tile))

    Tsumogiri discard doesn’t match drawn tile

  • DiscardClosedHandUnderRiichi

    Discard from closed hand while under riichi

  • NoSwapCalling(Tile, Meld)

    Discard considered swap-calling

  • TileNotExist(Tile)

    Tile doesn’t exist

  • DeclareRiichiAgain

    Redeclaring riichi when it’s already declared

  • DeclareRiichiWithoutPoints

    Declaring riichi without the requisite amount of points

  • DeclareRiichiWithOpenMeld

    Declaring riichi with an open hand

  • DeclareRiichiWhileNotReady

    Declaring riichi with a hand not ready after discarding

  • DiscardOnlyAfterChiiPon

    Discard not after chii/pon

  • CannotKanOnLastDraw

    Attempting to call ankan/kakan on last draw

  • InvalidAnkanUnderRiichi(Tile)

    Attempting invalid ankan under riichi

  • NotEnoughForAnkan(Tile)

    Attempting ankan without enough tiles in hand

  • NoPonForKakan(Tile)

    Attempting kakan without matching pon

  • NotEnoughKindsForNineKinds(Int)

    Declaring nine kinds of terminals/honors without enough of them in hand

  • NotInitAbortable

    Aborting after first go-around

  • MustDeclareTsumoAgariOnDraw

    Declaring tsumo-agari not on drawn tile

  • CannotTsumoAgari

    Declaring tsumo-agari without ready hand/without yaku

Conclusion of an action-reaction cycle.

pub type ActionResult {
  Pass
  CalledBy(Player)
  Agari(AgariKind)
  Abort(AbortReason)
}

Constructors

  • Pass

    The action has successfully taken place without any reaction.

  • CalledBy(Player)

    A meld (chii/pon/daiminkan) has been called by another player. Kakan and ankan do not count.

  • Agari(AgariKind)

    At least one player has won, either by ron or by tsumo.

  • Abort(AbortReason)

    The round has ended without a win.

pub type AgariDetails {
  AgariDetails(winning_tile: Tile, score: Score, dora: DoraHits)
}

Constructors

Whether the hand was won by self-draw (tsumo) or by claiming a discard (ron).

pub type AgariKind {
  Tsumo
  Ron
}

Constructors

  • Tsumo
  • Ron

Finalized winning hand.

pub type AgariResult {
  AgariResult(
    winner: Player,
    contributor: Player,
    liable_player: Player,
    points_delta: List(Int),
    details: AgariDetails,
  )
}

Constructors

  • AgariResult(
      winner: Player,
      contributor: Player,
      liable_player: Player,
      points_delta: List(Int),
      details: AgariDetails,
    )

    Arguments

    winner

    Who won?

    contributor

    Who supplied the winning tile? For a tsumo, this is the winner.

    liable_player

    The player liable under pao / liability rules, or the winner if none.

    points_delta

    The net effect on each player’s points due to this win, including the pot and honba bonuses.

    details

    The highest-scoring interpretation of the winning hand.

A discarded tile with extra info. This is both an Action and an entry in a player’s Discard stream.

pub type Discard {
  Discard(
    tile: Tile,
    called_by: Player,
    declares_riichi: Bool,
    is_tsumogiri: Bool,
  )
}

Constructors

  • Discard(
      tile: Tile,
      called_by: Player,
      declares_riichi: Bool,
      is_tsumogiri: Bool,
    )

    Arguments

    tile

    The discarded tile.

    called_by

    If called by another player, that player. Otherwise, the player who discarded this. This is ignored if the discard is nested in an ActionDiscard.

    declares_riichi

    Whether this tile was discarded as part of declaring Riichi.

    is_tsumogiri

    Whether this tile was discarded immediately after being drawn.

pub type DoraHits {
  DoraHits(dora: Int, ura_dora: Int, aka_dora: Int)
}

Constructors

  • DoraHits(dora: Int, ura_dora: Int, aka_dora: Int)

Errors from the engine.

pub type EngineError {
  RoundNotStarted
  RoundEnded
  NoActionRegistered
  InvalidAction(ActionError)
  InvalidReaction(ReactionError)
}

Constructors

  • RoundNotStarted

    No round has been started yet

  • RoundEnded

    The round has already ended

  • NoActionRegistered

    No action has been registered for the current turn

  • InvalidAction(ActionError)

    The action was rejected by validation

  • InvalidReaction(ReactionError)

    The reaction was rejected by validation

Furiten status of a player.

pub type FuritenFlags {
  FuritenFlags(
    by_discard: Bool,
    miss_temporary: Bool,
    miss_permanent: Bool,
  )
}

Constructors

  • FuritenFlags(
      by_discard: Bool,
      miss_temporary: Bool,
      miss_permanent: Bool,
    )

    Arguments

    by_discard

    A tile in the player’s waiting set has been discarded by the player.

    miss_temporary

    Another player discarded a tile in the waiting set and this player did not call ron. Cleared after this player discards.

    miss_permanent

    Same trigger while under riichi. Permanent for the round.

pub type GameError {
  GameFinished
  RoundAlreadyStarted
}

Constructors

  • GameFinished
  • RoundAlreadyStarted

Points / point differences. Usually increments of 100.

pub type GamePoints =
  Int
pub type GameStatus {
  NotStarted
  InRound
  BetweenRounds
  Finished
}

Constructors

  • NotStarted
  • InRound
  • BetweenRounds
  • Finished

A meld of any kind.

pub type Meld {
  Chii(own: List(Tile), called: Tile, min: Tile)
  Pon(own: List(Tile), called: Tile, dir: Player)
  Kakan(own: List(Tile), called: Tile, dir: Player, added: Tile)
  Daiminkan(own: List(Tile), called: Tile, dir: Player)
  Ankan(own: List(Tile))
}

Constructors

  • Chii(own: List(Tile), called: Tile, min: Tile)

    An open group of 3 consecutive tiles. The called tile may only come from the previous player’s discard.

  • Pon(own: List(Tile), called: Tile, dir: Player)

    An open group of 3 identical (ignoring red) tiles. May be called from any other player’s discard.

  • Kakan(own: List(Tile), called: Tile, dir: Player, added: Tile)

    A kan formed by an existing Pon + the 1 last identical tile from closed hand. This can be formed when the owner of the Pon is in action.

  • Daiminkan(own: List(Tile), called: Tile, dir: Player)

    “Big Open Kan” formed by calling 1 with 3 of the same kind in the closed hand. Similar to Pon, may be called from any other player’s discard.

  • Ankan(own: List(Tile))

    Closed Kan, formed by setting aside 4 tiles of the same kind in a player’s closed hand. This can be done during the player’s own turn. Declaring Ankan does not technically open one’s hand (making closed yaku still effective), but it does reveal that part of the hand to others.

Player index: 0, 1, 2, 3, the player assigned east, south, west, north in the initial round.

This is forced to mod-4 arithmetic, and can represent both the absolute player index or the relative differences between the player indices.

pub type Player {
  Player(Int)
}

Constructors

  • Player(Int)

Reaction from an out-of-turn player. The lack of reaction (“passing”), or an unknown reaction can be represented by Option(Reaction).

pub type Reaction {
  ReactionChii(Tile, Tile)
  ReactionPon(Tile, Tile)
  ReactionDaiminkan
  ReactionRonAgari
}

Constructors

  • ReactionChii(Tile, Tile)

    Declare a Chii on the recent discard with the specified own tiles.

  • ReactionPon(Tile, Tile)

    Declare a Pon on the recent discard with the specified own tiles.

  • ReactionDaiminkan

    Declare a Daiminkan on the recent discard, the own tiles are implicit.

  • ReactionRonAgari

    Declare win-by-steal on the recent action. The action can be:

    • ActionDiscard (common)
    • ActionKakan (rare)
    • ActionAnkan (very rare)

Reasons a reaction could be rejected.

pub type ReactionError {
  TerminalAction
  CannotReactToSelf
  OpenMeldUnderRiichi
  MeldOnLastDraw
  NotDiscard(Action)
  CanOnlyChiiPrevPlayer
  InvalidChii(Tile, Tile, Tile)
  InvalidPon(Tile, Tile, Tile)
  InvalidDaiminkan
  Furiten
  CannotRonAgari
  CannotRonAgariOverAnkan
}

Constructors

  • TerminalAction

    Action is terminal and no reactions are possible

  • CannotReactToSelf

    Acting player cannot react to their own action

  • OpenMeldUnderRiichi

    Cannot declare an open meld under riichi

  • MeldOnLastDraw

    Cannot declare an open meld on the last draw

  • NotDiscard(Action)

    Can only call a discarded tile

  • CanOnlyChiiPrevPlayer

    Chii can only be called on the previous player’s discard

  • InvalidChii(Tile, Tile, Tile)

    Invalid chii with the given own tiles and called tile

  • InvalidPon(Tile, Tile, Tile)

    Invalid pon with the given own tiles and called tile

  • InvalidDaiminkan

    Invalid daiminkan

  • Furiten

    Cannot ron while under furiten

  • CannotRonAgari

    Cannot ron, either not waiting, or no yaku

  • CannotRonAgariOverAnkan

    Cannot ron over an ankan (with the exception of kokushi)

pub type Riichi {
  RiichiState(double: Bool, ippatsu: Bool)
}

Constructors

  • RiichiState(double: Bool, ippatsu: Bool)

    Arguments

    double

    Whether riichi was declared on the player’s first discard (double riichi).

    ippatsu

    Whether the win happened within one player turn cycle of the declaration.

Details of how a round concluded.

pub type RoundEnd {
  RoundEnd(
    round_result: ActionResult,
    pot: Int,
    points: List(Int),
    points_delta: List(Int),
    renchan: Bool,
    next_round_id: option.Option(RoundId),
    agari_result: List(option.Option(AgariResult)),
  )
}

Constructors

  • RoundEnd(
      round_result: ActionResult,
      pot: Int,
      points: List(Int),
      points_delta: List(Int),
      renchan: Bool,
      next_round_id: option.Option(RoundId),
      agari_result: List(option.Option(AgariResult)),
    )

    Arguments

    round_result

    The terminal action result of the round.

    pot

    Points left on the table at round end.

    points

    Points for each player at round end.

    points_delta

    Point increments for each player (end - begin).

    renchan

    Whether the next round is “this round + 1 honba”.

    next_round_id

    The next round, or None if the game ends.

    agari_result

    How each player won, if they did.

“Ba-Kyoku-Honba” triplet that identifies a round.

pub type RoundId {
  RoundId(kyoku: Int, honba: Int)
}

Constructors

  • RoundId(kyoku: Int, honba: Int)

    Arguments

    kyoku

    Round index together with the prevailing wind.

    • 0 => east 1, 3 => east 4
    • 4 => south 1, 7 => south 4
    • 8 => west 1, 15 => north 4
    honba

    Number of honba, commonly shown as 100-point sticks.

Configurable rules and interpretations of rules for a game.

The default ruleset is based on Tenhou-style 4-player public-lobby rules.

You cannot configure the number of red tiles here, see ritchie.make_shuffled_wall for that. Similarly, you can’t configure the starting points for each player, that’s handled in ritchie.start_round.

The Ruleset type is guaranteed not to change in patch versions. Minor versions may add new fields, as long as the outcome of a game would not change. Only major versions can change/ remove existing fields.

Use the record update syntax to vary one rule at a time:

types.Ruleset(..types.default_rules(), kiriage_mangan: True)
pub type Ruleset {
  Ruleset(
    version: String,
    kyoku_max_soft: Int,
    kyoku_max_hard: Int,
    points_min_qualify: Int,
    ron_max_num_players: Int,
    ron_first_only: Bool,
    dora_allow_ura: Bool,
    dora_allow_kan: Bool,
    dora_allow_kan_ura: Bool,
    kiriage_mangan: Bool,
    kazoe_yakuman: Bool,
    yaku_allow_double_yakuman: Bool,
    yaku_allow_open_tanyao: Bool,
    yaku_extra: List(Yaku),
    yaku_block: List(Yaku),
    extra_kokushi_allow_chankan_ankan: Bool,
    extra_swap_call_allow_same: Bool,
    extra_swap_call_allow_other: Bool,
    extra_riichi_ankan_strict: Bool,
  )
}

Constructors

  • Ruleset(
      version: String,
      kyoku_max_soft: Int,
      kyoku_max_hard: Int,
      points_min_qualify: Int,
      ron_max_num_players: Int,
      ron_first_only: Bool,
      dora_allow_ura: Bool,
      dora_allow_kan: Bool,
      dora_allow_kan_ura: Bool,
      kiriage_mangan: Bool,
      kazoe_yakuman: Bool,
      yaku_allow_double_yakuman: Bool,
      yaku_allow_open_tanyao: Bool,
      yaku_extra: List(Yaku),
      yaku_block: List(Yaku),
      extra_kokushi_allow_chankan_ankan: Bool,
      extra_swap_call_allow_same: Bool,
      extra_swap_call_allow_other: Bool,
      extra_riichi_ankan_strict: Bool,
    )

    Arguments

    kyoku_max_soft

    The kyoku index for the “all-last” round. This is 3 for an east-only game, and 7 for an east-south game. The default is east-only.

    kyoku_max_hard

    The absolute maximum amount of rounds that can be played. Any attempt to increment the kyoku number beyond this must end the game. Default is 11 (west 4 kyoku).

    points_min_qualify

    The minimum amount of points a player needs to have for the game to end after the “all-last” round. Default is 30000, assuming 25000 starting points.

    ron_max_num_players

    Maximum number of players that can simultaneously win by ron over the same tile. More player than allowed here would end the round on a forced abortion. Default is allowing two players.

    ron_first_only

    Does only the first player CCW from the contributor win when multiple rons happen at the same time? Default is no.

    dora_allow_ura

    Do we count ura-doras when a player wins under riichi? Default: yes.

    dora_allow_kan

    Do we reveal new doras after kan? Default: yes.

    dora_allow_kan_ura

    Do we count ura-doras for newly revealed kans? Default: yes.

    kiriage_mangan

    A 4 han 30 fu or 3 han 60 fu hand scores mangan (2000 basic points) instead of its raw value.

    kazoe_yakuman

    13 or more han is a counted yakuman (8000 basic points). When disabled, such hands cap at sanbaiman instead.

    yaku_allow_double_yakuman

    Do daisuushi, suuankou tanki, kokushi 13-sided wait and junsei chuurenpoutou count as double yakuman? Default: no.

    yaku_allow_open_tanyao

    Is Tanyao allowed in an open hand? Default is yes.

    yaku_extra

    A list of extra, nonstandard Yaku to enable. Currently there are none, but some may be implemented in the future. Default is none.

    yaku_block

    A list of standard Yaku to disable. Default is none.

    extra_kokushi_allow_chankan_ankan

    Can a player who is waiting on a kokushi (thirteen orphans) call a chankan ron over an ankan (i.e. steal the ankan)? This is in addition to calling chankan on kakan, which is already allowed under all rules. The default is no.

    extra_swap_call_allow_same

    After calling chii/pon on a certain tile, is it okay to immediately discard another copy of the same tile, where red 5s are considered the same as the normal tile? The default is no.

    extra_swap_call_allow_other

    After calling chii on a certain tile (e.g. 45m + 6m), is it okay to immediately discard a tile that would have formed a group on the other end (e.g. 3m)? The default is no.

    extra_riichi_ankan_strict

    Do we use the strict rules for calling ankan while under riichi? The strict rule only allows ankan from the self draw immediately before it. It must also not change the waiting pattern, and it must not destroy the chuurenpoutou form, if it exists. The relaxed rule means the ankan must not change the waiting pattern, but nothing else. The default is true.

pub type Score {
  Score(
    yakus: List(Yaku),
    han: Int,
    fu: Int,
    yakuman: Int,
    basic_points: Int,
  )
}

Constructors

  • Score(
      yakus: List(Yaku),
      han: Int,
      fu: Int,
      yakuman: Int,
      basic_points: Int,
    )

    Arguments

    yakuman

    Number of yakuman, counting double yakuman as 2. 0 for a normal hand.

    basic_points

    Points before the dealer/ron/tsumo multipliers.

Represents one tile.

Encoded as a 6-bit integer:

EncodingShorthandCategory (EN)Category (JP)
0 ..= 81m ..= 9mcharacters萬子
9 ..= 171p ..= 9pdots筒子
18 ..= 261s ..= 9sbamboos索子
27 ..= 301z ..= 4zwinds風牌
31, 32, 335z, 6z, 7zdragons三元牌
34, 35, 360m, 0p, 0sreds赤牌

Note that only red 5’s can be represented (not other numbers or honors).

Details of this encoding is significant and implicitly assumed across the library. It should never be changed.

pub type Tile {
  Tile(Int)
}

Constructors

  • Tile(Int)

Wind index — 0, 1, 2, 3 => east, south, west, north.

Note that this is identical to Player, see its definition.

pub type Wind =
  Player
pub type Yaku {
  Riichi(double: Bool)
  Ippatsu
  MenzenTsumo
  Haitei
  Houtei
  Rinshan
  Chankan
  Tenhou
  Chiihou
  Pinfu
  Tanyao
  Iipeikou
  Ryanpeikou
  YakuhaiDragon(tile: Tile)
  YakuhaiSeatWind(tile: Tile)
  YakuhaiRoundWind(tile: Tile)
  SanshokuDoujun(closed: Bool)
  SanshokuDoukou
  Ittsu(closed: Bool)
  Chanta(closed: Bool)
  Junchan(closed: Bool)
  Toitoi
  Sanankou
  Sankantsu
  Shousangen
  Honroutou
  Honitsu(closed: Bool)
  Chinitsu(closed: Bool)
  Chiitoitsu
  Kokushi
  Kokushi13
  Suuankou
  SuuankouTanki
  Daisangen
  Daisuushi
  Shousuushi
  Tsuuiisou
  Chinroutou
  Ryuuiisou
  Chuurenpoutou
  Junseichuurenpoutou
  Suukantsu
}

Constructors

  • Riichi(double: Bool)

    Win when riichi is declared. Double riichi means riichi in the initial round.

  • Ippatsu

    Win before first riichi discard.

  • MenzenTsumo

    Tsumo with closed hand.

  • Haitei

    Tsumo on the last tile of the live wall.

  • Houtei

    Ron on the last discard.

  • Rinshan

    Tsumo on the replacement tile after a kan.

  • Chankan

    Ron on a tile used for another player’s kan.

  • Tenhou

    Dealer win on the first turn. Yakuman.

  • Chiihou

    Non-dealer win on the first turn. Yakuman.

  • Pinfu

    Hand without fu.

  • Tanyao

    Hand without terminals and honors.

  • Iipeikou

    Hand with two identical sequences. Closed only.

  • Ryanpeikou

    Hand with three identical sequences. Closed only.

  • YakuhaiDragon(tile: Tile)

    Yakuhai with dragon triplet.

  • YakuhaiSeatWind(tile: Tile)

    Yakuhai with seat wind triplet.

  • YakuhaiRoundWind(tile: Tile)

    Yakuhai with round wind triplet.

  • SanshokuDoujun(closed: Bool)

    Hand with three same-number sequences in different suits.

  • SanshokuDoukou

    Hand with three same-number triplets in different suits.

  • Ittsu(closed: Bool)

    Pure straight (1-9).

  • Chanta(closed: Bool)

    Hand where all tile groups contain a honor or terminal.

  • Junchan(closed: Bool)

    Hand where all tile groups contain a terminal.

  • Toitoi

    Hand with only triplets.

  • Sanankou

    Hand with three closed triplets.

  • Sankantsu

    Hand with three quads.

  • Shousangen

    Hand with three dragon triplets.

  • Honroutou

    Hand with only terminals and honors.

  • Honitsu(closed: Bool)

    Hand with one single suit and honor tiles.

  • Chinitsu(closed: Bool)

    Hand with only one single suit.

  • Chiitoitsu

    Seven unique pairs.

  • Kokushi

    Thirteen orphans with a single wait. Yakuman.

  • Kokushi13

    Thirteen orphans with a thirteen-sided wait. Yakuman.

  • Suuankou

    Four closed triplets. Yakuman.

  • SuuankouTanki

    Four closed triplets with a pair wait. Yakuman.

  • Daisangen

    Three dragon triplets. Yakuman.

  • Daisuushi

    Four wind triplets. Yakuman.

  • Shousuushi

    Three wind triplets and a wind pair. Yakuman.

  • Tsuuiisou

    Hand with only honor tiles. Yakuman.

  • Chinroutou

    Hand with only terminal tiles. Yakuman.

  • Ryuuiisou

    Hand with only green tiles. Yakuman.

  • Chuurenpoutou

    Nine gates. Yakuman.

  • Junseichuurenpoutou

    Pure nine gates. Yakuman.

  • Suukantsu

    Four quads. Yakuman.

The scoring value of a yaku: either han, or whole yakuman multiples.

pub type YakuValue {
  Han(Int)
  Yakuman(Int)
}

Constructors

  • Han(Int)
  • Yakuman(Int)

Values

pub fn default_rules() -> Ruleset
pub fn wind_from_int(i: Int) -> Player
✨ Search Document