/** @return node that contains the path traveled from start to finish */ protected Node mazeSearch() { Node currNode = this.pq.remove(); // found solution if current node is the on FIN if (this.solution[currNode.getX()][currNode.getY()] == FIN) { return currNode; } exploreMoves(currNode); return mazeSearch(); }
/** * explore the neighbor nodes and add them to the queue if they have not been explored * * @param currNode */ protected void exploreMoves(Node currNode) { int x = currNode.getX(); int y = currNode.getY(); // Add all adjacent nodes that are not WALLS to the list // Left boolean l = checkAndAddNodeToList(currNode, x - 1, y); // Up boolean u = checkAndAddNodeToList(currNode, x, y - 1); // Right boolean r = checkAndAddNodeToList(currNode, x + 1, y); // Down boolean d = checkAndAddNodeToList(currNode, x, y + 1); }
/** execute algorithm and create the solution maze matrix */ public void search() { findStartFin(); this.totalCost = 0; this.expands = 0; Node solNode = mazeSearch(); this.solNode = solNode; this.expands -= this.pq.size(); // backtrack from fin to start while (solNode.getParent() != null) { int x = solNode.getX(); int y = solNode.getY(); this.solution[x][y] = FIN; // MARK THE SOLUTION PATH WITH THE START SYMBOL solNode = solNode.getParent(); this.totalCost++; debugSolution(solNode); } }
/** * create a new node and add queue if the new node cost is less than the current cost * * @param currNode * @param x * @param y */ protected boolean checkAndAddNodeToList(Node currNode, int x, int y) { int cost = currNode.getCurrentPathCost() + 1; int h = 0; // heuristic(x,y); if (h + cost < this.maze[x][y]) { this.pq.add(new Node(currNode, x, y, cost, h)); this.maze[x][y] = h + cost; this.expands++; return true; } return false; }