From 1d9c528d9ba03cf5869d87b47b922c054ce3d262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Hauspie?= <michael.hauspie@univ-lille.fr> Date: Sun, 27 Nov 2022 18:02:29 +0100 Subject: [PATCH] Add impl for Board --- src/main.rs | 64 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4b16319..331c6fc 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(); } -- GitLab