riichienv-core 0.4.7

Japanese Mahjong (Riichi) game engine with MJAI protocol support
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
use crate::types::TILE_MAX;

// Shanten tables (Nyanten / Cryolite algorithm)
// Hash tables: [tile_index][cumulative_count][tile_count]
#[rustfmt::skip]
const SHUPAI_TABLE: [[[u32; 5]; 15]; 9] = [
    [ // i = 0
        [0,0,0,0,0], [0,139150,0,0,0], [0,105150,244300,0,0],
        [0,75750,180900,320050,0], [0,51810,127560,232710,371860],
        [0,33490,85300,161050,266200], [0,0,33490,85300,161050],
        [0,0,0,33490,85300], [0,0,0,0,33490],
        [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0],
        [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0],
    ],
    [ // i = 1
        [0,0,0,0,0], [0,43130,0,0,0], [0,34995,78125,0,0],
        [0,27120,62115,105245,0], [0,19980,47100,82095,125225],
        [0,13925,33905,61025,96020], [0,9130,23055,43035,70155],
        [0,5595,14725,28650,48630], [0,3180,8775,17905,31830],
        [0,1660,4840,10435,19565], [0,0,1660,4840,10435],
        [0,0,0,1660,4840], [0,0,0,0,1660],
        [0,0,0,0,0], [0,0,0,0,0],
    ],
    [ // i = 2
        [0,0,0,0,0], [0,11880,0,0,0], [0,10374,22254,0,0],
        [0,8688,19062,30942,0], [0,6937,15625,25999,37879],
        [0,5251,12188,20876,31250], [0,3745,8996,15933,24621],
        [0,2499,6244,11495,18432], [0,1548,4047,7792,13043],
        [0,882,2430,4929,8674], [0,456,1338,2886,5385],
        [0,210,666,1548,3096], [0,84,294,750,1632],
        [0,28,112,322,778], [0,0,28,112,322],
    ],
    [ // i = 3
        [0,0,0,0,0], [0,2878,0,0,0], [0,2693,5571,0,0],
        [0,2438,5131,8009,0], [0,2118,4556,7249,10127],
        [0,1753,3871,6309,9002], [0,1372,3125,5243,7681],
        [0,1007,2379,4132,6250], [0,687,1694,3066,4819],
        [0,432,1119,2126,3498], [0,247,679,1366,2373],
        [0,126,373,805,1492], [0,56,182,429,861],
        [0,21,77,203,450], [0,6,27,83,209],
    ],
    [ // i = 4
        [0,0,0,0,0], [0,620,0,0,0], [0,610,1230,0,0],
        [0,590,1200,1820,0], [0,555,1145,1755,2375],
        [0,503,1058,1648,2258], [0,435,938,1493,2083],
        [0,355,790,1293,1848], [0,270,625,1060,1563],
        [0,190,460,815,1250], [0,122,312,582,937],
        [0,70,192,382,652], [0,35,105,227,417],
        [0,15,50,120,242], [0,5,20,55,125],
    ],
    [ // i = 5
        [0,0,0,0,0], [0,125,0,0,0], [0,125,250,0,0],
        [0,125,250,375,0], [0,124,249,374,499],
        [0,121,245,370,495], [0,115,236,360,485],
        [0,105,220,341,465], [0,90,195,310,431],
        [0,72,162,267,382], [0,53,125,215,320],
        [0,35,88,160,250], [0,20,55,108,180],
        [0,10,30,65,118], [0,4,14,34,69],
    ],
    [ // i = 6
        [0,0,0,0,0], [0,25,0,0,0], [0,25,50,0,0],
        [0,25,50,75,0], [0,25,50,75,100],
        [0,25,50,75,100], [0,25,50,75,100],
        [0,25,50,75,100], [0,24,49,74,99],
        [0,22,46,71,96], [0,19,41,65,90],
        [0,15,34,56,80], [0,10,25,44,66],
        [0,6,16,31,50], [0,3,9,19,34],
    ],
    [ // i = 7
        [0,0,0,0,0], [0,5,0,0,0], [0,5,10,0,0],
        [0,5,10,15,0], [0,5,10,15,20],
        [0,5,10,15,20], [0,5,10,15,20],
        [0,5,10,15,20], [0,5,10,15,20],
        [0,5,10,15,20], [0,5,10,15,20],
        [0,5,10,15,20], [0,4,9,14,19],
        [0,3,7,12,17], [0,2,5,9,14],
    ],
    [ // i = 8
        [0,0,0,0,0], [0,1,0,0,0], [0,1,2,0,0],
        [0,1,2,3,0], [0,1,2,3,4],
        [0,1,2,3,4], [0,1,2,3,4],
        [0,1,2,3,4], [0,1,2,3,4],
        [0,1,2,3,4], [0,1,2,3,4],
        [0,1,2,3,4], [0,1,2,3,4],
        [0,1,2,3,4], [0,1,2,3,4],
    ],
];

#[rustfmt::skip]
const ZIPAI_TABLE: [[[u32; 5]; 15]; 7] = [
    [ // i = 0
        [0,0,0,0,0], [0,11880,0,0,0], [0,10374,22254,0,0],
        [0,8688,19062,30942,0], [0,6937,15625,25999,37879],
        [0,5251,12188,20876,31250], [0,0,5251,12188,20876],
        [0,0,0,5251,12188], [0,0,0,0,5251],
        [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0],
        [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0],
    ],
    [ // i = 1
        [0,0,0,0,0], [0,2878,0,0,0], [0,2693,5571,0,0],
        [0,2438,5131,8009,0], [0,2118,4556,7249,10127],
        [0,1753,3871,6309,9002], [0,1372,3125,5243,7681],
        [0,1007,2379,4132,6250], [0,687,1694,3066,4819],
        [0,432,1119,2126,3498], [0,0,432,1119,2126],
        [0,0,0,432,1119], [0,0,0,0,432],
        [0,0,0,0,0], [0,0,0,0,0],
    ],
    [ // i = 2
        [0,0,0,0,0], [0,620,0,0,0], [0,610,1230,0,0],
        [0,590,1200,1820,0], [0,555,1145,1755,2375],
        [0,503,1058,1648,2258], [0,435,938,1493,2083],
        [0,355,790,1293,1848], [0,270,625,1060,1563],
        [0,190,460,815,1250], [0,122,312,582,937],
        [0,70,192,382,652], [0,35,105,227,417],
        [0,15,50,120,242], [0,0,15,50,120],
    ],
    [ // i = 3
        [0,0,0,0,0], [0,125,0,0,0], [0,125,250,0,0],
        [0,125,250,375,0], [0,124,249,374,499],
        [0,121,245,370,495], [0,115,236,360,485],
        [0,105,220,341,465], [0,90,195,310,431],
        [0,72,162,267,382], [0,53,125,215,320],
        [0,35,88,160,250], [0,20,55,108,180],
        [0,10,30,65,118], [0,4,14,34,69],
    ],
    [ // i = 4
        [0,0,0,0,0], [0,25,0,0,0], [0,25,50,0,0],
        [0,25,50,75,0], [0,25,50,75,100],
        [0,25,50,75,100], [0,25,50,75,100],
        [0,25,50,75,100], [0,24,49,74,99],
        [0,22,46,71,96], [0,19,41,65,90],
        [0,15,34,56,80], [0,10,25,44,66],
        [0,6,16,31,50], [0,3,9,19,34],
    ],
    [ // i = 5
        [0,0,0,0,0], [0,5,0,0,0], [0,5,10,0,0],
        [0,5,10,15,0], [0,5,10,15,20],
        [0,5,10,15,20], [0,5,10,15,20],
        [0,5,10,15,20], [0,5,10,15,20],
        [0,5,10,15,20], [0,5,10,15,20],
        [0,5,10,15,20], [0,4,9,14,19],
        [0,3,7,12,17], [0,2,5,9,14],
    ],
    [ // i = 6
        [0,0,0,0,0], [0,1,0,0,0], [0,1,2,0,0],
        [0,1,2,3,0], [0,1,2,3,4],
        [0,1,2,3,4], [0,1,2,3,4],
        [0,1,2,3,4], [0,1,2,3,4],
        [0,1,2,3,4], [0,1,2,3,4],
        [0,1,2,3,4], [0,1,2,3,4],
        [0,1,2,3,4], [0,1,2,3,4],
    ],
];

// Hierarchical key lookup tables (generated by scripts/convert_nyanten_tables.py)
static SHUPAI_KEYS: &[u8; 405_350] = include_bytes!("data/nyanten_shupai_keys.bin");
static ZIPAI_KEYS: &[u8; 43_130] = include_bytes!("data/nyanten_zipai_keys.bin");
static KEYS1: &[u8; 15_876] = include_bytes!("data/nyanten_keys1.bin");
static KEYS2: &[u8; 22_680] = include_bytes!("data/nyanten_keys2.bin");
static KEYS3: &[u8; 49_500] = include_bytes!("data/nyanten_keys3.bin");

#[inline]
fn hash_shupai(tiles: &[u8]) -> usize {
    let mut n: usize = 0;
    let mut h: usize = 0;
    for (i, &c) in tiles.iter().enumerate() {
        let c = c as usize;
        n += c;
        h += SHUPAI_TABLE[i][n][c] as usize;
    }
    h
}

#[inline]
fn hash_zipai(tiles: &[u8]) -> usize {
    let mut n: usize = 0;
    let mut h: usize = 0;
    for (i, &c) in tiles.iter().enumerate() {
        let c = c as usize;
        n += c;
        h += ZIPAI_TABLE[i][n][c] as usize;
    }
    h
}

fn calc_normal(tiles: &[u8; TILE_MAX], len_div3: u8) -> i8 {
    let m = len_div3 as usize;
    let k0_m = SHUPAI_KEYS[hash_shupai(&tiles[0..9])] as usize;
    let k0_p = SHUPAI_KEYS[hash_shupai(&tiles[9..18])] as usize;
    let k1 = KEYS1[k0_m * 126 + k0_p] as usize;
    let k0_s = SHUPAI_KEYS[hash_shupai(&tiles[18..27])] as usize;
    let k2 = KEYS2[k1 * 126 + k0_s] as usize;
    let k0_z = ZIPAI_KEYS[hash_zipai(&tiles[27..34])] as usize;
    let replacement = KEYS3[(k2 * 55 + k0_z) * 5 + m];
    (replacement as i8) - 1
}

fn calc_chitoi(tiles: &[u8; TILE_MAX]) -> i8 {
    let mut pairs = 0u8;
    let mut kinds = 0u8;
    for &c in tiles.iter() {
        if c > 0 {
            kinds += 1;
            if c >= 2 {
                pairs += 1;
            }
        }
    }
    let redunct = 7u8.saturating_sub(kinds) as i8;
    7 - pairs as i8 + redunct - 1
}

fn calc_kokushi(tiles: &[u8; TILE_MAX]) -> i8 {
    const TERMINALS: [usize; 13] = [0, 8, 9, 17, 18, 26, 27, 28, 29, 30, 31, 32, 33];
    let mut kinds = 0i8;
    let mut has_pair = false;
    for &idx in &TERMINALS {
        if tiles[idx] > 0 {
            kinds += 1;
            if tiles[idx] >= 2 {
                has_pair = true;
            }
        }
    }
    14 - kinds - has_pair as i8 - 1
}

pub fn calc_shanten_from_counts(tehai: &[u8; TILE_MAX], tehai_len_div3: u8) -> i8 {
    let mut shanten = calc_normal(tehai, tehai_len_div3);
    if shanten <= 0 || tehai_len_div3 < 4 {
        return shanten;
    }
    shanten = shanten.min(calc_chitoi(tehai));
    if shanten > 0 {
        shanten.min(calc_kokushi(tehai))
    } else {
        shanten
    }
}

/// Valid tile types for 3-player mahjong (sanma): 1m, 9m, 1-9p, 1-9s, 7 honor tiles.
/// Excludes 2m-8m (tile types 1-7) which don't exist in sanma.
#[cfg(feature = "python")]
const SANMA_VALID_TILE_TYPES: [u32; 27] = [
    0, 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,
];

/// Calculate shanten from 136-tile ID hand.
pub fn calculate_shanten(hand_tiles: &[u32]) -> i32 {
    let mut tile_counts = [0u8; TILE_MAX];
    for &tile in hand_tiles {
        let tile_type = (tile / 4) as usize;
        if tile_type < TILE_MAX {
            tile_counts[tile_type] += 1;
        }
    }
    let num_tiles: u8 = tile_counts.iter().sum();
    let len_div3 = num_tiles / 3;
    calc_shanten_from_counts(&tile_counts, len_div3) as i32
}

/// Calculate effective tiles (tiles that reduce shanten when drawn)
#[cfg(feature = "python")]
pub fn calculate_effective_tiles(hand_tiles: &[u32]) -> u32 {
    assert!(
        hand_tiles.len() % 3 == 1,
        "calculate_effective_tiles requires a 3n+1 hand (e.g. 13 tiles), got {}",
        hand_tiles.len()
    );
    let current_shanten = calculate_shanten(hand_tiles);
    let mut effective_count = 0;

    let mut hand_counts = [0u8; TILE_MAX];
    for &tile in hand_tiles {
        let tile_type = (tile / 4) as usize;
        if tile_type < TILE_MAX {
            hand_counts[tile_type] += 1;
        }
    }

    for tile_type in 0..34u32 {
        // skip: already holding all 4 copies
        if hand_counts[tile_type as usize] >= 4 {
            continue;
        }

        let mut new_hand = hand_tiles.to_vec();
        new_hand.push(tile_type * 4);
        let new_shanten = calculate_shanten(&new_hand);

        if new_shanten < current_shanten {
            effective_count += 1;
        }
    }

    effective_count
}

/// Calculate effective tiles, handling both 3n+1 (13-tile) and 3n+2 (14-tile) hands.
/// For a 14-tile hand, returns the best effective-tile count across all discard
/// candidates that don't increase shanten.
#[cfg(feature = "python")]
pub fn calculate_effective_tiles_with_discard(hand_tiles: &[u32]) -> u32 {
    if hand_tiles.len() % 3 == 1 {
        return calculate_effective_tiles(hand_tiles);
    }
    assert!(
        hand_tiles.len() % 3 == 2,
        "calculate_effective_tiles_with_discard requires a 3n+1 or 3n+2 hand, got {}",
        hand_tiles.len()
    );
    let shanten = calculate_shanten(hand_tiles);
    let mut max_eff = 0u32;
    for idx in 0..hand_tiles.len() {
        let sub: Vec<u32> = hand_tiles
            .iter()
            .enumerate()
            .filter(|(i, _)| *i != idx)
            .map(|(_, &t)| t)
            .collect();
        if calculate_shanten(&sub) <= shanten {
            max_eff = max_eff.max(calculate_effective_tiles(&sub));
        }
    }
    max_eff
}

