mahjong_core/ai/
best_drops.rs

1use crate::{game_summary::GameSummary, PlayerId, TileId};
2
3use super::StandardAI;
4
5impl StandardAI<'_> {
6    // This can become complex if it takes into account different scoring rules.
7    // For now it should only take into account the possibility to create a meld.
8    // In future it should review which tiles have been claimed by other players.
9    // Should have some unit tests.
10    // TODO: finalise
11    pub fn get_best_drops(&self, player_id: &PlayerId) -> Option<Vec<TileId>> {
12        let game_clone = self.game.clone();
13        let game_summary = GameSummary::from_game(&game_clone, player_id)?;
14
15        if !game_summary.hand.clone().unwrap().can_drop_tile() {
16            return None;
17        }
18
19        struct TileDrop {
20            id: TileId,
21            score: usize,
22        }
23
24        let mut drops: Vec<TileDrop> = vec![];
25
26        for tile in game_summary.hand.unwrap().list.iter() {
27            if tile.set_id.is_some() {
28                drops.push(TileDrop {
29                    id: tile.id,
30                    score: 0,
31                });
32            }
33
34            // - Check how possible is it to build a meld with this tile (score can also be one if
35            // one tile left)
36        }
37
38        // Best drops sorted from left to right
39        drops.sort_by(|a, b| a.score.cmp(&b.score));
40
41        let best_drops = drops.iter().map(|drop| drop.id).collect::<Vec<TileId>>();
42
43        Some(best_drops)
44    }
45}