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_rotate_robot(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: SharedGridAccess._validate_robot(grid, robot) except WrongTileError as e: return HitInformation(HitType.ERROR, e) robot.rotate_to_direction(direction) grid.update_tile(robot) return HitInformation(HitType.ROTATED, updated_robot=robot)
def test_move_far_scenario(self): base_grid = BaseGrid(5, 5) goal_building = GoalBuilding(""" 0 0 0 0 0 0 0 1 0 0 0 1 2 1 0 0 0 1 2 0 0 0 0 0 0 """) # rotate, move, end coordinates: # if move or rotate is None then don't do it robot_states = [(Direction.UP, None, Coordinates(0, 0)), (None, Direction.UP, Coordinates(0, 1)), (Direction.DOWN, Direction.UP, Coordinates(0, 2)), (Direction.RIGHT, Direction.RIGHT, Coordinates(1, 2)), (None, Direction.RIGHT, Coordinates(2, 2)), (None, Direction.LEFT, Coordinates(1, 2))] rotate_directions = [state[0] for state in robot_states] moving_directions = [state[1] for state in robot_states] robot_coordinates = [state[2] for state in robot_states] robot = Robot(rotate_directions[0]) base_grid.add_tile_to_grid(robot, robot_coordinates[0]) shared_grid_access = SharedGridAccess(base_grid, Manager()) for i in range(1, len(robot_states)): rotate = rotate_directions[i] move = moving_directions[i] robot_executor = RobotRotateMoveMockExecutor( rotate, move, robot, shared_grid_access, goal_building) robot_executor.start_working() robot_executor.wait_for_finish() if rotate is not None: robot.rotate_to_direction(rotate) grid_copy = shared_grid_access.get_private_copy() # robot from grid has wrong direction robot_grid = grid_copy.get_tile_from_grid(robot_coordinates[i]) assert robot_grid == robot
def test_shared_sync(self): base_grid = BaseGrid(5, 5) robot1 = Robot(Direction.RIGHT) robot1_coordinates = Coordinates(2, 2) robot2 = Robot(Direction.UP) robot2_coordinates = Coordinates(1, 3) base_grid.add_tile_to_grid(robot1, robot1_coordinates) base_grid.add_tile_to_grid(robot2, robot2_coordinates) shared_grid_access = SharedGridAccess(base_grid, Manager()) shared_grid_access.try_rotate_robot(robot1, Direction.LEFT) robot1.rotate_to_direction(Direction.LEFT) shared_grid_access.try_move_robot(robot2, Direction.UP) robot2_coordinates = robot2_coordinates.create_neighbour_coordinate(Direction.UP) with shared_grid_access.grid_lock_sync as grid: assert grid.get_tile_from_grid(robot1_coordinates) == robot1 assert grid.get_tile_from_grid(robot2_coordinates) == robot2 print(grid)