Question
Java maze The following grid of hashes(#) and dots(.) is a 2D array representation of a maze # # # # # # # #
Java maze
The following grid of hashes(#) and dots(.) is a 2D array representation of a maze
# # # # # # # # # # # # # . . . # . . . . . . # . . # . # . # # # # . # # # # . # . . . . # . # # . . . . # # # . # . # # # # # . # F # . # . # # . . # . # . # . # . # # # . # . # . # . # . # # . . . . . . . . # . # # # # # # # . # # # . # # . . . . . . # . . . # # # # # # # # # # # # #
The hashes represent the 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.
(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.
What to do:
Write a program to walk through the maze. You must write a recursive function. The function should recieve 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).
This algorithm doesn't work for all mazes. Can you come up with a maze where this doesn't work?
(Movement Rules)
*You can only see one step to the left, right and one step in front of you for each turn.
*Each call to the recursive move method can only do one of three things......
1.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.
2.Take one step forward.
3.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 on that logic I can check what my hand is touching and what is in front of me.
1.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.
2.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, ........
3.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.
4..........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 travelled......but now instead of .'s you will see x's if you are showing the route you travelled with x's. Your if statements should handle that......example: if(maze[y+1][x] == '.' || maze[y+1][x] == 'x')
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started