From 6dde0ff29fac703a79de47bb44cb71920cc636ff Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Mon, 27 Jan 2025 11:06:34 -0500 Subject: [PATCH] board printing --- src/main.rs | 7 ++++++- src/repr.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 5f81149..10ab0fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,12 @@ +use repr::{Board, Piece}; + mod agent; mod game; mod repr; fn main() { - println!("Hello, world!"); + let mut board = Board::new(); + board.place(0, 1, Piece::Black).unwrap(); + board.place(0, 3, Piece::White).unwrap(); + println!("{}", board); } diff --git a/src/repr.rs b/src/repr.rs index 3bc1abe..9a2dd1a 100644 --- a/src/repr.rs +++ b/src/repr.rs @@ -1,3 +1,5 @@ +use std::fmt; + #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub enum Piece { Black, @@ -11,6 +13,13 @@ impl Piece { Piece::White => Piece::Black, } } + + pub const fn text(&self) -> &'static str { + match self { + Piece::White => "■", + Piece::Black => "□", + } + } } const BOARD_SIZE: usize = 8; @@ -20,6 +29,23 @@ pub struct Board { board: [[Option; BOARD_SIZE]; BOARD_SIZE], } +impl fmt::Display for Board { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for i in 0..BOARD_SIZE { + write!(f, "|")?; + for j in 0..BOARD_SIZE { + write!( + f, + "{}|", + self.get(i, j).as_ref().map(Piece::text).unwrap_or(" ") + )?; + } + write!(f, "\n")?; + } + Ok(()) + } +} + impl Board { pub const fn new() -> Self { Self {