ritchie

A riichi mahjong library.

Types

A match in progress.

pub opaque type Game

One turn in a round’s history.

pub type GameStep {
  GameStep(
    actor: types.Player,
    action: types.Action,
    reactor_reaction: option.Option(
      #(types.Player, types.Reaction),
    ),
    action_result: types.ActionResult,
  )
}

Constructors

Result of advancing one turn.

pub type GameStepOutcome {
  GameStepOutcome(
    game: Game,
    game_step: GameStep,
    round_end: option.Option(types.RoundEnd),
  )
}

Constructors

The full history of a round.

pub type RoundHistory {
  RoundHistory(
    begin: StartInfo,
    steps: List(GameStep),
    ron: List(Bool),
  )
}

Constructors

  • RoundHistory(
      begin: StartInfo,
      steps: List(GameStep),
      ron: List(Bool),
    )

    Arguments

    ron

    Which players declared ron at the last step. This can encode multi-ron.

Information about the beginning of a round.

pub type StartInfo {
  StartInfo(
    ruleset: types.Ruleset,
    round_id: types.RoundId,
    wall: Wall,
    pot: Int,
    points: List(Int),
  )
}

Constructors

The state of a round.

pub type State =
  @internal State

The wall of tiles.

How we represent the wall

                         _______________      ______________
                        <--- TAIL (CCW) |    / HEAD (CW) --->
 self draws |  dora indicators  |rinshan|   |            initial deal             | self draws
     118 120|122 124 126 128 130|132 134|   | 0   2   4   6   8  10        48  50 |52  54
... +---+---*---+---+---+---+###*---+---+   +---+---+---+---+---+---+ ... +---+---*---+---+ ...
    |#66|#68| D4| D3| D2| D1| D0|RS2|RS0|   |E0 |E2 |S0 |S2 |W0 |W2 |     |E12|W12|#00|#02|      TOP
... +===+===*===+===+===+===+===*===+===+   +===+===+===+===+===+===+ ... +===+===*===+===+ ...
    |#67|#69|UD4|UD3|UD2|UD1|UD0|RS3|RS1|   |E1 |E3 |S1 |S3 |W1 |W3 |     |S12|N12|#01|#03|      BOTTOM
... +---+---*---+---+---+---+---*---+---+   +---+---+---+---+---+---+ ... +---+---*---+---+ ...
     119 121|123 125 127 129 131|133 135|   | 1   3   5   7   9  11        49  51 |53  55
 self draws |ura-dora indicators|rinshan|   |            initial deal             | self draws

