Question: Java - I have two arrays. one is for display, M for mouse moves from top right to somewhere in the matrix. The other matrix
Java -
I have two arrays. one is for display, M for mouse moves from top right to somewhere in the matrix. The other matrix called realMaze is where I want to use a random number and move the mouse to another square depending the random number. Im stuck, this is my second question, I tried a switch but that was too complicated. It is not a GUI base program. Basically. here's the 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 mouse 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 forward, 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 starts over with location [0][0]. Going forward is defined as the sum of the two array indexes either increasing or staying the same.
Here is my code so far:
import java.util.Scanner;
public class MouseMaze
{
public static void main (String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("*** Mouse Maze ***");
int x=0,y=0;
//char [][] maze = new char [10][10];
char cat='C';
char mouse='M';
System.out.println("Enter array dimensions (note: it will be a square so if you enter 5, the maze is 5x5)");
int dim=keyboard.nextInt();
System.out.println("The array will be "+dim+"x"+dim);
char [][] maze = new char [dim][dim];
int [][] realMaze= new int [dim+1][dim+1];
// System.out.println("Array is 10x10");
// if (dim>100)
// System.out.println("You must be crazy, im not making a mouse run that far.");
//initilaze realMaze
int test=0;
//end of initilaze realMaze
do{
int randomNumber=(int)(Math.random()*4-2);
System.out.println("Random Number is: "+randomNumber);
test++;
} while (test<4);
// initialize array
//int dim=10;
for (int row=0; row for (int column=0; column maze[row][column]='-'; // end of initialize array maze[0][0]=mouse; // print out array for (int r=0;r { for (int c=0; c { System.out.print(maze[r][c]+" "); } //end of c loop System.out.println(); } //end of r loop //initilaze second array to hold real moves for (x=0; x for (y=0; y realMaze[x][y]=-1; System.out.println(); for (int r=0;r { for (int c=0; c { System.out.print(realMaze[r][c]+" "); } //end of c loop System.out.println(); } // end of second array } // public static void main end } // public class MouseMaze end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
