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
use crate::types::{
    board::{Board, BoardCoord},
    prim::{BOrW, Bw},
};

use super::MoveGen;

pub struct BasicMoveGenerator;

fn any_not_home(player: Bw, board: &Board) -> bool {
    for (coord, val) in board.iter_inner_board() {
        if val.matches(player) {
            if !coord.is_home(player) {
                return true;
            }
        }
    }

    false
}

impl MoveGen for BasicMoveGenerator {
    fn gen_moves_one_die(
        board: &crate::types::board::Board,
        die: crate::types::dice::Die,
        player: crate::types::prim::Bw,
    ) -> Vec<Board> {
        let bar = BoardCoord::bar(player);

        if !board[bar].is_empty() {
            let to = (bar + (player, die)).unwrap();

            match board[to].to_bwn() {
                Some((own, 1)) if own != player => {
                    let mut b = board.clone();

                    b[bar] -= 1;
                    b[to] = BOrW::from((player, 1));
                    b.inc_bar(own);

                    return vec![b];
                }
                Some((own, _)) if own != player => return vec![],
                Some((_, _)) => {
                    let mut b = board.clone();

                    b[bar] -= 1;
                    b[to] += 1;

                    return vec![b];
                }
                None => {
                    let mut b = board.clone();

                    b[bar] -= 1;
                    b[to] = BOrW::from((player, 1));

                    return vec![b];
                }
            };
        }

        let mut moves = Vec::new();

        if !any_not_home(player, board) {
            for (coord, val) in board.iter_inner_board() {
                let Some((owner, _)) = val.to_bwn() else {
                    continue;
                };

                if owner != player {
                    continue;
                }

                if coord.perspective(player) <= die.0 {
                    let mut b = board.clone();

                    b[coord] -= 1;
                    // b[coord + (player, die)] += 1;

                    moves.push(b);
                }
            }
        }

        for (coord, val) in board.iter_inner_board() {
            if let Some((own, _)) = val.to_bwn() {
                if own != player {
                    continue;
                }

                let Some(to) = coord + (player, die) else {
                    continue;
                };

                match board[to].to_bwn() {
                    Some((own, 1)) if own != player => {
                        let mut b = board.clone();

                        b[coord] -= 1;
                        b[to] = BOrW::from((player, 1));
                        b.inc_bar(own);

                        moves.push(b);
                    }
                    Some((own, _)) if own != player => continue,
                    Some((_, _)) => {
                        let mut b = board.clone();

                        b[coord] -= 1;
                        b[to] += 1;

                        moves.push(b);
                    }
                    None => {
                        let mut b = board.clone();

                        b[coord] -= 1;
                        b[to] = BOrW::from((player, 1));

                        moves.push(b);
                    }
                };
            }
        }

        moves
    }
}