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

Java: Create a path for a mouse to travel in a maze. Use a 2 dimensional array a

ID: 3856231 • Letter: J

Question

Java:

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.  

With each safe mouse move introduce a cat that may eat the mouse. Display a mouse as one m and cat as 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 the operations I have described and make those operations methods that can be called in order to accomplish the cat aspect of the problem. I used 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 the 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 off 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’.) Using the DecimalFormat class found in Appendix 4 add commas to the output. Example: 1,564,678.

DecimalFormat Decimal_Format_Object_Name = DecimalFromat(Pattern);

DecimalFormat formattingObject = new DecimalFprmat("###,###,###)

Explanation / Answer

import java.io.*;
import java.util.*;
class maze
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int maze[][]=new int[n][n];
int size=n;
maze[0][0]=2;
int fall=0;int cats=0;int start=0;
while(true)
{
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if(maze[i][j]==2)System.out.println("M");
else if(maze[i][j]==-1)System.out.println("C");
}
System.out.println();
}
System.out.println("Enter your move: 8-North,4-West,2-South,6-East,1-NorthWest,9-NorthEast,1-SouthWest,3-SouthEast");
int move=sc.nextInt();
if(move=>1 && move<=8)
{int ca=0;
maze[rr][rc]=0;
if(move==7)
{
if(rr-1<0 || rc-1<0){rr=0;rc=0;fall++;start++;}
else {
rr=rr-1;
rc=rc-1;
}
}
if(move==8)
{
if(rr-1<0){rr=0;rc=0;fall++;start++;}
else {
rr=rr-1;
}
}
if(move==9)
{
if(rr-1<0 || rc+1>=n){rr=0;rc=0;fall++;start++;}
else {
rr=rr-1;
rc=rc+1;
}
}
if(move==4)
{
if(rc-1<0){rr=0;rc=0;fall++;start++;}
else {
rc=rc-1;
}
}
if(move==6)
{
if(rc+1>=n){rr=0;rc=0;fall++;start++;}
else {
rc=rc+1;
}
}
if(move==1)
{
if(rr+1> || rc-1<0){rr=0;rc=0;fall++;start++;}
else {
rr=rr-1;
rc=rc-1;
}
}
if(move==2)
{
if(rr+1>=n){rr=0;rc=0;fall++;start++;}
else {
rr=rr+1;
}
}
if(move==3)
{
if(rr+1>=n || rc+1>=n){rr=0;rc=0;fall++;start++;}
else {
rr=rr+1;
rc=rc+1;
}
}
maze[rr][rc]=2;
if(maze[rr][rc]==-1)
{
cats++;
rr=rc=0;
maze[rr][rc]=2;
}
do
{
Random rn = new Random();
cr = rn.nextInt(n) + 1;
cc= rn.nextInt(n)+1;
}while(cr+1>=n || cc+1>=n);
maze[cr][cc]=maze[cr+1][cc]=maze[cr][cc+1]=maze[cr+1][cc+1]=-1;
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
if(maze[i][j]==2)System.out.println("M");
else if(maze[i][j]==-1)System.out.println("C");
}
System.out.println();
}
}
else
{
rr=rc=0;
maze[rr][rc]=0;
}
if(rr==n-1 && rc==n-1){
System.out.println("Falls of:"+fall);
System.out.println("caught by cat:"+cats);
System.out.println("Start over:"+start);
}
}
}
}

The above code displays a maze of a size entered by user and then proceeds as per the given rules till rat reaches the end of the maze. I am not spoon feeding the solution to you but i have written the whole code which implements the required program. Now you can tweak it a bit to suit your requisites. Currently this code asks you for your move, adds cat if move is invalid else moves the mouse as per the move. The code terminates when end is reached and prints the number of times the mouse falls off , starts over or is caught by the cat.

Good luck and Happy coding!

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