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

Add board module

parent d6aa69ae
Branches
Tags 09-modules
No related merge requests found
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Square {
Empty,
Cross,
Circle,
}
impl std::fmt::Display for Square {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Square::Empty => write!(f, "."),
Square::Cross => write!(f, "X"),
Square::Circle => write!(f, "O"),
}
}
}
pub struct Board {
board: [Square; 9],
}
impl Board {
/// Creates a new empty board
pub fn new() -> Self {
Self {
board: [Square::Empty; 9],
}
}
/// Display the board to standard output
pub fn display(&self) {
println!("{}", self);
}
}
impl std::fmt::Display for Board {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for row in 0..3 {
writeln!(f, " {} | {} | {}",
self.board[row * 3],
self.board[row * 3 + 1],
self.board[row * 3 + 2],
)?;
if row == 0 || row == 1 {
writeln!(f, "---+---+---")?;
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_board_squares_are_empty() {
let board = Board::new();
for square in board.board {
assert_eq!(square, Square::Empty);
}
}
#[test]
fn empty_board_to_string() {
let expected = " . | . | .
---+---+---
. | . | .
---+---+---
. | . | .
";
assert_eq!(expected, format!("{}", Board::new()));
}
#[test]
fn empty_square_to_string_dot() {
assert_eq!(".", format!("{}", Square::Empty));
}
#[test]
fn cross_square_to_string_x() {
assert_eq!("X", format!("{}", Square::Cross));
}
#[test]
fn circle_square_to_string_big_o() {
assert_eq!("O", format!("{}", Square::Circle));
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Square {
Empty,
Cross,
Circle,
}
impl std::fmt::Display for Square {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Square::Empty => write!(f, "."),
Square::Cross => write!(f, "X"),
Square::Circle => write!(f, "O"),
}
}
}
mod board;
struct Board {
board: [Square; 9],
}
impl Board {
/// Creates a new empty board
fn new() -> Self {
Self {
board: [Square::Empty; 9],
}
}
/// Display the board to standard output
fn display(&self) {
println!("{}", self);
}
}
impl std::fmt::Display for Board {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for row in 0..3 {
writeln!(f, " {} | {} | {}",
self.board[row * 3],
self.board[row * 3 + 1],
self.board[row * 3 + 2],
)?;
if row == 0 || row == 1 {
writeln!(f, "---+---+---")?;
}
}
Ok(())
}
}
use board::Board;
fn main() {
let board = Board::new();
......@@ -59,8 +12,6 @@ fn main() {
#[cfg(test)]
// A module where the tests are located, more on modules later
mod tests {
use super::*;
#[test]
fn should_pass() {
assert!(true);
......@@ -70,37 +21,4 @@ mod tests {
fn should_panic() {
panic!("Doing wrong things on purpose here!");
}
#[test]
fn new_board_squares_are_empty() {
let board = Board::new();
for square in board.board {
assert_eq!(square, Square::Empty);
}
}
#[test]
fn empty_board_to_string() {
let expected = " . | . | .
---+---+---
. | . | .
---+---+---
. | . | .
";
assert_eq!(expected, format!("{}", Board::new()));
}
#[test]
fn empty_square_to_string_dot() {
assert_eq!(".", format!("{}", Square::Empty));
}
#[test]
fn cross_square_to_string_x() {
assert_eq!("X", format!("{}", Square::Cross));
}
#[test]
fn circle_square_to_string_big_o() {
assert_eq!("O", format!("{}", Square::Circle));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment