board printing

This commit is contained in:
Simon Gardling 2025-01-27 11:06:34 -05:00
parent ad551391b2
commit 6dde0ff29f
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 32 additions and 1 deletions

View File

@ -1,7 +1,12 @@
use repr::{Board, Piece};
mod agent; mod agent;
mod game; mod game;
mod repr; mod repr;
fn main() { 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);
} }

View File

@ -1,3 +1,5 @@
use std::fmt;
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Piece { pub enum Piece {
Black, Black,
@ -11,6 +13,13 @@ impl Piece {
Piece::White => Piece::Black, Piece::White => Piece::Black,
} }
} }
pub const fn text(&self) -> &'static str {
match self {
Piece::White => "",
Piece::Black => "",
}
}
} }
const BOARD_SIZE: usize = 8; const BOARD_SIZE: usize = 8;
@ -20,6 +29,23 @@ pub struct Board {
board: [[Option<Piece>; BOARD_SIZE]; BOARD_SIZE], board: [[Option<Piece>; 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 { impl Board {
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {