编程农场 - 代码示例
冒泡排序 — 自动收获仙人掌
完整代码:
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 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 do(x,worldsize=0):
if worldsize == 0:
worldsize=get_world_size()
dir=East
for i in range(worldsize):
for j in range(worldsize-1):
x()
move(dir)
x()
move(North)
if(dir==East):
dir=West
else:
dir=East
def cactusProject(size=3):
while True:
dir=East
vdir=North
sorted=False
vsorted=False
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)
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)
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
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)
vdir=reverse(vdir)
back()
harvest()
clear()
cactusProject(6)