agari 0.20.0

A Riichi Mahjong hand calculator and scoring engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Display utilities for pretty-printing mahjong tiles and hands.
//!
//! Supports both Unicode mahjong characters (🀇🀈🀉...) and ASCII fallback.

use crate::hand::{HandStructure, KanType, Meld};
use crate::parse::ParsedHand;
use crate::tile::{Honor, KOKUSHI_TILES, Suit, Tile};

/// Get the Unicode character for a tile with a trailing space for better rendering.
pub fn tile_to_unicode(tile: &Tile) -> String {
    match tile {
        Tile::Suited { suit, value } => {
            let base = match suit {
                Suit::Man => 0x1F007, // 🀇 = 1-man
                Suit::Pin => 0x1F019, // 🀙 = 1-pin
                Suit::Sou => 0x1F010, // 🀐 = 1-sou
            };
            let c = char::from_u32(base + (*value as u32) - 1).unwrap_or('?');
            format!("{c} ")
        }
        Tile::Honor(honor) => {
            let s = match honor {
                Honor::East => "🀀 ",
                Honor::South => "🀁 ",
                Honor::West => "🀂 ",
                Honor::North => "🀃 ",
                Honor::White => "🀆 ",
                Honor::Green => "🀅 ",
                Honor::Red => "🀄︎ ", // Includes variation selector + space
            };
            s.to_string()
        }
    }
}

/// Get a colored ASCII representation of a tile
pub fn tile_to_ascii(tile: &Tile) -> String {
    match tile {
        Tile::Suited { suit, value } => {
            let s = match suit {
                Suit::Man => 'm',
                Suit::Pin => 'p',
                Suit::Sou => 's',
            };
            format!("{}{}", value, s)
        }
        Tile::Honor(honor) => match honor {
            Honor::East => "E".to_string(),
            Honor::South => "S".to_string(),
            Honor::West => "W".to_string(),
            Honor::North => "N".to_string(),
            Honor::White => "Wh".to_string(),
            Honor::Green => "Gr".to_string(),
            Honor::Red => "Rd".to_string(),
        },
    }
}

/// Format a slice of tiles as Unicode characters
pub fn tiles_to_unicode(tiles: &[Tile]) -> String {
    tiles.iter().map(tile_to_unicode).collect()
}

/// Format a slice of tiles as ASCII
pub fn tiles_to_ascii(tiles: &[Tile]) -> String {
    let mut result = String::new();
    let mut current_suit: Option<Suit> = None;
    let mut pending_values: Vec<u8> = Vec::new();
    let mut honors: Vec<&Honor> = Vec::new();

    for tile in tiles {
        match tile {
            Tile::Suited { suit, value } => {
                if current_suit == Some(*suit) {
                    pending_values.push(*value);
                } else {
                    if let Some(s) = current_suit {
                        for v in &pending_values {
                            result.push_str(&v.to_string());
                        }
                        result.push(match s {
                            Suit::Man => 'm',
                            Suit::Pin => 'p',
                            Suit::Sou => 's',
                        });
                    }
                    pending_values.clear();
                    pending_values.push(*value);
                    current_suit = Some(*suit);
                }
            }
            Tile::Honor(h) => {
                if let Some(s) = current_suit {
                    for v in &pending_values {
                        result.push_str(&v.to_string());
                    }
                    result.push(match s {
                        Suit::Man => 'm',
                        Suit::Pin => 'p',
                        Suit::Sou => 's',
                    });
                    pending_values.clear();
                    current_suit = None;
                }
                honors.push(h);
            }
        }
    }

    if let Some(s) = current_suit {
        for v in &pending_values {
            result.push_str(&v.to_string());
        }
        result.push(match s {
            Suit::Man => 'm',
            Suit::Pin => 'p',
            Suit::Sou => 's',
        });
    }

    if !honors.is_empty() {
        for h in &honors {
            let n = match h {
                Honor::East => '1',
                Honor::South => '2',
                Honor::West => '3',
                Honor::North => '4',
                Honor::White => '5',
                Honor::Green => '6',
                Honor::Red => '7',
            };
            result.push(n);
        }
        result.push('z');
    }

    result
}