/// Calculate best ukeire (number of tiles that improve hand)
#[cfg(feature = "python")]
pub fn calculate_best_ukeire(hand_tiles: &[u32], visible_tiles: &[u32]) -> u32 {
    let mut max_ukeire = 0;
    let mut visible_counts = [0u32; 34];

    for &tile in visible_tiles {
        let tile_type = (tile / 4) as usize;
        if tile_type < 34 {
            visible_counts[tile_type] += 1;
        }
    }

    let current_shanten = calculate_shanten(hand_tiles);

    let mut base_counts = [0u8; TILE_MAX];
    for &tile in hand_tiles {
        let tile_type = (tile / 4) as usize;
        if tile_type < TILE_MAX {
            base_counts[tile_type] += 1;
        }
    }

    for (idx, _) in hand_tiles.iter().enumerate() {
        let new_hand: Vec<u32> = hand_tiles
            .iter()
            .enumerate()
            .filter(|(i, _)| *i != idx)
            .map(|(_, &t)| t)
            .collect();
        let removed_type = (hand_tiles[idx] / 4) as usize;
        let mut new_hand_counts = base_counts;
        if removed_type < TILE_MAX {
            new_hand_counts[removed_type] -= 1;
        }

        let new_shanten = calculate_shanten(&new_hand);
        if new_shanten > current_shanten {
            continue;
        }

        let mut ukeire = 0;
        for tile_type in 0..34u32 {
            // skip: already holding all 4 copies
            if new_hand_counts[tile_type as usize] >= 4 {
                continue;
            }

            let mut test_hand = new_hand.clone();
            test_hand.push(tile_type * 4);
            let test_shanten = calculate_shanten(&test_hand);

            if test_shanten < new_shanten {
                let remaining = 4u32
                    .saturating_sub(visible_counts[tile_type as usize])
                    .saturating_sub(new_hand_counts[tile_type as usize] as u32);
                ukeire += remaining;
            }
        }

        max_ukeire = max_ukeire.max(ukeire);
    }

    max_ukeire
}

/// Normal-form shanten for 3-player mahjong using the nyanten lookup tables.
///
/// In 3P mahjong, manzu tiles (1m, 9m) cannot form sequences — they behave
/// like honor tiles (koutsu/pair only). The shupai hash gives tiles sequence
/// adjacency potential, which is incorrect for 3P manzu. We fix this by
/// relocating 1m/9m counts into empty honor (zipai) slots, then using the
/// standard 4P lookup chain. Zipai keys are position-independent, so the
/// relocation produces the correct key regardless of which slot is used.
///
/// When all 7 honor slots are occupied and both 1m and 9m are present (rare),
/// we relocate as many as possible and leave the rest in manzu. The residual
/// adjacency error (at most 1) is masked by kokushi in such scattered hands.
fn calc_normal_3p(tiles: &[u8; TILE_MAX], len_div3: u8) -> i8 {
    let mut t = *tiles;

    // Relocate manzu tiles to empty honor slots
    let manzu_counts = [t[0], t[8]];
    let manzu_positions = [0usize, 8];
    t[0] = 0;
    t[8] = 0;

    let mut next_slot = 27;
    for i in 0..2 {
        if manzu_counts[i] == 0 {
            continue;
        }
        // Find the next empty honor slot
        while next_slot < 34 && t[next_slot] != 0 {
            next_slot += 1;
        }
        if next_slot < 34 {
            t[next_slot] = manzu_counts[i];
            next_slot += 1;
        } else {
            // No empty slot: put back in manzu (fallback for overflow)
            t[manzu_positions[i]] = manzu_counts[i];
        }
    }

    calc_normal(&t, len_div3)
}

fn calc_chitoi_3p(tiles: &[u8; TILE_MAX]) -> i8 {
    let mut pairs = 0u8;
    let mut kinds = 0u8;
    for (i, &c) in tiles.iter().enumerate() {
        // Skip 2m-8m (indices 1-7) which don't exist in 3P
        if (1..=7).contains(&i) {
            continue;
        }
        if c > 0 {
            kinds += 1;
            if c >= 2 {
                pairs += 1;
            }
        }
    }
    let redunct = 7u8.saturating_sub(kinds) as i8;
    7 - pairs as i8 + redunct - 1
}

pub fn calc_shanten_from_counts_3p(tehai: &[u8; TILE_MAX], tehai_len_div3: u8) -> i8 {
    let mut shanten = calc_normal_3p(tehai, tehai_len_div3);
    if shanten <= 0 || tehai_len_div3 < 4 {
        return shanten;
    }
    shanten = shanten.min(calc_chitoi_3p(tehai));
    if shanten > 0 {
        // Kokushi terminals are same for 3P: 1m,9m,1p,9p,1s,9s,1-7z = 13
        shanten.min(calc_kokushi(tehai))
    } else {
        shanten
    }
}

