diff --git a/main.py b/main.py index 7eb6f10..48bfd8e 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ import utils import pumpkins +import sunflowers from __builtins__ import * def plant_carrots(width, height): @@ -18,8 +19,11 @@ if __name__ == "__main__": sectionSize = worldSize // 3 + utils.move_to(0, 0) - pumpkins.plant_and_verify(sectionSize, sectionSize) + grid = sunflowers.place(sectionSize, sectionSize) + utils.move_to(0, 0) + sunflowers.harvest_grid(sectionSize, sectionSize, grid) utils.move_to(sectionSize+1, 0) plant_carrots(sectionSize, sectionSize) @@ -37,7 +41,7 @@ if __name__ == "__main__": plant_carrots(sectionSize, sectionSize) utils.move_to(0, sectionSize*2+1) - plant_alternating(sectionSize, sectionSize, [ Entities.Tree, Entities.Bush ]) + pumpkins.plant_and_verify(sectionSize, sectionSize) utils.move_to(sectionSize+1, sectionSize*2+1) plant_alternating(sectionSize, sectionSize, [ Entities.Tree, Entities.Bush ]) diff --git a/sunflowers.py b/sunflowers.py index ca4a1fb..8b17f3d 100644 --- a/sunflowers.py +++ b/sunflowers.py @@ -2,8 +2,24 @@ from __builtins__ import * import utils + +# Places sunflowers at random locations in the specified area. +# +# We cannot simply just use utils.plant_grid here, because we need to also keep track +# of the number of petals on each sunflower, and return a dictionary mapping petal count +# to a list of sunflower coordinates containing that petal count. +# +# Parameters: +# width (int): The number of columns in the grid. +# height (int): The number of rows in the grid. +# +# Returns: +# dictionary: A dictionary mapping petal count to a list of sunflower coordinates containing that petal count. +# +# Important: +# utils.plant_grid uses very similar logic with this function, so any changes to this function should be reflected in utils.plant_grid as well. def place(width, height): - utils.plant_grid(width, height, [Entities.Sunflower], 0.75, True) + return utils.plant_grid_return_measurements(width, height, [Entities.Sunflower], 0.75, True) # Harvests all sunflowers in the specified area. # @@ -13,5 +29,13 @@ def place(width, height): # 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): +# flowers (dictionary): A dictionary mapping petal count to a list of sunflower coordinates containing that petal count. +def harvest_grid(width, height, flowers): + result = {} + + # in reverse order of petals + petals = [15, 14, 13, 12, 11, 10, 9, 8, 7] + for count in petals: + for coord in flowers[count]: + utils.move_to(coord[0], coord[1]) + harvest() diff --git a/utils.py b/utils.py index bf7c6d5..672086f 100644 --- a/utils.py +++ b/utils.py @@ -16,10 +16,94 @@ def move_to(x, y): -# plant_grid will plant a square of the given list of entities. If the water level is below waterBelow, +# 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. +# +# Parameters: +# width (int): The number of columns in the grid. +# height (int): The number of rows in the grid. +# plantWith (list): A list of entities to plant. +# waterBelow (float): The water level below which the tile will be watered. +# requireSoil (bool): If true, the tile will be tilled if it's not soil. +# +# Returns: +# None +# +# Important: +# plant_grid_returns_measurements uses identical logic but has added needed measurement calls. Any logic changes here +# Any changes to this function should be reflected in plant_grid_returns_measurements as well. def plant_grid(width, height, plantWith, waterBelow, requireSoil): + res = {} + x = get_pos_x() + y = get_pos_y() + + gridSize = get_world_size() + + if (x + width) > gridSize: + width = gridSize - x + + if (y + height) > gridSize: + height = gridSize - y + + tracker = 0 + + 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() + + while get_water() < waterBelow: + if not use_item(Items.Water): + break + + 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 row < height - 1: + sideDir = others[sideDir] + move(North) + + return res + +# 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. +# +# Parameters: +# width (int): The number of columns in the grid. +# height (int): The number of rows in the grid. +# plantWith (list): A list of entities to plant. +# waterBelow (float): The water level below which the tile will be watered. +# requireSoil (bool): If true, the tile will be tilled if it's not soil. +# +# Returns: +# None +# +# Important: +# plant_grid uses identical logic but has added needed measurement calls. Any logic changes here +# Any changes to this function should be reflected in plant_grid as well. +def plant_grid_return_measurements(width, height, plantWith, waterBelow, requireSoil): + res = {} x = get_pos_x() y = get_pos_y() @@ -56,6 +140,12 @@ def plant_grid(width, height, plantWith, waterBelow, requireSoil): tracker += 1 plant(plantWith[index]) + measurement = measure() + + if measurement not in res: + res[measurement] = [] + res[measurement].append((get_pos_x(), get_pos_y())) + # if it's not the last pass, then move if col < width - 1: move(sideDir) @@ -63,4 +153,6 @@ def plant_grid(width, height, plantWith, waterBelow, requireSoil): if row < height - 1: sideDir = others[sideDir] - move(North) \ No newline at end of file + move(North) + + return res \ No newline at end of file