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:
43
rs/src/engine.rs
Normal file
43
rs/src/engine.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user