/// Calculate shanten for 3-player mahjong.
/// Manzu tiles (1m, 9m) cannot form sequences — only koutsu/pair.
#[cfg(feature = "python")]
pub fn calculate_shanten_3p(hand_tiles: &[u32]) -> i32 {
    let mut tile_counts = [0u8; TILE_MAX];
    for &tile in hand_tiles {
        let tile_type = (tile / 4) as usize;
        if tile_type < TILE_MAX {
            tile_counts[tile_type] += 1;
        }
    }
    let num_tiles: u8 = tile_counts.iter().sum();
    let len_div3 = num_tiles / 3;
    calc_shanten_from_counts_3p(&tile_counts, len_div3) as i32
}

/// Calculate effective tiles for 3-player mahjong (only valid sanma tile types).
#[cfg(feature = "python")]
pub fn calculate_effective_tiles_3p(hand_tiles: &[u32]) -> u32 {
    assert!(
        hand_tiles.len() % 3 == 1,
        "calculate_effective_tiles_3p requires a 3n+1 hand (e.g. 13 tiles), got {}",
        hand_tiles.len()
    );
    let current_shanten = calculate_shanten_3p(hand_tiles);
    let mut effective_count = 0;

    let mut hand_counts = [0u8; TILE_MAX];
    for &tile in hand_tiles {
        let tile_type = (tile / 4) as usize;
        if tile_type < TILE_MAX {
            hand_counts[tile_type] += 1;
        }
    }

    for &tile_type in &SANMA_VALID_TILE_TYPES {
        // skip: already holding all 4 copies
        if hand_counts[tile_type as usize] >= 4 {
            continue;
        }

        let mut new_hand = hand_tiles.to_vec();
        new_hand.push(tile_type * 4);
        let new_shanten = calculate_shanten_3p(&new_hand);

        if new_shanten < current_shanten {
            effective_count += 1;
        }
    }

    effective_count
}

/// Calculate effective tiles for 3-player mahjong, handling both 3n+1 and 3n+2 hands.
#[cfg(feature = "python")]
pub fn calculate_effective_tiles_3p_with_discard(hand_tiles: &[u32]) -> u32 {
    if hand_tiles.len() % 3 == 1 {
        return calculate_effective_tiles_3p(hand_tiles);
    }
    assert!(
        hand_tiles.len() % 3 == 2,
        "calculate_effective_tiles_3p_with_discard requires a 3n+1 or 3n+2 hand, got {}",
        hand_tiles.len()
    );
    let shanten = calculate_shanten_3p(hand_tiles);
    let mut max_eff = 0u32;
    for idx in 0..hand_tiles.len() {
        let sub: Vec<u32> = hand_tiles
            .iter()
            .enumerate()
            .filter(|(i, _)| *i != idx)
            .map(|(_, &t)| t)
            .collect();
        if calculate_shanten_3p(&sub) <= shanten {
            max_eff = max_eff.max(calculate_effective_tiles_3p(&sub));
        }
    }
    max_eff
}

/// Calculate best ukeire for 3-player mahjong (only valid sanma tile types).
#[cfg(feature = "python")]
pub fn calculate_best_ukeire_3p(hand_tiles: &[u32], visible_tiles: &[u32]) -> u32 {
    let mut max_ukeire = 0;
    let mut visible_counts = [0u32; 34];

    for &tile in visible_tiles {
        let tile_type = (tile / 4) as usize;
        if tile_type < 34 {
            visible_counts[tile_type] += 1;
        }
    }

    let current_shanten = calculate_shanten_3p(hand_tiles);

    let mut base_counts = [0u8; TILE_MAX];
    for &tile in hand_tiles {
        let tile_type = (tile / 4) as usize;
        if tile_type < TILE_MAX {
            base_counts[tile_type] += 1;
        }
    }

    for (idx, _) in hand_tiles.iter().enumerate() {
        let new_hand: Vec<u32> = hand_tiles
            .iter()
            .enumerate()
            .filter(|(i, _)| *i != idx)
            .map(|(_, &t)| t)
            .collect();
        let removed_type = (hand_tiles[idx] / 4) as usize;
        let mut new_hand_counts = base_counts;
        if removed_type < TILE_MAX {
            new_hand_counts[removed_type] -= 1;
        }

        let new_shanten = calculate_shanten_3p(&new_hand);
        if new_shanten > current_shanten {
            continue;
        }

        let mut ukeire = 0;
        for &tile_type in &SANMA_VALID_TILE_TYPES {
            // skip: already holding all 4 copies
            if new_hand_counts[tile_type as usize] >= 4 {
                continue;
            }

            let mut test_hand = new_hand.clone();
            test_hand.push(tile_type * 4);
            let test_shanten = calculate_shanten_3p(&test_hand);

            if test_shanten < new_shanten {
                let remaining = 4u32
                    .saturating_sub(visible_counts[tile_type as usize])
                    .saturating_sub(new_hand_counts[tile_type as usize] as u32);
                ukeire += remaining;
            }
        }

        max_ukeire = max_ukeire.max(ukeire);
    }

    max_ukeire
}

