Packages

A library providing a representation of Riichi Mahjong

Current section

Files

Jump to
ritchie README.md
Raw

README.md

# ritchie

[![Package Version](https://img.shields.io/hexpm/v/ritchie)](https://hex.pm/packages/ritchie)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://ritchie.hexdocs.pm/)

A complete Riichi Mahjong representation in Gleam. Heavily inspired by [`summivox`'s Rust riichi library](https://github.com/summivox/riichi-rs). Notable _differences_ include:
- The public API surface is way smaller.
- The hand decomposition parser is entirely hand-rolled, it doesn't use lookup tables (that would be weird and hard with Gleam's restrictions)
- Possible actions/reactions can be listed at any point during the game, which makes it significantly easier to build upon
  (the logic is already there, after all)

```sh
gleam add ritchie
```
```gleam
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:

- [x] Shuffle wall. `game.start_round` currently expects a pre-shuffled wall
- [x] More types should be opaque, especially the ones that the player uses.
      There should instead be constructors for most things
- [x] _Massively_ reduce API surface. Most stuff should probably live in the
      `internal/` directory instead, especially engine internals
- [ ] Player bankruptcy should be handled
- [ ] Renhou is not detected
- [ ] Probably JSON serialization
- [ ] Properly implement `yaku_extra` and `yaku_block` in the ruleset
## License

Licensed under the LGPL-3.0.