def test_put_block(self): base_grid = BaseGrid(5, 5) robot = Robot(Direction.UP) robot_coordinates = Coordinates(4, 4) inner_block = Tile(TileType.BLOCK) robot.take_block(inner_block) base_grid.add_tile_to_grid(robot, robot_coordinates) shared_grid_access = SharedGridAccess(base_grid, Manager()) h_info = shared_grid_access.try_put_block(robot, Direction.UP) assert h_info.hit_type == HitType.ERROR assert isinstance(h_info.inner_error, OutOfBoundCoordinatesError) h_info = shared_grid_access.try_put_block(robot, Direction.LEFT) assert h_info.hit_type == HitType.ERROR assert isinstance(h_info.inner_error, WrongBlockPutDirection) h_info = shared_grid_access.try_rotate_robot(robot, Direction.LEFT) assert h_info.hit_type == HitType.ROTATED robot.rotate_to_direction(Direction.LEFT) h_info = shared_grid_access.try_put_block(robot, Direction.LEFT) assert h_info.hit_type == HitType.PLACED_BLOCK inner_block = robot.pop_block() h_info = shared_grid_access.try_move_robot(robot, Direction.LEFT) assert h_info.hit_type == HitType.BLOCK assert isinstance(h_info.inner_error, TileTakenException) assert inner_block == h_info.inner_error.get_tile()
def try_put_block(self, robot: Robot, direction: Direction) -> HitInformation: # we need to make copy to not mess with input robot robot = robot.copy() with self.grid_lock_sync as grid: try: coordinates = SharedGridAccess._get_robot_coordinates(grid, robot) robot.validate_put_block_direction(direction) block_coordinates = coordinates.create_neighbour_coordinate(direction) grid.add_tile_to_grid(robot.pop_block(), block_coordinates) except (OutOfBoundCoordinatesError, WrongTileError, NoInnerBlockError, WrongBlockPutDirection) as e: return HitInformation(HitType.ERROR, e) except TileTakenException as e: tile = e.get_tile() return HitInformation(HitType.from_tile_type(tile.get_type()), e) grid.update_tile(robot) print("robot: ", robot, "placed to ", block_coordinates) return HitInformation(HitType.PLACED_BLOCK, updated_robot=robot)