コード例 #1
0
    def best_food_around_body(snake: Snake, board: BoardState) -> Tuple[int, Optional[Position]]:

        length = snake.get_length()
        health = snake.get_health()
        body = snake.get_body()
        foods = [] #Tuple[int, Position]
        position = 0
        any_f = False
        for part in body:
            for direction in Direction:
                if board.is_occupied_by_food(part.advanced(direction)):
                    diff = length - position
                    add = diff, part
                    foods.append(add)
                    any_f = True
            position += 1
        best_dist = 999999
        best_food = None
        if any_f:
            for dist, food in foods:
                if (dist == snake.get_length()) and (health <= 1):
                    return 1, food
                if dist < best_dist and dist <= health:
                    if dist == health:
                        return dist, food
                    best_dist = dist
                    best_food = food
            return best_dist, best_food
        else:
            return 0, None
コード例 #2
0
    def food_all_around_body(snake: Snake, board: BoardState) -> bool:

        body = snake.get_body()
        food_next_to_body = 0
        for part in body:
            thispart = False
            for direction in Direction:
                if board.is_occupied_by_food(part.advanced(direction)):
                    thispart = True
            if thispart:
                food_next_to_body += 1
        if food_next_to_body == len(body):
            return True
        else:
            return False
コード例 #3
0
    def next_step(snake: Snake, board: BoardState, grid_map: GridMap) -> Direction:

        head = snake.get_head()
        #tail = snake.get_tail()
        middle = Position(board.height // 2, board.width // 2)
        valid = ValidActions.get_valid_actions(board, snake.possible_actions(), [snake], snake, grid_map, False)
        #for direction in Direction:
        #    if not board.is_out_of_bounds(head.advanced(direction)):
        #        if not (head.advanced(direction) in snake.get_body()):
        #            valid.append(direction)
        #if snake.get_length() > 10:
        #    for action in valid:
        #        _, path = AStar.a_star_search(head.advanced(action), tail, board, grid_map)
        #       print(len(path))
        #        end, pat = path[len(path)-1]
        #        if not end == tail:
        #            valid.remove(action)

        if middle in snake.get_body():
            need, next_direction = SoloSurvival.need_food(snake, board, grid_map, valid)
            if need:
                if next_direction in valid:
                    return next_direction
                else:
                    #print("not valid")
                    return np.random.choice(valid)
            else:
                next_direction = SoloSurvival.tail_gate(snake, board, grid_map)
                if next_direction in valid:
                    return next_direction
                else:
                    return np.random.choice(valid)

        else:
            if SoloSurvival.food_around_head(head, board):
                _, path = AStar.a_star_search(head, middle, board, grid_map)
                _, next_direction = path[0]
            else:
                _, path = AStar.a_star_search(head, middle, board, grid_map)
                _, next_direction = path[0]
        if next_direction in valid:
            return next_direction
        else:
            return np.random.choice(valid)
コード例 #4
0
    def tail_gate(snake: Snake, board: BoardState, grid_map : GridMap) -> Direction:

        head = snake.get_head()
        tail = snake.get_tail()
        body = snake.get_body()
        distance = Distance.manhattan_dist(head, tail)
        if distance == 1:
            for direction in Direction:
                if head.advanced(direction) == tail:
                    return direction
        else:
            dist = 9999
            next_direction = None
            for direction in Direction:
                advanced_head = head.advanced(direction)
                d = Distance.manhattan_dist(advanced_head, tail)
                if advanced_head not in body:
                    if d < dist:
                        dist = d
                        next_direction = direction

            return next_direction