getCost() / STRAIGHT_DISTANCE;
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result
+ ((position == null) 0 : position.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PositionWeight other = (PositionWeight) obj;
if (position == null)
{
if (other.position != null)
return false;
}
else
if (!position.equals(other.position))
return false;
return true;
}
@Override
public String toString()
{
return "PositionWeight [position=" + position + ", distanceOfPrince="
+ distanceOfPrince + ", distanceOfPrincess="
+ distanceOfPrincess + ", father=" + father.getPosition()
+ ", cost=" + cost + "]";
}
/**
* 设置到目标节点的距离
*/
private void countDistanceToTarget(PositionWeight target)
{
Position targetPosition = target.getPosition();
int distanceToTarget = getPosition()
.getDistanceOfTarget(targetPosition);
setDistanceOfPrincess(distanceToTarget);
}
private void setDistanceOfPrince(int distanceOfPrince)
{
this.distanceOfPrince = distanceOfPrince;
}
private int getDistanceOfPrincess()
{
return distanceOfPrincess;
}
private void setDistanceOfPrincess(int distanceOfPrincess)
{
this.distanceOfPrincess = distanceOfPrincess;
}
private void setFather(PositionWeight father)
{
this.father = father;
}
private void setCost(int cost)
{
this.cost = cost;
}
}
/**
* 位置信息的权值
*/
public class PositionWeight
{
/**
* 水平或者垂直移动一格的距离
*/
private final static int STRAIGHT_DISTANCE = 10;
/**
* 当前的位置信息
*/
private Position position;
/**
* 起始点(王子的起始位置),经由当前点的父节点后,到当前点的距离(仅包括垂直和水平直线上的)
*/
private int distanceOfPrince;
/**
* 当前点到目标点(公主位置)的距离
*/
private int distanceOfPrincess;
/**
* 父节点的权值
*/
private PositionWeight father;
/**
* 总开销:包括起始点到当前点和当前点到终点的开销之和
*/
private int cost;
public PositionWeight(Position position)
{
this.position = position;
}
public PositionWeight(Position position, PositionWeight father,
PositionWeight target)
{
this(position);
countDistanceToTarget(target);
updateByFather(father);
}
/**
* 获取父子节点间的距离:对角线为14,水平、垂直为10
*/
public int getDistanceFromAttemptFather(PositionWeight father)
{
Position fatherPosition = father.getPosition();
return fatherPosition.getOffsetOfDistance(getPosition());
}
/**
* 更新父节点,并设置当前点的权值
*/
public void updateByFather(PositionWeight father)
{
setFather(father);
int distanceOfPrince = getDistanceFromAttemptFather(father);
setDistanceOfPrince(distanceOfPrince + father.getDistanceOfPrince());
setCost(getDistanceOfPrince() + getDistanceOfPrincess());
}
public Position getPosition()
{
return position;
}
public PositionWeight getFather()
{
return father;
}
public int getCost()
{
return cost;
}
public int getDistanc