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

Implement Clone, Copy, Debug, PartialEq for square

parent 59c16f17
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ trait ConvertToString { ...@@ -3,6 +3,7 @@ trait ConvertToString {
fn to_string(&self) -> String; fn to_string(&self) -> String;
} }
#[derive(Clone, Copy, Debug, PartialEq)]
enum Square { enum Square {
Empty, Empty,
Cross, Cross,
...@@ -27,17 +28,7 @@ impl Board { ...@@ -27,17 +28,7 @@ impl Board {
/// Creates a new empty board /// Creates a new empty board
fn new() -> Self { fn new() -> Self {
Self { Self {
board: [ board: [Square::Empty; 9],
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
],
} }
} }
...@@ -51,12 +42,15 @@ impl ConvertToString for Board { ...@@ -51,12 +42,15 @@ impl ConvertToString for Board {
fn to_string(&self) -> String { fn to_string(&self) -> String {
let mut out = String::new(); let mut out = String::new();
for row in 0..3 { for row in 0..3 {
out.push_str(format!( out.push_str(
format!(
" {} | {} | {}", " {} | {} | {}",
self.board[row * 3].to_string(), self.board[row * 3].to_string(),
self.board[row * 3 + 1].to_string(), self.board[row * 3 + 1].to_string(),
self.board[row * 3 + 2].to_string() self.board[row * 3 + 2].to_string()
).as_str()); )
.as_str(),
);
if row == 0 || row == 1 { if row == 0 || row == 1 {
out.push_str("\n---+---+---\n"); out.push_str("\n---+---+---\n");
} }
...@@ -92,13 +86,7 @@ mod tests { ...@@ -92,13 +86,7 @@ mod tests {
let board = Board::new(); let board = Board::new();
for square in board.board { for square in board.board {
// Cannot do that due to 'trait' issues, more on that assert_eq!(square, Square::Empty);
// later
// assert_eq!(square, Square::Empty);
match square {
Square::Empty => (),
Square::Cross | Square::Circle => panic!("Square is not empty"),
}
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment