Montana State University Bozeman Halloween Escape from The Hay Maze Algorithm
Question Description
This lab will use 2D arrays, recursive algorithms, and logical thinking.
The following grid of hashes(#) and dots(.) is a 2D array representation of a maze…….a maze made out of a cornfield or haystacks for Halloween
# # # # # # # # # # # # # . . . # . . . . . . # . . # . # . # # # # . # # # # . # . . . . # . # # . . . . # # # . # . # # # # # . # F # . # . # # . . # . # . # . # . # # # . # . # . # . # . # # . . . . . . . . # . # # # # # # # . # # # . # # . . . . . . # . . . # # # # # # # # # # # # #
The hashes represent the corn walls of the maze and the dots represent the possible paths through the maze. Moves can only be made to a location in the array that contains a dot.
This maze is only allowed to be accessed at dark when you can’t see more than one step in front of you at a time. Even more spooky.
Algorithm Explanation
There is a simple algorithm for walking through a maze that GUARANTEES finding the exit (assuming there is an exit). If there is not an exit, you will arrive at the starting location again. Place your right hand on the wall to your right and begin walking forward. Never remove your hand from the wall. If the maze turns to the right, you follow the wall to the right. As long as you do not remove your hand from the wall, eventually, you will arrive at the exit of the maze. If you follow the algorithm.
In an episode of The Simpsons, Homer Simpson couldn’t find his way out of a corn maze and he almost starved to death. If he would have known this algorithm he would have made it out without trouble. He didn’t starve to death because he eventually realized he could walk through the porous walls made of corn plants. Like in Field of Dreams.
What to do:
Write a program to walk through the maze. You must write a recursive function. The function should receive as arguments 12-by-12 character array representing the maze and the starting point (x, y location in the maze).
As the function attempts to escape from the maze it should place an X for the current path taken. The program should display the maze at each step so you can watch as the maze is solved. Put an X where you have traveled and maybe an O where you currently are located. The end of the maze will be a capital ‘F’ (stands for finished or food if you prefer the Homer story).
This algorithm doesn’t work for all mazes. Can you come up with a maze where this doesn’t work?
Rules you must follow:
- Recursive Algorithm must use a recursive method.
- The method gets four parameters (maze, xLoc, YLoc, handLocationX, handLocationY) no instance fields needed.
- Your method can only see one step, you can only see what’s in front of you and what your hand is on……no teleporting, no looking beyond one step at a time.
Movement Rules
- You can only see one step to the left, right and one step in front of you for each turn. (No looking ahead, one step only).
- Each call to the recursive move method can only do one of three things……
- Take a 90 degree turn to right and step one step, turn and step in one turn so you don’t do a infinite loop.
- Take one step forward.
- Turn 90 degrees to the left.
Hints
Make a move, print out the maze and see where you are, make another move, print the maze. Don’t try to solve the whole maze at once, get the different moves working first.
Think of it like this……”I am at x, y, my hand is at Hx, Hy which tells me the direction I’m facing based on Hx being less than or greater than x, and Hy being less than or greater than y. Based on that logic I can check what my hand is touching and what is in front of me.
- IF my hand is on a dot I will turn 90 degrees right and move to that dot. I will recursively call my method with a new x, y and a new hx, hy.
- If my hand is not on a dot, I will look in front of me, if it is a . in front of me I’ll move forward and then recursively call this method with my new x, y, and hx, hy, ……..
- If it’s a # in front of me I can’t go forward so I’ll turn 90 degrees to left and call the same method recursively with the same x, y, and new hx, hy.
- ………so on and so on.
Lots of if statements to figure out which way you are facing, where you need to check to see if you can move forward, of if you need to turn.The maze should be put into a 2D array. I have more mazes I’ll link in below.Also, the starting point is always on the outside of the maze, you can search the outside of the input to find a . then you know your starting point. Do that last, wait until you have everything else working first……hard code in the starting point until you get the traversal working.
If you hit a dead-end you must turn left, then turn left (2 recursive calls according to the rules) and then head back along the path you already traveled……but now instead of .’s you will see x’s if you are showing the route you traveled with x’s. Your if statements should handle that……example: if(maze[y+1][x] == ‘.’ || maze[y+1][x] == ‘x’)
Always keep your code for this assignment, there’s a good chance you’ll see it again in C programming, Software Engineering, or some other upper-division CS course. They’ve all been known to use it.
I have made the theory of this maze into Android and IOS games, I have 2D versions and 3D versions and I started working on a VR version.
I’ll show it in a video…..I have made several versions of it, including a version using 3D cameras and augmented reality.
"Place your order now for a similar assignment and have exceptional work written by our team of experts, guaranteeing you "A" results."