Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • main
  • 01-simple-board
  • 02-typed-squares
  • 03-board-struct
  • 04-impl-for-board
  • 05-unit-tests
  • 06-trait
  • 07-trait-derive
  • 08-Display
  • 09-modules
  • 09.5-before-main-generic
  • 10-generics
  • 11-flip-coin
13 results

Target

Select target project
  • michael.hauspie/intro-rust-game
1 result
Select Git revision
  • main
  • 01-simple-board
  • 02-typed-squares
  • 03-board-struct
  • 04-impl-for-board
  • 05-unit-tests
  • 06-trait
  • 07-trait-derive
  • 08-Display
  • 09-modules
  • 09.5-before-main-generic
  • 10-generics
  • 11-flip-coin
13 results
Show changes
Commits on Source (2)
trait ConvertToString {
/// Returns a string representation of self
fn to_string(&self) -> String;
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Square { enum Square {
Empty, Empty,
Cross, Cross,
Circle, Circle,
} }
impl ConvertToString for Square {
fn to_string(&self) -> String {
match self {
Square::Empty => String::from("."),
Square::Cross => String::from("X"),
Square::Circle => String::from("O"),
}
}
}
struct Board { struct Board {
board: [Square; 9], board: [Square; 9],
} }
...@@ -12,41 +28,34 @@ impl Board { ...@@ -12,41 +28,34 @@ 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::Cross,
Square::Circle,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
Square::Empty,
],
} }
} }
/// Display the board to standard output /// Display the board to standard output
fn display(&self) { fn display(&self) {
println!("{}", self.to_string());
}
}
impl ConvertToString for Board {
fn to_string(&self) -> String {
let mut out = String::new();
for row in 0..3 { for row in 0..3 {
println!( out.push_str(
" {} | {} | {}", format!(
square_to_char(&self.board[row * 3]), " {} | {} | {}",
square_to_char(&self.board[row * 3 + 1]), self.board[row * 3].to_string(),
square_to_char(&self.board[row * 3 + 2]) self.board[row * 3 + 1].to_string(),
self.board[row * 3 + 2].to_string()
)
.as_str(),
); );
if row == 0 || row == 1 { if row == 0 || row == 1 {
println!("---+---+---"); out.push_str("\n---+---+---\n");
} }
} }
} out
}
fn square_to_char(s: &Square) -> char {
match s {
Square::Empty => '.',
Square::Cross => 'X',
Square::Circle => 'O',
} }
} }
...@@ -61,7 +70,7 @@ fn main() { ...@@ -61,7 +70,7 @@ fn main() {
// A module where the tests are located, more on modules later // A module where the tests are located, more on modules later
mod tests { mod tests {
use super::*; use super::*;
#[test] #[test]
fn should_pass() { fn should_pass() {
assert!(true); assert!(true);
...@@ -77,14 +86,30 @@ mod tests { ...@@ -77,14 +86,30 @@ mod tests {
let board = Board::new(); let board = Board::new();
for square in board.board { for square in board.board {
assert_eq!(square, Square::Empty);
// Cannot do that due to 'trait' issues, more on that
// later
// assert_eq!(square, Square::Empty);
match square {
Square::Empty => (),
Square::Cross | Square::Circle => panic!("Square is not empty"),
}
} }
} }
#[test]
fn empty_board_to_string() {
let expected = " . | . | .
---+---+---
. | . | .
---+---+---
. | . | .";
assert_eq!(expected, Board::new().to_string());
}
#[test]
fn empty_square_to_string_dot() {
assert_eq!(".", Square::Empty.to_string());
}
#[test]
fn cross_square_to_string_x() {
assert_eq!("X", Square::Cross.to_string());
}
#[test]
fn circle_square_to_string_big_o() {
assert_eq!("O", Square::Circle.to_string());
}
} }