Implement make_move function on engine
This commit is contained in:
@@ -205,19 +205,25 @@ pub struct Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
pub fn get_at(&self, position: &Position) -> Option<&Piece> {
|
fn get_at(&self, position: &Position) -> Option<&Piece> {
|
||||||
self.state.get(position)
|
self.state.get(position)
|
||||||
}
|
}
|
||||||
pub fn set_at(
|
fn set_at(&mut self, position: Position, piece_or_empty: Option<Piece>) {
|
||||||
&mut self,
|
|
||||||
position: Position,
|
|
||||||
piece_or_empty: Option<Piece>,
|
|
||||||
) {
|
|
||||||
match piece_or_empty {
|
match piece_or_empty {
|
||||||
Some(piece) => self.state.insert(position, piece),
|
Some(piece) => self.state.insert(position, piece),
|
||||||
None => self.state.remove(&position),
|
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 {
|
pub fn new() -> Board {
|
||||||
Board {
|
Board {
|
||||||
state: Default::default(),
|
state: Default::default(),
|
||||||
@@ -255,4 +261,16 @@ impl Board {
|
|||||||
});
|
});
|
||||||
Some(valid_moves.chain(valid_captures).collect())
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
rs/src/ui.rs
16
rs/src/ui.rs
@@ -120,6 +120,21 @@ impl Ui {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn make_move(&mut self, args: &[&str]) -> Result<String, String> {
|
||||||
|
if args.len() != 2 {
|
||||||
|
return Err("make_move takes 2 args".to_owned());
|
||||||
|
};
|
||||||
|
let source = board::Position::parse(args[0])
|
||||||
|
.map_err(|_| format!("Error parsing source {}", args[0]))?;
|
||||||
|
let target = board::Position::parse(args[1])
|
||||||
|
.map_err(|_| format!("Error parsing target {}", args[1]))?;
|
||||||
|
match self.board.make_move(&source, target) {
|
||||||
|
Err(()) => {
|
||||||
|
Err(format!("Move not possible {}-{}", args[0], args[1]))
|
||||||
|
}
|
||||||
|
Ok(()) => Ok(self.board.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
fn handle_command(&mut self, s: &str) -> Result<String, String> {
|
fn handle_command(&mut self, s: &str) -> Result<String, String> {
|
||||||
let mut cmd = s.split(',');
|
let mut cmd = s.split(',');
|
||||||
// There will be at least an empty string => otherwise panic
|
// There will be at least an empty string => otherwise panic
|
||||||
@@ -128,6 +143,7 @@ impl Ui {
|
|||||||
match cmd_type {
|
match cmd_type {
|
||||||
"get_state" => self.get_state(&args),
|
"get_state" => self.get_state(&args),
|
||||||
"get_moves" => self.get_moves(&args),
|
"get_moves" => self.get_moves(&args),
|
||||||
|
"make_move" => self.make_move(&args),
|
||||||
"" => Err("No command given".to_owned()),
|
"" => Err("No command given".to_owned()),
|
||||||
_ => Err("Invalid command".to_owned()),
|
_ => Err("Invalid command".to_owned()),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user