diff --git a/src/main.rs b/src/main.rs index 4b163196c0ccc8a879e4b66c0cbb80ceffb5d825..331c6fc731f5b0bf1a8a87bd25acc1cbd24b7eda 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,40 @@ struct Board { board: [Square; 9], } +impl Board { + /// Creates a new empty board + fn new() -> Self { + Self { + board: [ + Square::Empty, + Square::Cross, + Square::Circle, + Square::Empty, + Square::Empty, + Square::Empty, + Square::Empty, + Square::Empty, + Square::Empty, + ], + } + } + + /// Display the board to standard output + fn display(&self) { + for row in 0..3 { + println!( + " {} | {} | {}", + square_to_char(&self.board[row * 3]), + square_to_char(&self.board[row * 3 + 1]), + square_to_char(&self.board[row * 3 + 2]) + ); + if row == 0 || row == 1 { + println!("---+---+---"); + } + } + } +} + fn square_to_char(s: &Square) -> char { match s { Square::Empty => '.', @@ -16,34 +50,8 @@ fn square_to_char(s: &Square) -> char { } } -fn display(board: Board) { - for row in 0..3 { - println!( - " {} | {} | {}", - square_to_char(&board.board[row * 3]), - square_to_char(&board.board[row * 3 + 1]), - square_to_char(&board.board[row * 3 + 2]) - ); - if row == 0 || row == 1 { - println!("---+---+---"); - } - } -} - fn main() { - let board = Board { - board: [ - Square::Empty, - Square::Cross, - Square::Circle, - Square::Empty, - Square::Empty, - Square::Empty, - Square::Empty, - Square::Empty, - Square::Empty, - ], - }; + let board = Board::new(); - display(board); + board.display(); }