下载客户端

我最好的贪吃蛇成绩 19分 32*32场地

2026-02-19 10:00:44
发布在编程农场
转载

AI智能总结导读

作者分享了自己在32*32场地取得19分的贪吃蛇最优程序,该程序由三部分算法构成:可追踪蛇尾并最优寻路到苹果的算法、沿场地边缘移动的算法、无苹果追踪可覆盖全场地的循环移动算法,还附上了代码及YouTube讲解视频链接。

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)

评论

共0条评论
face
inputImg
最新更新

Recycling Achievement

轻松获取重复使用同一迷宫300次成就的提示与方法 获取【Recycling】成就的方法: 要获得【Recycling】成就,你需要重复使用同一个迷宫300次。 …

2026-04-07 07:000赞 · 0评论

使用嵌套函数解决迷宫问题

一个用于解决迷宫的极简嵌套函数方案。 其工作原理: 一个用于解决迷宫的极简嵌套函数方案。 定义执行分支(起点): 若获取实体类型()等于实体.宝藏: 收获() …

2026-02-20 16:000赞 · 0评论

俄语翻译

很遗憾,这款游戏没有被翻译成所有语言,因为像这种实用的、教授编程的游戏应该面向所有人,包括那些英语掌握不够熟练的人。有一种观点认为,如果从事编程工作,就应该懂英…

2026-02-20 16:000赞 · 0评论

解锁成本

自动化 farming 的终极挑战是尽可能快速地自动完成游戏流程。以下是游戏自动化相关的解锁内容概述: 解锁树 本指南包含游戏 1.0 版本前的解锁成本信息。

2026-02-20 16:000赞 · 0评论

《仙人掌农场》(2025)

It's the cactus farm, with explanations inside the code as comments. You can jus…

2026-02-20 10:000赞 · 0评论

编程初学者的代码

*DUE TO STILL GOING THROUGH THE GAME, THIS IS A WIP. * I have never created, lea…

2026-02-20 07:000赞 · 0评论

成就解锁器.exe

# 奖杯自动化 解锁(成就) 循环导入和栈溢出 此成就需要2个脚本:f0和f1 循环导入f0: import f1 f1: import f0

2026-02-20 07:000赞 · 0评论

解开迷宫

The maze levels are very different than the remainder of the game. It's possible…

2026-02-20 01:000赞 · 0评论

大多数成就代码

Title is self explaining I want to say something First of all im not a native sp…

2026-02-19 22:000赞 · 0评论

《编程农场》全成就指南

这是一篇关于《编程农场》全成就达成思路的指南。 请注意:本篇内容并不包含实现代码,而只是成就达成思路的提示。 介绍与提示 首先需要注意的是:本篇内容并不包含实现…

2026-02-19 22:000赞 · 0评论
暂无更多