Change Board state to HashMap
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Color {
|
||||
Black,
|
||||
@@ -204,17 +206,22 @@ impl Piece {
|
||||
}
|
||||
|
||||
pub struct Board {
|
||||
state: [[Option<Piece>; File::ALL_VALUES.len()]; Rank::ALL_VALUES.len()],
|
||||
state: HashMap<Position, Piece>,
|
||||
}
|
||||
|
||||
impl Board {
|
||||
pub fn get_at(&self, position: &Position) -> &Option<Piece> {
|
||||
&self.state[position.rank.get_index() as usize]
|
||||
[position.file.get_index() as usize]
|
||||
pub fn get_at(&self, position: &Position) -> Option<&Piece> {
|
||||
return self.state.get(position);
|
||||
}
|
||||
fn set_at(&mut self, position: &Position, piece_or_empty: Option<Piece>) {
|
||||
self.state[position.rank.get_index() as usize]
|
||||
[position.file.get_index() as usize] = piece_or_empty;
|
||||
pub fn set_at(
|
||||
&mut self,
|
||||
position: Position,
|
||||
piece_or_empty: Option<Piece>,
|
||||
) {
|
||||
match piece_or_empty {
|
||||
Some(piece) => self.state.insert(position, piece),
|
||||
None => self.state.remove(&position),
|
||||
};
|
||||
}
|
||||
pub fn new() -> Board {
|
||||
let mut board = Board {
|
||||
@@ -222,9 +229,12 @@ impl Board {
|
||||
};
|
||||
for piece_type in &[PieceType::Pawn] {
|
||||
for (piece, position) in piece_type.initial_setup() {
|
||||
board.set_at(&position, Some(piece));
|
||||
board.set_at(position, Some(piece));
|
||||
}
|
||||
}
|
||||
board
|
||||
}
|
||||
pub fn iter(&self) -> impl Iterator<Item = (&Position, &Piece)> {
|
||||
self.state.iter()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user