def _is_legal_move(self, i, j): board_size = self.board.board_size i = cyclic(i, board_size) j = cyclic(j, board_size) return \ 0 <= i < board_size and 0 <= j < board_size \ and (i, j) not in self.board.obstacles and (i, j) not in self.board.snake
def legal(self, action: Direction, state): row, col = state.board.snake[0] if action == Direction.UP: row -= 1 elif action == Direction.DOWN: row += 1 elif action == Direction.LEFT: col -= 1 elif action == Direction.RIGHT: col += 1 row = cyclic(row, state.board.board_size) col = cyclic(col, state.board.board_size) # if row < 0 or row >= state.board.board_size or col < 0 or col >= state.board.board_size: # return False tail_y, tail_x = state.board.snake[~0] tail_pos = self.maze.get_path_number(tail_x, tail_y) normalized_snake = np.asarray( list(map(lambda part: self.maze.get_path_number(part[1], part[0]), state.board.snake))) normalized_snake = (normalized_snake - tail_pos + self.maze.arena_size) % self.maze.arena_size pos = self.maze.get_path_number(col, row) normalized_pos = (pos - tail_pos + self.maze.arena_size) % self.maze.arena_size if normalized_pos < normalized_snake[0]: return False return True
def get_current_state_(self, board): """ DIDNT USE... Check the 3 blocks around the head of the snake and relative positions of fruit and tail to head :param board: :return: """ # 3 places around head snake = board.snake snake_x = snake[0][0] snake_y = snake[0][1] fruit_position = board.fruit_location board_size = board.board_size fruit_x = fruit_position[0] fruit_y = fruit_position[1] direction = board.next_move left = right = up = 0 # print(cyclic(5, board_size)) if direction == Direction.LEFT: left = (cyclic(snake_x + 1, board_size), snake_y) up = (snake_x, cyclic(snake_y - 1, board_size)) right = (cyclic(snake_x - 1, board_size), snake_y) elif direction == Direction.RIGHT: left = (cyclic(snake_x - 1, board_size), snake_y) up = (snake_x, cyclic(snake_y + 1, board_size)) right = (cyclic(snake_x + 1, board_size), snake_y) elif direction == Direction.UP: left = (snake_x, cyclic(snake_y - 1, board_size)) up = (cyclic(snake_x - 1, board_size), snake_y) right = (snake_x, cyclic(snake_y + 1, board_size)) elif direction == Direction.DOWN: left = (snake_x, cyclic(snake_y + 1, board_size)) up = (cyclic(snake_x + 1, board_size), snake_y) right = (snake_x, cyclic(snake_y - 1, board_size)) if left in snake or left in board.obstacles: state_name = '1' elif left == fruit_position: state_name = '2' else: state_name = '0' if up in snake or up in board.obstacles: state_name += '1' elif up == fruit_position: state_name += '2' else: state_name += '0' if right in snake or right in board.obstacles: state_name += '1' elif right == fruit_position: state_name += '2' else: state_name += '0' state_name += str(abs(fruit_x - snake_x)) + str(abs(fruit_y - snake_y)) state_name += str(abs(snake[len(snake) - 1][0] - snake_x)) + str( abs(snake[len(snake) - 1][1] - snake_y)) return state_name
def get_current_state(self, board): snake = board.snake snake_x = snake[0][0] snake_y = snake[0][1] fruit_position = board.fruit_location board_size = board.board_size fruit_x = fruit_position[0] fruit_y = fruit_position[1] left = 1 if (snake_x, cyclic(snake_y - 1, board_size)) in snake or \ (snake_x, cyclic(snake_y - 1, board_size)) in board.obstacles else 0 right = 1 if (snake_x, cyclic(snake_y + 1, board_size)) in snake or \ (snake_x, cyclic(snake_y + 1, board_size)) in board.obstacles else 0 up = 1 if (cyclic(snake_x - 1, board_size), snake_y) in snake or \ (cyclic(snake_x - 1, board_size), snake_y) in board.obstacles else 0 down = 1 if (cyclic(snake_x + 1, board_size), snake_y) in snake or \ (cyclic(snake_x + 1, board_size), snake_y) in board.obstacles else 0 left = 2 if (snake_x, cyclic(snake_y - 1, board_size)) == fruit_position else left right = 2 if (snake_x, cyclic(snake_y + 1, board_size)) == fruit_position else right up = 2 if (cyclic(snake_x - 1, board_size), snake_y) == fruit_position else up down = 2 if (cyclic(snake_x + 1, board_size), snake_y) == fruit_position else down state_name = str(left) + str(up) + str(right) + str(down) fruit_relative_x = fruit_x - snake_x fruit_relative_y = fruit_y - snake_y if fruit_relative_x < 0 and fruit_relative_y < 0: state_name += '10000000' elif fruit_relative_x < 0 and fruit_relative_y == 0: state_name += '01000000' elif fruit_relative_x < 0 and fruit_relative_y > 0: state_name += '00100000' elif fruit_relative_x == 0 and fruit_relative_y > 0: state_name += '00010000' elif fruit_relative_x > 0 and fruit_relative_y > 0: state_name += '00001000' elif fruit_relative_x > 0 and fruit_relative_y == 0: state_name += '00000100' elif fruit_relative_x > 0 and fruit_relative_y < 0: state_name += '00000010' elif fruit_relative_x == 0 and fruit_relative_y < 0: state_name += '00000001' return state_name