Moslty confused with assigning the 4 indexes of neighbor, with North = neighbor[
ID: 3704658 • Letter: M
Question
Moslty confused with assigning the 4 indexes of neighbor, with North = neighbor[0], east = neighbor[1], south = neighbor[2], west = neighbor[3]
Everything needed to answer the question is below...need help writing the loadMazeRandom() method...just assume my code for the Cell class is correct
Cell | FIELDS You may apply package access on the fields. If you make them private define getters and setters as needed row: int column: int 1t tart ih ow and colum index of the cell when changed to true when the cell has been visited in the search process visited: boolean An array of length 4 to store the accessible neighbors of this cell. The north, east, south and west neighbors are stored at index 0, 1, 2, 3 in order. neighbor[k] is null if that neighbor does nc exists or there is no passage from this cell to neighbor[k] neighbor: Cell ]Explanation / Answer
Find the below code for the Solution
import java.util.Scanner;
public class MazeBuilder {
private double p;
public MazeBuilder() {
// TODO Auto-generated constructor stub
p = Math.random();
}
Cell[][] loadMazeBuilder() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of rows in the grid.");
int rows = sc.nextInt();
System.out.println("Enter Number of columns in the grid.");
int columns = sc.nextInt();
System.out
.println("Enter the probability of choosing a neighbour(between 0 and 1");
double p = sc.nextDouble();
Cell[][] grid = new Cell[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
grid[i][j] = new Cell(i, j);
if ((i - 1) >= 0) {
grid[i][j].neighbours[0] = (Math.random() < p) ? grid[i - 1][j]
: null;
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if ((i - 1) >= 0)
grid[i][j].neighbours[0] = (Math.random() < p) ? grid[i - 1][j]
: null;
if (j + 1 < columns)
grid[i][j].neighbours[1] = (Math.random() < p) ? grid[i][j + 1]
: null;
if ((i + 1) < rows)
grid[i][j].neighbours[2] = (Math.random() < p) ? grid[i + 1][j]
: null;
if (j - 1 >= 0)
grid[i][j].neighbours[3] = (Math.random() < p) ? grid[i][j - 1]
: null;
}
}
return grid;
}
}
class Cell {
private int row;
private int column;
private boolean visited;
protected Cell[] neighbours;
private int lastOut;
public Cell(int row, int column) {
// TODO Auto-generated constructor stub
this.row = row;
this.column = column;
visited = false;
neighbours = new Cell[4];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.