Skip to content
Snippets Groups Projects
Commit 1d9c528d authored by Michael Hauspie's avatar Michael Hauspie
Browse files

Add impl for Board

parent b64d652a
No related branches found
No related tags found
No related merge requests found
...@@ -8,42 +8,50 @@ struct Board { ...@@ -8,42 +8,50 @@ struct Board {
board: [Square; 9], board: [Square; 9],
} }
fn square_to_char(s: &Square) -> char { impl Board {
match s { /// Creates a new empty board
Square::Empty => '.', fn new() -> Self {
Square::Cross => 'X', Self {
Square::Circle => 'O', board: [
Square::Empty,
Square::Cross,
Square::Circle,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
],
} }
} }
fn display(board: Board) { /// Display the board to standard output
fn display(&self) {
for row in 0..3 { for row in 0..3 {
println!( println!(
" {} | {} | {}", " {} | {} | {}",
square_to_char(&board.board[row * 3]), square_to_char(&self.board[row * 3]),
square_to_char(&board.board[row * 3 + 1]), square_to_char(&self.board[row * 3 + 1]),
square_to_char(&board.board[row * 3 + 2]) square_to_char(&self.board[row * 3 + 2])
); );
if row == 0 || row == 1 { if row == 0 || row == 1 {
println!("---+---+---"); println!("---+---+---");
} }
} }
} }
}
fn square_to_char(s: &Square) -> char {
match s {
Square::Empty => '.',
Square::Cross => 'X',
Square::Circle => 'O',
}
}
fn main() { fn main() {
let board = Board { let board = Board::new();
board: [
Square::Empty,
Square::Cross,
Square::Circle,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
],
};
display(board); board.display();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment