PROCEDURES: You will write a program which creates a 2D array and show the steps
ID: 3631642 • Letter: P
Question
PROCEDURES: You will write a program which creates a 2D array and show the steps of a random walk.PROGRAM: 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 0's 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. Thus, with the printing of each step the trail of the random walk through the matrix cells can be seen
? Your program should call a method that describes the program’s operation. …something similar to …..
This program builds a 2D square matrix from a
positive odd number which is entered. The
input number defines the matrix in terms of
rows and columns. The program initially fills
the square matrix with zeros and also puts an X
in the center cell. The program then begins a
random walk through cells without retracing
any steps. Each step is denoted by replacing
a cell's initial value of '0' with an 'X'. The
pattern of replacing the contents of cells
continues for each of the matrix cells that are in
the walk. The program ends when the next step
is outside the dimensions of the matrix.
? As a minimum your program should have several methods. For example methods should be created for filling and printing the matrix, making the next step, and for various tests if the cell is legal or not.
Examples of a sample input and output are given below
Enter an odd number for the size of the matrix: 5
Matrix start....
0 0 0 0 0
0 0 0 0 0
0 0 X 0 0
0 0 0 0 0
0 0 0 0 0
Step 1
0 0 0 0 0
0 0 X 0 0
0 0 X 0 0
0 0 0 0 0
0 0 0 0 0
Step 2
0 0 X 0 0
0 0 X 0 0
0 0 X 0 0
0 0 0 0 0
0 0 0 0 0
Step 3
0 X X 0 0
0 0 X 0 0
0 0 X 0 0
0 0 0 0 0
0 0 0 0 0
Step 4
X X X 0 0
0 0 X 0 0
0 0 X 0 0
0 0 0 0 0
0 0 0 0 0
Step 5
X X X 0 0
X 0 X 0 0
0 0 X 0 0
0 0 0 0 0
0 0 0 0 0
Step 6
X X X 0 0
X 0 X 0 0
X 0 X 0 0
0 0 0 0 0
0 0 0 0 0
Step 7
X X X 0 0
X 0 X 0 0
X 0 X 0 0
X 0 0 0 0
0 0 0 0 0
Program end because of boundary limits.
Enter an odd number for the size of the matrix: 7 Matrix start....
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 X 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Step 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 X 0 0 0
0 0 0 X 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
….? steps 2 – 4 not shown
Step 5
0 0 0 0 0 0 0
0 0 0 0 0 X X
0 0 0 X X X 0
0 0 0 X 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
….? steps 6 – 7 not shown
Step 8
0 0 0 0 X X X
0 0 0 0 0 X X
0 0 0 X X X 0
0 0 0 X 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Program end because of boundary limits.
Enter an odd number for the size of the matrix: 3
Matrix start....
0 0 0
0 X 0
0 0 0
Step 1
0 0 0
0 X X
0 0 0
Step 2
0 0 X
0 X X
0 0 0
Program end because of boundary limits.
Program is to be written in Java
I appreciate any and all help. Thnak you
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();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.