As Luchian already wrote, the algorithm (even if it is executed correctly) is not suitable in order to find a way out of all kinds of labyrinths: if you have a loop inside your labyrinth, you can simply run along this loop wall.
In addition, as it seems to you, you really do not create a maze, but rather a large field with walls on the borders and an “exit” somewhere inside it. An algorithm that really "sticks to the wall" will never find a way out unless the way out is next to the wall (which again is only at the borders of your "maze").
Since you are not SomeDude s, that is, the positions that you have already been, and you treat SomeDude same as Wall , you slowly fill the maze of some “SomeDude-Wall”: you go down until you click on the border and then go to the big opponents counterclockwise around the field, leaving a trace of SomeDude s.
Depending on your starting point and exit, you can easily encounter a situation where all four directions are blocked, either by a “real” wall, or by some previous SomeDude that you left there. Then none of the four if -Statements is executed, and you just have an infinite loop (since nothing changes in the loop body).
For an algorithm that attaches to a wall (and therefore can find a way out of some types of mazes), I would suggest the following steps:
- First, go in one direction until you click on the wall.
- Set the current direction so that the wall is on the right.
- Follow your current direction (remember to remove
SomeDude -trace) until- You have found a way out.
- There is no wall on the right side: in this case, turn right and take a step forward.
- Or there is a wall right in front of you. In this case, turn left until your path is clear.
This way you guarantee that there is always the “same” wall on your right side, so you stick to that wall.
Remember that this algorithm cannot find the outputs if the output is inside some free space (since it always sticks to the wall, the output must also be near the wall to be found).
For an algorithm that finds its way out of all the possible mazes, you need to have some kind of rollback: remember every point where you have several options for continuing. Choose one of the methods and follow it. If this is a dead end, go back to the last decision point and make the next choice. If no path exits, go to the previous last point and so on. This is a recursive approach, known as "depth search in graph theory" (feel free to do some search queries, I'm sure you will find a lot of material about this :))
NTN Martin