Toy maze solver (stateless) for https://store.steampowered.com/app/2060160/The_Farmer_Was_Replaced/
-
-
Save andrew-raphael-lukasik/ad3b7eb048bb9135734feb76a96f9f9f to your computer and use it in GitHub Desktop.
def create_maze(): | |
clear() | |
for i in range(get_world_size()): | |
plant(Entities.Bush) | |
while get_water()<0.9: | |
use_item(Items.Water) | |
move(North) | |
for i in range(get_world_size()): | |
while can_harvest()==False: | |
pass | |
while get_entity_type()==Entities.Bush: | |
if num_items(Items.Fertilizer)==0: | |
trade(Items.Fertilizer) | |
#if num_items(Items.Fertilizer)==0: | |
#main() | |
use_item(Items.Fertilizer) | |
treasure_hunt() |
def treasure_hunt(): | |
dir = West | |
x = get_pos_x() | |
y = get_pos_y() | |
while True: | |
move(dir) | |
x2 = get_pos_x() | |
y2 = get_pos_y() | |
if x==x2 and y==y2: | |
if dir==West: | |
dir = North | |
elif dir==North: | |
dir = East | |
elif dir==East: | |
dir = South | |
elif dir==South: | |
dir = West | |
else: | |
x = get_pos_x() | |
y = get_pos_y() | |
if dir==West: | |
dir = South | |
elif dir==North: | |
dir = West | |
elif dir==East: | |
dir = North | |
elif dir==South: | |
dir = East | |
if get_entity_type()==Entities.Treasure: | |
harvest() | |
create_maze() |
The maze code works well. I think maybe someone could find a more optimized maze solver in python but that doesn't really matter.
Ope. I'll see what I can do to prevent this.
Fixed Code: Run main to start the program.
main
while True:
if create_maze():
if treasure_hunt():
continue
create_maze
# https://gist.github.com/andrew-raphael-lukasik/ad3b7eb048bb9135734feb76a96f9f9f
def create_maze():
clear()
plant(Entities.Bush)
while get_entity_type()==Entities.Bush:
if num_items(Items.Weird_Substance) > get_world_size():
use_item(Items.Weird_Substance, get_world_size())
return 1
if can_harvest():
harvest()
plant(Entities.Bush)
if num_items(Items.Fertilizer)==0:
# I do not have trade unlocked
#trade(Items.Fertilizer)
return 0
use_item(Items.Fertilizer)
return 1
treasure_hunt
# https://gist.github.com/andrew-raphael-lukasik/ad3b7eb048bb9135734feb76a96f9f9f
def treasure_hunt():
dir = West
x = get_pos_x()
y = get_pos_y()
while True:
move(dir)
x2 = get_pos_x()
y2 = get_pos_y()
if x==x2 and y==y2:
if dir==West:
dir = North
elif dir==North:
dir = East
elif dir==East:
dir = South
elif dir==South:
dir = West
else:
x = get_pos_x()
y = get_pos_y()
if dir==West:
dir = South
elif dir==North:
dir = West
elif dir==East:
dir = North
elif dir==South:
dir = East
if get_entity_type()==Entities.Treasure:
harvest()
return 1
in the newer version you need to change just a little bit in the code for it to run. but its only in the main script because of the new import function
its a simple fix:
import create_maze
import treasure_hunt
while True:
if create_maze.create_maze():
if treasure_hunt.treasure_hunt():
continue
Can someone explain why this algorithm works?
@mahdi-mezghani this is straight-forward wall-hugging. Every valid maze can be solved by following a wall and this is exactly what treasure_hunt()
does here. It's nothing complicated. Simplicity is an underappreciated code quality.
This approach won't produce an optimal path every time but it doesn't matter that much in this game. At least while mazes are relatively small. For big mazes this method becomes ill-fitted, but it's a problem most players won't even have.
Ope. I'll see what I can do to prevent this.
Stack overflow is caused by recursive function calls.
use_item(Items.Water_Tank)
is no morechange it to
use_item(Items.Water)
Also in the Dec 18th updated version the code doesn't work.
I am going to try to update it and post it back here.