/// Format a ParsedHand to normalized notation string (standard numeric notation)
/// This produces machine-readable output suitable for JSON, using notation like "123m456p789s11144z"
pub fn format_hand_normalized(parsed: &ParsedHand) -> String {
    let mut result = String::new();

    // Format called melds first (they appear at the start in the original notation)
    for called in &parsed.called_melds {
        let bracket = match &called.meld {
            Meld::Kan(_, KanType::Closed) => ('[', ']'),
            _ => ('(', ')'),
        };

        result.push(bracket.0);
        result.push_str(&tiles_to_ascii(&called.tiles));
        result.push(bracket.1);
    }

    // Then format the hand tiles
    result.push_str(&tiles_to_ascii(&parsed.tiles));

    result
}

/// Format a tile to compact notation (e.g., "1m", "5z")
fn tile_to_notation(tile: &Tile) -> (String, char) {
    match tile {
        Tile::Suited { suit, value } => {
            let suit_char = match suit {
                Suit::Man => 'm',
                Suit::Pin => 'p',
                Suit::Sou => 's',
            };
            (value.to_string(), suit_char)
        }
        Tile::Honor(honor) => {
            let value = match honor {
                Honor::East => '1',
                Honor::South => '2',
                Honor::West => '3',
                Honor::North => '4',
                Honor::White => '5',
                Honor::Green => '6',
                Honor::Red => '7',
            };
            (value.to_string(), 'z')
        }
    }
}

/// Format a meld using standard numeric notation (e.g., [123m], [111z])
/// This is used for machine-readable JSON output
fn format_meld_normalized(meld: &Meld) -> String {
    match meld {
        Meld::Shuntsu(start, _is_open) => {
            if let Tile::Suited { suit, value } = start {
                let suit_char = match suit {
                    Suit::Man => 'm',
                    Suit::Pin => 'p',
                    Suit::Sou => 's',
                };
                format!("[{}{}{}{}]", value, value + 1, value + 2, suit_char)
            } else {
                "???".to_string()
            }
        }
        Meld::Koutsu(tile, _is_open) => {
            let (val, suit) = tile_to_notation(tile);
            format!("[{}{}{}{}]", val, val, val, suit)
        }
        Meld::Kan(tile, _kan_type) => {
            let (val, suit) = tile_to_notation(tile);
            format!("[{}{}{}{}{}]", val, val, val, val, suit)
        }
    }
}

/// Format a hand structure using standard numeric notation
/// This produces machine-readable output suitable for JSON, e.g., "[123m] [456p] [789s] [111z] [44z]"
pub fn format_structure_normalized(structure: &HandStructure) -> String {
    match structure {
        HandStructure::Chiitoitsu { pairs } => {
            let mut sorted_pairs = pairs.clone();
            sorted_pairs.sort();

            sorted_pairs
                .iter()
                .map(|t| {
                    let (val, suit) = tile_to_notation(t);
                    format!("[{}{}{}]", val, val, suit)
                })
                .collect::<Vec<_>>()
                .join(" ")
        }
        HandStructure::Kokushi { pair } => {
            let mut tiles: Vec<Tile> = KOKUSHI_TILES.to_vec();
            tiles.sort();

            let tile_strs: Vec<String> = tiles
                .iter()
                .map(|t| {
                    let (val, suit) = tile_to_notation(t);
                    if t == pair {
                        format!("[{}{}{}]", val, val, suit)
                    } else {
                        format!("[{}{}]", val, suit)
                    }
                })
                .collect();
            tile_strs.join(" ")
        }
        HandStructure::Standard { melds, pair } => {
            let mut parts: Vec<String> = melds.iter().map(format_meld_normalized).collect();

            let (val, suit) = tile_to_notation(pair);
            parts.push(format!("[{}{}{}]", val, val, suit));

            parts.join(" ")
        }
    }
}

