ritchie/elements/tile
Represents one tile.
Encoded as a 6-bit integer:
| Encoding | Shorthand | Category (EN) | Category (JP) |
|---|---|---|---|
| 0 ..= 8 | 1m ..= 9m | characters | 萬子 |
| 9 ..= 17 | 1p ..= 9p | dots | 筒子 |
| 18 ..= 26 | 1s ..= 9s | bamboos | 索子 |
| 27 ..= 30 | 1z ..= 4z | winds | 風牌 |
| 31, 32, 33 | 5z, 6z, 7z | dragons | 三元牌 |
| 34, 35, 36 | 0m, 0p, 0s | reds | 赤牌 |
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
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_wind(wind: player.Player) -> Tile
Constructs a wind tile from a given Wind
pub fn indicated_dora(tile: Tile) -> Tile
Given this tile as a dora indicator, returns the indicated dora tile
pub fn is_middle(tile: Tile) -> Bool
Checks if tile is a middle numeral ({2..8}{m,p,s})
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_terminal(tile: Tile) -> Bool
Checks if the tile is a terminal (pure terminals + honors)
pub const max_encoding: Int
pub const min_encoding: Int
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 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 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