Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_a_star_exceptions(self):
test_layout = (
""" ############
#0. #.1#
############ """)
universe = CTFUniverse.create(test_layout, 2)
al = Graph(universe.free_positions())
with pytest.raises(NoPathException):
al.a_star((1, 1), (10, 1))
with pytest.raises(NoPathException):
al.a_star((0, 1), (10, 1))
with pytest.raises(NoPathException):
al.a_star((1, 1), (11, 1))
def len_of_shortest_path(layout):
uni = CTFUniverse.create(layout, 2)
al = Graph(uni.free_positions())
path = al.a_star(uni.bots[0].current_pos, uni.bots[1].current_pos)
return len(path)
def test_bfs_exceptions(self):
test_layout = (
""" ############
#0. #.1#
############ """)
universe = CTFUniverse.create(test_layout, 2)
al = Graph(universe.free_positions())
with pytest.raises(NoPathException):
al.bfs((1, 1), [(10, 1)])
with pytest.raises(NoPathException):
al.bfs((1, 1), [(10, 1), (9, 1)])
with pytest.raises(NoPathException):
al.bfs((0, 1), [(10, 1)])
with pytest.raises(NoPathException):
al.bfs((1, 1), [(11, 1)])
def test_extended_adjacency_list(self):
test_layout = (
""" ##################
#0#. . # . #
# ##### ##### #
# . # . .#1#
################## """)
universe = CTFUniverse.create(test_layout, 2)
al = Graph(universe.free_positions())
adjacency_target = {(7, 3): [(7, 2), (7, 3), (6, 3)],
(1, 3): [(1, 2), (2, 3), (1, 3)],
(12, 1): [(13, 1), (12, 1), (11, 1)],
(16, 2): [(16, 3), (16, 1), (16, 2)],
(15, 1): [(16, 1), (15, 1), (14, 1)],
(5, 1): [(6, 1), (5, 1), (4, 1)],
(10, 3): [(10, 2), (11, 3), (10, 3), (9, 3)],
(7, 2): [(7, 3), (7, 1), (8, 2), (7, 2)],
(1, 2): [(1, 3), (1, 1), (1, 2)],
(3, 3): [(4, 3), (3, 3), (2, 3)],
(13, 3): [(14, 3), (13, 3), (12, 3)],
(8, 1): [(8, 2), (8, 1), (7, 1)],
(16, 3): [(16, 2), (16, 3)],
(6, 3): [(7, 3), (6, 3), (5, 3)],
(14, 1): [(15, 1), (14, 1), (13, 1)],
def test_a_star3(self):
test_layout = (
"""
################################################################
#0# # # # # # #
# ######### ###### # # ### #
# # # ######## ## ## # # # # # #
# ############ # # # # # ## ############### #
# # # ### # # # ### ## # #### ###
# ####### #### # # # # # # #
# # 1 # ### ##### ## ############# ###########
# # # # # # # # ## # # #
# ######################### ## ## ######### ##############"""
)
universe = CTFUniverse.create(test_layout, 2)
al = Graph(universe.free_positions())
#Test distance to middle from both sides
assert 15 == len(al.a_star(universe.bots[0].current_pos, universe.bots[1].current_pos))
assert 15 == len(al.a_star(universe.bots[1].current_pos, universe.bots[0].current_pos))
def test_pos_within(self):
test_layout = (
""" ##################
#0#. . # . #
#2##### #####1#
# . # . .#3#
################## """)
universe = CTFUniverse.create(test_layout, 4)
al = Graph(universe.free_positions())
free = {pos for pos, val in universe.maze.items() if not val}
assert not ((0, 0) in al)
with pytest.raises(NoPathException):
al.pos_within((0, 0), 0)
assert not ((6, 2) in al)
with pytest.raises(NoPathException):
al.pos_within((6, 2), 0)
assert (1, 1) in al
unittest.TestCase().assertCountEqual([(1, 1)], al.pos_within((1, 1), 0))
target = [(1, 1), (1, 2), (1,3), (2, 3), (3, 3)]
unittest.TestCase().assertCountEqual(target, al.pos_within((1, 1), 5))
# assuming a_star is working properly
for pos in target:
assert len(al.a_star((1, 1), pos)) < 5
def test_path_to_same_position(self):
test_layout = (
""" ##################
#0#. . # . #
#2##### #####1#
# . # . .#3#
################## """)
universe = CTFUniverse.create(test_layout, 4)
al = Graph(universe.free_positions())
assert [] == al.a_star((1, 1), (1, 1))
assert [] == al.bfs((1, 1), [(1, 1)])
def __init__(self, universe, noise_radius=5, sight_distance=5, seed=None):
self.adjacency = Graph(universe.free_positions())
self.noise_radius = noise_radius
self.sight_distance = sight_distance
self.rnd = random.Random(seed)
def set_initial(datadict, storage):
storage['graph'] = Graph(reachable_positions(datadict, [self.initial_pos]))
storage['next_food'] = None