Move some functionality into the new Engine mod
This will help with separating the "thinking" logic from the move rules and stuff.
This commit is contained in:
@@ -247,7 +247,7 @@ impl Board {
|
||||
state: Default::default(),
|
||||
}
|
||||
}
|
||||
fn get_at(&self, position: &Position) -> Option<&Piece> {
|
||||
pub fn get_at(&self, position: &Position) -> Option<&Piece> {
|
||||
self.state.get(position)
|
||||
}
|
||||
pub fn set_at(
|
||||
@@ -260,7 +260,7 @@ impl Board {
|
||||
None => self.state.remove(&position),
|
||||
};
|
||||
}
|
||||
fn relocate(
|
||||
pub fn relocate(
|
||||
&mut self,
|
||||
source: &Position,
|
||||
target: Position,
|
||||
@@ -269,9 +269,6 @@ impl Board {
|
||||
self.set_at(target, Some(piece));
|
||||
Ok(())
|
||||
}
|
||||
pub fn clear(&mut self) {
|
||||
self.state.clear();
|
||||
}
|
||||
pub fn reset(&mut self) {
|
||||
self.state.clear();
|
||||
for piece_type in &[PieceType::Pawn] {
|
||||
@@ -286,7 +283,7 @@ impl Board {
|
||||
fn cut_move_ray(&self, ray: Ray) -> impl Iterator<Item = Position> + '_ {
|
||||
ray.into_iter().take_while(|position| self.get_at(position).is_none())
|
||||
}
|
||||
fn find_capture(&self, ray: Ray) -> Option<Position> {
|
||||
fn find_capture_along_ray(&self, ray: Ray) -> Option<Position> {
|
||||
// I'm not expecting to call this if there's no piece in the position
|
||||
let crt_piece = self.get_at(&ray.source).unwrap();
|
||||
ray.into_iter().find(|p| match self.get_at(p) {
|
||||
@@ -294,34 +291,30 @@ impl Board {
|
||||
Some(piece) => piece.color != crt_piece.color,
|
||||
})
|
||||
}
|
||||
pub fn get_legal_moves(
|
||||
pub fn find_moves(
|
||||
&self,
|
||||
position: &Position,
|
||||
) -> Option<Vec<Position>> {
|
||||
let piece = match self.get_at(position) {
|
||||
None => return None,
|
||||
Some(piece) => piece,
|
||||
};
|
||||
let valid_moves = piece
|
||||
.get_move_rays(position)
|
||||
.into_iter()
|
||||
.flat_map(|ray| self.cut_move_ray(ray));
|
||||
let valid_captures = piece
|
||||
.get_capture_rays(position)
|
||||
.into_iter()
|
||||
.filter_map(|ray| self.find_capture(ray));
|
||||
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(())
|
||||
) -> Result<impl Iterator<Item = Position> + '_, ()> {
|
||||
if let Some(piece) = self.get_at(position) {
|
||||
Ok(piece
|
||||
.get_move_rays(position)
|
||||
.into_iter()
|
||||
.flat_map(|ray| self.cut_move_ray(ray)))
|
||||
} else {
|
||||
// We checked that there is a piece at source in get_legal_moves
|
||||
self.relocate(source, target)
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
pub fn find_captures(
|
||||
&self,
|
||||
position: &Position,
|
||||
) -> Result<impl Iterator<Item = Position> + '_, ()> {
|
||||
if let Some(piece) = self.get_at(position) {
|
||||
Ok(piece
|
||||
.get_capture_rays(position)
|
||||
.into_iter()
|
||||
.filter_map(|ray| self.find_capture_along_ray(ray)))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user