Exemplo n.º 1
0
def test_island_value():

    board = graph.Board.from_string(g1)
    g = graph.Graph.from_board(board)

    walkable = graph.make_walkable(g)
    island = graph.split_into_subgraphs(walkable)[0]

    value = island.calculate_island_value()
    assert value == 15*2 + 3 + 7*4


    board = graph.Board.from_string(g2)
    g = graph.Graph.from_board(board)

    walkable = graph.make_walkable(g)
    island = graph.split_into_subgraphs(walkable)[0]

    value = island.calculate_island_value()
    assert value == 14*2 + 2*3 + 10*4


    board = graph.Board.from_string(g_sub)
    g = graph.Graph.from_board(board)

    walkable = graph.make_walkable(g)

    island1, island2 = graph.split_into_subgraphs(walkable)


    value = island1.calculate_island_value()
    assert value in (1*3 + 2*4, 6*2 + 3*4)

    value = island2.calculate_island_value()
    assert value in (1*3 + 2*4, 6*2 + 3*4)
Exemplo n.º 2
0
def test_get_next_node_on_path_to():

    board = graph.Board.from_string(g_sub)
    g = graph.Graph.from_board(board)

    walkable = graph.make_walkable(g)

    island1, island2 = sorted(graph.split_into_subgraphs(walkable), key=lambda i: len(i.nodes))

    start = island1.get_node(0, 0)
    target = island2.get_node(4, 2)

    assert island1.get_next_node_on_path_to(start, target) is None

    start = island2.get_node(4, 0)

    assert island2.get_next_node_on_path_to(start, target) is island2.get_node(4, 1)


    # test when target is next to start

    start = walkable.get_node(4, 2)
    target = walkable.get_node(3, 2)

    next_node = walkable.get_next_node_on_path_to(start, target)
    assert next_node is target
Exemplo n.º 3
0
    def get_actions(self, graph, position):

        self.flooded_islands = None
        self.position = position

        while len(self.actions) < 3:
            dried = False
            while len(self.actions) < 3 and self.dry_one_if_possible(graph):
                dried = True

            if dried or self.flooded_islands is None:
                self.flooded_islands = split_into_subgraphs(make_flooded(graph))

            if len(self.actions) == 3:
                return self.commit()

            did_succeed = self.go_towards_best_flooded(graph)

            if not did_succeed:

                current_node = graph.get_node(*self.position)
                if current_node.distance_to_flooded == -1:
                    target = self.find_target(graph)
                    self.go(graph, current_node, target)
                else:
                    target = min(current_node.neighbors, key=lambda n: n.distance_to_flooded)
                    self.go(graph, current_node, target)

        return self.commit()
Exemplo n.º 4
0
def test_subgraphs():

    board = graph.Board.from_string(g_sub)
    g = graph.Graph.from_board(board)

    walkable = graph.make_walkable(g)
    dry = graph.make_dry(g)

    walkable_islands = graph.split_into_subgraphs(walkable)
    dry_islands = graph.split_into_subgraphs(dry)

    assert len(walkable_islands) == 2
    for island in walkable_islands:
        assert len(island.nodes) in (3, 9)

    assert len(dry_islands) == 3
    for island in dry_islands:
        assert len(island.nodes) in (1, 2, 3)
Exemplo n.º 5
0
def test_contains_subgraph():

    board = graph.Board.from_string(g_sub)
    g = graph.Graph.from_board(board)

    walkable = graph.make_walkable(g)
    dry = graph.make_dry(g)

    walkable_islands = graph.split_into_subgraphs(walkable)
    dry_islands = graph.split_into_subgraphs(dry)

    for walkable_island in walkable_islands:
        if walkable_island.get_node(4, 2) is not None: break

    for dry_island in dry_islands:
        if dry_island.get_node(4, 2) is not None: break

    assert dry_island in walkable_island
    assert walkable_island not in dry_island
Exemplo n.º 6
0
def test_next_to_water():

    board = graph.Board.from_string(g2)
    g = graph.Graph.from_board(board)

    assert g.get_node(1, 1).is_next_to_water
    assert g.get_node(4, 1).is_next_to_water
    assert not g.get_node(2, 2).is_next_to_water

    # flooded or drowned nodes are by definition not next to water
    assert not g.get_node(0, 0).is_next_to_water
    assert not g.get_node(1, 0).is_next_to_water

    dry = graph.split_into_subgraphs(graph.make_dry(g))[0] # get some None nodes in the graph

    assert dry.get_node(1, 1).is_next_to_water
    assert not dry.get_node(2, 2).is_next_to_water
Exemplo n.º 7
0
def test_split_extended_islands_big():

    board = graph.Board.from_string(g_big_cluttered)
    g = graph.Graph.from_board(board)

    walkable = graph.make_walkable(g)

    extended_islands = graph.split_into_extended_islands(walkable)

    assert len(extended_islands) == len(graph.split_into_subgraphs(graph.make_dry(g)))

    for island in extended_islands:
        if island.get_node(18, 6) is not None:
            assert len(island.nodes) == 5
            assert island.get_node(19, 6) is not None
            assert island.get_node(19, 5) is not None
            assert island.get_node(19, 4) is not None
            assert island.get_node(20, 4) is not None

        if island.get_node(5, 9) is not None:
            node = island.get_node(5, 9)
            assert node.distance_to_land == 3

        if island.get_node(8, 9) is not None:
            assert island.get_node(5, 9) is not None
        if island.get_node(5, 13) is not None:
            assert island.get_node(5, 9) is not None

        if island.get_node(17, 2) is not None:
            assert island.get_node(18, 2) is not None
        if island.get_node(18, 1) is not None:
            assert island.get_node(18, 2) is not None
        if island.get_node(19, 2) is not None:
            assert island.get_node(18, 2) is not None


        if island.get_node(30, 3)  is not None:
            assert island.get_node(4, 1) is None
        if island.get_node(4, 1) is not None:
            assert island.get_node(30, 3) is None

        assert island.get_node(31, 0) is None