Skip to content

Commit

Permalink
Allow dogy moves by the bot
Browse files Browse the repository at this point in the history
See #1
  • Loading branch information
LinusCDE committed Oct 4, 2020
1 parent 115bc04 commit e97cb95
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/scene/game_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,23 @@ impl GameScene {
bot_bit_move
}

fn try_move(&mut self, bit_move: BitMove) -> Result<(), String> {
let mut selected_move: Option<BitMove> = None;
for legal_move in self.board.generate_moves().iter() {
if legal_move.get_src_u8() == bit_move.get_src_u8()
&& legal_move.get_dest_u8() == bit_move.get_dest_u8()
{
selected_move = Some(legal_move.clone());
fn try_move(&mut self, bit_move: BitMove, trust_move: bool) -> Result<(), String> {
let selected_move: BitMove = if !trust_move {
let mut selected_move: Option<BitMove> = None;
for legal_move in self.board.generate_moves().iter() {
if legal_move.get_src_u8() == bit_move.get_src_u8()
&& legal_move.get_dest_u8() == bit_move.get_dest_u8()
{
selected_move = Some(legal_move.clone());
}
}
}
if selected_move.is_none() {
return Err("Move not found as possibility".to_owned());
}
let selected_move = selected_move.unwrap();
if selected_move.is_none() {
return Err("Move not found as possibility".to_owned());
}
selected_move.unwrap()
} else {
bit_move
};

self.board.apply_move(selected_move);
if let Err(e) = self.board.is_okay() {
Expand Down Expand Up @@ -399,7 +403,7 @@ impl GameScene {
self.finger_down_square = None;
self.clear_move_hints();
let bit_move = BitMove::make(0, src, dest);
if let Err(e) = self.try_move(bit_move) {
if let Err(e) = self.try_move(bit_move, false) {
println!("Invalid move: {}", e);
} else {
self.redraw_squares.insert(dest.clone());
Expand Down Expand Up @@ -565,7 +569,7 @@ impl Scene for GameScene {

// Await bot move
if let Ok(bot_bit_move) = self.bot_move.try_recv() {
if let Err(e) = self.try_move(bot_bit_move) {
if let Err(e) = self.try_move(bot_bit_move, true) {
panic!("Invalid move by bot: {}", e);
}
self.redraw_squares.insert(bot_bit_move.get_src());
Expand Down

0 comments on commit e97cb95

Please sign in to comment.