patternjavaMinor
A-maze-ing maze entry point guessing
Viewed 0 times
entryingpointmazeguessing
Problem
You're a blind adventurer (bad luck eh). To go through the maze that'll lead you to freedom, you must first find the maze's wall, then follow this wall to find the maze's entry. Then, to get an idea about the size of the maze, you want to see how large is the said entry, so you walk until you touch another wall, which is the other border of the entrance. Still following? If not, here's a
1,1,1,1,1,0,0,0,1,1,1,1,1
0,0,0,1,1,0,0,0,1,1,0,0,0
The entry of the maze can be found on the last index of the array, meaning :
In my example, the entrance is found at
If the maze contained an even number of "columns", the entrance could be either the "left" or "right", it doesn't matter.
To find the entrance to my maze, I wrote the function
```
package com.mazr.domain;
import java.awt.Point;
public class Maze {
private boolean[][] content;
public Maze(boolean[][] content) {
this.content = content;
}
public Point guessEntry() throws InvalidMazeException {
int openingBorder, closingBorder;
int xIndex = 0;
final int bottomYIndex = content[0].length-1;
try {
//Find the wall
while (!content[xIndex][bottomYIndex])
xIndex++;
//Follow the wall to the entry
while (content[xIndex][bottomYIndex])
xIndex++;
openingBorder = xIndex;
//Follow the entry until the end of it
while (!content[xIndex][bottomYIndex])
xIndex++;
closingBorder = xIndex;
return new Point((closingBorder - o
boolean[][] that'll show my example :1,1,1,1,1,0,0,0,1,1,1,1,1
0,0,0,1,1,0,0,0,1,1,0,0,0
- Zeros implies there is no wall
- Ones implies there is a wall.
The entry of the maze can be found on the last index of the array, meaning :
maze[x,maze.length-1]In my example, the entrance is found at
[6,1]. As it is a first iteration, the maze is expected to have only one entry, which is valid, which will be situated on the last row of the array. @DoubleDouble already pointed out a flaw of my algorithm in his answer.If the maze contained an even number of "columns", the entrance could be either the "left" or "right", it doesn't matter.
To find the entrance to my maze, I wrote the function
guessEntry, in my Maze class :```
package com.mazr.domain;
import java.awt.Point;
public class Maze {
private boolean[][] content;
public Maze(boolean[][] content) {
this.content = content;
}
public Point guessEntry() throws InvalidMazeException {
int openingBorder, closingBorder;
int xIndex = 0;
final int bottomYIndex = content[0].length-1;
try {
//Find the wall
while (!content[xIndex][bottomYIndex])
xIndex++;
//Follow the wall to the entry
while (content[xIndex][bottomYIndex])
xIndex++;
openingBorder = xIndex;
//Follow the entry until the end of it
while (!content[xIndex][bottomYIndex])
xIndex++;
closingBorder = xIndex;
return new Point((closingBorder - o
Solution
I believe the
You could use a
One other thing I'd like to mention: if your input is always going to be similar to your example you have nothing to worry about, but your code might not work correctly on the following examples:
Extra line of blank space:
1,1,1,1,1,0,0,0,1,1,1,1,1
0,0,0,1,1,0,0,0,1,1,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0
No wall separator before the entrance:
1,1,1,1,1,0,0,0,1,1,1,1,1
0,0,0,0,0,0,0,0,1,1,0,0,0
No wall separator before the entrance #2:
0,0,0,1,1,1,1,1,1,1,1,1,1
0,0,0,1,1,1,1,1,1,1,1,1,1
You'd catch the
while() loops are the best way to accomplish your task with how your code is now, so I'm not sure why you "can't stand" them.You could use a
for() loop, and break out of it using an if statement to check the same thing that your while loops are doing, but I would prefer the while loops myself.One other thing I'd like to mention: if your input is always going to be similar to your example you have nothing to worry about, but your code might not work correctly on the following examples:
Extra line of blank space:
1,1,1,1,1,0,0,0,1,1,1,1,1
0,0,0,1,1,0,0,0,1,1,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0
No wall separator before the entrance:
1,1,1,1,1,0,0,0,1,1,1,1,1
0,0,0,0,0,0,0,0,1,1,0,0,0
No wall separator before the entrance #2:
0,0,0,1,1,1,1,1,1,1,1,1,1
0,0,0,1,1,1,1,1,1,1,1,1,1
You'd catch the
ArrayIndexOutOfBounds exception as an "unguessable entry", which I assume is the intentional response - but thought I'd put this here just in case.Context
StackExchange Code Review Q#106079, answer score: 3
Revisions (0)
No revisions yet.