From e20e7f0a9029d33c67869951371cc03965127b31 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Fri, 28 Jun 2019 20:15:13 +0200 Subject: Function to give all valid bomb moves (also rustfmt) --- src/game.rs | 82 +++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/src/game.rs b/src/game.rs index 5fe22a3..7c6b649 100644 --- a/src/game.rs +++ b/src/game.rs @@ -448,16 +448,16 @@ impl GameBoard { pub fn valid_moves_for_worm(&self, worm: &Worm) -> ArrayVec<[Action; 8]> { Direction::all() - .iter() - .map(Direction::as_vec) - .map(|d| worm.position + d) - .filter(|p| !self.occupied_cells.contains(p)) - .filter_map(|p| match self.map.at(p) { - Some(false) => Some(Action::Move(p)), - Some(true) => Some(Action::Dig(p)), - _ => None, - }) - .collect() + .iter() + .map(Direction::as_vec) + .map(|d| worm.position + d) + .filter(|p| !self.occupied_cells.contains(p)) + .filter_map(|p| match self.map.at(p) { + Some(false) => Some(Action::Move(p)), + Some(true) => Some(Action::Dig(p)), + _ => None, + }) + .collect() } pub fn valid_move_commands(&self, player_index: usize) -> ArrayVec<[Command; 24]> { @@ -467,31 +467,65 @@ impl GameBoard { .flat_map(|w| self.valid_moves_for_worm(w)) .map(Command::new); - self.valid_selects(player_index).iter() + self.valid_selects(player_index) + .iter() .flat_map(|select_worm| self.players[player_index].find_worm(*select_worm)) - .flat_map(move |w| self.valid_moves_for_worm(w).into_iter().map(move |a| Command::with_select(w.id, a))) + .flat_map(move |w| { + self.valid_moves_for_worm(w) + .into_iter() + .map(move |a| Command::with_select(w.id, a)) + }) .chain(no_select) .collect() } pub fn valid_shoot_commands(&self, player_index: usize) -> ArrayVec<[Command; 24]> { - let all_dirs = Direction::all(); - let no_select = all_dirs + let all_dirs = Direction::all(); + let no_select = all_dirs.iter().map(|d| Command::new(Action::Shoot(*d))); + + self.valid_selects(player_index) .iter() - .map(|d| Command::new(Action::Shoot(*d))); - - self.valid_selects(player_index).iter().flat_map(|select_worm| { - all_dirs - .iter() - .map(move |d| Command::with_select(*select_worm, Action::Shoot(*d))) - }).chain(no_select) + .flat_map(|select_worm| { + all_dirs + .iter() + .map(move |d| Command::with_select(*select_worm, Action::Shoot(*d))) + }) + .chain(no_select) .collect() } pub fn valid_bomb_commands(&self, player_index: usize) -> Vec { - // TODO: Bombs - // TODO: Select and bomb - Vec::new() + let agent_worm = self.players[player_index] + .worms + .iter() + .enumerate() + .find(|(_p, w)| w.bombs > 0); + match agent_worm { + Some((worm_i, worm)) => { + let select = if worm_i == self.players[player_index].active_worm { + None + } else { + Some(worm.id) + }; + + let mut result = Vec::with_capacity(11*11-12); + + for y in worm.position.y - 5..=worm.position.y + 5 { + for x in worm.position.x - 5..=worm.position.x + 5 { + let target = Point2d::new(x, y); + if (worm.position - target).magnitude_squared() < 36 { + result.push(Command { + worm: select, + action: Action::Bomb(target) + }); + } + } + } + + result + } + None => Vec::new(), + } } pub fn sensible_shoot_commands( -- cgit v1.2.3