#[cfg(all(test, feature = "python"))]
mod tests {
    use super::*;

    /// Helper: build tile IDs from tile-type indices (each using instance 0).
    fn tiles_from_types(types: &[u32]) -> Vec<u32> {
        types.iter().map(|&t| t * 4).collect()
    }

    #[test]
    fn test_effective_tiles_auto_13_tile() {
        // 123m 456m 789m 12p 11z (13 tiles, tenpai waiting on 3p)
        let hand = tiles_from_types(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 27, 27]);
        assert_eq!(hand.len() % 3, 1);
        let eff = calculate_effective_tiles_with_discard(&hand);
        // tenpai hand: only 3p (type 11) reduces shanten from 0 to -1
        assert_eq!(eff, calculate_effective_tiles(&hand));
        assert_eq!(eff, 1);
    }

    #[test]
    fn test_effective_tiles_auto_14_tile() {
        // 123m 456m 789m 12p 112z (14 tiles, shanten=0)
        let hand = tiles_from_types(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 27, 27, 28]);
        assert_eq!(hand.len() % 3, 2);
        let eff = calculate_effective_tiles_with_discard(&hand);
        // Best discard: 2z -> tenpai waiting 3p -> 1 effective tile
        assert!(
            eff >= 1,
            "14-tile hand should have at least 1 effective tile"
        );
    }

    #[test]
    #[should_panic(expected = "requires a 3n+1 or 3n+2 hand")]
    fn test_effective_tiles_auto_rejects_3n_hand() {
        let hand = tiles_from_types(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 27]);
        assert_eq!(hand.len() % 3, 0);
        calculate_effective_tiles_with_discard(&hand);
    }

    #[test]
    fn test_effective_tiles_3p_auto_14_tile() {
        // 123p 456p 789p 12s 112z (14 tiles)
        let hand = tiles_from_types(&[9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 27, 27, 28]);
        assert_eq!(hand.len() % 3, 2);
        let eff = calculate_effective_tiles_3p_with_discard(&hand);
        assert!(
            eff >= 1,
            "14-tile 3p hand should have at least 1 effective tile"
        );
    }

    #[test]
    #[should_panic(expected = "requires a 3n+1 or 3n+2 hand")]
    fn test_effective_tiles_3p_auto_rejects_3n_hand() {
        let hand = tiles_from_types(&[9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 27]);
        assert_eq!(hand.len() % 3, 0);
        calculate_effective_tiles_3p_with_discard(&hand);
    }

    #[test]
    fn test_best_ukeire_subtracts_hand_counts() {
        // 123m 456m 789m 12p 112z (14 tiles, shanten=0).
        // Best discard: 2z → 123m 456m 789m 12p 11z (tenpai, waiting 3p).
        // Draw 3p: remaining = 4 - 0(visible) - 0(hand) = 4.
        // Total ukeire = 4.
        let hand = tiles_from_types(&[
            0, 1, 2, // 123m
            3, 4, 5, // 456m
            6, 7, 8, // 789m
            9, 10, // 12p
            27, 27, 28, // 112z
        ]);
        let visible: Vec<u32> = vec![];

        let ukeire = calculate_best_ukeire(&hand, &visible);
        assert_eq!(ukeire, 4, "tenpai waiting on 3p should have ukeire=4");
    }

    #[test]
    fn test_best_ukeire_3p_subtracts_hand_counts() {
        // 123p 456p 789p 12s 112z (14 tiles, shanten=0).
        // Best discard: 2z → 123p 456p 789p 12s 11z (tenpai, waiting 3s).
        // Draw 3s: remaining = 4 - 0(visible) - 0(hand) = 4.
        let hand = tiles_from_types(&[
            9, 10, 11, // 123p
            12, 13, 14, // 456p
            15, 16, 17, // 789p
            18, 19, // 12s
            27, 27, 28, // 112z
        ]);
        let visible: Vec<u32> = vec![];

        let ukeire = calculate_best_ukeire_3p(&hand, &visible);
        assert_eq!(ukeire, 4, "tenpai waiting on 3s should have ukeire=4");
    }

    #[test]
    fn test_best_ukeire_hand_copies_reduce_remaining() {
        // 123m 456m 789m 33p 332z (14 tiles).
        // Discard 2z → 123m 456m 789m 33p 33z (13 tiles, tenpai).
        // Winning draw: 3p → 333p as koutsu, or 3z → 333z as koutsu.
        //   3p in hand: 2 copies → remaining = 4 - 0 - 2 = 2
        //   3z in hand: 2 copies → remaining = 4 - 0 - 2 = 2
        // Total ukeire = 4 (not 8 as old code would compute).
        let hand = tiles_from_types(&[
            0, 1, 2, // 123m
            3, 4, 5, // 456m
            6, 7, 8, // 789m
            11, 11, // 33p
            29, 29, 28, // 332z
        ]);
        let visible: Vec<u32> = vec![];

        let ukeire = calculate_best_ukeire(&hand, &visible);
        assert_eq!(ukeire, 4, "hand copies should reduce remaining count");
    }

    #[test]
    fn test_best_ukeire_visible_and_hand_both_subtracted() {
        // 123m 456m 789m 33p 332z (14 tiles).
        // 1 copy of 3p visible → 3p remaining = 4 - 1 - 2 = 1
        // 3z remaining = 4 - 0 - 2 = 2
        // Total ukeire = 3.
        let hand = tiles_from_types(&[
            0, 1, 2, // 123m
            3, 4, 5, // 456m
            6, 7, 8, // 789m
            11, 11, // 33p
            29, 29, 28, // 332z
        ]);
        let visible = vec![11 * 4 + 2]; // 3p instance 2

        let ukeire = calculate_best_ukeire(&hand, &visible);
        assert_eq!(ukeire, 3, "visible + hand copies should both reduce count");
    }

    #[test]
    fn test_best_ukeire_saturates_when_visible_exceeds_remaining() {
        let hand = tiles_from_types(&[
            0, 1, 2, // 123m
            3, 4, 5, // 456m
            6, 7, 8, // 789m
            11, 11, // 33p
            29, 29, 28, // 332z
        ]);
        let visible = vec![11 * 4, 11 * 4 + 1, 11 * 4 + 2];

        let ukeire = calculate_best_ukeire(&hand, &visible);
        assert_eq!(
            ukeire, 2,
            "over-visible waits should clamp at zero remaining"
        );
    }

    #[test]
    fn test_best_ukeire_3p_saturates_when_visible_exceeds_remaining() {
        let hand = tiles_from_types(&[
            9, 10, 11, // 123p
            12, 13, 14, // 456p
            15, 16, 17, // 789p
            20, 20, // 33s
            29, 29, 28, // 332z
        ]);
        let visible = vec![20 * 4, 20 * 4 + 1, 20 * 4 + 2];

        let ukeire = calculate_best_ukeire_3p(&hand, &visible);
        assert_eq!(
            ukeire, 2,
            "over-visible waits should clamp at zero remaining"
        );
    }
}