Here's a picture displaying the current state of my program (bugged, and inaccurately displayed) :

I have set Square 1 as my starting point, and Square 9 as my finishing point. From Square 1 to Square 9, I set the default colors to different hues of dark red. From Square 1 to Square 3, those are the squares added into the "best" array list selected out, and marked as white to distinguish as the "best path" from starting point to finishing point.

I expected to have Square 1, Square 2, Square 5, Square 6, and Square 9 (Or 1, 4, 5, 8, and 9) be marked white, and all three of the squares are added to the "best" array list. The picture above is not what I wanted.

I'm asking for help in determining where my bug occurred, where the "best" path is unabled to be generated correctly. One major problem is that my source code is pretty huge when considering how jumpy my code is.

Currently, I'm reading through my codes to try and figure out where it's not working.

Source code, place where I suspect it's where the bug is located at. I probably narrowed it down quite a lot:

package core; import java.util.ArrayList; import java.util.List; import java.util.Set; public class Grid { public List<Square> grids = new ArrayList<Square>(); public List<Square> exists = new ArrayList<Square>(); public int size = 9; public int width; public int height; public Square target = new Square(0, this); // ======================================================= public int rows = 0; public int columns = 0; public Square goal; public Square[][] squares; public List<Square> opened = new ArrayList<Square>(); public List<Square> closed = new ArrayList<Square>(); public List<Square> best = new ArrayList<Square>(); int xStart = 0; int yStart = 0; int xFinish = 2; int yFinish = 2; public Grid(int rows, int columns) { this.rows = rows; this.columns = columns; this.squares = new Square[rows][columns]; int count = 0; for(int j = 0; j < rows; j++) { for(int i = 0; i < columns; i++) { this.squares[j][i] = new Square(count++, this); this.squares[j][i].setCoordinate(j, i); } } squares[xStart][yStart].setFlag(Square.START); squares[xFinish][yFinish].setFlag(Square.FINISH); this.goal = squares[xFinish][yFinish]; for(int ro = 0; ro < squares.length; ro++) for(int c = 0; c < squares[ro].length; c++) squares[ro][c].checkAdjacencies(); } public void findingPath() { Set<Square> adjacSet = squares[xStart][yStart].adjacencies; for(Square adjacent : adjacSet) { adjacent.parent = squares[xStart][yStart]; if(adjacent.flag != Square.START) opened.add(adjacent); } } public Square findBestPath() { Square best = null; Square goal = null; for(int i = 0; i < squares.length; i++) for(int j = 0; j < squares[i].length; j++) if(squares[i][j].flag == Square.FINISH) goal = squares[i][j]; for(Square square : opened) { if(best == null || square.getCost(goal) < best.getCost(goal)) { best = square; } } return best; } // ============================================ private void populateBestList(Square square) { best.add(square); if(square.parent.flag != Square.START) populateBestList(square.parent); return; } boolean testFlag = false; public void tick() { if(testFlag == false) { findingPath(); testFlag = true; } if(opened.size() > 0) { Square best = findBestPath(); opened.remove(best); closed.add(best); if(best.flag == Square.FINISH) { populateBestList(goal); return; } else { Set<Square> neighbors = best.adjacencies; for(Square neighbor : neighbors) { if(opened.contains(neighbor)) { Square temp = new Square(neighbor.id, this); temp.setCoordinate(neighbor.x, neighbor.y); temp.parent = best; if(temp.getCost(goal) >= neighbor.getCost(goal)) continue; } if(closed.contains(neighbor)) { Square temp = new Square(neighbor.id, this); temp.setCoordinate(neighbor.x, neighbor.y); temp.parent = best; if(temp.getCost(goal) >= neighbor.getCost(goal)) continue; } neighbor.parent = best; opened.remove(neighbor); closed.remove(neighbor); opened.add(0, neighbor); } } } if(opened.size() <= 0) { Square temp = null; for(Square t : best) if(t.flag == Square.FINISH) temp = t; while(temp != null) { temp = temp.parent; } } } public void render(int[] pixels) { for(Square object : closed) { pixels[object.x + object.y * width] = object.color; } } }

Thanks for your help in reading this thread in advance.