def test_avoid_pathing_to_long_snake_head(self): test_data = default_data() my_snake = { "id": "me", "head": (2, 3), "body": [(2, 3), (2, 4), (1, 4)], "health": 100 } merge_dict(test_data, { "board": { "width": 5, "height": 5, "snakes": [ my_snake, { "id": "them", "head": (0, 3), "body": [(0, 3), (0, 2), (1, 2), (1, 1), (1, 0)], "health": 100 } ] }, "you": my_snake }) snake = Snake(test_data) """ | |ø|ø| | | |^| |v| | | |■|■| | | | | |■| | | | | |■| | | | """ self.assertNotEqual(snake.next_move(), "left")
def test_take_safer_path(self): test_data = default_data() my_snake = { "id": "me", "head": (3, 3), "body": [(3, 3), (3, 2), (3, 1), (2, 1)], "health": 100 } merge_dict(test_data, { "board": { "width": 5, "height": 5, "snakes": [ my_snake, { "id": "them", "head": (2, 4), "body": [(2, 4), (1, 4), (0, 4), (0, 3), (0, 2), (0, 1), (0, 0)], "health": 100 } ] }, "you": my_snake }) snake = Snake(test_data) """ |■|■|>| | | |■| | |^| | |■| | |ø| | |■| |ø|ø| | |■| | | | | """ self.assertEqual(snake.next_move(), "right")
def test_safe_neighbor(self): board = Board(default_data()) start = (0, 0) goal = (1, 0) """ | | | | | | | | |S|G| | """ path = a_star(board, start, goal) self.assertEqual(path, [start, goal])
def test_nearest_food(self): test_data = default_data() test_data["board"]["food"] = [(0, 1), (1, 2), (2, 2)] board = Board(test_data) nearest = board.nearest_food((1, 0)) """ | |F|F| |F| | | | |X| | """ self.assertEqual(nearest, (0, 1))
def test_token(self): test_data = default_data() test_data["board"]["food"] = [(0, 0)] test_data["board"]["hazards"] = [(0, 1)] test_data["board"]["snakes"] = [{ "id": "them", "health": 100, "body": [(1, 0)] }] board = Board(test_data) self.assertEqual(TokenType.FOOD, board.token((0, 0))) self.assertEqual(TokenType.HAZARD, board.token((0, 1))) self.assertEqual(TokenType.SNAKE, board.token((1, 0))) self.assertEqual(TokenType.EMPTY, board.token((1, 1)))
def test_head_to_head_worse_than_hazards(self): test_data = default_data() my_snake = { "id": "me", "head": (4, 2), "body": [(4, 2), (3, 2), (3, 1), (2, 1), (2, 2)], "health": 100 } merge_dict(test_data, { "board": { "width": 7, "height": 7, "hazards": [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)], "snakes": [ my_snake, { "id": "them", "head": (4, 4), "body": [(4, 4), (3, 4), (3, 5), (4, 5), (4, 6), (3, 6)], "health": 100 } ] }, "you": my_snake }) snake = Snake(test_data) """ |■| | |■|■| | | |■| | |■|■| | | |■| | |■|>| | | |■| | | | | | | |■| |ø|ø|>| | | |■| |ø|ø| | | | |■| | | | | | | """ self.assertEqual(snake.next_move(), "down")
def test_safe_tail(self): test_data = default_data() merge_dict( test_data, { "board": { "width": 2, "height": 2, "snakes": [{ "id": "me", "health": 99, "body": [(1, 1), (1, 0), (0, 0), (0, 1)] }] } }) board = Board(test_data) """ |o|<| |o|o| """ self.assertEqual([(0, 1)], board.safe_neighbors((1, 1)))
def test_navigate_around_obstacle(self): test_data = default_data() merge_dict( test_data, { "board": { "snakes": [{ "id": "them", "health": 100, "body": [(1, 0), (1, 1)] }] } }) board = Board(test_data) start = (0, 0) goal = (2, 0) """ | | | | | |█| | |S|█|G| """ path = a_star(board, start, goal) self.assertEqual(path, [start, (0, 1), (0, 2), (1, 2), (2, 2), (2, 1), goal])
def test_avoid_long_snake_bodies(self): test_data = default_data() my_snake = { "id": "me", "health": 100, "head": (1, 0), "body": [(1, 0), (1, 1), (2, 1)] } merge_dict(test_data, { "board": { "width": 5, "height": 5, "food": [(0, 0)], "snakes": [ my_snake, { "id": "them", "head": (3, 2), "body": [(3, 2), (3, 1), (3, 0), (4, 0), (4, 1)], "health": 100 } ] }, "you": my_snake }) snake = Snake(test_data) """ | | | | | | | | | | | | | | | |^| | | |ø|ø|■|■| |o|v| |■|■| """ self.assertEqual(snake.next_move(), "left")