Description: The program you are to write first asks for an odd number from whic
ID: 3632220 • Letter: D
Question
Description: The program you are to write first asks for an odd number from which the program will build a 2D matrix. You should keep prompting until the input number is odd. The 2D matrix is initially filled with 0s except for the center cell which contains an X. The program then begins a random walk though the matrix. Each step in the random walk places an X in the cell just visited. The random walk is done one cell at a time where each next step is one matrix cell horizontally or vertically from the last visited cell. A move can only be made to a cell that contains a 0. Thus, cells which have been visited before are excluded as being a valid step. The program continues until the next move would place the X outside of the matrix boundaries. Problem: Im having trouble making a loop for the random generated X. How do i do the random walk while checking to make it enters an index with a 0 and make sure it ends when it tries to go outside the matrix?Explanation / Answer
please rate - thanks
message me if any proplem
import java.util.*;
public class randomWalk
{public static int iloc,jloc;
public static void main(String[] args)
{Scanner in = new Scanner(System.in);
int n,step=1;
n=getSize();
char [][] maze=new char[n][n];
initMaze(maze,n);
System.out.println("Matrix start");
printMaze(maze,n);
while(move(maze,n))
{System.out.println("Step "+step);
printMaze(maze,n);
step++;
}
System.out.println("Program end because of boundary limits.");
}
public static boolean move (char m[][],int n)
{int move;
int imove[]={-1,1,0,0};
int jmove[]={0,0,-1,1};
Random r=new Random();
boolean good=false;
do
{move=r.nextInt(4);
if(!checkBound(move,n))
return false;
}while(!checkFill(move,m));
setLoc(move,m);
return true;
}
public static void setLoc(int move,char m[][])
{int imove[]={-1,1,0,0};
int jmove[]={0,0,-1,1};
int newI,newJ;
iloc+=imove[move];
jloc+=jmove[move];
m[iloc][jloc]='X';
}
public static boolean checkBound(int move,int n)
{int imove[]={-1,1,0,0};
int jmove[]={0,0,-1,1};
int newI,newJ;
newI=iloc+imove[move];
newJ=jloc+jmove[move];
if(newI<0||newJ<0||newI>=n||newJ>=n)
return false;
return true;
}
public static boolean checkFill(int move,char m[][])
{int imove[]={-1,1,0,0};
int jmove[]={0,0,-1,1};
int newI,newJ;
newI=iloc+imove[move];
newJ=jloc+jmove[move];
if(m[newI][newJ]=='X')
return false;
return true;
}
public static int getSize()
{int n;
Scanner in = new Scanner(System.in);
System.out.print("Enter an odd number for the size of the matrix: ");
n=in.nextInt();
while(n%2==0)
{System.out.println("Must be odd");
System.out.print("Enter an odd number for the size of the matrix: ");
n=in.nextInt();
}
return n;
}
public static void initMaze(char m[][],int n)
{int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
m[i][j]='O';
m[n/2][n/2]='X';
iloc=n/2;
jloc=n/2;
}
public static void printMaze(char m[][],int n)
{int i,j;
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
System.out.print(m[i][j]+" ");
System.out.println();
}
System.out.println();
}