def find_next_food(snake: Snake, board: BoardState) -> Optional[Tuple[int, Position]]: head = snake.get_head() health = snake.get_health() all_food = board.food if len(all_food) == 0: print("KEIN FOOD") return None best_dist = 99999 best_food = None for food in all_food: food_dist = Distance.manhattan_dist(head, food) if food_dist > health: continue #print("food_dist: ",food_dist, "health", health) if food_dist == health: # print("1: ", food_dist, food) return food_dist, food else: diff = (health - food_dist) #print("diff: ",diff) if (best_dist > diff) and diff > 0: best_dist = diff best_food = food #print("2: ", best_dist, best_food) return best_dist, best_food
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
def need_food(snake: Snake, board: BoardState, grid_map: GridMap, valid: List[Direction]) -> Tuple[bool, Optional[Direction]]: health = snake.get_health() head = snake.get_head() food_around = SoloSurvival.food_all_around_body(snake, board) if health < 15: if food_around: if health == 1: _, food_pos = SoloSurvival.best_food_around_body(snake, board) food_dir = SoloSurvival.direction_to_food(snake, food_pos) return True, food_dir else: dist, food_pos = SoloSurvival.find_next_food(snake, board) #direction, path = Hungry.hunger(snake, board, grid_map, [], valid, SnakeAutomat(snake, False)) #food_dir = SoloSurvival.direction_to_food(snake, food_pos) #print("head: ", head, " food: ", food_pos) _, path_ = AStar.a_star_search(head, food_pos, board, grid_map) _, path = path_[0] #print("path: ",path) #food_dir = SoloSurvival.direction_to_food(snake, path[0]) return True, path #print("hungry health: ", health) dist, food_pos = SoloSurvival.best_food_around_body(snake, board) if food_pos is None: dist, food_pos = SoloSurvival.find_next_food(snake, board) if (health - dist) <= 1: #print("get the perfect food") food_dir = SoloSurvival.direction_to_food(snake, food_pos) return True, food_dir else: #print("anderes food nehmen") dist, food_pos = SoloSurvival.find_next_food(snake, board) if dist <= health: food_dir = SoloSurvival.direction_to_food(snake, food_pos) return True, food_dir else: return False, None