Parse FEN string into game state
This commit is contained in:
60
src/board.rs
60
src/board.rs
@@ -1,6 +1,7 @@
|
||||
use std::fmt;
|
||||
use u64 as Bitboard;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Board {
|
||||
white_pieces: [Piece; 6],
|
||||
black_pieces: [Piece; 6],
|
||||
@@ -22,11 +23,45 @@ impl Board {
|
||||
Piece::new(0x8100000000000000, Kind::Rook, Color::Black),
|
||||
Piece::new(0x4200000000000000, Kind::Knight, Color::Black),
|
||||
Piece::new(0x2400000000000000, Kind::Bishop, Color::Black),
|
||||
Piece::new(0x1000000000000000, Kind::Queen, Color::Black),
|
||||
Piece::new(0x800000000000000, Kind::King, Color::Black),
|
||||
Piece::new(0x800000000000000, Kind::Queen, Color::Black),
|
||||
Piece::new(0x1000000000000000, Kind::King, Color::Black),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn empty_board() -> Board {
|
||||
Board {
|
||||
white_pieces: [
|
||||
Piece::new(0x0, Kind::Pawn, Color::White),
|
||||
Piece::new(0x0, Kind::Rook, Color::White),
|
||||
Piece::new(0x0, Kind::Knight, Color::White),
|
||||
Piece::new(0x0, Kind::Bishop, Color::White),
|
||||
Piece::new(0x0, Kind::Queen, Color::White),
|
||||
Piece::new(0x0, Kind::King, Color::White),
|
||||
],
|
||||
black_pieces: [
|
||||
Piece::new(0x0, Kind::Pawn, Color::Black),
|
||||
Piece::new(0x0, Kind::Rook, Color::Black),
|
||||
Piece::new(0x0, Kind::Knight, Color::Black),
|
||||
Piece::new(0x0, Kind::Bishop, Color::Black),
|
||||
Piece::new(0x0, Kind::Queen, Color::Black),
|
||||
Piece::new(0x0, Kind::King, Color::Black),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_piece(&mut self, piece: Piece) {
|
||||
match piece.color {
|
||||
Color::Black => self.black_pieces[piece.kind as usize].bitboard |= piece.bitboard,
|
||||
Color::White => self.white_pieces[piece.kind as usize].bitboard |= piece.bitboard,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Board {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Board {
|
||||
@@ -38,7 +73,7 @@ impl fmt::Display for Board {
|
||||
piece.color, piece.kind
|
||||
)?;
|
||||
}
|
||||
writeln!(f, "")?;
|
||||
writeln!(f)?;
|
||||
for piece in &self.black_pieces {
|
||||
writeln!(
|
||||
f,
|
||||
@@ -46,20 +81,21 @@ impl fmt::Display for Board {
|
||||
piece.color, piece.kind
|
||||
)?;
|
||||
}
|
||||
write!(f, "")
|
||||
writeln!(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Piece {
|
||||
pub position: Bitboard,
|
||||
pub bitboard: Bitboard,
|
||||
pub kind: Kind,
|
||||
pub color: Color,
|
||||
}
|
||||
|
||||
impl Piece {
|
||||
pub fn new(position: Bitboard, kind: Kind, color: Color) -> Piece {
|
||||
pub fn new(bitboard: Bitboard, kind: Kind, color: Color) -> Piece {
|
||||
Piece {
|
||||
position,
|
||||
bitboard,
|
||||
kind,
|
||||
color,
|
||||
}
|
||||
@@ -71,16 +107,16 @@ impl fmt::Display for Piece {
|
||||
for rank in (0..8).rev() {
|
||||
for file in 0..8 {
|
||||
let square = 1 << (rank * 8 + file);
|
||||
let position = if square & self.position != 0x0 { 1 } else { 0 };
|
||||
let position = if square & self.bitboard != 0x0 { 1 } else { 0 };
|
||||
write!(f, "{} ", position)?;
|
||||
}
|
||||
writeln!(f, "")?;
|
||||
writeln!(f)?;
|
||||
}
|
||||
write!(f, "")
|
||||
writeln!(f)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Kind {
|
||||
Pawn,
|
||||
Rook,
|
||||
@@ -90,7 +126,7 @@ pub enum Kind {
|
||||
King,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Color {
|
||||
White,
|
||||
Black,
|
||||
|
||||
Reference in New Issue
Block a user