Skip to main content

chess_lab/core/
game_status.rs

1use std::fmt;
2
3/// Represents the status of a chess game
4///
5#[derive(Debug, PartialEq, Eq, Clone, Copy)]
6pub enum GameStatus {
7    /// The [Game](crate::logic::Game) is still ongoing
8    InProgress,
9    /// The [Game](crate::logic::Game) has ended in a draw
10    Draw(DrawReason),
11    /// White has won the [Game](crate::logic::Game)
12    WhiteWins(WinReason),
13    /// Black has won the [Game](crate::logic::Game)
14    BlackWins(WinReason),
15}
16
17/// Represents the reason for a draw
18///
19#[derive(Debug, PartialEq, Eq, Clone, Copy)]
20pub enum DrawReason {
21    /// The [Game](crate::logic::Game) is a stalemate
22    Stalemate,
23    /// The [Game](crate::logic::Game) is a draw due to insufficient material
24    InsufficientMaterial,
25    /// The [Game](crate::logic::Game) is a draw due to threefold repetition
26    ThreefoldRepetition,
27    /// The [Game](crate::logic::Game) is a draw due to the fifty move rule
28    FiftyMoveRule,
29    /// The [Game](crate::logic::Game) is a draw due to agreement
30    Agreement,
31}
32
33impl fmt::Display for DrawReason {
34    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35        match self {
36            DrawReason::Stalemate => write!(f, "Stalemate"),
37            DrawReason::InsufficientMaterial => write!(f, "Insufficient material"),
38            DrawReason::ThreefoldRepetition => write!(f, "Threefold repetition"),
39            DrawReason::FiftyMoveRule => write!(f, "Fifty move rule"),
40            DrawReason::Agreement => write!(f, "Agreement"),
41        }
42    }
43}
44
45/// Represents winning reasons
46///
47#[derive(Debug, PartialEq, Eq, Clone, Copy)]
48pub enum WinReason {
49    /// The [Game](crate::logic::Game) was won due to checkmate
50    Checkmate,
51    /// The [Game](crate::logic::Game) was won due to resignation
52    Resignation,
53    /// The [Game](crate::logic::Game) was won due to time
54    Time,
55}
56
57impl fmt::Display for WinReason {
58    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59        match self {
60            WinReason::Checkmate => write!(f, "Checkmate"),
61            WinReason::Resignation => write!(f, "Resignation"),
62            WinReason::Time => write!(f, "Time"),
63        }
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70    #[test]
71    fn test_game_status_display() {
72        let mut draw_reason = DrawReason::Stalemate;
73        assert_eq!(draw_reason.to_string(), "Stalemate");
74        draw_reason = DrawReason::InsufficientMaterial;
75        assert_eq!(draw_reason.to_string(), "Insufficient material");
76        draw_reason = DrawReason::ThreefoldRepetition;
77        assert_eq!(draw_reason.to_string(), "Threefold repetition");
78        draw_reason = DrawReason::FiftyMoveRule;
79        assert_eq!(draw_reason.to_string(), "Fifty move rule");
80        draw_reason = DrawReason::Agreement;
81        assert_eq!(draw_reason.to_string(), "Agreement");
82
83        let mut win_reason = WinReason::Checkmate;
84        assert_eq!(win_reason.to_string(), "Checkmate");
85        win_reason = WinReason::Resignation;
86        assert_eq!(win_reason.to_string(), "Resignation");
87        win_reason = WinReason::Time;
88        assert_eq!(win_reason.to_string(), "Time");
89    }
90}