
This is my best program, consisting of three parts: The first program is a very good algorithm that tracks the tail's position and navigates to the apples quite optimally. The second program simply follows the edge of the field. If the tail is in the way, we climb up to x = 1 and then descend. If we detect an apple on the way up, we simply climb up to it. The third program, without tracking apples, repeats the movements, covering the entire field. Это моя лучшая программа из 3 частей: Первая программа очень хороший алгоритм отслеживающий положение хвоста и довольно оптимально идущий к яблокам Вторая программа просто едем по краю поля, Если на пути хвост, то поднимаемся до x = 1 и потом опускаемся Если фиксируем, что при подъеме будет яблоко, то просто поднимаемся к нему Третья программа без отслеживания яблок повторяем движения покрывая все поле Ссылка на мой youtube: https://www.youtube.com/@Andrey_Besjashiy/videos Ссылка на видео с объяснением кода: https://youtu.be/K3Tu9fWnKYw Programm clear() # Глобальные переменные для отслеживания хвоста tail = [] # список позиций хвоста: [(x0,y0), (x1,y1), ...], голова НЕ включена tail_length = 0 world_size = get_world_size() middle_tail = [] # список позиций хвоста [y и когда достигает] def simple_move_like_snake(): xnew = get_pos_x() ynew = get_pos_y() WorldSize = get_world_size() moved = False if xnew == 0 and ynew > 0: if not move(South): return False moved = True elif ynew % 2 == 0: if xnew == WorldSize - 1: if not move(North): return False moved = True else: if not move(East): return False moved = True elif xnew == 1 and ynew != WorldSize - 1: if not move(North): return False moved = True else: if not move(West): return False moved = True return moved # Двигаемся змейкой def move_like_snake(): xnew = get_pos_x() ynew = get_pos_y() WorldSize = get_world_size() moved = False if xnew == 0 and ynew > 0: if not move(South): return False add_to_tail(xnew, ynew - 1) moved = True elif ynew % 2 == 0: if xnew == WorldSize - 1: if not move(North): return False add_to_tail(xnew, ynew + 1) moved = True else: if not move(East): return False add_to_tail(xnew + 1, ynew) moved = True elif xnew == 1 and ynew != WorldSize - 1: if not move(North): return False add_to_tail(xnew, ynew + 1) moved = True else: if not move(West): return False add_to_tail(xnew - 1, ynew) moved = True return moved def move_to_YX(next_x, next_y): x = get_pos_x() y = get_pos_y() any_move = False while next_y > y: IfMove = move(North) if not IfMove: break add_to_tail(x, y + 1) y = get_pos_y() any_move = True while next_x < x: IfMove = move(West) if not IfMove: break x = get_pos_x() add_to_tail(x - 1, y) any_move = True if not any_move: move_like_snake() def reset_tail(): global tail global tail_length tail = [] tail_length = 0 def add_to_tail(x, y): #"""Добавить старую позицию головы в хвост""" global tail global tail_length tail.insert(0, (x, y)) # новая голова была здесь if len(tail) > tail_length: tail.pop() # удаляем кончик хвоста (он освобождается) def movement_to_point(next_x, next_y): x = get_pos_x() y = get_pos_y() return abs(next_x - x) + abs(next_y - y) def not_in_tail(next_x, next_y): global tail road = 0 y= get_pos_y() if y > next_y: next_y = y for i in range(next_y): if (next_x, y) in tail: x = get_pos_x() y = get_pos_y() road = abs(x - next_x) + abs(y - next_y) return road def move_not_tail(next_x, next_y): x = get_pos_x() y = get_pos_y() global tail global tail_length i = 0 for (x_tail, y_tail) in tail: i += 1 if x_tail != 0: if y_tail > y and y_tail <= next_y: count_movement = tail_length - i - movement_to_point(x_tail, y_tail) + 3 if count_movement > 0: return count_movement return 0 def move_to_apple(next_x, next_y): x = get_pos_x() y = get_pos_y() moved = False if x == 0: IfMove = move(East) if not IfMove: return False add_to_tail(x + 1, y) moved = True x = get_pos_x() if x > next_x and y % 2 == 0 and x != 0: IfMove = move(North) if not IfMove: return False add_to_tail(x, y + 1) moved = True elif x < next_x and y % 2 == 1 and x != 0: IfMove = move(North) if not IfMove: return False add_to_tail(x, y + 1) moved = True y = get_pos_y() while x != next_x: if move_like_snake(): moved = True else: break x = get_pos_x() while y < next_y: IfMove = move(North) if not IfMove: return False add_to_tail(x, y + 1) y = get_pos_y() moved = True return moved def move_not_tail_x0(): x = get_pos_x() y = get_pos_y() global tail global tail_length i = 0 for (x_tail, y_tail) in tail: i += 1 if x_tail == 0 and y_tail >= y: count_movement = tail_length - i - movement_to_point(x_tail, y_tail) + 3 if count_movement > 0: return count_movement return 0 def move_to_tail_x0(): x = get_pos_x() y = get_pos_y() moved = False if y % 2 == 0 and x != 0: IfMove = move(North) if not IfMove: return False add_to_tail(x, y + 1) return True else: while x != 0: IfMove = move(West) if not IfMove: break moved = True add_to_tail(x - 1, y) x = get_pos_x() return moved def check_apple(next_x, next_y): x = get_pos_x() y = get_pos_y() global tail global tail_length if x == next_x and y == next_y: next_x, next_y = measure() tail_length += 1 return next_x, next_y def new_snake(target): global tail global tail_length WorldSize = get_world_size() next_x, next_y = measure() tail_length += 1 move_like_snake() count_movement = 0 while tail_length < target: x = get_pos_x() y = get_pos_y() while count_movement > 0: if x == next_x and y == next_y: next_x, next_y = measure() tail_length += 1 move_like_snake() count_movement -= 1 x = get_pos_x() y = get_pos_y() if x == next_x and y == next_y: if tail_length < target - 1: next_x, next_y = measure() tail_length += 1 else: break if tail_length > target: if not simple_move_like_snake(): break elif y == WorldSize -1: if not move_like_snake(): break elif next_x == 0: count_movement = not_in_tail(next_x, next_y) if count_movement == 0: move_to_YX(next_x, next_y) elif y > next_y and x != 0: count_movement = move_not_tail_x0() if count_movement == 0: if not move_to_tail_x0(): x = get_pos_x() y = get_pos_y() if x == next_x and y == next_y: next_x, next_y = measure() tail_length += 1 move_like_snake() elif not move_like_snake(): break elif x == 0: if y < next_y: count_movement = move_not_tail(next_x, next_y) if count_movement == 0: if move(East): add_to_tail(x + 1, y) else: move_like_snake() if not move_like_snake(): break else: count_movement = move_not_tail(next_x, next_y) if count_movement == 0: if not move_to_apple(next_x, next_y): x = get_pos_x() y = get_pos_y() if x == next_x and y == next_y: next_x, next_y = measure() tail_length += 1 move_like_snake() else: if not move_like_snake(): break return next_x, next_y def late_snake(): while True: if not simple_move_like_snake(): break def midle_snake(target, next_x, next_y): global tail_length WorldSize = get_world_size() while tail_length < target: x = get_pos_x() y = get_pos_y() next_x, next_y = check_apple(next_x, next_y) if x == WorldSize - 1: if y == WorldSize - 1: simple_move_like_snake() elif (y == next_y - 2 or y == next_y - 1) and y < WorldSize - 2: new_len = x - next_x move(North) for i in range(new_len): simple_move_like_snake() next_x, next_y = check_apple(next_x, next_y) move(North) elif move(North): next_x, next_y = check_apple(next_x, next_y) move(North) else: next_x, next_y = check_apple(next_x, next_y) simple_move_like_snake() elif not simple_move_like_snake(): break return next_x, next_y WorldSize = get_world_size() while num_items(Items.Bone) > 33488928: change_hat(Hats.Dinosaur_Hat) reset_tail() next_x, next_y = new_snake(WorldSize*3) next_x, next_y = midle_snake(WorldSize*WorldSize / 3, next_x, next_y) late_snake() change_hat(Hats.Straw_Hat)
2026-02-19 10:00:44 发布在
编程农场
说点好听的...
收藏
0
0
