package AI; import java.util.ArrayList; import Character.Character; import main.GamePanel; public class pathFinder { GamePanel gp; Node[][] node; ArrayList openList = new ArrayList<>(); public ArrayList pathList = new ArrayList<>(); Node startNode, goalNode, currentNode; boolean goalReached = false; int step = 0; public pathFinder(GamePanel gp) { this.gp = gp; initializingNode(); } public void initializingNode() { node = new Node[gp.maxScreenCol][gp.maxScreenRow]; int col =0 ; int row = 0; while(col< gp.maxScreenCol && row= 0 ) { openNode(node[col][row-1]); } if(col -1 >= 0 ) { openNode(node[col-1][row]); } if(row +1 < gp.maxScreenRow ) { openNode(node[col][row+1]); } if(col +1 < gp.maxScreenCol) { openNode(node[col+1][row]); } //find the best node int bestNodeindex = 0; int bestNodefCost = 999; for (int i = 0; i < openList.size(); i++ ) { if(openList.get(i).fcost < bestNodefCost) { bestNodeindex = i; bestNodefCost = openList.get(i).fcost; } else if(openList.get(i).fcost == bestNodefCost) { if(openList.get(i).gcost < openList.get(bestNodeindex).gcost) { bestNodeindex = i; } } } //IF NO NODE, THEN WE END if(openList.size() == 0) { break; } currentNode = openList.get(bestNodeindex); if(currentNode == goalNode) { goalReached = true; trackPath(); } step++; } return goalReached; } public void openNode(Node node) { if(node.open == false && node.checked == false && node.solid == false) { node.open = true; node.parent = currentNode; openList.add(node); } } public void trackPath() { Node current = goalNode; while(current != startNode) { pathList.add(0, current); current = current.parent; } } }