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:
2021-12-21 21:07:19 +01:00
parent 802b069269
commit 95c0ccfbd2
4 changed files with 83 additions and 44 deletions

43
rs/src/engine.rs Normal file
View File

@@ -0,0 +1,43 @@
use crate::board;
pub struct Engine {
board: board::Board,
}
impl Engine {
pub fn new() -> Self {
let mut board = board::Board::new();
board.reset();
Engine {
board,
}
}
pub fn get_state(&self) -> &board::Board {
&self.board
}
pub fn set_state(&mut self, board: board::Board) {
self.board = board;
}
pub fn get_legal_moves(
&self,
position: &board::Position,
) -> Result<Vec<board::Position>, ()> {
Ok(self
.board
.find_moves(position)?
.chain(self.board.find_captures(position)?)
.collect())
}
pub fn make_move(
&mut self,
source: &board::Position,
target: board::Position,
) -> Result<(), ()> {
if !self.get_legal_moves(source)?.contains(&target) {
Err(())
} else {
// We checked that there is a piece at source in get_legal_moves
self.board.relocate(source, target)
}
}
}