Android Open Source - TheFirstMyth02 A Star






From Project

Back to project page TheFirstMyth02.

License

The source code is released under:

MIT License

If you think the Android project TheFirstMyth02 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.game.commen;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/* www  . ja va  2s  .  c  om*/

//A*??
public class AStar {
    private int[][] map;//??(1????? 0???????)
    private List<Node> openList;//??????
    private List<Node> closeList;//????
    private final int COST_STRAIGHT = 10;//????????????????????
    private final int COST_DIAGONAL = 14;//????????????
    private int row;//?
    private int column;//?
    
    public int maps[] = new int[12*2];
    
    public AStar(int[][] map,int row,int column){
        this.map=map;
        this.row=row;
        this.column=column;
        openList=new ArrayList<Node>();
        closeList=new ArrayList<Node>();
    }
    
    //???????-1????0?????1?????
    public int search(int x1,int y1,int x2,int y2){
        if(x1<0||x1>=row||x2<0||x2>=row||y1<0||y1>=column||y2<0||y2>=column){
            return -1;
        }
        if(map[x1][y1]==0||map[x2][y2]==0){
            return -1;
        }
        Node sNode=new Node(x1,y1,null);
        Node eNode=new Node(x2,y2,null);
        openList.add(sNode);
        List<Node> resultList=search(sNode, eNode);
        if(resultList.size()==0){
            return 0;
        }
        int c=0;
        for(Node node:resultList){
            map[node.getX()][node.getY()]=-1;
            maps[c] = node.getX();
            maps[c+1] = node.getY();
            c+=2;
        }
       
        return 1;
    }
    
    //??????
    private List<Node> search(Node sNode,Node eNode){
        List<Node> resultList=new ArrayList<Node>();
        boolean isFind=false;
        Node node=null;
        while(openList.size()>0){
            //?????????????F?????????????F????
            node=openList.get(0);
            //???????????
            if(node.getX()==eNode.getX()&&node.getY()==eNode.getY()){
                isFind=true;
                break;
            }
            //?
            if((node.getY()-1)>=0){
                checkPath(node.getX(),node.getY()-1,node, eNode, COST_STRAIGHT);
            }
            //?
            if((node.getY()+1)<column){
                checkPath(node.getX(),node.getY()+1,node, eNode, COST_STRAIGHT);
            }
            //?
            if((node.getX()-1)>=0){
                checkPath(node.getX()-1,node.getY(),node, eNode, COST_STRAIGHT);
            }
            //???
            if((node.getX()+1)<row){
                checkPath(node.getX()+1,node.getY(),node, eNode, COST_STRAIGHT);
            }
//            //??
//            if((node.getX()-1)>=0&&(node.getY()-1)>=0){
//                checkPath(node.getX()-1,node.getY()-1,node, eNode, COST_DIAGONAL);
//            }
//            //??
//            if((node.getX()-1)>=0&&(node.getY()+1)<column){
//                checkPath(node.getX()-1,node.getY()+1,node, eNode, COST_DIAGONAL);
//            }
//            //????
//            if((node.getX()+1)<row&&(node.getY()-1)>=0){
//                checkPath(node.getX()+1,node.getY()-1,node, eNode, COST_DIAGONAL);
//            }
//            //????
//            if((node.getX()+1)<row&&(node.getY()+1)<column){
//                checkPath(node.getX()+1,node.getY()+1,node, eNode, COST_DIAGONAL);
//            }
            //??????????
            //????????
            closeList.add(openList.remove(0));
            //????????????F?????????
            Collections.sort(openList, new NodeFComparator());
        }
        if(isFind){
            getPath(resultList, node);
        }
        return resultList;
    }
    
    //???????????
    private boolean checkPath(int x,int y,Node parentNode,Node eNode,int cost){
        Node node=new Node(x, y, parentNode);
        //????????????
        if(map[x][y]==0){
            closeList.add(node);
            return false;
        }
        //?????????????
        if(isListContains(closeList, x, y)!=-1){
            return false;
        }
        //???????????????
        int index=-1;
        if((index=isListContains(openList, x, y))!=-1){
            //G??????????????????G?F?
            if((parentNode.getG()+cost)<openList.get(index).getG()){
                node.setParentNode(parentNode);
                countG(node, eNode, cost);
                countF(node);
                openList.set(index, node);
            }
        }else{
            //??????????
            node.setParentNode(parentNode);
            count(node, eNode, cost);
            openList.add(node);
        }
        return true;
    }
    
    //??????????????????(-1?????????????????)
    private int isListContains(List<Node> list,int x,int y){
        for(int i=0;i<list.size();i++){
            Node node=list.get(i);
            if(node.getX()==x&&node.getY()==y){
                return i;
            }
        }
        return -1;
    }
    
    //?????????
    private void getPath(List<Node> resultList,Node node){
        if(node.getParentNode()!=null){
            getPath(resultList, node.getParentNode());
        }
        resultList.add(node);
    }
    
    //??G,H,F?
    private void count(Node node,Node eNode,int cost){
        countG(node, eNode, cost);
        countH(node, eNode);
        countF(eNode);
    }
    //??G?
    private void countG(Node node,Node eNode,int cost){
        if(node.getParentNode()==null){
            node.setG(cost);
        }else{
            node.setG(node.getParentNode().getG()+cost);
        }
    }
    //??H?
    private void countH(Node node,Node eNode){
        node.setF(Math.abs(node.getX()-eNode.getX())+Math.abs(node.getY()-eNode.getY()));
    }
    //??F?
    private void countF(Node node){
        node.setF(node.getG()+node.getF());
    }
    
}
//???
class Node {
    private int x;//X????
    private int y;//Y????
    private Node parentNode;//????
    private int g;//????????????
    private int h;//??????????????????????|x1-x2|+|y1-y2|(??????)
    private int f;//f=g+h
    
    public Node(int x,int y,Node parentNode){
        this.x=x;
        this.y=y;
        this.parentNode=parentNode;
    }
    
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public Node getParentNode() {
        return parentNode;
    }
    public void setParentNode(Node parentNode) {
        this.parentNode = parentNode;
    }
    public int getG() {
        return g;
    }
    public void setG(int g) {
        this.g = g;
    }
    public int getH() {
        return h;
    }
    public void setH(int h) {
        this.h = h;
    }
    public int getF() {
        return f;
    }
    public void setF(int f) {
        this.f = f;
    }
}
//?????
class NodeFComparator implements Comparator<Node>{

    public int compare(Node o1, Node o2) {
        return o1.getF()-o2.getF();
    }
    
}




Java Source Code List

.Test.java
com.game.base.BaseInfo.java
com.game.base.GameBackGround.java
com.game.base.GameChapterBaseSet_Shentan.java
com.game.base.GameMap.java
com.game.base.PubSet.java
com.game.commen.AStarMap.java
com.game.commen.AStarNode.java
com.game.commen.AStar.java
com.game.commen.ActionToDo.java
com.game.commen.BitmapTouchChecker.java
com.game.commen.BitmapUtil.java
com.game.commen.Direction.java
com.game.commen.EffectName.java
com.game.commen.GameXmlcommen.java
com.game.commen.GetImgCommen.java
com.game.commen.IrregularButton.java
com.game.commen.MapName.java
com.game.commen.Paintforziti.java
com.game.commen.ToDo.java
com.game.data.BaseInfo.java
com.game.data.RoleData_Main.java
com.game.data.RoleData.java
com.game.data.StroyTipData.java
com.game.effect.SpecialEffect.java
com.game.fengshen.GameActivity.java
com.game.fengshen.GameMainActivity.java
com.game.fengshen.GameView.java
com.game.fengshen.MoveTest.java
com.game.renwu.Objs.java
com.game.renwu.SpiritMain.java
com.game.renwu.Spirit_Main.java
com.game.renwu.Spirit_NPC.java
com.game.renwu.Spirit.java
com.game.renwu.Spiritgirl.java
com.game.stroy.MainStroy.java
com.game.tip.PublicTips.java