modularized planting logic by splitting f2.py into main.py, pumpkins.py, and sunflowers.py; updated utilities and added .gitignore

This commit is contained in:
2026-01-10 00:19:43 -05:00
parent cb702bed32
commit db310c2a19
6 changed files with 198 additions and 122 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/save.json
/__builtins__.py
.idea

81
f2.py
View File

@@ -1,81 +0,0 @@
import utils
from __builtins__ import Entities
def plant_pumpkin(width, height):
x = get_pos_x()
y = get_pos_y()
utils.plant_grid(width, height, [Entities.Pumpkin], 0.75, True)
utils.move_to(x, y)
while verify_pumpkin(sectionSize, sectionSize):
utils.move_to(x, y)
def plant_carrots(width, height):
utils.plant_grid(width, height, [Entities.Carrot], 0.75, True)
def plant_grass(width, height):
utils.plant_grid(width, height, [Entities.Grass], 0.75, False)
def plant_alternating(width, height, plantWith):
utils.plant_grid(width, height, plantWith, 0.75, False)
# verify_pumpkin will verify that a pumpkin is planted in all tiles of a quadrant, and it will check that
# the pumpkin isn't dead. if it's dead it will replant it. Returns whether any dead pumpkins were found.
def verify_pumpkin(width, height):
result = False
others = {East:West, West:East}
sideDir = East
for row in range(height):
for col in range(width):
if get_entity_type() == Entities.Dead_Pumpkin:
# ~20% of pumpkins die, we just replant
plant(Entities.Pumpkin)
result = True
# if it's not the last pass, then move
if col < width - 1:
move(sideDir)
if row < height - 1:
sideDir = others[sideDir]
move(North)
return result
#reset_pos()
while True:
worldSize = get_world_size()
sectionSize = worldSize // 3
utils.move_to(0, 0)
plant_pumpkin(sectionSize, sectionSize)
utils.move_to(sectionSize+1, 0)
plant_carrots(sectionSize, sectionSize)
utils.move_to(sectionSize*2+1, 0)
plant_grass(sectionSize, sectionSize)
utils.move_to(0, sectionSize+1)
plant_alternating(sectionSize, sectionSize, [ Entities.Tree, Entities.Bush ])
utils.move_to(sectionSize+1, sectionSize+1)
plant_pumpkin(sectionSize, sectionSize)
utils.move_to(sectionSize*2+1, sectionSize+1)
plant_carrots(sectionSize, sectionSize)
utils.move_to(0, sectionSize*2+1)
plant_alternating(sectionSize, sectionSize, [ Entities.Tree, Entities.Bush ])
utils.move_to(sectionSize+1, sectionSize*2+1)
plant_alternating(sectionSize, sectionSize, [ Entities.Tree, Entities.Bush ])
utils.move_to(sectionSize*2+1, sectionSize*2+1)
plant_pumpkin(sectionSize, sectionSize)

46
main.py Normal file
View File

@@ -0,0 +1,46 @@
import utils
import pumpkins
from __builtins__ import *
def plant_carrots(width, height):
utils.plant_grid(width, height, [Entities.Carrot], 0.75, True)
def plant_grass(width, height):
utils.plant_grid(width, height, [Entities.Grass], 0.75, False)
def plant_alternating(width, height, plantWith):
utils.plant_grid(width, height, plantWith, 0.75, False)
if __name__ == "__main__":
while True:
worldSize = get_world_size()
sectionSize = worldSize // 3
utils.move_to(0, 0)
pumpkins.plant_and_verify(sectionSize, sectionSize)
utils.move_to(sectionSize+1, 0)
plant_carrots(sectionSize, sectionSize)
utils.move_to(sectionSize*2+1, 0)
plant_grass(sectionSize, sectionSize)
utils.move_to(0, sectionSize+1)
plant_alternating(sectionSize, sectionSize, [ Entities.Tree, Entities.Bush ])
utils.move_to(sectionSize+1, sectionSize+1)
pumpkins.plant_and_verify(sectionSize, sectionSize)
utils.move_to(sectionSize*2+1, sectionSize+1)
plant_carrots(sectionSize, sectionSize)
utils.move_to(0, sectionSize*2+1)
plant_alternating(sectionSize, sectionSize, [ Entities.Tree, Entities.Bush ])
utils.move_to(sectionSize+1, sectionSize*2+1)
plant_alternating(sectionSize, sectionSize, [ Entities.Tree, Entities.Bush ])
utils.move_to(sectionSize*2+1, sectionSize*2+1)
pumpkins.plant_and_verify(sectionSize, sectionSize)

87
pumpkins.py Normal file
View File

