在j2me平台用A*实现自动寻路

 package tools;
import java.util.Vector;

import main.Map;
import main.Node;

/*
 * 看不明白的话发邮箱到  alex917@live.cn   或者叫  qq:376313600
 */

public class A {
 
 public A(){
  
 }
  public Vector v;
  
  
  Vector chkList = new Vector();
  Vector openList = new Vector();
  public static byte[][] map ;
  
  public static void init_MapData(byte[] bs ,int col,int row){
   map=null;
   map=new byte[col][row];
   for(int i=0;i<col;i++){
    for(int j=0;j<row;j++){
     map[j][i]=bs[i*col+j];
    }
   }
  }

  public Vector getPath (int x0,int y0,int x1,int y1){

   System.out.println("getPath()");
   int add=0;
   this.openList=null;
   this.openList=new Vector();
   this.chkList=null;
   this.chkList=new Vector();
   
   Vector tp = new Vector();
   try{
    
    Vector open = this.openList;
    byte[][] map = this.map;
    
    if(map[x0][y0]>=Map.start_canotpass || map[x1][y1]>=Map.start_canotpass){
     return tp;
    }
    int _sh = getH(x0,y0,x1,y1);
    open.addElement(new Node(_sh,0,_sh,x0,y0,null));
    int oll=0;
    Node nowNode=null;
    
    while(0<(oll=open.size())){
     add++;
     Node maxNode;
     int minIndex=0;
     int minf=10000000;
     
     
     for(int i=0;i<oll;i++){
      maxNode = ((Node)open.elementAt(i));
      if(minf>maxNode.f){
       minf = maxNode.f;
       minIndex = i;
       
      }
     }
     
     nowNode =(Node) open.elementAt(minIndex);
     open.removeElementAt(minIndex);
     chkList.addElement(nowNode);
     

     if(nowNode.x==x1 && nowNode.y==y1){
      while(nowNode.p!=null){
       tp.addElement(nowNode);
       nowNode = nowNode.p;
      }
      tp.addElement(nowNode);
      break;
     }
     updateNode(nowNode,x1,y1);
     
     
    }
    
   }catch(Exception e){
    e.printStackTrace();
    return null;
   }
   System.out.println("不能到达的地点");
    return tp;
   
  }
  private int getG(int x0,int y0,int x1,int y1){
   if(Math.abs(x0-x1)==1 && Math.abs(y0-y1)==1){
    return 14;
   }else{
    return 10;
   }
  }
  private int getH(int x0,int y0,int x1,int y1){
   int xDistance = Math.abs(x0-x1);
   int yDistance = Math.abs(y0-y1);
   if(xDistance > yDistance){
    return (14*yDistance + 10*(xDistance-yDistance));
   }else{
    return (14*xDistance + 10*(yDistance-xDistance));
   }
  }
  
  
  private void updateNode (Node _node,int x1,int y1){
   System.out.println("setNode()");
    byte[][] map = this.map;
    int x = _node.x;
    int y = _node.y;
    int g = _node.g;
    int t = x-1;
    int b = x+1;
    int l = y-1;
    int r = y+1;
    int maxX = map.length;
    int maxY = map[0].length;
    
    if(t>=0 && map[t][y]<Map.start_canotpass) {
     this.chkNode(_node,x,y,g,x-1,y,x1,y1);//2
    }else{
     System.out.println("不能到达的地点 2");
    }
    
    if(l>=0 && map[x][l]<Map.start_canotpass){
     this.chkNode(_node,x,y,g,x,y-1,x1,y1);//4
    }else{
     System.out.println("不能到达的地点 4");
    }
    if(r<maxY && map[x][r]<Map.start_canotpass) {
     this.chkNode(_node,x,y,g,x,y+1,x1,y1);//6
    }else{
     
     System.out.println("不能到达的地点 6");
    }
    
    if(b<maxX && map[b][y]<Map.start_canotpass){
     this.chkNode(_node,x,y,g,x+1,y,x1,y1);//8
    }else{
     System.out.println("不能到达的地点 8");
    }
    
 
  }
  private void chkNode (Node _fnode,int _x0,int _y0,int _g,int _x1,int _y1,int _x2,int _y2){
    
    Vector open = this.openList;
    Vector chk = this.chkList;
    Node _Node = null;
    
    if(chk.size()>0){
     
     Node p;
     for(int i=0;i<chk.size();i++){
      p=(Node)chk.elementAt(i);
      if(_x1==p.x&&_y1==p.y){
       _Node=p;
       return;
      }
     }
     
    }
    if(open.size()>0){
     Node p;
     for(int i=0;i<open.size();i++){
      p=(Node)open.elementAt(i);
      if(_x1==p.x&&_y1==p.y){
       _Node=p;
       break;
      }
     }
     
    }
    
    
    if(_Node==null){
     
     _Node = new Node(0,0,0,_x1,_y1,_fnode);
     
     open.addElement(_Node) ;
     
     chk.addElement(_Node);
     
     _Node.g = _g + this.getG(_x0,_y0,_x1,_y1);
     _Node.h = this.getH(_x1,_y1,_x2,_y2);  
     _Node.f = _Node.g + _Node.h;
     
    }else{
    
     int _CNG = _g + this.getG(_x0,_y0,_x1,_y1);
    
     if(_Node.g>_CNG){
      _Node.g = _CNG;
      _Node.f = _Node.g + _Node.h;
      _Node.p = _fnode;
     }
    
    }
   
  }
  


 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值