From bcf28f2fcb7935b316a5a2f660b065faae51f0d9 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Wed, 7 Aug 2019 18:44:52 +0200 Subject: There is only one valid move once frozen --- src/game.rs | 26 +++++++++++++++----------- src/game/player.rs | 6 ++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/game.rs b/src/game.rs index 769de73..7ece88d 100644 --- a/src/game.rs +++ b/src/game.rs @@ -139,7 +139,9 @@ impl GameBoard { } } - // TODO: Good enough for now, but what if these worms are frozen? + // TODO: Good enough for now, but what if these worms are + // frozen? Then it looks like the move was a banana, but it + // actually does nothing. if json .opponents .iter() @@ -705,7 +707,6 @@ impl GameBoard { let mut result = Vec::with_capacity((SNOWBALL_RANGE * 2 + 1).pow(2) as usize - 12); // -12 is from 3 on each corner - // TODO: Extract to share with bomb throwing for y in worm.position.y - SNOWBALL_RANGE..=worm.position.y + SNOWBALL_RANGE { for x in worm.position.x - SNOWBALL_RANGE..=worm.position.x + SNOWBALL_RANGE { @@ -785,17 +786,20 @@ impl GameBoard { .collect() } - // TODO: If worm is frozen, there's only one option :( // TODO: If all of this can be iterators, I can pass in the final filter to this function and only collect once. pub fn valid_moves(&self, player_index: usize) -> Vec { - self.valid_shoot_commands(player_index) - .iter() - .chain(self.valid_move_commands(player_index).iter()) - .chain(self.valid_bomb_commands(player_index).iter()) - .chain(self.valid_snowball_commands(player_index).iter()) - .chain([Command::new(Action::DoNothing)].iter()) - .cloned() - .collect() + if self.players[player_index].active_worm_is_frozen_after_tick() { + vec![Command::new(Action::DoNothing)] + } else { + self.valid_shoot_commands(player_index) + .iter() + .chain(self.valid_move_commands(player_index).iter()) + .chain(self.valid_bomb_commands(player_index).iter()) + .chain(self.valid_snowball_commands(player_index).iter()) + .chain([Command::new(Action::DoNothing)].iter()) + .cloned() + .collect() + } } } diff --git a/src/game/player.rs b/src/game/player.rs index be9d515..6c2efb2 100644 --- a/src/game/player.rs +++ b/src/game/player.rs @@ -82,6 +82,12 @@ impl Player { .map(|worm| worm.rounds_until_unfrozen > 0) .unwrap_or(false) } + + pub fn active_worm_is_frozen_after_tick(&self) -> bool { + self.active_worm() + .map(|worm| worm.rounds_until_unfrozen > 1) + .unwrap_or(false) + } } #[cfg(test)] -- cgit v1.2.3