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 for pos in free.difference(target): assert len(al.a_star((1, 1), pos)) >= 5
def test_a_star2(self): test_layout = ( """ ######## #1# # # # #0 # # # ######## """ ) universe = CTFUniverse.create(test_layout, 2) al = Graph(universe.free_positions()) #Test distance to middle from both sides print(al.a_star(universe.bots[0].current_pos, universe.bots[1].current_pos)) print(al.a_star(universe.bots[1].current_pos, universe.bots[0].current_pos)) assert 7 == len(al.a_star(universe.bots[0].current_pos, universe.bots[1].current_pos)) assert 7 == len(al.a_star(universe.bots[1].current_pos, universe.bots[0].current_pos))
class FoodEatingPlayer(AbstractPlayer): def set_initial(self): # At game initialisation, we make a graph of all reachable positions self.graph = Graph(self.current_uni.reachable([self.initial_pos])) self.next_food = None def goto_pos(self, pos): """A helper method to give us the right move towards the given pos""" return self.graph.a_star(self.current_pos, pos)[-1] def get_move(self): # check if food we wanted to pick is still present if self.next_food is None or self.next_food not in self.enemy_food: if not self.enemy_food: # all food has been eaten? ok, game is over and i’ll stop return datamodel.stop # Then make a choice and stick to it self.next_food = self.rnd.choice(self.enemy_food) try: next_pos = self.goto_pos(self.next_food) move = diff_pos(self.current_pos, next_pos) return move except NoPathException: # This is case something unexpected happens return datamodel.stop
class SmartEatingPlayer(AbstractPlayer): def set_initial(self): self.graph = Graph(self.current_uni.reachable([self.initial_pos])) self.next_food = None def goto_pos(self, pos): return self.graph.a_star(self.current_pos, pos)[-1] def get_move(self): # check, if food is still present if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop self.next_food = self.rnd.choice(self.enemy_food) try: dangerous_enemy_pos = [ bot.current_pos for bot in self.enemy_bots if bot.is_destroyer ] next_pos = self.goto_pos(self.next_food) # check, if the next_pos has an enemy on it if next_pos in dangerous_enemy_pos: # whoops, better wait this round and take another food next time self.next_food = None return datamodel.stop move = diff_pos(self.current_pos, next_pos) return move except NoPathException: return datamodel.stop
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 test_a_star(self): test_layout = ( """ ################## #02.# . # . # # # ### ####1 # # ### . # . ##3# # # ################## """) universe = CTFUniverse.create(test_layout, 4) al = Graph(universe.free_positions()) #Test distance to middle from both sides assert 11 == len(al.a_star((1, 1), (7, 2))) assert 12 == len(al.a_star((2, 1), (7, 2))) assert 14 == len(al.a_star((16, 1), (7, 2))) assert 15 == len(al.a_star((15, 1), (7, 2))) # Test basic assertions assert 0 == len(al.a_star((1, 1), (1, 1))) assert 1 == len(al.a_star((1, 1), (2, 1))) assert 1 == len(al.a_star((2, 1), (1, 1))) # Test distance to middle from both sides assert 11 == len(al.a_star((1, 1), (7, 2))) assert 12 == len(al.a_star((2, 1), (7, 2))) assert 14 == len(al.a_star((16, 1), (7, 2))) assert 15 == len(al.a_star((15, 1), (7, 2)))
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))
class FoodEatingPlayer(AbstractPlayer): def set_initial(self): self.graph = Graph(self.current_uni.reachable([self.initial_pos])) self.next_food = None def goto_pos(self, pos): return self.graph.a_star(self.current_pos, pos)[-1] def get_move(self): # check, if food is still present if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop self.next_food = self.rnd.choice(self.enemy_food) try: next_pos = self.goto_pos(self.next_food) move = diff_pos(self.current_pos, next_pos) return move except NoPathException: return datamodel.stop
class FoodEatingPlayer(AbstractPlayer): def set_initial(self): self.graph = Graph(self.current_uni.reachable([self.initial_pos])) self.next_food = None def goto_pos(self, pos): return self.graph.a_star(self.current_pos, pos)[-1] def get_move(self): # check, if food is still present if (self.next_food is None or self.next_food not in self.enemy_food): if not self.enemy_food: # all food has been eaten? ok. i’ll stop return datamodel.stop self.next_food = self.rnd.choice(self.enemy_food) try: next_pos = self.goto_pos(self.next_food) move = diff_pos(self.current_pos, next_pos) return move except NoPathException: return datamodel.stop
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)