I have been wanting to create a game for some time and have just recently started trying to get path finding to work. I have come up with code that works once(it will get one tile closer to the goal then stay at that node) but i can not figure out how to get it to go all the way to the goal node. can someone point out the problem area or possible fixes for me?
(my code is too long look in the next post for the rest of it)
package pathfinding;
import java.awt.Graphics;
import java.util.LinkedList;
import mainPackage.Main;
/*
* call this class to find a path between start node and goal node
*/
@SuppressWarnings("serial")
public class PathFinder extends LinkedList {
public LinkedList<Node> openList = new LinkedList<Node>();
public LinkedList<Node>closedList = new LinkedList<Node>();
public LinkedList<Node> pathList = new LinkedList<Node>();
int D = 4;
public Node[][] nodeBoard=new Node[15][15];
private Object lowestF;
public PathFinder(int tilesX, int tilesY) {
for(int y = 0; y < tilesY; y++) {
for(int x = 0; x < tilesX; x++) {
Node n = new Node(x, y, 0, 0);
nodeBoard[x][y] = n;
//System.out.println("nodeBoard["+x+"]["+y+"]: "+n);
}
}
}
public void findPath(Node startNode, Node goalNode) {
System.out.println("started pathfinding ");
Node n = startNode;
Node gn = goalNode;
Node lowestF = null;
if(openList.contains(n) == false)
openList.add(n);//add the current node to the open list
//for(int time = 0; time < 10; time++) {//used for testing purposes
//while(closedList.contains(goalNode) != true) {//while the goal node isnt on the closed list
while(lowestF != goalNode) {
//System.out.println("started the while loop gn x:"+gn.X+" gn y:"+gn.Y);
//System.out.println("n X:"+n.X+" n Y:"+ n.Y);
if(closedList.contains(gn) == false) {
//System.out.println("closedList does not contain goalNode");
}
if(lowestF != null) {//if it isnt the first time doing the loop
n = lowestF;//switch lowestF to the current node (n)
}
lowestF = null;// the node with the lowest f score
//System.out.println(""+closedList.contains(nodeBoard[n.X-1][n.Y-1]));
System.out.println("printing n "+n);