Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to write a program where a mouse \"M\" moves through a maze by a ran

ID: 3853286 • Letter: I

Question

I am trying to write a program where a mouse "M" moves through a maze by a random number. So either it moves up, down, left, right or diagonal. So, a total of 4 possible. The mouse can only move forward so either diagnolly down toward the furthest bottom right corner. it can also move down or right, but never back or off the edge of the maze. Im trying to move the mouse one time with a switch statement. I have two array setup. One with what I would display as the maze, and the other where I could use the rows and columns to determine the movement of the mouse. I am stuck on how to get the mouse to move with my switch. can you help? here is my code so far... (JAVA)

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];

   int [][] realMaze= new int [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];

System.out.println("Array is 10x10");

//   if (dim>100)

//       System.out.println("You must be crazy, im not making a mouse run that far.");

  

   int randomNumber=(int)(Math.random()*8+1);

   System.out.println("Random Number is: "+randomNumber);

   // initialize array

   int dim=10;

   for (int row=0; row<dim; row++)

       for (int column=0; column<dim; column++)

           maze[row][column]='-';

   // end of initialize array

   maze[0][0]=mouse;

   // print out array

   for (int r=0;r<dim;r++)

   {

       for (int c=0; c<dim; 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<dim; x++)

           for (y=0; y<dim; y++)

               realMaze[x][y]=0;

     

   // end of second array

     

   switch(randomNumber){

   // case 1: System.out.println("Mouse moved back");

   // break;

   // case 2:

   case 3: System.out.println("Mouse moved back");

   break;

   case 4:

   //   break;

   case 5: x+=1;

   case 6:

   case 7:

   case 8:

     

   }

   // maze[0][0]=mouse;

     

} // public static void main end

} // public class MouseMaze end

Explanation / Answer

mport 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];

   int [][] realMaze= new int [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];

System.out.println("Array is 10x10");

//   if (dim>100)

//       System.out.println("You must be crazy, im not making a mouse run that far.");

  

   int randomNumber=(int)(Math.random()*8+1);

   System.out.println("Random Number is: "+randomNumber);

   // initialize array

   int dim=10;

   for (int row=0; row<dim; row++)

       for (int column=0; column<dim; column++)

           maze[row][column]='-';

   // end of initialize array

   maze[0][0]=mouse;

   print out array

   for (int r=0;r<dim;r++)

   {

       for (int c=0; c<dim; c++)

       {

           System.out.print(maze[r][c]+" ");

       }

       System.out.println();

   }

  

  

   for (x=0; x<dim; x++)

           for (y=0; y<dim; y++)

               realMaze[x][y]=0;

     

   end of second array

     

   switch(randomNumber){

   case 1: System.out.println("Mouse moved back");

   break;

   case 2:

   case 3: System.out.println("Mouse moved back");

   break;

   case 4:

   break;

   case 5: x+=1;

   case 6:

   case 7:

   case 8:

     

   }

   maze[0][0]=mouse;

     

} public static void main end

} public class MouseMaze end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote