ritchie

Package Version Hex Docs

A complete Riichi Mahjong representation in Gleam. Heavily inspired by summivox’s Rust riichi library. Notable differences include:

gleam add ritchie
import ritchie
import ritchie/types

pub fn main() -> Nil {
  // Initialize a new game with standard rules and 25,000 points per player.
  let game = ritchie.new_default()

  // Start a round with a freshly shuffled wall (and one red five per suit).
  let assert Ok(game) =
    ritchie.start_round(game, ritchie.make_shuffled_wall(#(1, 1, 1)))

  // You can inspect the current state through accessor methods!
  let assert Some(state) = ritchie.current_state(game)
  let actor = ritchie.state_actor(state)
  let _draw = ritchie.state_draw(state)
  let _hand = ritchie.state_hand(state, actor)

  // Construct an action for the player whose turn it is.
  let action =
    types.ActionDiscard(types.Discard(
      tile: ritchie.tile("3m"),
      called_by: actor,
      declares_riichi: False,
      is_tsumogiri: True,
    ))

  // Register the action.
  let assert Ok(game) = ritchie.register_action(game, action)
  // You could also register one or more reaction using
  // `ritchie.register_reaction` here!

  // After actions and reactions are registered and validated, let's
  // advance the game by one step.
  case ritchie.step(game) {
    Ok(ritchie.GameStepOutcome(_game, _step, None)) -> {
      // The next turn has started!
    }
    Ok(ritchie.GameStepOutcome(_game, _step, Some(_))) -> {
      // The round has ended! This can also mean that the whole
      // game is finished.
    }
    Error(_) -> {
      // The action was rejected :(
    }
  }
}

Further documentation can be found at https://ritchie.hexdocs.pm/.

Todo

Major and minor things that need doing:

License

Licensed under the LGPL-3.0.

✨ Search Document