使用Java編寫的B*算法
package rpg
import java
import java
import java
import rpg
public class BFinding {
public BFinding() {
}
protected HashSet<Point> openList = new HashSet<Point>();
protected HashSet<Point> leftList = new HashSet<Point>();
protected HashSet<Point> rightList = new HashSet<Point>();
protected HashSet<Point> closeList = new HashSet<Point>();
public synchronized ArrayList<int[]> find(Point start
if(end == null){
return new ArrayList<int[]>();
}
if(start == null){
return new ArrayList<int[]>();
}
end
start
openList
openList
leftList
rightList
closeList
int count =
while(!openList
count ++;
if(count>
break;
Iterator<Point> it = erator();
if(it
Point p = it
it
if(sideNext(p
}
it = erator();
if(it
Point p = it
it
if(sideNext(p
}
it = erator();
if(it
Point p = it
it
if(sideNext(p
}
}
final ArrayList<int[]> list = new ArrayList<int[]>();
while(end
list
end = end
}
return list;
}
/**
*
* @param p
* @param end 目標點
* @param side
* @param canPenetrate 可否穿透
*/
protected boolean sideNext(Point p
int dir = Point
Point nextp = null;
if(ntains(p)){
nextp = nextPassPointSide(p
if(nextp != null){
if(nextp == end){
nextp
return true;
}
if(ntains(nextp))
// return sideNext(nextp
return false;
else if(!ntains(nextp))
addToSearch(p
}
nextp = nextPassPointSide(p
if(nextp != null){
if(nextp == end){
nextp
return true;
}
if(ntains(nextp))
// return sideNext(nextp
return false;
else if(!ntains(nextp))
addToSearch(p
}
return false;
}
this
if(side ==
if(p
nextp = p
if(nextp == end){
nextp
return true;
}
if(!ntains(nextp)){
addToSearch(p
}
}
else//不可走
{
nextp = nextPassPointSide(p
if(nextp == end){
nextp
return true;
}
if(nextp != null){
if(ntains(nextp))
return sideNext(nextp
// return false;
else if(!ntains(nextp))
addToSearch(p
}
nextp = nextPassPointSide(p
if(nextp == end){
nextp
return true;
}
if(nextp != null){
if(ntains(nextp))
return sideNext(nextp
// return false;
else if(!ntains(nextp))
addToSearch(p
}
}
}
else if(side>
nextp = p
if(nextp == end){
nextp
return true;
}
if(nextp != null && !ntains(nextp)){
addToSearch(p
}
else
{
nextp = nextPassPointSide(p
if(nextp == end){
nextp
return true;
}
if(nextp != null && !ntains(nextp) && !ntains(nextp)){
addToSearch(p
}
}
}
else if(side<
nextp = p
if(nextp == end){
nextp
return true;
}
if(nextp != null && !ntains(nextp)){
addToSearch(p
}
else
{
nextp = nextPassPointSide(p
if(nextp == end){
nextp
return true;
}
if(nextp != null && !ntains(nextp) && !ntains(nextp)){
addToSearch(p
}
}
}
return false;
}
protected void addToSearch(Point parent
next
next
list
}
/**
*
* @param p
* @param side >
* @param canPenetrate
* @return
*/
protected Point nextPassPointSide(Point p
int dir = Point
Point nextp = null;
if(side<
while(side>=
dir = Point
if(p
nextp = p
if(!ntains(nextp)){
break;
}
}
side
}
}
else
{
while(side<=
dir = Point
if(p
nextp = p
if(!ntains(nextp)){
break;
}
}
side++;
}
}
return nextp;
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26927.html