@@ -0,0 +1,87 @@
import utils
from __builtins__ import *
# Plants an object at a specified location and repositions back to the original
# position while verifying the placement.
#
# Args:
# width (int): The width of the object to be planted.
# height (int): The height of the object to be planted.
def plant_and_verify(width, height):
x = get_pos_x()
y = get_pos_y()
place(width, height)
utils.move_to(x, y)
coords = verify(width, width)
while len(coords) > 0:
coords = verify_coordinates(coords)
# Places pumpkins in a grid pattern.
# This function uses the `utils.plant_grid` method to populate a grid of the specified
# width and height with pumpkins. The grid is filled with pumpkins at a density of
# 75%, and the function ensures that the grid is symmetric by planting pumpkins in
# mirrored positions.
# Parameters:
# width (int): The number of columns in the grid.
# height (int): The number of rows in the grid.
def place(width, height):
utils.plant_grid(width, height, [Entities.Pumpkin], 0.75, True)
# Verifies and replants pumpkins within a specified area.
#
# This function iterates through a grid of the specified width and height, checking
# for dead pumpkins, and attempting to replant them where necessary. The function
# navigates the grid row by row, altering its direction after each row. The traversal
# follows a serpentine pattern to ensure proper coverage. It returns a boolean value
# indicating whether any pumpkins were replanted.
#
# Parameters:
# width (int): The number of columns in the grid.
# height (int): The number of rows in the grid.
#
# Returns:
# list: a list of tuples of coordinates where pumpkins were replanted.
def verify(width, height):
result = []
others = {East: West, West: East}
sideDir = East
for row in range(height):
for col in range(width):
if get_entity_type() == Entities.Dead_Pumpkin:
# ~20% of pumpkins die, we just replant
plant(Entities.Pumpkin)
result.append((get_pos_x(), get_pos_y()))
# if it's not the last pass, then move
if col < width - 1:
move(sideDir)
if row < height - 1:
sideDir = others[sideDir]
move(North)
return result
# Verifies that none of the provided coordinates are dead pumpkins.
#
# The function iterates through all coords and will check to see if any tiles are dead pumpkins,
# if so it will replant the pumpkin and note this coordinate down.
#
# Parameters:
# coords (list): A list of tuples of coordinates to check.
#
# Returns:
# list: a list of tuples of coordinates where pumpkins were replanted.
def verify_coordinates(coords):
result = []
for coord in coords:
utils.move_to(coord[0], coord[1])
if get_entity_type() == Entities.Dead_Pumpkin:
plant(Entities.Pumpkin)
result.append(coord)
return result

17
sunflowers.py Normal file
View File

@@ -0,0 +1,17 @@
from __builtins__ import *
import utils
def place(width, height):
utils.plant_grid(width, height, [Entities.Sunflower], 0.75, True)
# Harvests all sunflowers in the specified area.
#
# The rules for sunflower harvesting is that we must harvest the flowers with the most petals first
# as doing so gives us an 8x bonus.
#
# Parameters:
# width (int): The number of columns in the grid.
# height (int): The number of rows in the grid.
# flowers (list): A list of sunflower entities to be harvested.
def harvest(width, height, flowers):

View File

@@ -1,62 +1,66 @@
from __builtins__ import *
world_size = get_world_size()
def move_to(x, y):
while x > get_pos_x():
move(East)
while x < get_pos_x():
move(West)
while x > get_pos_x():
move(East)
while x < get_pos_x():
move(West)
while y > get_pos_y():
move(North)
while y < get_pos_y():
move(South)
while y > get_pos_y():
move(North)
while y < get_pos_y():
move(South)
# plant_grid will plant a square of the given list of entities. If the water level is below waterBelow,
# it will water the tile. If requireSoil is true, it will till the tile if it's not soil.
# plantWith - list of entities to plant. The entities will alternate if repeated.
def plant_grid(width, height, plantWith, waterBelow, requireSoil):
x = get_pos_x()
y = get_pos_y()
x = get_pos_x()
y = get_pos_y()
gridSize = get_world_size()
gridSize = get_world_size()
if (x + width) > gridSize:
width = gridSize - x
if (x + width) > gridSize:
width = gridSize - x
if (y + height) > gridSize:
height = gridSize - y
if (y + height) > gridSize:
height = gridSize - y
tracker = 0
tracker = 0
others = {East:West, West:East}
others = {East:West, West:East}
sideDir = East
# first pass over, checking if anything can be harvested. if so, harvest.
# then check what type of ground is down, if it's not soil then till.
# then if it's below 75% water and i have some water, give it some.
# then plant a pumpkin.
for row in range(height):
for col in range(width):
if can_harvest():
harvest()
if requireSoil:
if get_ground_type() != Grounds.Soil:
till()
sideDir = East
# first pass over, checking if anything can be harvested. if so, harvest.
# then check what type of ground is down, if it's not soil then till.
# then if it's below 75% water and i have some water, give it some.
# then plant a pumpkin.
for row in range(height):
for col in range(width):
if can_harvest():
harvest()
if requireSoil:
if get_ground_type() != Grounds.Soil:
till()
while get_water() < waterBelow:
if not use_item(Items.Water):
break
while get_water() < waterBelow:
if not use_item(Items.Water):
break
index = tracker % len(plantWith)
tracker += 1
plant(plantWith[index])
index = tracker % len(plantWith)
tracker += 1
plant(plantWith[index])
# if it's not the last pass, then move
if col < width - 1:
move(sideDir)
# if it's not the last pass, then move
if col < width - 1:
move(sideDir)
if row < height - 1:
sideDir = others[sideDir]
move(North)
if row < height - 1:
sideDir = others[sideDir]
move(North)