Mastering Cactus Automation: 2D Bubble Sort
Cactus farming is unique because sorting them by size can yield better rewards. This guide explains how to apply the classic Bubble Sort algorithm to your farm grid.
The Strategy
Unlike other crops, simply harvesting cacti isn't enough. We need to measure them and swap their positions until they are perfectly ordered.
- Measure: Compare current cactus size with neighbors.
- Swap: If current is larger than neighbor, swap them.
- Repeat: Repeat across the grid until no swaps are needed.
Step-by-Step Code Analysis
Step 1 Smart Navigation & Direction
Before sorting, we need helper functions to return the drone to origin (`back`) and handle direction reversal (`reverse`).
def back(x=0,y=0):
while x!=get_pos_x() or y!=get_pos_y():
if(get_pos_x()<x):
move(East)
else:
move(West)
if(get_pos_y()<y):
move(North)
else:
move(South)
def reverse(dir):
if(dir == West): return East
if(dir == East): return West
if(dir == North): return South
if(dir == South): return North Step 2 Core Sorting Logic
This is the brain of the code. It checks not just horizontally but also vertically. If the order is wrong, it calls `swap()`.
# ... inside the loop
current=measure()
neighbor=measure(dir)
down=measure(South)
up=measure(North)
# Swap Horizontal (水平交换)
if(x!=size-1 and neighbor!=None and dir==East and current>neighbor):
swap(East)
sorted=False # Mark as not fully sorted yet
# Swap Vertical (垂直交换)
if(y!=0 and down != None and current<down):
swap(South)
vsorted=False Complete Source Code
Pythondef back(x=0,y=0):
while x!=get_pos_x() or y!=get_pos_y():
if(get_pos_x()<x):
move(East)
else:
move(West)
if(get_pos_y()<y):
move(North)
else:
move(South)
def plantCactus():
if get_ground_type()==Grounds.Grassland:
till()
plant(Entities.Cactus)
def reverse(dir):
if(dir == West): return East
if(dir == East): return West
if(dir == North): return South
if(dir == South): return North
def cactusProject(size=3):
while True:
dir=East
vdir=North
sorted=False
vsorted=False
# 1. Planting Phase
for i in range(size):
for j in range(size):
plantCactus()
if(j!=size-1):
move(dir)
if(i!=size-1):
move(vdir)
dir=reverse(dir)
vdir=reverse(vdir)
# 2. Sorting Phase
while not sorted or not vsorted:
sorted=True
vsorted=True
for i in range(size):
for j in range(size):
x=get_pos_x()
y=get_pos_y()
current=measure()
neighbor=measure(dir)
down=measure(South)
up=measure(North)
# Logic to swap East/West
if(x!=size-1 and neighbor!=None and dir==East and current>neighbor):
swap(East)
sorted=False
if(x!=0 and neighbor!=None and dir==West and current<neighbor):
swap(West)
sorted=False
# Logic to swap North/South
if(y!=0 and down != None and current<down):
swap(South)
vsorted=False
if(y!=size-1 and up != None and current>up):
swap(North)
vsorted=False
if(j!=size-1):
move(dir)
if(i!=size-1):
move(vdir)
dir=reverse(dir)
# 3. Harvesting Phase (Once sorted)
back()
harvest()
clear()
# Change '6' to your grid size
cactusProject(6) Optimization Tips
- • Ensure the `size` parameter matches your actual land size to avoid drone crashes.
- • This algorithm might be slow on large grids (e.g., 10x10) due to the nature of Bubble Sort.