tic_tac_toe_rust/logic/models/
mark.rs

1//! The `Mark` enum represents a mark on the board in a Tic Tac Toe game.
2//! It can be either a cross or a naught.
3
4/// Represents a mark on the board in a Tic Tac Toe game.
5#[derive(Clone, Copy, Eq, PartialEq, Debug)]
6pub enum Mark {
7    /// The mark representing a cross, which is denoted by the string "X".
8    Cross,
9    /// The mark representing a naught, which is denoted by the string "O".
10    Naught,
11}
12
13impl Mark {
14    /// Returns a new instance of the enum with the opposite variant.
15    pub(super) fn other(&self) -> Self {
16        match self {
17            Mark::Cross => Mark::Naught,
18            Mark::Naught => Mark::Cross,
19        }
20    }
21}
22
23impl std::fmt::Display for Mark {
24    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
25        match *self {
26            Mark::Cross => write!(f, "X"),
27            Mark::Naught => write!(f, "O"),
28        }
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn test_other_naught() {
38        let cross = Mark::Cross;
39        let naught = cross.other();
40        assert_eq!(naught, Mark::Naught);
41    }
42
43    #[test]
44    fn test_other_cross() {
45        let naught = Mark::Naught;
46        let cross = naught.other();
47        assert_eq!(cross, Mark::Cross);
48    }
49}