📄 node.java
字号:
import java.util.ArrayList;
public class Node implements Comparable{
// STATE: the state in the state space to which the node corresponds;
private HexpuzzleItems state;
// PARENT-NODE: the node in the search tree that generated this node;
private Node parent;
// DEPTH: the number of steps along the path from the initial state.
private int depth;
Node(HexpuzzleItems state) {
this.state = state;
this.depth = 0;
}
Node(Node parent, HexpuzzleItems state) {
this(state);
this.parent = parent;
this.depth = parent.getDepth() + 1;
}
public int getDepth() {
return depth;
}
public int getCost()
{
//evaluate the current state's cost
//f(n) = g(n) + h(n)
return getDepth() + state.getHeuristicCost();
}
public boolean goalTest()
{
return (this.getState()).goalTest();
}
public ArrayList<Node> successor()
{
ArrayList<Node> nodeList = new ArrayList<Node>();
if(nodeList == null)
return null;
Item blankItem = state.findBlankItem();
if(blankItem != null)
{
ArrayList<Item> itemList = blankItem.getAdjacentItems();
for(int i = 0; i < itemList.size(); i++)
{
Item item = itemList.get(i);
HexpuzzleItems newPuzzleItem = new HexpuzzleItems(state);
if(item != null && newPuzzleItem != null)
{
newPuzzleItem.swapItems(item, blankItem);
//create successor Node
Node newNode = new Node(this, newPuzzleItem);
nodeList.add(newNode);
}
}
}
return nodeList;
}
public boolean isRootNode() {
return parent == null;
}
public Node getParent() {
return parent;
}
public HexpuzzleItems getState() {
return state;
}
public String toString() {
return getState().toString();
}
public int compareTo(Object obj) {
// TODO Auto-generated method stub
Node temp = (Node)obj;
if(this.getCost() == temp.getCost())
{
return 0;
}
else if(this.getCost() > temp.getCost())
{
return 1;
}
else
{
return -1;
}
}
public boolean equals(Object obj)
{
Node temp = (Node)obj;
if(this.getState().equals(temp.getState()))
return false;
else
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -