def test_single_robot_finish_single_block_goal(self):
     goal_building = GoalBuilding2D("""
     0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 1 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0
     """)
     robot = Robot(Direction.UP)
     robot.take_block(Tile(TileType.BLOCK))
     line_start_coordinates = Coordinates(0, 3)
     base_grid = BaseGrid(goal_building.width, goal_building.height)
     base_grid.add_tile_to_grid(robot, line_start_coordinates)
     shared_grid_access = SharedGridAccess(base_grid, manager=Manager())
     shared_actions_executor = RobotSharedActionsExecutor(
         robot=robot,
         shared_grid_access=shared_grid_access,
         private_grid=shared_grid_access.get_private_copy(),
         robot_coordinates=line_start_coordinates.copy())
     line_scanner_executor = LineScannerExecutor(
         shared_actions_executor=shared_actions_executor)
     line_to_middle = LineToMiddle(
         start_coordinates=line_start_coordinates.copy(),
         direction=Direction.RIGHT,
         block_line=map(bool, [0, 0, 0, 0, 1]))
     line_scanner_executor.scan_line(line=line_to_middle)
     grid = shared_grid_access.get_private_copy()
     assert line_to_middle.is_finished()
     assert goal_building.validate_grid(grid)
     assert grid.get_coord_from_tile(robot) == line_start_coordinates
Exemple #2
0
    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_get_block(self, robot: Robot, direction: Direction):
     robot = robot.copy()
     with self.grid_lock_sync as grid:
         try:
             coordinates = SharedGridAccess._get_robot_coordinates(grid, robot)
             source_coordinates = coordinates.create_neighbour_coordinate(direction)
             robot.validate_get_block_direction(direction)
             block = grid.get_tile_from_source(source_coordinates)
             robot.take_block(block)
         except (OutOfBoundCoordinatesError, TileNotExistsException, TileNotSourceError,
                 WrongTileError, HasInnerBlockError, WrongBlockGetDirection) as e:
             return HitInformation(HitType.ERROR, e)
         grid.update_tile(robot)
         return HitInformation(HitType.GOT_BLOCK, updated_robot=robot)
    def test_another_robot_update_private_grid(self):
        goal_building = GoalBuilding2D("""
        0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0
        0 0 1 0 1 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0
        0 0 0 0 0 0 0 0 0 0 0
        """)
        robot_1 = Robot(Direction.DOWN)
        robot_1.take_block(Tile(TileType.BLOCK))
        robot_2 = Robot(Direction.UP)
        robot_2.take_block(Tile(TileType.BLOCK))
        line_start_coordinates = Coordinates(0, 3)
        robot_1_coordinates = Coordinates(0, 4)
        robot_2_coordinates = Coordinates(0, 2)
        base_grid = BaseGrid(goal_building.width, goal_building.height)
        base_grid.add_tile_to_grid(robot_1, robot_1_coordinates)
        base_grid.add_tile_to_grid(robot_2, robot_2_coordinates)
        shared_grid_access = SharedGridAccess(base_grid, manager=Manager())
        line_to_middle = LineToMiddle(start_coordinates=line_start_coordinates,
                                      direction=Direction.RIGHT,
                                      block_line=list(
                                          map(bool, [0, 0, 1, 0, 1])))
        robot_1_executor = LineScannerWrapperExecutor(
            line=line_to_middle,
            robot=robot_1,
            shared_grid_access=shared_grid_access,
            goal_building=goal_building)
        robot_2_executor = LineScannerWrapperExecutor(
            line=line_to_middle,
            robot=robot_2,
            shared_grid_access=shared_grid_access,
            goal_building=goal_building)
        robot_1_executor.start_working()
        robot_2_executor.start_working()

        robot_1_executor.wait_for_finish()
        robot_2_executor.wait_for_finish()

        grid = shared_grid_access.get_private_copy()
        assert goal_building.validate_grid(grid)