diff --git a/src/main.rs b/src/main.rs index b03debfb460b0249695806a2944cfcd57fd47a01..f0cf8f50c326be89d5117ad43a98b246f2405836 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ trait ConvertToString { fn to_string(&self) -> String; } +#[derive(Clone, Copy, Debug, PartialEq)] enum Square { Empty, Cross, @@ -27,17 +28,7 @@ impl Board { /// Creates a new empty board fn new() -> Self { Self { - board: [ - Square::Empty, - Square::Empty, - Square::Empty, - Square::Empty, - Square::Empty, - Square::Empty, - Square::Empty, - Square::Empty, - Square::Empty, - ], + board: [Square::Empty; 9], } } @@ -51,12 +42,15 @@ impl ConvertToString for Board { fn to_string(&self) -> String { let mut out = String::new(); for row in 0..3 { - out.push_str(format!( - " {} | {} | {}", - self.board[row * 3].to_string(), - self.board[row * 3 + 1].to_string(), - self.board[row * 3 + 2].to_string() - ).as_str()); + out.push_str( + format!( + " {} | {} | {}", + self.board[row * 3].to_string(), + self.board[row * 3 + 1].to_string(), + self.board[row * 3 + 2].to_string() + ) + .as_str(), + ); if row == 0 || row == 1 { out.push_str("\n---+---+---\n"); } @@ -92,13 +86,7 @@ mod tests { let board = Board::new(); for square in board.board { - // Cannot do that due to 'trait' issues, more on that - // later - // assert_eq!(square, Square::Empty); - match square { - Square::Empty => (), - Square::Cross | Square::Circle => panic!("Square is not empty"), - } + assert_eq!(square, Square::Empty); } } @@ -111,7 +99,7 @@ mod tests { . | . | ."; assert_eq!(expected, Board::new().to_string()); } - + #[test] fn empty_square_to_string_dot() { assert_eq!(".", Square::Empty.to_string());