Implement make_move function on engine
This commit is contained in:
@@ -205,19 +205,25 @@ pub struct Board {
|
||||
}
|
||||
|
||||
impl Board {
|
||||
pub fn get_at(&self, position: &Position) -> Option<&Piece> {
|
||||
fn get_at(&self, position: &Position) -> Option<&Piece> {
|
||||
self.state.get(position)
|
||||
}
|
||||
pub fn set_at(
|
||||
&mut self,
|
||||
position: Position,
|
||||
piece_or_empty: Option<Piece>,
|
||||
) {
|
||||
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),
|
||||
};
|
||||
}
|
||||
fn move_from_to(
|
||||
&mut self,
|
||||
source: &Position,
|
||||
target: Position,
|
||||
) -> Result<(), ()> {
|
||||
let (_, piece) = self.state.remove_entry(source).ok_or(())?;
|
||||
self.set_at(target, Some(piece));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn new() -> Board {
|
||||
Board {
|
||||
state: Default::default(),
|
||||
@@ -255,4 +261,16 @@ impl Board {
|
||||
});
|
||||
Some(valid_moves.chain(valid_captures).collect())
|
||||
}
|
||||
pub fn make_move(
|
||||
&mut self,
|
||||
source: &Position,
|
||||
target: Position,
|
||||
) -> Result<(), ()> {
|
||||
if !self.get_legal_moves(source).ok_or(())?.contains(&target) {
|
||||
Err(())
|
||||
} else {
|
||||
// We checked that there is a piece at source in get_legal_moves
|
||||
self.move_from_to(source, target)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user