def test_my_length(): # ##### # ##### # ##M## # ##MM# # ##### board = Board.from_height_and_width(5, 5) board._my_head = Coordinate(2, 2) board._board[board.my_head] = MY_HEAD board._board[Coordinate(2, 3)] = MY_BODY board._board[Coordinate(3, 3)] = MY_BODY result = board.my_length assert result == 3
def test_wants_to_move_towards_apple(): # ##### # ##### # A#M## # ##### # ##### board = Board.from_height_and_width(5, 5) board._my_head = Coordinate(2, 2) board._board[Coordinate(0, 2)] = FOOD board._board[board.my_head] = MY_HEAD result = moves_sorted_by_area_rank(board, board.available_moves()) assert result == [(MOVE_DOWN, 24.5), (MOVE_LEFT, 24.5), (MOVE_UP, 24.5), (MOVE_RIGHT, 24.5)]
def test_update_board_with_food(): board = Board.from_height_and_width(height=3, width=3) board_data = MOVE_PAYLOAD board_data["board"]["snakes"] = [] board_data["board"]["food"] = [{"x": 2, "y": 1}] board.update(board_data) assert board.board[Coordinate(0, 0)] == EMPTY assert board.board[Coordinate(0, 1)] == EMPTY assert board.board[Coordinate(0, 2)] == EMPTY assert board.board[Coordinate(1, 0)] == EMPTY assert board.board[Coordinate(1, 1)] == EMPTY assert board.board[Coordinate(1, 2)] == EMPTY assert board.board[Coordinate(2, 0)] == EMPTY assert board.board[Coordinate(2, 1)] == FOOD assert board.board[Coordinate(2, 2)] == EMPTY
def test_area_value_all_empty(): # ##### # ##### # ##M## # ##### # ##### board = Board.from_height_and_width(5, 5) board._my_head = Coordinate(2, 2) board._board[board.my_head] = MY_HEAD result = board.area_rank(MOVE_RIGHT) assert result == 24
def test_opponent_heads(): board = Board.from_height_and_width(3, 3) board._board[Coordinate(0, 1)] = OTHER_SNAKE_HEAD board._board[Coordinate(2, 2)] = OTHER_SNAKE_HEAD board._board[Coordinate(1, 2)] = OTHER_SNAKE_HEAD result = board.opponent_heads assert result == [ Coordinate(0, 1), Coordinate(1, 2), Coordinate(2, 2), ]
def test_food_coordinates(): board = Board.from_height_and_width(3, 3) board._board[Coordinate(0, 1)] = FOOD board._board[Coordinate(2, 2)] = FOOD board._board[Coordinate(1, 2)] = FOOD result = board.food_coordinates assert result == [ Coordinate(0, 1), Coordinate(1, 2), Coordinate(2, 2), ]
def test_init(): height = 2 width = 3 board = Board.from_height_and_width(height=height, width=width) expected = { Coordinate(0, 0): '', Coordinate(0, 1): '', Coordinate(1, 0): '', Coordinate(1, 1): '', Coordinate(2, 0): '', Coordinate(2, 1): '', } assert expected == board.board
def test_moving_left(): board = Board.from_height_and_width(height=3, width=3) board_data = MOVE_PAYLOAD board_data["board"]["snakes"] = [] board_data["board"]["food"] = [] board_data["you"] = { "body": [ { "x": 0, "y": 0 }, { "x": 1, "y": 0 }, ] } board.update(board_data) assert board._my_direction == DIRECTION_LEFT assert board._my_head == Coordinate(0, 0) assert board._board[board._my_head] == MY_HEAD
def test_area_value_above(): # ##F## # SS#SS # ##M## # ##### # ##### board = Board.from_height_and_width(5, 5) board._board[Coordinate(0, 1)] = OTHER_SNAKE_HEAD board._board[Coordinate(1, 1)] = OTHER_SNAKE_HEAD board._board[Coordinate(3, 1)] = OTHER_SNAKE_HEAD board._board[Coordinate(4, 1)] = OTHER_SNAKE_HEAD board._board[Coordinate(2, 0)] = FOOD board._my_head = Coordinate(2, 2) board._board[board.my_head] = MY_HEAD result = board.area_rank(MOVE_UP) assert result == 6.5
def step(self, string): coord = Coordinate.from_str(string) return self._game.put(self, coord)
def test_move_towards_best_value(): # ##A## # SS#SS # #SM## # ##S## # ##### board = Board({ Coordinate(0, 0): EMPTY, Coordinate(0, 1): OTHER_SNAKE_HEAD, Coordinate(0, 2): EMPTY, Coordinate(0, 3): EMPTY, Coordinate(0, 4): EMPTY, Coordinate(1, 0): EMPTY, Coordinate(1, 1): OTHER_SNAKE_HEAD, Coordinate(1, 2): OTHER_SNAKE_HEAD, Coordinate(1, 3): EMPTY, Coordinate(1, 4): EMPTY, Coordinate(2, 0): FOOD, Coordinate(2, 1): EMPTY, Coordinate(2, 2): MY_HEAD, Coordinate(2, 3): OTHER_SNAKE_HEAD, Coordinate(2, 4): EMPTY, Coordinate(3, 0): EMPTY, Coordinate(3, 1): OTHER_SNAKE_HEAD, Coordinate(3, 2): EMPTY, Coordinate(3, 3): EMPTY, Coordinate(3, 4): EMPTY, Coordinate(4, 0): EMPTY, Coordinate(4, 1): OTHER_SNAKE_HEAD, Coordinate(4, 2): EMPTY, Coordinate(4, 3): EMPTY, Coordinate(4, 4): EMPTY, }) board._my_head = Coordinate(2, 2) result = moves_sorted_by_area_rank(board, board.available_moves()) assert result == [(MOVE_RIGHT, 12), (MOVE_UP, 6.5)]
def test_food_that_i_am_closest_with_other_snakes(): # ####F # ##### # ###FM # ###S# # F###F board = Board({ Coordinate(0, 0): EMPTY, Coordinate(0, 1): EMPTY, Coordinate(0, 2): EMPTY, Coordinate(0, 3): EMPTY, Coordinate(0, 4): FOOD, Coordinate(1, 0): EMPTY, Coordinate(1, 1): EMPTY, Coordinate(1, 2): EMPTY, Coordinate(1, 3): EMPTY, Coordinate(1, 4): EMPTY, Coordinate(2, 0): EMPTY, Coordinate(2, 1): EMPTY, Coordinate(2, 2): EMPTY, Coordinate(2, 3): FOOD, Coordinate(2, 4): MY_HEAD, Coordinate(3, 0): EMPTY, Coordinate(3, 1): EMPTY, Coordinate(3, 2): EMPTY, Coordinate(3, 3): OTHER_SNAKE_HEAD, Coordinate(3, 4): EMPTY, Coordinate(4, 0): EMPTY, Coordinate(4, 1): FOOD, Coordinate(4, 2): EMPTY, Coordinate(4, 3): EMPTY, Coordinate(4, 4): FOOD, }) board._my_head = Coordinate(2, 4) result = food_that_i_am_closest(board, board.available_moves()) assert result == MOVE_UP
from forest import Node, ParcialTree from board import Coordinate from anytree import RenderTree, PreOrderIter from PIL import Image a = Coordinate(0, 0) b = Coordinate(0, 1) c = Coordinate(0, 2) d = Coordinate(1, 0) # e = Coordinate(1,1) f = Coordinate(1, 2) g = Coordinate(2, 0) h = Coordinate(2, 1) i = Coordinate(2, 2) a.neighbors = [b, d] b.neighbors = [c, a] c.neighbors = [f, b] d.neighbors = [a, g] #e.neighbors = [b,d,f,h]#Bloqueo f.neighbors = [c, i] g.neighbors = [d, h] h.neighbors = [i, g] i.neighbors = [f, h] initial = a finish = i located = False iteration = 0
def test_update_board_with_snakes(): board = Board.from_height_and_width(height=3, width=3) board_data = MOVE_PAYLOAD board_data["board"]["food"] = [] board_data["board"]["snakes"] = [{ "id": "snake1", "body": [ { "x": 0, "y": 0 }, { "x": 1, "y": 0 }, ] }, { "id": "snake2", "body": [ { "x": 2, "y": 2 }, { "x": 2, "y": 1 }, ] }] board_data["you"]["body"] = [ { "x": 0, "y": 2 }, { "x": 0, "y": 1 }, { "x": 1, "y": 1 }, { "x": 1, "y": 2 }, ] board.update(board_data) assert board.board[Coordinate(0, 0)] == OTHER_SNAKE_HEAD assert board.board[Coordinate(0, 1)] == MY_BODY assert board.board[Coordinate(0, 2)] == MY_HEAD assert board.board[Coordinate(1, 0)] == SNAKE assert board.board[Coordinate(1, 1)] == MY_BODY assert board.board[Coordinate(1, 2)] == MY_BODY assert board.board[Coordinate(2, 0)] == EMPTY assert board.board[Coordinate(2, 1)] == SNAKE assert board.board[Coordinate(2, 2)] == OTHER_SNAKE_HEAD