Пример #1
0
def _step_moving_obstacle(grid: Grid,
                          position: Position,
                          *,
                          rng: Optional[rnd.Generator] = None):
    """Utility for moving a single MovingObstacle randomly"""
    assert isinstance(grid[position], MovingObstacle)

    rng = get_gv_rng_if_none(rng)

    next_positions = [
        next_position
        for next_position in get_manhattan_boundary(position, distance=1)
        if next_position in grid and isinstance(grid[next_position], Floor)
    ]

    try:
        next_position = rng.choice(next_positions)
    except ValueError:
        pass
    else:
        grid.swap(position, next_position)
Пример #2
0
def test_grid_swap():
    grid = Grid(3, 4)

    # caching positions and objects before swap
    objects_before = {
        position: grid[position]
        for position in grid.positions()
    }

    pos1 = Position(0, 0)
    pos2 = Position(1, 1)
    grid.swap(pos1, pos2)

    # caching positions and objects after swap
    objects_after = {position: grid[position] for position in grid.positions()}

    # testing swapped objects
    assert objects_before[pos1] is objects_after[pos2]
    assert objects_before[pos2] is objects_after[pos1]

    # testing all other objects are the same
    for position in grid.positions():
        if position not in (pos1, pos2):
            assert objects_before[position] is objects_after[position]