tiler_core/bot/
mod.rs

1#[cfg(feature = "serde")]
2use crate::locale::LocaleExport;
3
4use crate::{game::Game, locale::Locale};
5
6mod easy;
7mod medium;
8
9fn try_step(game: &Game, color: usize) -> Option<(Game, usize)> {
10    let player = game.current_player;
11    let points = game.players[player].points;
12    let game = game.step(color);
13
14    match game {
15        Ok(game) => {
16            let diff = game.players[player].points - points;
17
18            Some((game, diff))
19        }
20        _ => None,
21    }
22}
23
24struct BotType<'a> {
25    title: Locale<'a>,
26    step: fn(game: &Game) -> Game,
27}
28
29const BOT_TYPES: &'static [BotType<'static>] = &[
30    BotType {
31        title: Locale {
32            ru: "Простой",
33            en: "Easy",
34        },
35        step: easy::step,
36    },
37    BotType {
38        title: Locale {
39            ru: "Средний",
40            en: "Medium",
41        },
42        step: medium::step,
43    },
44];
45
46#[cfg(feature = "serde")]
47pub fn get_bot_types() -> Vec<LocaleExport> {
48    BOT_TYPES.iter().map(|el| el.title.export()).collect()
49}
50
51pub fn step(bot_type: usize, game: Game) -> Game {
52    if bot_type > BOT_TYPES.len() - 1 {
53        panic!(
54            "Cannot find bot with id {}. Last id is {}, '{}'",
55            bot_type,
56            BOT_TYPES.len() - 1,
57            BOT_TYPES.last().unwrap().title.en
58        );
59    }
60
61    let step = BOT_TYPES[bot_type].step;
62    step(&game)
63}