def _action_step(self, bot_id, bot_field, **kwargs): actual_position = self._bots_positions.get(bot_id) bot_orientation = bot_field.orientation new_position = get_next_position(actual_position, bot_orientation) try: new_field = self.map[new_position] except OutOfMapError: raise MovementError('New position is out of map.') if isinstance(new_field, TreasureField): raise GameFinished() elif isinstance(new_field, BotField): raise MovementError('Cannot step on another bot.') elif isinstance(new_field, BlockField): raise MovementError('Cannot step on block.') if self._configuration.battery_game and isinstance( bot_field, LaserBatteryBotField): bot_field.drain(bot_field.step_battery_cost) self._map[new_position], self._map[ actual_position] = bot_field, new_field self._bots_positions[bot_id] = new_position
def _action_step(self, bot_id, bot_field, **kwargs): actual_position = self._bots_positions.get(bot_id) bot_orientation = bot_field.orientation new_position = get_next_position( actual_position, bot_orientation ) try: new_field = self.map[new_position] except OutOfMapError: raise MovementError('New position is out of map.') if isinstance(new_field, TreasureField): raise GameFinished() elif isinstance(new_field, BotField): raise MovementError('Cannot step on another bot.') elif isinstance(new_field, BlockField): raise MovementError('Cannot step on block.') if self._configuration.battery_game and isinstance(bot_field, LaserBatteryBotField): bot_field.drain(bot_field.step_battery_cost) self._map[new_position], self._map[actual_position] = bot_field, new_field self._bots_positions[bot_id] = new_position
def get_next_field(self, position, orientation): self._getitem(position) position = get_next_position(position, orientation) try: return self[position] except OutOfMapError: return None
def get_next_field(self, position, orientation): self._getitem(position) position = get_next_position(position, orientation) try: return self[position] except OutOfMapError as e: return None
def test_get_next_position(self): with self.assertRaises(AssertionError): get_next_position((0,), Orientation(Orientation.NORTH)) with self.assertRaises(AssertionError): get_next_position((0, 0), 0) position = 0, 0 self.assertEqual(get_next_position(position, Orientation.NORTH), (0, -1), "Next position to NORTH.") self.assertEqual(get_next_position(position, Orientation.EAST), (1, 0), "Next position to EAST.") self.assertEqual(get_next_position(position, Orientation.SOUTH), (0, 1), "Next position to SOUTH.") self.assertEqual(get_next_position(position, Orientation.WEST), (-1, 0), "Next position to WEST.")
def test_get_next_position(self): with self.assertRaises(AssertionError): get_next_position((0, ), Orientation(Orientation.NORTH)) with self.assertRaises(AssertionError): get_next_position((0, 0), 0) position = 0, 0 self.assertEqual(get_next_position(position, Orientation.NORTH), (0, -1), 'Next position to NORTH.') self.assertEqual(get_next_position(position, Orientation.EAST), (1, 0), 'Next position to EAST.') self.assertEqual(get_next_position(position, Orientation.SOUTH), (0, 1), 'Next position to SOUTH.') self.assertEqual(get_next_position(position, Orientation.WEST), (-1, 0), 'Next position to WEST.')
def place_blocks_line(position, orientation): blocks = 0 max_blocks = len(game_map.map if orientation.is_vertical else game_map.map[0]) max_blocks -= max_blocks % 2 while blocks < max_blocks: # TODO: as constant or configuration? game_map[position] = field_class() position = get_next_position(position, orientation) blocks += 1 try: field = game_map[position] except OutOfMapError: return if isinstance(field, field_class): return if position in bases: bases.remove(position)
def _action_step(self, bot_id, **kwargs): actual_position = self._bots_positions[bot_id] actual_field = self._map[actual_position] bot_orientation = self.map[actual_position].orientation new_position = get_next_position( actual_position, bot_orientation ) try: new_field = self.map[new_position] except OutOfMapError: raise MovementError('New position is out of map.') if isinstance(new_field, TreasureField): raise GameFinished elif isinstance(new_field, BotField): raise MovementError('Cannot step on another bot.') elif isinstance(new_field, BlockField): raise MovementError('Cannot step on block.') self._map[new_position], self._map[actual_position] = actual_field, new_field self._bots_positions[bot_id] = new_position