add cactus utilities and grid-wide planting/harvesting logic

This commit is contained in:
2026-01-11 02:49:47 -05:00
parent 70caea1243
commit 4e774109ef
3 changed files with 273 additions and 3 deletions

146
cactus.py Normal file
View File

@@ -0,0 +1,146 @@
from __builtins__ import *
import utils
# Calculates the cost of placing a cactus in the specified area.
#
# Returns:
# Tuple
def compute_cost(width, height):
cost = get_cost(Unlocks.Cactus)
seeds = width * height
for item in cost:
if item == Items.Cactus:
cost[item] = seeds * cost[item]
return cost
def place(width, height):
utils.plant_grid(width, height, [Entities.Cactus], 0.0, True)
# Harvests all cacti in the specified area.
#
# To maximize cactus returns, we must ensure that the grid is in proper order. To achieve this, all
# individual cacti must be in order. A cactus is considered in order if all neighboring cacti to the North and East
# are fully grown and larger or equal in size, and all neighboring cacti to the South and West are fully grown and
# smaller or equal in size.
# Once this is achieved, then harvesting one cactus will harvest all cacti in the grid, and award the player
# cactus^2 where cactus is the number of cacti harvested.
#
# Parameters:
# width (int): The number of columns in the grid.
# height (int): The number of rows in the grid.
def sort_and_harvest(width, height):
x = get_pos_x()
y = get_pos_y()
# first sort all by row, and in this loop we keep tally of the sum of all cacti in each row
for row in range(height):
_sort_row_bubble(width)
if row < height - 1:
move(North)
y += 1
utils.move_to(x, y)
y -= (height - 1)
utils.move_to(x, y)
# now sort each row by column to account for north/south, but we don't need to keep track of the sum of each column
for col in range(width):
_sort_column_bubble(height)
if col < width - 1:
move(East)
x += 1
utils.move_to(x, y)
move(West)
# we should be fully sorted! so just harvest...
harvest()
# Sort a row of cacti based on size. Implement bubble sort.
#
# Parameters:
# width (int): The number of columns in the grid.
def _sort_row_bubble(width):
start_index = 0
end_index = width - 1
swapped = True
while start_index < end_index and swapped:
swapped = False
# Forward pass (East): Bubble the largest item to the right (end_index)
# We travel from start_index to end_index
for _ in range(end_index - start_index):
if measure(East) < measure():
swap(East)
swapped = True
move(East)
# The rightmost element is now sorted
end_index -= 1
if not swapped:
break
swapped = False
# Backward pass (West): Bubble the smallest item to the left (start_index)
# We travel from end_index back to start_index
# FIXED: Added +1 to range to ensure we reach start_index
for _ in range(end_index - start_index + 1):
if measure(West) > measure():
swap(West)
swapped = True
move(West)
# The leftmost element is now sorted
start_index += 1
# Move to the start of the new window
# We are currently at (start_index - 1), so move East once to get to start_index
if start_index < end_index:
move(East)
# Sort a column of cacti based on size. Implement bubble sort.
#
# Parameters:
# height (int): The number of rows in the grid.
def _sort_column_bubble(height):
start_index = 0
end_index = height - 1
swapped = True
while start_index < end_index and swapped:
swapped = False
# Forward pass (North): Bubble the largest item to the top (end_index)
for _ in range(end_index - start_index):
if measure(North) < measure():
swap(North)
swapped = True
move(North)
end_index -= 1
if not swapped:
break
swapped = False
# Backward pass (South): Bubble the smallest item to the bottom (start_index)
# FIXED: Added +1 to range to ensure we reach start_index
for _ in range(end_index - start_index + 1):
if measure(South) > measure():
swap(South)
swapped = True
move(South)
start_index += 1
if start_index < end_index:
move(North)

77
main.py
View File

