The Farmer Was Replaced

The Farmer Was Replaced

*SPOILER* Solution to automating the maze solving
I couldn't find any guides on how to solve the maze at the end so I thought I would create my own if anyone gets stuck

Hint:
You can use recursive function to implement backtracking until you find the treasure. link: https://en.wikipedia.org/wiki/Backtracking

Solution:
```python def get_new_direction(direction): return [get_pos_x() + direction[0], get_pos_y() + direction[1]] def coord_in_array(elem, coord_x, coord_y): i = len(coord_x) - 1 while i >= 0: if elem[0] == coord_x and elem[1] == coord_y:
return True
i -= 1
return False

def move_direction(d):
if d == 0:
move(North)
elif d == 1:
move(East)
elif d == 2:
move(South)
else:
move(West)

def solve_maze_inner(visited_x, visited_y, preferred_direction):
maze_width = get_world_size()
maze_height = get_world_size()

if is_over(Entities.Treasure) and len(visited_x) < (maze_width * maze_height):
wait(1000) # prevents out of sync issues
harvest()
return True

directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]
# direction_names = [North, East, South, West]


visited_x.append(get_pos_x())
visited_y.append(get_pos_y())
direction_index = preferred_direction
i = 0

while i < len(directions):
next_direction = (direction_index + i) % len(directions)
new_position = get_new_direction(directions[next_direction])

if not coord_in_array(new_position, visited_x, visited_y):
old_position = [get_pos_x(), get_pos_y()]
move_direction(next_direction)

# collided with wall
if not (old_position[0] == get_pos_x() and old_position[1] == get_pos_y()):
result = solve_maze_inner(visited_x, visited_y, next_direction)
if result:
return True
else:
move_direction((next_direction + 2) % len(directions))
i += 1
return False

def solve_maze():
solve_maze_inner([], [], 0)
```[/code]

Hope this helps!
< >
Showing 1-7 of 7 comments
How do you get the maze to spawn? Fertilizing a bush, grown or not, doesn't seem to be doing anything :(

** EDIT **
Nevermind, I had no fertilizer and didn't realise it didn't show items you don't have when get_inventory() is used
Solution without backtracking/recursion.

Helper Functions:
def checked_move(dir): x = get_pos_x() y = get_pos_y() move(dir) if get_pos_x() == x and get_pos_y() == y: return False else: return True def turn_left(dir): if dir == North: return West if dir == East: return North if dir == South: return East if dir == West: return South def turn_right(dir): if dir == North: return East if dir == East: return South if dir == South: return West if dir == West: return North def plant_maze(): success = False while success == False: plant(Entities.Bush) if num_items(Items.Fertilizer) == 0: trade(Items.Fertilizer) use_item(Items.Fertilizer) if checked_move(South): move(North) else: success = True

Actual Maze solving
def maze_solver(): dir = North while get_entity_type() != Entities.Treasure: dir = turn_right(dir) success = checked_move(dir) if success == False: dir = turn_left(dir) success = checked_move(dir) if success == False: dir = turn_left(dir) else: dir = turn_right(dir) success = checked_move(dir) if success == False: dir = turn_left(dir) harvest()
Originally posted by Unknown:
Hey same as me! :D
Can optimize further removing all the success storing
My ver:
def maze_solve(): dir = North while get_entity_type() != Entities.Treasure: left = rotate_left(dir) if try_move(left): dir = left else: dir = rotate_right(dir) harvest()
I don't know if this is a new feature or not and that's why you didn't use it, but you don't need checked_move() because the move() function returns a boolean indicating whether or not it succeeded.

So your checked_move() calls can be replaced with just move().
The above functions (not the original post) do not work consistently. There are situations where "following the left/right wall" strategies will miss the treasure and loop around forever. The only thing that works 100% of the time is recursion and backtracking, meaning you'll have to store the coordinates you've already tried. This is especially true when fertilizing the treasure, which causes it, and walls, to move. The original script is flawed (there's a bug, and it needs migrating to newest update that removes/changes some functions), however, and can be optimized with tuples (it's incredibly slow without it). Just be careful with how many times you fertilize the treasure, as you can fill up the call stack size and throw an error.
Originally posted by Aztec:
I couldn't find any guides on how to solve the maze at the end so I thought I would create my own if anyone gets stuck
There are 2 guides in the guides section ...
Originally posted by Koenich:
Originally posted by Aztec:
I couldn't find any guides on how to solve the maze at the end so I thought I would create my own if anyone gets stuck
There are 2 guides in the guides section ...
OG was a year + ago, fairly certain it's changed a decent amount since then
< >
Showing 1-7 of 7 comments
Per page: 1530 50

Date Posted: Feb 21, 2023 @ 10:10am
Posts: 7