/// Format a meld for display
pub fn format_meld(meld: &Meld, use_unicode: bool) -> String {
    match meld {
        Meld::Shuntsu(start, _is_open) => {
            if let Tile::Suited { suit, value } = start {
                let tiles = [
                    Tile::suited(*suit, *value),
                    Tile::suited(*suit, *value + 1),
                    Tile::suited(*suit, *value + 2),
                ];
                if use_unicode {
                    tiles_to_unicode(&tiles)
                } else {
                    format!(
                        "[{}{}{}{}]",
                        value,
                        value + 1,
                        value + 2,
                        match suit {
                            Suit::Man => 'm',
                            Suit::Pin => 'p',
                            Suit::Sou => 's',
                        }
                    )
                }
            } else {
                "???".to_string()
            }
        }
        Meld::Koutsu(tile, _is_open) => {
            let tiles = [*tile, *tile, *tile];
            if use_unicode {
                tiles_to_unicode(&tiles)
            } else {
                let ascii = tile_to_ascii(tile);
                format!("[{ascii}{ascii}{ascii}]")
            }
        }
        Meld::Kan(tile, _kan_type) => {
            let tiles = [*tile, *tile, *tile, *tile];
            if use_unicode {
                tiles_to_unicode(&tiles)
            } else {
                let ascii = tile_to_ascii(tile);
                format!("[{ascii}{ascii}{ascii}{ascii}]")
            }
        }
    }
}

/// Format a hand structure for display
pub fn format_structure(structure: &HandStructure, use_unicode: bool) -> String {
    match structure {
        HandStructure::Chiitoitsu { pairs } => {
            let mut sorted_pairs = pairs.clone();
            sorted_pairs.sort();

            if use_unicode {
                sorted_pairs
                    .iter()
                    .map(|t| {
                        let uni = tile_to_unicode(t);
                        format!("{uni}{uni}")
                    })
                    .collect::<Vec<_>>()
                    .join(" ")
            } else {
                sorted_pairs
                    .iter()
                    .map(|t| {
                        let ascii = tile_to_ascii(t);
                        format!("[{ascii}{ascii}]")
                    })
                    .collect::<Vec<_>>()
                    .join(" ")
            }
        }
        HandStructure::Kokushi { pair } => {
            // Display all 13 kokushi tiles, with the pair tile shown twice
            let mut tiles: Vec<Tile> = KOKUSHI_TILES.to_vec();
            tiles.sort();

            if use_unicode {
                let tile_strs: Vec<String> = tiles
                    .iter()
                    .map(|t| {
                        if t == pair {
                            // Show pair tile twice
                            format!("{}{}", tile_to_unicode(t), tile_to_unicode(t))
                        } else {
                            tile_to_unicode(t)
                        }
                    })
                    .collect();
                tile_strs.join(" ")
            } else {
                let tile_strs: Vec<String> = tiles
                    .iter()
                    .map(|t| {
                        let ascii = tile_to_ascii(t);
                        if t == pair {
                            format!("[{ascii}{ascii}]")
                        } else {
                            format!("[{ascii}]")
                        }
                    })
                    .collect();
                tile_strs.join(" ")
            }
        }
        HandStructure::Standard { melds, pair } => {
            let mut parts: Vec<String> =
                melds.iter().map(|m| format_meld(m, use_unicode)).collect();

            if use_unicode {
                let uni = tile_to_unicode(pair);
                parts.push(format!("{uni}{uni}"));
            } else {
                let ascii = tile_to_ascii(pair);
                parts.push(format!("[{ascii}{ascii}]"));
            }

            parts.join(" ")
        }
    }
}

/// Get honor name for display
pub fn honor_name(honor: &Honor) -> &'static str {
    match honor {
        Honor::East => "East",
        Honor::South => "South",
        Honor::West => "West",
        Honor::North => "North",
        Honor::White => "White Dragon",
        Honor::Green => "Green Dragon",
        Honor::Red => "Red Dragon",
    }
}

/// Suit name for display
pub fn suit_name(suit: &Suit) -> &'static str {
    match suit {
        Suit::Man => "Man (Characters)",
        Suit::Pin => "Pin (Dots)",
        Suit::Sou => "Sou (Bamboo)",
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tile_to_unicode() {
        assert_eq!(tile_to_unicode(&Tile::suited(Suit::Man, 1)), "🀇 ");
        assert_eq!(tile_to_unicode(&Tile::suited(Suit::Man, 9)), "🀏 ");
        assert_eq!(tile_to_unicode(&Tile::honor(Honor::East)), "🀀 ");
        assert_eq!(tile_to_unicode(&Tile::honor(Honor::Red)), "🀄︎ ");
    }

    #[test]
    fn test_tiles_to_unicode() {
        let tiles = [
            Tile::suited(Suit::Man, 1),
            Tile::suited(Suit::Man, 2),
            Tile::suited(Suit::Man, 3),
        ];
        assert_eq!(tiles_to_unicode(&tiles), "🀇 🀈 🀉 ");
    }
}