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)
def test_distance_to_flooded(): board = graph.Board.from_string(g2) g = graph.make_walkable(graph.Graph.from_board(board)) g.calculate_distance_to_flooded() for node in g.nodes: if node.state == graph.State.flooded: assert node.distance_to_flooded == 0 elif any(n.state == graph.State.flooded for n in node.neighbors): assert node.distance_to_flooded == 0 assert g.get_node(2, 2).distance_to_flooded == 1 board = graph.Board.from_string(g_sub) g = graph.make_walkable(graph.Graph.from_board(board)) g.calculate_distance_to_flooded() for node in g.nodes: if node.state == graph.State.flooded: assert node.distance_to_flooded == 0 elif any(n.state == graph.State.flooded for n in node.neighbors): assert node.distance_to_flooded == 0 assert g.get_node(1, 0).distance_to_flooded == -1
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
def test_split_extended_islands(): board = graph.Board.from_string(g_sub) g = graph.Graph.from_board(board) walkable = graph.make_walkable(g) extended_islands = graph.split_into_extended_islands(walkable) assert len(extended_islands) == 3 for island in extended_islands: if island.get_node(0, 0) is not None: assert len(island.nodes) == 3 elif island.get_node(4, 0) is not None: island2 = island assert len(island.nodes) == 3 elif island.get_node(4, 2) is not None: island3 = island assert len(island.nodes) == 7 assert island2.get_node(4, 1) is not None assert island3.get_node(4, 1) is not None assert island2.get_node(3, 1) is None assert island3.get_node(3, 1) is not None
def test_reachable_big(): board = graph.Board.from_string(g_big) g = graph.Graph.from_board(board) walkable = graph.make_walkable(g) start = walkable.get_node(4, 1) target = walkable.get_node(7, 14) assert walkable.is_reachable(start, target)
def test_get_next_node_on_path_to_big(): board = graph.Board.from_string(g_big) g = graph.Graph.from_board(board) walkable = graph.make_walkable(g) start = walkable.get_node(4, 1) target = walkable.get_node(7, 14) expected = (walkable.get_node(4, 2), walkable.get_node(5, 1)) assert walkable.get_next_node_on_path_to(start, target) in expected
def test_get_next_node_on_path_to_3(): board = graph.Board.from_string(g_sub) g = graph.make_walkable(graph.Graph.from_board(board)) start = g.get_node(4, 0) target = g.get_node(4, 1) target2 = g.get_node(3, 0) unreachable = g.get_node(0, 0) assert g.get_next_node_on_path_to(start, target) is target assert g.get_next_node_on_path_to(start, target2) is target2 assert g.get_next_node_on_path_to(start, unreachable) is None
def test_reachable(): board = graph.Board.from_string(g_sub) g = graph.Graph.from_board(board) walkable = graph.make_walkable(g) upper_left = walkable.get_node(0, 0) upper_right = walkable.get_node(4, 0) lower_right = walkable.get_node(4, 2) assert not walkable.is_reachable(lower_right, upper_left) assert walkable.is_reachable(upper_right, lower_right)
def test_get_distance_between(): board = graph.Board.from_string(g_sub) g = graph.make_walkable(graph.Graph.from_board(board)) start = g.get_node(4, 0) target = g.get_node(4, 1) target2 = g.get_node(3, 2) unreachable = g.get_node(0, 0) assert g.get_distance_between(start, start) == 0 assert g.get_distance_between(start, target) == 1 assert g.get_distance_between(start, target2) == 3 assert g.get_distance_between(start, unreachable) == -1
def test_make_walkable(): board = graph.Board.from_string(g2) g = graph.Graph.from_board(board) walkable = graph.make_walkable(g) for y in range(walkable.rows): for x in range(walkable.columns): node = walkable.get_node(x, y) if node is None: assert g2.split()[y][x] == graph.State.drowned assert len(walkable.nodes) == 5*6 - 4 assert walkable.get_node(0, 1).west is None
def get_actions(self, graph, position): walkable = make_walkable(graph) self.split_graph_into_extended_islands(walkable) mode = self.evaluate_mode(walkable, position) if mode == 'MOVING': strategy = MovingStrategy(self.debug, self.round_) elif mode == 'FARMING': strategy = FarmingStrategy(self.debug, self.round_) elif mode == 'DRYMAX': strategy = DryMaxStrategy(self.debug, self.round_) strategy.extended_islands = self.extended_islands return strategy.get_actions(walkable, position), mode
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)
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
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
def test_make_dry(): board = graph.Board.from_string(g2) g = graph.Graph.from_board(board) dry = graph.make_dry(g) # Test that first making it walkable and then dry is the same as making it dry right away walkable = graph.make_walkable(g) dry2 = graph.make_dry(walkable) for y in range(dry.rows): for x in range(dry.columns): node = dry.get_node(x, y) assert node == dry2.get_node(x, y) if node is None: assert g2.split()[y][x] in (graph.State.flooded, graph.State.drowned) else: assert node.state in (graph.State.dry, graph.State.redry) assert len(dry.nodes) == 3*4 assert dry.get_node(1, 1).west is None