Question
Drunk Walker Write a Java program that generates a random (drunk) walk across a 10 X 10 array. Initially each cell of the array should
Drunk Walker
Write a Java program that generates a random (drunk) walk across a 10 X 10 array. Initially each cell of the array should contain a period (.)
The program must randomly walk from element to element always going up, down, right, or left (no diagonal) by one element.
The elements visited will be labeled by the letters A through Z in the order visited.
To determine your direction to move, use the random() method to generate a random integer
double rand = random();
Convert the random integer to a number 0~3 (hint: use the mod %)
If the number generated is a:
0 move up (north)
1 move right (east)
2 move down (south)
3 move left (west)
If the spot you are trying to move to is already occupied by a letter (has been visited), try the next spot. In other words
Random number is a 0 check N, E, S, W
1 check E, S, W, N
2 check S, W, N, E
3 check W, N, E, S
Keep within the array (check array bounds before moving)
If you hit a point where you cannot move, stop and print the array
If you get to the letter Z, stop and print the array
Ask for the user to enter the initial row and column on which to start.
Validate that row and column entered are valid (within the array)
Output if you stopped because you are blocked, use a heading of Arrested in jail. If you make it to the letter Z, use a heading of Made it home. Then print the array
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