[编程农场][The Farmer Was Replaced]——Gold/Treasure

166 阅读6分钟

写在前面

建议大家 无人机数量 ≥ 农场边长。注意以下两个科技树,使其匹配:

农场边长:

描述

无人机数量:

描述

Treasure

9个独立主无人机做dfs,遇到岔路spawn,只有主无人机可以spawn。

clear()
set_execution_speed(8)
set_world_size(32)
n=get_world_size()
fwd = [ North,East,West,South]
dir = [(0,1),(1,0),(-1,0),(0,-1)]
arr_2 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
set_d = []
def init_arr():
    for i in range(n):
        for j in range(n):
            arr_2[i][j]=0
def cal_len(nx, ny):
    return abs(nx-get_pos_x())+abs(ny-get_pos_y())
def find_dir(nx, ny):
    x=nx-get_pos_x()
    y=ny-get_pos_y()
    if abs(x)>=abs(y):
        if x>0:
            return 1
        else:
            return 2
    else:
        if y>=0:
            return 0
        else:
            return 3
def wait_hedge(b):
    if b==True:
        while get_entity_type()!=Entities.Hedge:
            pass
    else:
        while get_entity_type()==Entities.Hedge:
            pass
def move_to(x,y):
    x0=get_pos_x()
    y0=get_pos_y()
    for i in range(abs(x-x0)):
        if x-x0 >0:
            move(East)
        else:
            move(West)            
    for i in range(abs(y-y0)):
        if y-y0 >0:
            move(North)
        else:
            move(South)
def dfs(nx,ny,ma):
    if get_entity_type()==Entities.Grass:
        return
    if get_entity_type()==Entities.Treasure:
        use_item(Items.Weird_Substance,1024)
        return
    t = measure()
    if t==None:
        return
    else:
        if nx!=t[0] or ny!=t[1]:
            return
    x=get_pos_x()
    y=get_pos_y()
    arr_2[x][y]=1
    cnt=0
    for i in range(4):
        dx,dy=dir[i]
        if x+dx>=32 or y+dy>=32 or x+dx<0 or y+dy<0:
            continue
        if can_move(fwd[i]) and arr_2[x+dx][y+dy]==0:
            cnt+=1
    for j in range(4):
        i = (find_dir(nx,ny)+j)%4
        dx,dy=dir[i]
        if x+dx>=32 or y+dy>=32 or x+dx<0 or y+dy<0:
            continue
            
        if arr_2[x+dx][y+dy]==1:
            if i==1 or i==2:
                i = (find_dir(get_pos_x(),ny)+j)%4
                dx,dy=dir[i]
            else:
                i = (find_dir(nx,get_pos_y())+j)%4
                dx,dy=dir[i]
        if x+dx>=32 or y+dy>=32 or x+dx<0 or y+dy<0:
            continue
        if can_move(fwd[i]) and arr_2[x+dx][y+dy]==0:   
              if cnt>1 and cal_len(x,y)<=20 and ma ==1:
                  cnt-=1
                  move(fwd[i])
                  f=spawn_drone(folk_drone)
                  move(fwd[3-i]) 
                  set_d.append(f)
                  continue
              move(fwd[i])
              dfs(nx,ny,ma)
              if get_entity_type()!=Entities.Hedge:
                  return 
              move(fwd[3-i]) 
def folk_drone():
    t=measure()
    if t==None:
        nx,ny=0,0
    else:
        nx,ny=t[0],t[1]
    dfs(nx,ny,0)
def make_drone():
    fx = get_pos_x()
    fy = get_pos_y()
    while True:
        wait_hedge(True)
        t = measure()
        if t==None:
            nx,ny=0,0
        else:
            nx,ny=t[0],t[1]
        init_arr()
        if get_entity_type()==Entities.Treasure:
            use_item(Items.Weird_Substance,1024)
        dfs(nx,ny,1)
        move_to(fx,fy)
def make_drone_m():
    fx = get_pos_x()
    fy = get_pos_y()
    while True:
        init_arr()
        plant(Entities.Bush)
        use_item(Items.Weird_Substance,1024)
        if get_entity_type()==Entities.Treasure:
            use_item(Items.Weird_Substance,1024)
        t = measure()
        if t==None:
            nx,ny=0,0
        else:
            nx,ny=t[0],t[1]
        dfs(nx,ny,1)
        move_to(fx,fy)
for i in range(8):
    x = (i % 3) * n/3 + n/6-1
    y = (i //3) * n/3 + n/6-1
    move_to(x,y)
    spawn_drone(make_drone)
move_to(26,26)
make_drone_m()