Exemplo n.º 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
Exemplo n.º 2
0
    def get_valid_actions(board: BoardState, possible_actions: List[Direction],
                          snakes: List[Snake], my_snake: Snake,
                          grid_map: GridMap[Occupant],
                          avoid_food: bool) -> List[Direction]:

        my_head = my_snake.get_head()
        snake_tails = []
        val_actions = []
        forbidden_fields = []

        for snake in snakes:
            if snake.snake_id != my_snake.snake_id:
                for direc in snake.possible_actions():
                    enemy_head = snake.get_head()
                    forbidden_fields.append(enemy_head.advanced(direc))
            if snake.health == 100:
                continue
            snake_tails.append(snake.get_tail())

        for direction in possible_actions:
            next_position = my_head.advanced(direction)

            # avoid eating
            if my_snake.health > Params_ValidActions.FOOD_BOUNDARY and avoid_food and my_snake.get_length(
            ) > 5:
                if grid_map.get_value_at_position(
                        next_position) is Occupant.Food:
                    continue

            # outofbounds
            if board.is_out_of_bounds(next_position):
                continue

            # body crash -> ganze Gegner Schlange minus letzten Teil
            if grid_map.get_value_at_position(
                    next_position
            ) is Occupant.Snake and next_position not in snake_tails:
                continue

            # if next_position in forbidden_fields:
            #    continue

            val_actions.append(direction)

        if not val_actions:
            for direction in possible_actions:
                next_position = my_head.advanced(direction)
                # eat if its the only possible valid action
                if grid_map.get_value_at_position(
                        next_position) is Occupant.Food:
                    val_actions.append(direction)

        return val_actions