I’m implementing NxN puzzle in 2D array in Java. I have the following classes:
public class Node {
//private members
private int boardSize;
private int row, col;
int state[][] = new int[][]{}; //the state of a node
// the total cost from root node to current node
private int pathCost;
// this is the heuristic cost from the current node to the goal node
private int heuristicCost;
// functionCost = pathCost + heuristicCost
private int funcitonCost;
// parent of the current node
private Node parentNode;
......
// I have here all accessor functions and functions that return x and y cordinates when a //number in the array is given.
}
public class A*Algo {
private int[][] goalNode ={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};
private NodeComparator nodeComparator = new NodeComparator();
private PriorityQueue<Node> openList = null; // open list
private PriorityQueue<Node> closedList = null; // closed list
private int steps = 0;
private int BOARDSIZE;
// constructor
public AStar(Node startNode, int boardSize){
//this.node = new Node(tiles, null, boardSize);
//this.tiles = tiles;
this.BOARDSIZE = boardSize;
/* this.succesorNodes = new FifoNodeStore();
this.fringeNodes = new FifoNodeStore();*/
this.openList = new PriorityQueue<Node>(0, nodeComparator);
this.closedList = new PriorityQueue<Node>(0, nodeComparator);
startNode.setParentNode(null);
startNode.setPathCost(0);
// PROBLEM :::: goalNode must be a Node and not int[][]
// How can i represent the goal node?
startNode.setHeuristicCost(manhattan(startNode, goalNode));
this.addToOpenList(startNode);
this.search(startNode);
}
public int manhattan(Node currentNode, Node goalNode) {
return Math.abs(currentNode.x - goalNode.x) + Math.abs(currentNode.y - goalNode.y);
}
}
I have the following two problems:
1)
How should I represent the goal Node?
In the second class I declare the goal node as int[][] but I want it to be a node so that I can give it to the manhattan function.
2)
In the Node class, I have a state int state[][] which represent the state of the node. Now my problem is how do i get access to the individual coordinates in the state in the nodes. Assuming the goalNode declaration is correct then I must be able to call the manhattan like this:
manhattan(startNode, goalNode)
to calculate from the current node to the goal node.
Edit
I am required to use the manhattan heuristic in the following way:
the sum of the vertical and horizontal distances from
the current node to the goal node/tile
+(plus)
the number of moves to reach the goal node from the initial position
1) You can represent the goal node as
Node goalNode. You can have a constructor/function/property in you Node class to set the state of the node.something like this:
2) If I understand you correctly you are looking for something like this: