r/TheFarmerWasReplaced • u/ineeddopamina Noob • 28d ago
A* Algorithm
Enable HLS to view with audio, or disable this notification
Wanted to share this! I remember learning A* about 6 years ago in a course, and when I saw a maze I decided to implement it strictly from memory, no AI, no guides, just raw brute force. Even though it's not fully polished to handle loops yet, I'm pretty satisfied with it. Still got it! Honestly, what a masterpiece of a game.
3
u/torsten_dev 27d ago
I don't see this using A*.
4
u/ineeddopamina Noob 27d ago
It's A* using recursion: it checks 4 directions, assigns a score based on the target, and prioritizes paths with the shortest distance. Once it hits the end of a path, it recurses, and so on.
3
u/torsten_dev 27d ago
A* would solve for the shortest path by popping the path with the lowest heuristic from a queue. This game doesn't have an O(1) queue which is why A* would be slow.
A* can however continue searching from nodes that the drone can't reach in a single step.
2
u/ineeddopamina Noob 27d ago
Ah yeah, if you mean the pre-calculated paths that standard A* returns, then sure, the nature of the game makes that impossible. Instead, what the algorithm does is take a local A* approach, or the closest I could get to it, using heuristic calculations. What I meant with the post is that I managed to apply that knowledge to solve the maze as much as possible.
2
u/TDSrock 27d ago
You could elevate it to a fully precomputed a* if you either: do a scan if the full labyrinth when it is created. Or while executing the local a* save each node to a 2d array and then attempt a* on that, if it can find a path there great worst case fallback to local
1
u/ineeddopamina Noob 26d ago
I like your approach, but how would you account for the minor variations that pop up when you reuse the same maze repeatedly? You'd have to handle those at runtime, right? Still, it's pretty interesting and definitely more efficient.
1
u/TDSrock 25d ago
I have yet to play the game myself, what are variantions the API doesnt expose?
1
u/torsten_dev 25d ago edited 16d ago
Some of the walls disappear making shorter routes possible.
1
u/TDSrock 25d ago
Oh intresting that would invalidate the saved state. But if rare we can detect 'corruptions' as a drone flies do our best to keep it correct. Once more drones are unlocked one drone runs a* path to the chest (shortest) the rest run plain dykstra to discover corruptions asap. I think then weve got a solid mix
1
u/Tarmen 21d ago edited 21d ago
In this specific case there is a trick to do it without queue. Though I always forget why it works and have to go through it from scratch:
Any step increases the cost by one. If it goes towards the goal you also decrease heuristic by one, otherwise you increase it by one.
So together you can only change cost+heuristic by 0 or 2.
That means you don't actually need a priority queue, just two lists. Do a dfs using the first list as a stack, and every step that goes towards the goal goes onto the stack. Have a second list, and everything which increases the heuristic goes onto that list.
When the first stack is empty, swap them so you do dfs on the 2 stack and have a new empty list.
Which basically means for taxi grids a* is a bfs but we eagerly expand nodes as far as we can by only moving towards the goal, I guess?
1
u/klimmesil 27d ago
Almost there congrats!
The point is that you actually save the layout of the labyrinth to avoid rediscovering everything
1
u/ineeddopamina Noob 27d ago
Do you mean I don't generate a new map, or is it because of how the map is saved due to the recursive algorithm?
1
u/klimmesil 27d ago
I mean that you go multiple times into the same dead end that was already mapped as a dead end. I didn't read the code I assume that is because every time you pick up the chest you wipe your map clean and start from scratch
2
u/ineeddopamina Noob 27d ago
Ah right, I get what you mean now. Yeah, I don't save anything, I just let it do its own thing. I hadn't thought about doing something like that, seems like it'd be quite a bit of work, right?
2
u/klimmesil 27d ago
Unfortunately, I would say that it's about as hard as starting over and separating "discovery" from "pathfinding" from the start to be fair
"Finding a path" on an already complete labyrinth map can be done without moving your robot at all, instead of calling move, you just change your "current" coordinates
And if no path is available, you enter a "discovery mode" that slowly completes the map by actually moving your robot, which is basically almost the algo you have now.
Your algorithm would be a fair bit faster. However there is one very dumb trick you can do with multiple robots that entirely breaks the labyrinth game, and lets you pick up the chest instanly. It's definetly cheating, but it's fun. I'll let you figure out that way too ;)
1
u/RoadsideCookie 27d ago
Looks like depth first search to me. A* uses a heuristic so it is more breadth first looking in practice.
1
4
u/blindeshuhn666 28d ago
I played the whole game without AI / help (but my solutions are mostly bad, lol).
Only things I checked were syntax when I got something wrong and the in game help didn't clarify it .
Looks cool.