I would say that this part of your code actually is a bit of unnecessary code duplication:
if (((col - 1) >= 0) && (maze[row][col - 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col - 1, set);
}
if (((row - 1) >= 0) && (maze[row - 1][col] == 1) && !getMaze) {
getMaze = getMazePath(row - 1, col, set);
}
if (((col + 1) < maze[0].length) && (maze[row][col + 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col + 1, set);
}
if (((row + 1) < maze.length) && (maze[row + 1][col] == 1) && !getMaze) {
getMaze = getMazePath(row + 1, col, set);
}
I think that you could use a direction enum direction enum, loop through the directions and then for each direction you can check:
- Is the coordinate in the current direction within the bounds? (You can use one method to check all four bounds)
- If it is, call
getMazePath
for then new coordinate.
Using the same Direction4
enum as in the linked answer above,
public enum Direction4 {
NORTH(0, -1), EAST(1, 0), SOUTH(0, 1), WEST(-1, 0);
private Direction4(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
public int getX() { return dx; }
public int getY() { return dy; }
}
You can replace the code snippet with:
for (Direction4 dir : Direction4.values()) {
int newRow = row + dir.getY();
int newCol = col + dir.getX();
if (!getMaze && isInBounds(newRow, newCol)) {
getMaze = getMazePath(newRow, newCol, set);
}
}
boolean isInBounds(int row, int col) {
return row >= 0 && col >= 0 && row < maze.length && col < maze[0].length;
}
I would say that this part of your code actually is a bit of unnecessary code duplication:
if (((col - 1) >= 0) && (maze[row][col - 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col - 1, set);
}
if (((row - 1) >= 0) && (maze[row - 1][col] == 1) && !getMaze) {
getMaze = getMazePath(row - 1, col, set);
}
if (((col + 1) < maze[0].length) && (maze[row][col + 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col + 1, set);
}
if (((row + 1) < maze.length) && (maze[row + 1][col] == 1) && !getMaze) {
getMaze = getMazePath(row + 1, col, set);
}
I think that you could use a direction enum, loop through the directions and then for each direction you can check:
- Is the coordinate in the current direction within the bounds? (You can use one method to check all four bounds)
- If it is, call
getMazePath
for then new coordinate.
Using the same Direction4
enum as in the linked answer above,
public enum Direction4 {
NORTH(0, -1), EAST(1, 0), SOUTH(0, 1), WEST(-1, 0);
private Direction4(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
public int getX() { return dx; }
public int getY() { return dy; }
}
You can replace the code snippet with:
for (Direction4 dir : Direction4.values()) {
int newRow = row + dir.getY();
int newCol = col + dir.getX();
if (!getMaze && isInBounds(newRow, newCol)) {
getMaze = getMazePath(newRow, newCol, set);
}
}
boolean isInBounds(int row, int col) {
return row >= 0 && col >= 0 && row < maze.length && col < maze[0].length;
}
I would say that this part of your code actually is a bit of unnecessary code duplication:
if (((col - 1) >= 0) && (maze[row][col - 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col - 1, set);
}
if (((row - 1) >= 0) && (maze[row - 1][col] == 1) && !getMaze) {
getMaze = getMazePath(row - 1, col, set);
}
if (((col + 1) < maze[0].length) && (maze[row][col + 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col + 1, set);
}
if (((row + 1) < maze.length) && (maze[row + 1][col] == 1) && !getMaze) {
getMaze = getMazePath(row + 1, col, set);
}
I think that you could use a direction enum, loop through the directions and then for each direction you can check:
- Is the coordinate in the current direction within the bounds? (You can use one method to check all four bounds)
- If it is, call
getMazePath
for then new coordinate.
Using the same Direction4
enum as in the linked answer above,
public enum Direction4 {
NORTH(0, -1), EAST(1, 0), SOUTH(0, 1), WEST(-1, 0);
private Direction4(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
public int getX() { return dx; }
public int getY() { return dy; }
}
You can replace the code snippet with:
for (Direction4 dir : Direction4.values()) {
int newRow = row + dir.getY();
int newCol = col + dir.getX();
if (!getMaze && isInBounds(newRow, newCol)) {
getMaze = getMazePath(newRow, newCol, set);
}
}
boolean isInBounds(int row, int col) {
return row >= 0 && col >= 0 && row < maze.length && col < maze[0].length;
}
I would say that this part of your code actually is a bit of unnecessary code duplication:
if (((col - 1) >= 0) && (maze[row][col - 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col - 1, set);
}
if (((row - 1) >= 0) && (maze[row - 1][col] == 1) && !getMaze) {
getMaze = getMazePath(row - 1, col, set);
}
if (((col + 1) < maze[0].length) && (maze[row][col + 1] == 1) && !getMaze) {
getMaze = getMazePath(row, col + 1, set);
}
if (((row + 1) < maze.length) && (maze[row + 1][col] == 1) && !getMaze) {
getMaze = getMazePath(row + 1, col, set);
}
I think that you could use a direction enum, loop through the directions and then for each direction you can check:
- Is the coordinate in the current direction within the bounds? (You can use one method to check all four bounds)
- If it is, call
getMazePath
for then new coordinate.
Using the same Direction4
enum as in the linked answer above,
public enum Direction4 {
NORTH(0, -1), EAST(1, 0), SOUTH(0, 1), WEST(-1, 0);
private Direction4(int dx, int dy) {
this.dx = dx;
this.dy = dy;
}
public int getX() { return dx; }
public int getY() { return dy; }
}
You can replace the code snippet with:
for (Direction4 dir : Direction4.values()) {
int newRow = row + dir.getY();
int newCol = col + dir.getX();
if (!getMaze && isInBounds(newRow, newCol)) {
getMaze = getMazePath(newRow, newCol, set);
}
}
boolean isInBounds(int row, int col) {
return row >= 0 && col >= 0 && row < maze.length && col < maze[0].length;
}