board printing
This commit is contained in:
parent
ad551391b2
commit
6dde0ff29f
@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
26
src/repr.rs
26
src/repr.rs
@ -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 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user