Normalize indentation in cactus.py for improved readability and consistency.
This commit is contained in:
184
cactus.py
184
cactus.py
@@ -6,17 +6,17 @@ import utils
|
|||||||
# Returns:
|
# Returns:
|
||||||
# Tuple
|
# Tuple
|
||||||
def compute_cost(width, height):
|
def compute_cost(width, height):
|
||||||
cost = get_cost(Unlocks.Cactus)
|
cost = get_cost(Unlocks.Cactus)
|
||||||
seeds = width * height
|
seeds = width * height
|
||||||
|
|
||||||
for item in cost:
|
for item in cost:
|
||||||
if item == Items.Cactus:
|
if item == Items.Cactus:
|
||||||
cost[item] = seeds * cost[item]
|
cost[item] = seeds * cost[item]
|
||||||
|
|
||||||
return cost
|
return cost
|
||||||
|
|
||||||
def place(width, height):
|
def place(width, height):
|
||||||
utils.plant_grid(width, height, [Entities.Cactus], 0.0, True)
|
utils.plant_grid(width, height, [Entities.Cactus], 0.0, True)
|
||||||
|
|
||||||
# Harvests all cacti in the specified area.
|
# Harvests all cacti in the specified area.
|
||||||
#
|
#
|
||||||
@@ -31,116 +31,116 @@ def place(width, height):
|
|||||||
# width (int): The number of columns in the grid.
|
# width (int): The number of columns in the grid.
|
||||||
# height (int): The number of rows in the grid.
|
# height (int): The number of rows in the grid.
|
||||||
def sort_and_harvest(width, height):
|
def sort_and_harvest(width, height):
|
||||||
x = get_pos_x()
|
x = get_pos_x()
|
||||||
y = get_pos_y()
|
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
|
# 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):
|
for row in range(height):
|
||||||
_sort_row_bubble(width)
|
_sort_row_bubble(width)
|
||||||
if row < height - 1:
|
if row < height - 1:
|
||||||
move(North)
|
move(North)
|
||||||
y += 1
|
y += 1
|
||||||
utils.move_to(x, y)
|
utils.move_to(x, y)
|
||||||
|
|
||||||
y -= (height - 1)
|
y -= (height - 1)
|
||||||
utils.move_to(x, y)
|
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
|
# 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):
|
for col in range(width):
|
||||||
_sort_column_bubble(height)
|
_sort_column_bubble(height)
|
||||||
if col < width - 1:
|
if col < width - 1:
|
||||||
move(East)
|
move(East)
|
||||||
x += 1
|
x += 1
|
||||||
utils.move_to(x, y)
|
utils.move_to(x, y)
|
||||||
|
|
||||||
move(West)
|
move(West)
|
||||||
|
|
||||||
|
|
||||||
# we should be fully sorted! so just harvest...
|
# we should be fully sorted! so just harvest...
|
||||||
harvest()
|
harvest()
|
||||||
|
|
||||||
|
|
||||||
# Sort a row of cacti based on size. Implement bubble sort.
|
# Sort a row of cacti based on size. Implement bubble sort.
|
||||||
#
|
#
|
||||||
# Parameters:
|
# Parameters:
|
||||||
# width (int): The number of columns in the grid.
|
# width (int): The number of columns in the grid.
|
||||||
def _sort_row_bubble(width):
|
def _sort_row_bubble(width):
|
||||||
start_index = 0
|
start_index = 0
|
||||||
end_index = width - 1
|
end_index = width - 1
|
||||||
swapped = True
|
swapped = True
|
||||||
|
|
||||||
while start_index < end_index and swapped:
|
while start_index < end_index and swapped:
|
||||||
swapped = False
|
swapped = False
|
||||||
|
|
||||||
# Forward pass (East): Bubble the largest item to the right (end_index)
|
# Forward pass (East): Bubble the largest item to the right (end_index)
|
||||||
# We travel from start_index to end_index
|
# We travel from start_index to end_index
|
||||||
for _ in range(end_index - start_index):
|
for _ in range(end_index - start_index):
|
||||||
if measure(East) < measure():
|
if measure(East) < measure():
|
||||||
swap(East)
|
swap(East)
|
||||||
swapped = True
|
swapped = True
|
||||||
move(East)
|
move(East)
|
||||||
|
|
||||||
# The rightmost element is now sorted
|
# The rightmost element is now sorted
|
||||||
end_index -= 1
|
end_index -= 1
|
||||||
|
|
||||||
if not swapped:
|
if not swapped:
|
||||||
break
|
break
|
||||||
|
|
||||||
swapped = False
|
swapped = False
|
||||||
|
|
||||||
# Backward pass (West): Bubble the smallest item to the left (start_index)
|
# Backward pass (West): Bubble the smallest item to the left (start_index)
|
||||||
# We travel from end_index back to start_index
|
# We travel from end_index back to start_index
|
||||||
# FIXED: Added +1 to range to ensure we reach start_index
|
# FIXED: Added +1 to range to ensure we reach start_index
|
||||||
for _ in range(end_index - start_index + 1):
|
for _ in range(end_index - start_index + 1):
|
||||||
if measure(West) > measure():
|
if measure(West) > measure():
|
||||||
swap(West)
|
swap(West)
|
||||||
swapped = True
|
swapped = True
|
||||||
move(West)
|
move(West)
|
||||||
|
|
||||||
# The leftmost element is now sorted
|
# The leftmost element is now sorted
|
||||||
start_index += 1
|
start_index += 1
|
||||||
|
|
||||||
# Move to the start of the new window
|
# Move to the start of the new window
|
||||||
# We are currently at (start_index - 1), so move East once to get to start_index
|
# We are currently at (start_index - 1), so move East once to get to start_index
|
||||||
if start_index < end_index:
|
if start_index < end_index:
|
||||||
move(East)
|
move(East)
|
||||||
|
|
||||||
|
|
||||||
# Sort a column of cacti based on size. Implement bubble sort.
|
# Sort a column of cacti based on size. Implement bubble sort.
|
||||||
#
|
#
|
||||||
# Parameters:
|
# Parameters:
|
||||||
# height (int): The number of rows in the grid.
|
# height (int): The number of rows in the grid.
|
||||||
def _sort_column_bubble(height):
|
def _sort_column_bubble(height):
|
||||||
start_index = 0
|
start_index = 0
|
||||||
end_index = height - 1
|
end_index = height - 1
|
||||||
swapped = True
|
swapped = True
|
||||||
|
|
||||||
while start_index < end_index and swapped:
|
while start_index < end_index and swapped:
|
||||||
swapped = False
|
swapped = False
|
||||||
|
|
||||||
# Forward pass (North): Bubble the largest item to the top (end_index)
|
# Forward pass (North): Bubble the largest item to the top (end_index)
|
||||||
for _ in range(end_index - start_index):
|
for _ in range(end_index - start_index):
|
||||||
if measure(North) < measure():
|
if measure(North) < measure():
|
||||||
swap(North)
|
swap(North)
|
||||||
swapped = True
|
swapped = True
|
||||||
move(North)
|
move(North)
|
||||||
|
|
||||||
end_index -= 1
|
end_index -= 1
|
||||||
|
|
||||||
if not swapped:
|
if not swapped:
|
||||||
break
|
break
|
||||||
|
|
||||||
swapped = False
|
swapped = False
|
||||||
|
|
||||||
# Backward pass (South): Bubble the smallest item to the bottom (start_index)
|
# Backward pass (South): Bubble the smallest item to the bottom (start_index)
|
||||||
# FIXED: Added +1 to range to ensure we reach start_index
|
# FIXED: Added +1 to range to ensure we reach start_index
|
||||||
for _ in range(end_index - start_index + 1):
|
for _ in range(end_index - start_index + 1):
|
||||||
if measure(South) > measure():
|
if measure(South) > measure():
|
||||||
swap(South)
|
swap(South)
|
||||||
swapped = True
|
swapped = True
|
||||||
move(South)
|
move(South)
|
||||||
|
|
||||||
start_index += 1
|
start_index += 1
|
||||||
|
|
||||||
if start_index < end_index:
|
if start_index < end_index:
|
||||||
move(North)
|
move(North)
|
||||||
|
|||||||
Reference in New Issue
Block a user