diff --git a/src/board.rs b/src/board.rs new file mode 100644 index 0000000000000000000000000000000000000000..0900f91a000d98b515cf8c18171f62d7d69fd41f --- /dev/null +++ b/src/board.rs @@ -0,0 +1,90 @@ + +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Square { + Empty, + Cross, + Circle, +} + +impl std::fmt::Display for Square { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Square::Empty => write!(f, "."), + Square::Cross => write!(f, "X"), + Square::Circle => write!(f, "O"), + } + } +} + +pub struct Board { + board: [Square; 9], +} + +impl Board { + /// Creates a new empty board + pub fn new() -> Self { + Self { + board: [Square::Empty; 9], + } + } + + /// Display the board to standard output + pub fn display(&self) { + println!("{}", self); + } +} + +impl std::fmt::Display for Board { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for row in 0..3 { + writeln!(f, " {} | {} | {}", + self.board[row * 3], + self.board[row * 3 + 1], + self.board[row * 3 + 2], + )?; + if row == 0 || row == 1 { + writeln!(f, "---+---+---")?; + } + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn new_board_squares_are_empty() { + let board = Board::new(); + + for square in board.board { + assert_eq!(square, Square::Empty); + } + } + + #[test] + fn empty_board_to_string() { + let expected = " . | . | . +---+---+--- + . | . | . +---+---+--- + . | . | . +"; + assert_eq!(expected, format!("{}", Board::new())); + } + + #[test] + fn empty_square_to_string_dot() { + assert_eq!(".", format!("{}", Square::Empty)); + } + #[test] + fn cross_square_to_string_x() { + assert_eq!("X", format!("{}", Square::Cross)); + } + #[test] + fn circle_square_to_string_big_o() { + assert_eq!("O", format!("{}", Square::Circle)); + } + +} diff --git a/src/main.rs b/src/main.rs index 2c3283225597fab9e51ab46587553255d925f169..a91e144c9a6189a1da273f4e94b9c05b2766bb54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,54 +1,7 @@ -#[derive(Clone, Copy, Debug, PartialEq)] -enum Square { - Empty, - Cross, - Circle, -} - -impl std::fmt::Display for Square { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Square::Empty => write!(f, "."), - Square::Cross => write!(f, "X"), - Square::Circle => write!(f, "O"), - } - } -} - -struct Board { - board: [Square; 9], -} - -impl Board { - /// Creates a new empty board - fn new() -> Self { - Self { - board: [Square::Empty; 9], - } - } - - /// Display the board to standard output - fn display(&self) { - println!("{}", self); - } -} - -impl std::fmt::Display for Board { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - for row in 0..3 { - writeln!(f, " {} | {} | {}", - self.board[row * 3], - self.board[row * 3 + 1], - self.board[row * 3 + 2], - )?; - if row == 0 || row == 1 { - writeln!(f, "---+---+---")?; - } - } - Ok(()) - } -} +mod board; +use board::Board; + fn main() { let board = Board::new(); @@ -59,8 +12,6 @@ fn main() { #[cfg(test)] // A module where the tests are located, more on modules later mod tests { - use super::*; - #[test] fn should_pass() { assert!(true); @@ -70,37 +21,4 @@ mod tests { fn should_panic() { panic!("Doing wrong things on purpose here!"); } - - #[test] - fn new_board_squares_are_empty() { - let board = Board::new(); - - for square in board.board { - assert_eq!(square, Square::Empty); - } - } - - #[test] - fn empty_board_to_string() { - let expected = " . | . | . ----+---+--- - . | . | . ----+---+--- - . | . | . -"; - assert_eq!(expected, format!("{}", Board::new())); - } - - #[test] - fn empty_square_to_string_dot() { - assert_eq!(".", format!("{}", Square::Empty)); - } - #[test] - fn cross_square_to_string_x() { - assert_eq!("X", format!("{}", Square::Cross)); - } - #[test] - fn circle_square_to_string_big_o() { - assert_eq!("O", format!("{}", Square::Circle)); - } }