You should treat each Point as a Node that keeps track of its ancestor and children. A child is added to the list if it is not the current nodes parent and its value == 'O' Start by adding your starting node to the Queue. Pop it out and mark it visited. Add all of its children to the Queue pop them out and mark them visited. Repeat this proccess until your Queue is empty or you have found a Node whose value is 'F'
Children can be found by considering its neighboring indices and evaluating three things:
- Is this neighboring index the parent? If yes, do not add to children
- Are the neighboring indices within the bounds of the maze? To avoid ArrayIndexOutofBounds exceptions
Does the value at this node equal 'O' or 'F'. If 'O' add to children list and to queue. If 'F' you are finished.
public class Point { int i; int j; char value; Point parent; ArrayList<Point> children; public static final maze[][]; public Point(int i, int j, char val) { this.i = i; this.j = j; this.val = val parent = null; children = new ArrayList<Point>(); } public void generateChildren() { if (i + 1 <= mazeHeight && j + 1 <= mazeWidth) { if (maze[i+1][j+1] == 'O') { if (!maze[i+1][j+1].equals[this.parent]) { Point child = new Child(i+1, j+1, 'O'); child.parent = this; children.add(child); } } } // ADD LOGIC FOR OTHER CASES i -1 j -1 etc ... } }
Something like this. Start my making a new Point with your Start and add it to the queue then generate its children get the children back and add them to the queue. Add logic to check if equals 'F'