Question
create a path for a mouse to travel in a maze. Use a 2 dimensional array and start the mouse in location array [0][0]. The
create a path for a mouse to travel in a maze. Use a 2 dimensional array and start the mouse in location array [0][0]. The moouse must find its way to the opposite corner. Repeatedly get a random number representing one of 8 possible moves. A legal move is one that moves foward, does not run off the "edge" of the maze and does not land on a previous move. If the move is illegal the poor mouse must start over with location [0][0]. Going forward is defined as the sum of the two array indexes either increasing or staying the same. with each safe mouse move introduce a cat that may eat the mouse. The cat is a block of 4 maze locations forming a square.with each safe move made by the mouse create a cat. The random number generator generates a location that serves as the upper left corner location of the cat. The random number is dependent on the size and shape of the maze when the maze is first created. If the cat "catches" the mouse, the mouse must begin again. Think through operation I have described and make those operations methods that can be called in order to accomplish the cat aspect of the problem. I use four small methods to implement the cat part of the project. Allow the mouse to repeatedly run the maze and choose the size of the two dimensional maze. The output consists of three numbers. The first is number of times the mouse must start over before he finds a path from beginning to the end, the second is the number of times he falls of the maze and the third is the number of times the cat catches the mouse. Then print the array to the screen showing the path that was successful and the last cat position. (HINT: use a two dimensional integer array, record the cat as 4 negative ones in the maze. When you print out the maze replace a -1 with the char 'C'.) Further in the program when you know the number of rows and columns from the client an array declarition would look like this.
int [] [] maze = new int [rowNumber][columnSize];
public class MazeLoopExample
{
private static int [] [] mouseArray;
static int rowSize = 9;
static int columnSize = 9;
public static void main (Striong [] args)
{
mouseArray = new int [rowSize] [columnSize];
fillArray();
printArray();
}
private static void printArray()
{
int value ;
for (int row = 0; row < rowSize; row++) {
for(int col = 0; col < columeSize; col++) {
value = mouseArray[row][col];
if (value > 9)
System.out.print(" " + value);
else
System.out.print(" " + value);
}
System.out.println();
}
}
private static void fillArray()
{
int number = 0;
for (int row = 0; row < rowSize ; row++) {
for (int column = 0; column < columnSize; column++ ) {
number++;
mouseArray[row][column] = number;
}
}
}
}
Sample output should be like this.
This program finds a path for a mouse from one corner to another in a two dimensional maze.
Please enter the size of the maze.
How many rows would you like?
12
how many columns would you like?
12
It took 4,515,068 attempts to find a path.
The cat got the mouse 106,587 times
The mouse fell off the maze 1,334,130 times.
1 0 0 0 0 0 0 0 0 0 0 0
0 2 0 4 0 0 C C 0 0 0 0
0 0 3 0 5 0 C C 0 0 0 0
0 0 0 0 6 0 0 0 0 0 0 0
0 0 0 0 7 0 9 0 0 0 0 0
0 0 0 0 0 8 10 11 12 0 0 0
0 0 0 0 0 0 0 0 0 13 0 0
0 0 0 0 0 0 0 0 0 14 16 0
0 0 0 0 0 0 0 0 0 15 17 0
0 0 0 0 0 0 0 0 0 0 18 19
0 0 0 0 0 0 0 0 0 0 20 0
0 0 0 0 0 0 0 0 0 0 0 21
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