Files
farmer-was-replaced/pumpkins.py

87 lines
2.7 KiB
Python

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