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
use std::{cmp::Ordering, ops::Not};

use takparse::Color;

use crate::{game_result::Reason, Game, GameResult};

const MAX_REVERSIBLE_PLIES: u16 = 50;

impl<const N: usize, const HALF_KOMI: i8> Game<N, HALF_KOMI> {
    #[must_use]
    pub fn result(&self) -> GameResult {
        // We check the result after a move, so for the dragon clause
        // we look at the other player's path first (they just played).
        if self.board.has_road(self.to_move.not()) {
            GameResult::Winner {
                color: self.to_move.not(),
                reason: Reason::Road,
            }
        } else if self.board.has_road(self.to_move) {
            GameResult::Winner {
                color: self.to_move,
                reason: Reason::Road,
            }
        } else if self.white_reserves.depleted()
            || self.black_reserves.depleted()
            || self.board.full()
        {
            self.flat_end()
        } else if self.reversible_plies >= MAX_REVERSIBLE_PLIES {
            GameResult::Draw {
                reason: Reason::ReversiblePlies,
            }
        } else {
            GameResult::Ongoing
        }
    }

    fn flat_end(&self) -> GameResult {
        let reason = if self.white_reserves.depleted() || self.black_reserves.depleted() {
            Reason::ReservesDepleted
        } else {
            Reason::BoardFill
        };
        let flat_diff = self.board.flat_diff();
        match flat_diff.cmp(&(HALF_KOMI / 2)) {
            Ordering::Greater => GameResult::Winner {
                color: Color::White,
                reason,
            },
            Ordering::Less => GameResult::Winner {
                color: Color::Black,
                reason,
            },
            Ordering::Equal => {
                if HALF_KOMI % 2 == 0 {
                    GameResult::Draw { reason }
                } else {
                    GameResult::Winner {
                        color: if HALF_KOMI > 0 {
                            Color::Black
                        } else {
                            Color::White
                        },
                        reason,
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use takparse::Color;

    use crate::{game_result::Reason, Game, GameResult};

    #[test]
    fn dragon_clause() {
        let game = Game::<6, 0>::from_ptn_moves(&[
            "a4", "a3", "b3", "b4", "c3", "c4", "d3", "d4", "d3+", "e4", "e3", "f4", "f3", "Cb5",
            "d4-",
        ]);
        assert_eq!(game.result(), GameResult::Winner {
            color: Color::White,
            reason: Reason::Road
        });
    }

    #[test]
    fn flat_win() {
        let game =
            Game::<3, 0>::from_ptn_moves(&["a3", "c1", "c2", "c3", "b3", "b2", "b1", "a1", "a2"]);
        assert_eq!(game.result(), GameResult::Winner {
            color: Color::White,
            reason: Reason::BoardFill
        });
    }

    #[test]
    fn road_win() {
        let game = Game::<5, 0>::from_ptn_moves(&[
            "d2", "a5", "b4", "d3", "Cc3", "Cc2", "b2", "b1", "b3", "a1", "c4", "c1", "e2", "e3",
        ]);
        assert_eq!(game.result(), GameResult::Winner {
            color: Color::Black,
            reason: Reason::Road,
        });
    }

    #[test]
    fn road_beats_flats() {
        let game =
            Game::<3, 0>::from_ptn_moves(&["a1", "c1", "c2", "a2", "Sa3", "b1", "Sb3", "b2", "c3"]);
        assert_eq!(game.result(), GameResult::Winner {
            color: Color::White,
            reason: Reason::Road,
        });
    }

    #[test]
    fn reserves() {
        let game = Game::<3, 0>::from_ptn_moves(&[
            "a3", "b3", "a2", "b2", "b3<", "b2<", "b3", "b2", "c2", "c3", "2a3-", "b2<", "b3>",
            "b3", "b2", "a3", "a1", "b1", "b2<", "c1", "c2<", "b1+", "b1", "2b2+", "b2", "a3-",
            "a3", "3b3<", "b1<", "3a2-", "b1",
        ]);
        assert_eq!(game.result(), GameResult::Winner {
            color: Color::White,
            reason: Reason::ReservesDepleted
        });
    }

    #[test]
    fn board_fill_komi() {
        let moves = [
            "a1", "a2", "b1", "b2", "c2", "c1", "d1", "d2", "d3", "c3", "b3", "a3", "a4", "b4",
            "c4", "d4",
        ];
        let game = Game::<4, 0>::from_ptn_moves(&moves);
        assert_eq!(game.result(), GameResult::Draw {
            reason: Reason::BoardFill
        });
        let game = Game::<4, -1>::from_ptn_moves(&moves);
        assert_eq!(game.result(), GameResult::Winner {
            color: Color::White,
            reason: Reason::BoardFill
        });
        let game = Game::<4, 1>::from_ptn_moves(&moves);
        assert_eq!(game.result(), GameResult::Winner {
            color: Color::Black,
            reason: Reason::BoardFill,
        });
        let game = Game::<4, 2>::from_ptn_moves(&moves);
        assert_eq!(game.result(), GameResult::Winner {
            color: Color::Black,
            reason: Reason::BoardFill
        });
    }
}