In a physical game, the following procedure is used to prepare the wall:

  1. Shuffle: 136 tiles => 4 sides x 17 stacks x 2 tiles per stack, treated as a ring.

  2. Randomly decide the splitting point on this ring (rules vary on this).

  3. From the splitting point: clockwise => head, counterclockwise => tail. Now the ring can be treated as a linear 68 x 2 array of tiles.

  4. Reveal the top tile of the 3rd stack from tail; this is the first Dora Indicator. (figure: ###)

  5. Initial deal: Players take turns (E->S->W->N->E->…) to draw 2 stacks (= 4 tiles) from the head until everyone has 12. Each player then draws one more tile. (figure: E0E3, S0S3, (…), W8W11, N8N11; E12, S12, W12, N12)

  6. The button player takes his first self-draw and the round starts. (figure: #00)

  7. Additional draw after each Kan is taken from the tail. (figure: RS0, RS1, RS2, RS3)

  8. Additional Dora Indicators are flipped further from tail since the initial one. (figure: D1, D2, D3, D4)

In this library, we assign an index to each of the 136 tiles in the “linear” wall after splitting. The split wall is indexed head-to-tail (major), top-to-bottom (minor). In the figure, this index is annotated as numbers next to the boxes (above/below).

pub type Wall =
  @internal Wall

Values

pub fn all_reactions(
  game: Game,
) -> List(#(types.Player, List(types.Reaction)))
pub fn completed_rounds(game: Game) -> List(RoundHistory)
pub fn current_state(game: Game) -> option.Option(State)
pub fn is_finished(game: Game) -> Bool
pub fn jump_to_state(
  game: Game,
  current: State,
) -> Result(Game, types.EngineError)

Replaces the current round state. Lets you jump around, for things like replay or reconnect.

pub fn legal_actions(game: Game) -> List(types.Action)
pub fn legal_reactions(
  game: Game,
  reactor: types.Player,
) -> List(types.Reaction)
pub fn make_dummy_wall() -> Wall
pub fn make_shuffled_wall(num_reds: #(Int, Int, Int)) -> Wall
pub fn make_sorted_wall(num_reds: #(Int, Int, Int)) -> Wall
pub fn new(ruleset: types.Ruleset, points: List(Int)) -> Game

Starts a new game with the provided rules and starting points.

pub fn new_at(
  ruleset: types.Ruleset,
  points: List(Int),
  round_id: types.RoundId,
  pot: Int,
) -> Game

Starts a game with rules, points, and additionally a round ID, and pot. Mostly there so you can “jump” to a specific round in progress, perhaps when reconstructing a game from nothing.

pub fn new_default() -> Game
pub fn opposite_player(p: types.Player) -> types.Player
pub const p0: types.Player
pub const p1: types.Player
pub const p2: types.Player
pub const p3: types.Player
pub fn pending_action(game: Game) -> option.Option(types.Action)
pub fn player(index: Int) -> types.Player

Constructs a player index, wrapping into 0..=3.

pub fn player_to_int(p: types.Player) -> Int
pub fn points(game: Game) -> List(Int)
pub fn pot(game: Game) -> Int
pub fn register_action(
  game: Game,
  act: types.Action,
) -> Result(Game, types.EngineError)

Registers and validates an action.

pub fn register_reaction(
  game: Game,
  reactor: types.Player,
  reaction: types.Reaction,
) -> Result(Game, types.EngineError)

Registers and validates a reaction.

pub fn round_begin(game: Game) -> option.Option(StartInfo)
pub fn round_history(game: Game) -> option.Option(RoundHistory)
pub fn round_id(game: Game) -> types.RoundId
pub fn ruleset(game: Game) -> types.Ruleset
pub fn shuffle_wall(w: Wall) -> Wall
pub fn start_round(
  game: Game,
  wall: Wall,
) -> Result(Game, types.GameError)

Starts the current round with the supplied wall.

pub fn state_actor(state: State) -> types.Player

The player whose turn it is.

pub fn state_discards(
  state: State,
  p: types.Player,
) -> List(types.Discard)

The discard stream (“river”) of a player.

pub fn state_draw(state: State) -> option.Option(types.Tile)

The tile drawn this turn, if any.

pub fn state_furiten(
  state: State,
  p: types.Player,
) -> types.FuritenFlags

The furiten status of a player.

pub fn state_hand(
  state: State,
  p: types.Player,
) -> List(types.Tile)

The concealed tiles of a player, excluding the current draw.

pub fn state_melds(
  state: State,
  p: types.Player,
) -> List(types.Meld)

The melds of a player.

pub fn state_riichi(
  state: State,
  p: types.Player,
) -> option.Option(types.Riichi)

The riichi status of a player.

pub fn status(game: Game) -> types.GameStatus
pub fn step(
  game: Game,
) -> Result(GameStepOutcome, types.EngineError)

Advances the round by one turn. When the round ends, updates points, pot, round id, and status for the next round.

pub fn tile(description: String) -> types.Tile

Constructs a tile from shorthand like "3m" or "0p".

pub fn tile_from_string(
  description: String,
) -> option.Option(types.Tile)

Parses a tile from shorthand, returning None if it is invalid.

pub fn tile_to_string(t: types.Tile) -> option.Option(String)

Formats a tile as shorthand like "3m".

pub fn tiles(description: String) -> List(types.Tile)

Constructs a list of tiles from shorthand like "123m456p".

pub fn wall_tiles(w: Wall) -> List(types.Tile)
✨ Search Document