def test_walls_to_nx_graph_impossible():
    # skip the test if networkx is not installed
    networkx = pytest.importorskip('networkx')

    layout = """
    ########
    #  #   #
    #  #   #
    ########
    """
    l = create_layout(layout)
    graph = utils.walls_to_nxgraph(l.walls)
    assert networkx.has_path(graph, (1, 1), (6, 1)) is False
def test_walls_to_nx_graph_one_wall():
    # skip the test if networkx is not installed
    networkx = pytest.importorskip('networkx')

    layout = """
    ########
    #  #   #
    #      #
    ########
    """
    l = create_layout(layout)
    graph = utils.walls_to_nxgraph(l.walls)
    assert networkx.shortest_path_length(graph, (1, 1), (6, 1)) == 7
    with pytest.raises(networkx.NodeNotFound):
        networkx.has_path(graph, (3, 1), (0, 0))
def test_walls_to_nx_graph_full():
    # skip the test if networkx is not installed
    networkx = pytest.importorskip('networkx')

    layout = """
    ########
    #      #
    #      #
    ########
    """
    l = create_layout(layout)
    graph = utils.walls_to_nxgraph(l.walls)
    # test that we generate a path of the proper size
    assert len(graph.nodes) == 12
    # test that the graph doesn't have walls in between
    assert networkx.shortest_path_length(graph, (1, 1), (6, 1)) == 5
def test_shortest_path():
    # is the Graph implementation in pelita giving us the shortest path to the
    # food pellet? And are we really following it?

    # straight line is the right choice
    layout1="""
    ########
    #0    .#
    #.1  EE#
    ########
    """
    path1 = [(6,1), (5,1), (4,1), (3,1), (2,1)]
    # there are more alternatives now, but the shortest is unique, so we can
    # test it
    layout2="""
    ########
    #0####.#
    #      #
    #      #
    #.1  EE#
    ########
    """
    path2 = [(6, 1), (6,2), (5,2), (4,2), (3,2), (2,2), (1,2)]
    for l, p in ((layout1, path1), (layout2, path2)):
        game = setup_test_game(layout=l, is_blue=True)
        # we can ignore this, we just call move to have the bot generate the graph
        # representation of the maze
        next_move = move(0, game)
        graph = game.state['graph']
        path = graph.a_star((1,1), (6,1))
        # test that the generated path is the shortest one
        assert path == p
        # given this layout, move our bot to the next position in the shortest
        # path and see if we follow it
        path.reverse() # flip the path so we have it in the right order
        for idx, step in enumerate(path[:-1]):
            # create a layout where we are starting from the current step in
            # the path
            new_layout = create_layout(l, bots=[step])
            game = setup_test_game(layout=new_layout, is_blue=True)
            next_move = move(0, game)
            next_pos = (step[0]+next_move[0], step[1]+next_move[1])
            assert next_pos == path[idx+1]
Esempio n. 5
0
def test_stays_there():
    # Given a simple layout, verify that the bot does not move, indipendent
    # of its initial position.
    layout = """
    ########
    #     .#
    #.1  EE#
    ########
    """
    # generate all possible locations within the maze
    all_locations = ((x, y) for x in range(8) for y in range(4))
    for loc in all_locations:
        try:
            loc_layout = create_layout(layout, bots=[loc])
        except ValueError:
            # loc is a wall, skip this position
            continue
        game = setup_test_game(layout=loc_layout, is_blue=True)
        next_move = move(0, game)
        assert next_move == (0, 0)
Esempio n. 6
0
def test_always_legal():
    # Given a simple layout, verify that the bot always returns a valid move,
    # indipendent of the initial position and of location of enemies and food
    layout = """
    ########
    #     .#
    #.1  EE#
    ########
    """
    # generate all possible locations within the maze
    all_locations = ((x, y) for x in range(8) for y in range(4))
    for loc in all_locations:
        try:
            loc_layout = create_layout(layout, bots=[loc])
        except ValueError:
            # loc is a wall, skip this position
            continue
        game = setup_test_game(layout=loc_layout, is_blue=True)
        next_move = move(0, game)
        legal_moves = game.team[0].legal_moves
        assert next_move in legal_moves