@@ -1,7 +1,10 @@
import cactus
import utils import utils
import pumpkins import pumpkins
import sunflowers import sunflowers
from __builtins__ import * from __builtins__ import *
from sunflowers import harvest_grid
def plant_carrots(width, height): def plant_carrots(width, height):
utils.plant_grid(width, height, [Entities.Carrot], 0.75, True) utils.plant_grid(width, height, [Entities.Carrot], 0.75, True)
@@ -12,19 +15,93 @@ def plant_grass(width, height):
def plant_alternating(width, height, plantWith): def plant_alternating(width, height, plantWith):
utils.plant_grid(width, height, plantWith, 0.75, False) utils.plant_grid(width, height, plantWith, 0.75, False)
# Entire grid utils:
# These are utilities to plant & harvest the entire grid with specific plants.
def entire_grid_carrots():
world_size = get_world_size()
utils.move_to(0, 0)
plant_carrots(world_size, world_size)
def entire_grid_grass():
world_size = get_world_size()
utils.move_to(0, 0)
plant_grass(world_size, world_size)
def entire_grid_trees_and_bushes():
world_size = get_world_size()
utils.move_to(0, 0)
plant_alternating(world_size, world_size, [ Entities.Tree, Entities.Bush ])
def entire_grid_trees_and_grass():
world_size = get_world_size()
utils.move_to(0, 0)
plant_alternating(world_size, world_size, [ Entities.Tree, Entities.Grass ])
def entire_grid_cactus():
world_size = get_world_size()
utils.move_to(0, 0)
cactus.place(world_size, world_size)
utils.move_to(0, 0)
cactus.sort_and_harvest(world_size, world_size)
def entire_grid_pumpkins():
world_size = get_world_size()
utils.move_to(0, 0)
pumpkins.plant_and_verify(world_size, world_size)
harvest()
def entire_grid_sunflowers():
world_size = get_world_size()
utils.move_to(0, 0)
flowers = sunflowers.place(world_size, world_size)
sunflowers.harvest_grid(flowers)
if __name__ == "__main__": if __name__ == "__main__":
while True:
while num_items(Items.Hay) < 1000000:
entire_grid_grass()
while num_items(Items.Wood) < 1000000:
entire_grid_trees_and_bushes()
while num_items(Items.Carrot) < 1000000:
entire_grid_carrots()
while num_items(Items.Power) < 5000:
entire_grid_sunflowers()
while num_items(Items.Pumpkin) < 500000:
entire_grid_pumpkins()
for i in range(10):
entire_grid_trees_and_grass()
entire_grid_carrots()
entire_grid_carrots()
entire_grid_pumpkins()
while True: while True:
worldSize = get_world_size() worldSize = get_world_size()
sectionSize = worldSize // 3 sectionSize = worldSize // 3
# only run this if our total power is below 5k.
# why? because the rest doesn't use over 5k power.
while num_items(Items.Power) < 5000:
utils.move_to(0, 0) utils.move_to(0, 0)
grid = sunflowers.place(sectionSize, sectionSize) grid = sunflowers.place(sectionSize, sectionSize)
utils.move_to(0, 0) utils.move_to(0, 0)
sunflowers.harvest_grid(sectionSize, sectionSize, grid) sunflowers.harvest_grid(sectionSize, sectionSize, grid)
utils.move_to(0, 0)
cactus.place(sectionSize, sectionSize)
utils.move_to(0, 0)
cactus.sort_and_harvest(sectionSize, sectionSize)
utils.move_to(sectionSize+1, 0) utils.move_to(sectionSize+1, 0)
plant_carrots(sectionSize, sectionSize) plant_carrots(sectionSize, sectionSize)

View File

@@ -16,6 +16,50 @@ def move_to(x, y):
# Harvest all tiles in a grid.
#
# Returns:
# None
def harvest_grid(width, height):
res = {}
x = get_pos_x()
y = get_pos_y()
grid_size = get_world_size()
if (x + width) > grid_size:
width = grid_size - x
if (y + height) > grid_size:
height = grid_size - y
others = {East: West, West: East}
side_dir = East
for row in range(height):
for col in range(width):
if can_harvest():
harvest()
# if it's not the last pass, then move
if col < width - 1:
if side_dir == East:
x = (x + 1) % grid_size
elif side_dir == West:
x = (x - 1) % grid_size
move(side_dir)
if row < height - 1:
side_dir = others[side_dir]
move(North)
y = (y + 1) % grid_size
return res
# Plant a square of the given list of entities. # Plant a square of the given list of entities.
# #
# If the water level is below waterBelow, # If the water level is below waterBelow,
@@ -68,7 +112,10 @@ def plant_grid(width, height, plant_with, water_below, require_soil, on_plant =
index = tracker % len(plant_with) index = tracker % len(plant_with)
tracker += 1 tracker += 1
plant(plant_with[index])
success = plant(plant_with[index])
if not success:
print("Failed to plant at " + str(x) + ", " + str(y))
# if on_plant is not None: is giving me errors in the game, but this seems to be what they want # if on_plant is not None: is giving me errors in the game, but this seems to be what they want
if on_plant: if on_plant: