编程农场仙人掌代码 — 先排序,再一次收 n²

按 1.0+ 重写。仙人掌大小 0–9。邻居都长成且有序时,收一棵会连锁收割整片有序方块,产量 n²。

有序定义(wiki)

  • 种在土上。种植消耗南瓜。
  • `measure()` → 0…9。`measure(East)` 读邻居,不用移动。
  • 有序:北/东邻居(已长成)都 ≥ 自己,南/西都 ≤ 自己。
  • 未排好呈棕色,排好变绿。连锁只穿过已长成且有序的邻居 → 产量 `n**2`。
  • `swap(direction)` 与邻居交换。

教学脚本:种 → 等长成 → 向东北冒泡 → 收

冒泡好懂,不是最快。等全部能收再交换——对未长成的格子 swap 浪费 ticks。

python
def go_to(x, y):
	while get_pos_x() != x:
		if get_pos_x() < x:
			move(East)
		else:
			move(West)
	while get_pos_y() != y:
		if get_pos_y() < y:
			move(North)
		else:
			move(South)

def plant_all():
	size = get_world_size()
	for y in range(size):
		for x in range(size):
			go_to(x, y)
			if get_ground_type() != Grounds.Soil:
				till()
			plant(Entities.Cactus)

def wait_until_grown():
	size = get_world_size()
	ready = False
	while not ready:
		ready = True
		for y in range(size):
			for x in range(size):
				go_to(x, y)
				if get_entity_type() != Entities.Cactus or not can_harvest():
					ready = False

def bubble_sort_once():
	# Push larger sizes toward North/East
	size = get_world_size()
	swapped = False
	for y in range(size):
		for x in range(size):
			go_to(x, y)
			here = measure()
			if x < size - 1:
				east = measure(East)
				if east != None and here > east:
					swap(East)
					swapped = True
					here = measure()
			if y < size - 1:
				north = measure(North)
				if north != None and here > north:
					swap(North)
					swapped = True
	return swapped

clear()
while True:
	plant_all()
	wait_until_grown()
	while bubble_sort_once():
		pass
	go_to(0, 0)
	harvest()

多无人机 / 速度说明

  • 32×32 冒泡很慢。更快排序见社区 GitHub——本页保持可读。
  • 无人机不共享内存。并行排序要划清格子,否则同一条边会被换两次。
  • 排行榜仙人掌目标 ≈ 32 次满田完美 n²(n=1024)。