《仙人掌农场》(2025)

0 点赞
编程农场
转载

It's the cactus farm, with explanations inside the code as comments. You can just copy and paste it in a new file. So, spoilers...I guess. Assorted sorted cacti code: W = get_world_size() WATER_THRESHOLD = 0.75 # ---------- Movement helpers ---------- def go_home(): # Move to (0,0) without wiping the field (do NOT call clear() here). x = get_pos_x() y = get_pos_y() k = 0 while k < x: move(West) k += 1 k = 0 while k < y: move(South) k += 1 def serpentine_row_pass_east_west(): # Position assumed at (0,0). Traverse row by row in serpentine pattern. i = 0 eastward = True while i < W: steps = W - 1 s = 0 while s < steps: if eastward: move(East) else: move(West) s += 1 if i < W - 1: move(North) eastward = not eastward i += 1 def serpentine_row_pass_west_east(): # Same as above but starting by going West first. i = 0 eastward = False while i < W: steps = W - 1 s = 0 while s < steps: if eastward: move(East) else: move(West) s += 1 if i < W - 1: move(North) eastward = not eastward i += 1 # ---------- Tile helpers ---------- def ensure_soil_and_cactus_here(): if get_ground_type() != Grounds.Soil: till() ent = get_entity_type() if ent == None: plant(Entities.Cactus) else: if ent != Entities.Cactus: if can_harvest(): harvest() plant(Entities.Cactus) def maybe_water_after_plant(): if get_water() < WATER_THRESHOLD: use_item(Items.Water) # ---------- Field maintenance ---------- def fill_field_with_cactus(): # One sweep that ensures every tile is cactus on soil (no harvesting mature cacti here). go_home() i = 0 while i < W: j = 0 while j < W: ensure_soil_and_cactus_here() j += 1 if j < W: move(East) if i < W - 1: move(North) i += 1 def all_mature_cacti(): go_home() i = 0 all_ok = True while i < W: j = 0 while j < W: ent = get_entity_type() if ent != Entities.Cactus: all_ok = False else: if not can_harvest(): all_ok = False j += 1 if j < W: move(East) if i < W - 1: move(North) i += 1 return all_ok def trigger_cascade_harvest(): # Only harvest if every cactus is mature; otherwise wait for next cycle if all_mature_cacti(): go_home() if can_harvest(): harvest() # ---------- Sorting passes ---------- def row_compare_swap_pass(): # Bubble once across each row, comparing with the neighbor in the travel direction. # Returns True if any swap happened. go_home() swapped = False i = 0 eastward = True while i < W: # At the start of each row, set how many comparisons we can do j = 0 # We do W-1 comparisons per row, but the neighbor direction changes with 'eastward' while j < W - 1: if eastward: # We're somewhere from col 0..W-2: compare with East neighbor a = measure() # current b = measure(East) # neighbor if a > b: swap(East) swapped = True move(East) else: # Traveling West: we're somewhere from col W-1..1: compare with West neighbor a = measure() # current b = measure(West) # neighbor if a < b: # Put the smaller on the West: swap with West neighbor swap(West) swapped = True move(West) j += 1 # Go up a row and flip direction if i < W - 1: move(North) eastward = not eastward i += 1 return swapped def column_compare_swap_pass(): # Bubble once along each column, always comparing toward North (ascending). # Returns True if any swap happened. go_home() swapped = False col = 0 while col < W: row = 0 while row < W - 1: a = measure() # current b = measure(North) # neighbor above if a > b: swap(North) # put smaller below, larger above swapped = True move(North) row += 1 # go back down to y=0 before stepping to next column while row > 0: move(South) row -= 1 if col < W - 1: move(East) col += 1 return swapped def sort_field_rows_then_columns(): # Repeatedly apply row pass then column pass until no swaps. #This converges to a matrix that is sorted by rows and columns. # Classic alternating relaxation: row-bubble then column-bubble # Continue until a full cycle makes no changes. while True: any_swapped = False s1 = row_compare_swap_pass() if s1: any_swapped = True s2 = column_compare_swap_pass() if s2: any_swapped = True if not any_swapped: break # ---------- Harvest trigger ---------- def trigger_cascade_harvest(): # Harvest from (0,0) once the matrix is sorted and all are mature. go_home() if can_harvest(): harvest() # ---------- Main loop ---------- # 1) Ensure field is filled with cactus clear() fill_field_with_cactus() # 2) Maintenance loop while True: # Let everything grow; keep tiles cactus if anything was disturbed fill_field_with_cactus() go_home() i = 0 while i < W: j = 0 while j < W: if get_water() < WATER_THRESHOLD: use_item(Items.Water) j += 1 if j < W: move(East) if i < W - 1: move(North) i += 1 # 3) If (and only if) every cactus is mature, sort & harvest if all_mature_cacti(): sort_field_rows_then_columns() trigger_cascade_harvest() # After the cascade, the field is cleared—loop will refill and repeat.