ritchie/elements/tile

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.

Types

pub type Tile {
  Tile(Int)
}

Constructors

  • Tile(Int)

Values

pub fn char_from_suit(suit: Int) -> option.Option(String)

Returns the shorthand char for the tile suit.

pub fn from_encoding(encoding: Int) -> option.Option(Tile)
pub fn from_num_suit(num: Int, suit: Int) -> option.Option(Tile)

Constructs a tile from a number in a suit

pub fn from_string(str: String) -> option.Option(Tile)

Parses a tile from a string.

pub fn from_wind(wind: player.Player) -> Tile

Constructs a wind tile from a given Wind

pub fn has_red(tile: Tile) -> Bool

Checks if tile is either red or a normal 5

pub fn indicated_dora(tile: Tile) -> Tile

Given this tile as a dora indicator, returns the indicated dora tile

pub fn is_dragon(tile: Tile) -> Bool

Checks if tile is a dragon tile

pub fn is_honor(tile: Tile) -> Bool

Checks if tile is a honor tile (wind or dragon)

pub fn is_middle(tile: Tile) -> Bool

Checks if tile is a middle numeral ({2..8}{m,p,s})

pub fn is_normal(tile: Tile) -> Bool

Checks if the tile is not a red 5

pub fn is_numeral(tile: Tile) -> Bool

Checks if tile is a numeral (bamboo, dot, character tile)

pub fn is_pure_terminal(tile: Tile) -> Bool

Checks if tile is a pure terminal ({1,9}{m,p,s})

pub fn is_red(tile: Tile) -> Bool

Checks if the tile is a red 5

pub fn is_terminal(tile: Tile) -> Bool

Checks if the tile is a terminal (pure terminals + honors)

pub fn is_valid(tile: Tile) -> Bool
pub fn is_wind(tile: Tile) -> Bool

Checks if tile is a wind tile

pub const max_encoding: Int
pub const min_encoding: Int
pub fn normalize(tile: Tile) -> Tile

Normalizes the encoding by converting red 5s to normal 5s

pub fn normalize_inner(tile: Tile) -> Int

Same as normalize, but returns the inner value

pub fn normalized_num(tile: Tile) -> Int

Returns the “number” part of the shorthand, with red 5s converted to non-red.

pub fn num(tile: Tile) -> Int

Returns the “number” part of the shorthand.

pub fn pred(tile: Tile) -> option.Option(Tile)

For numerals 2 to 9, returns 1 to 8 respectively, otherwise None

pub fn pred2(tile: Tile) -> option.Option(Tile)

For numerals 3 to 9, returns 1 to 7 respectively, otherwise None

pub fn redize(tile: Tile) -> Tile

Opposite of normalize: converts normal 5s to red 5s

pub fn redize_inner(tile: Tile) -> Int

Same as redize, but returns the inner value

pub fn sort(tiles: List(Tile)) -> List(Tile)

Sorts a list of tiles according to the standard hand ordering.

pub fn succ(tile: Tile) -> option.Option(Tile)

For numerals 1 to 8, returns 2 to 9 respectively, otherwise None

pub fn succ2(tile: Tile) -> option.Option(Tile)

For numerals 1 to 7, returns 3 to 9 respectively, otherwise None

pub fn suit(tile: Tile) -> Int

Returns the “suit” part of the shorthand (0, 1, 2, 3 for m, p, s, z respectively)

pub fn suit_char(tile: Tile) -> option.Option(String)

Returns the suit part of the shorthand as a character.

pub fn suit_from_char(c: String) -> option.Option(Int)

Returns the tile suit represented by the shorthand suit character.

pub fn suit_from_encoding(enc: Int) -> Int

Returns the suit from a given encoding directly.

pub fn t(str: String) -> Tile

Same as from_string, but works infallably by defaulting to 1z when the parsing fails otherwise. Use with caution! Mostly useful for tests. Probably don’t plug user input in here.

pub fn tiles_from_str(s: String) -> List(Tile)

Parses a shorthand string of tiles following the format (\d*[mpsz])*.

Example:

tile.tiles_from_str("1123m8p8s777z")
// -> [1m, 1m, 2m, 3m, 8p, 8s, 7z, 7z, 7z] (as tile values)
pub fn to_ordering_key(tile: Tile) -> Int

Converts the tile to the internal ordering key where: 1m < … < 4m < 0m < 5m < … < 9m < 1p < … < 9p < 1s < … < 9s < 1z < … 7z Helpful for ordering tiles visually.

pub fn to_string(tile: Tile) -> option.Option(String)

Returns the standard shorthand string of this tile.

pub fn wind(tile: Tile) -> option.Option(player.Player)

Converts to the corresponding Wind if this is a wind tile

✨ Search Document