Files
farmer-was-replaced/cactus.py

147 lines
4.4 KiB
Python

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)