算法-迷宫生成

package inkball.model;

public class Map {


    private Grid [][] grids;
    private int row;
    private int col;
    private int[][] autoPathTime;
    private Grid last;
    private int  mapSize;
    private int  imageSize;




    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }


    
    public Map(int row, int col,int mapSize,int imageSize) {
        this.mapSize = mapSize;
        this.imageSize = imageSize;
        this.row = row;
        this.col = col;
        grids = new Grid[row][col];
        autoPathTime = new int[row][col];
        int i,j;
        for (i = 0; i < row; i++) {
            for (j = 0; j < col; j++) {
                grids[i][j]=new Grid(j*mapSize+mapSize/2,i*mapSize+mapSize/2,i,j);
                autoPathTime[i][j]=0;
            }
        }
        last=new Grid(mapSize,mapSize,row,col);
        for (i = 1; i < col-5; i++) {
            pathByStart(1,i);
            pathByStart(row-5,i);
        }
        for (i = 1; i < row-5; i++) {
            pathByStart(i,1);
            pathByStart(i,col-5);
        }
    }

    public  boolean ifWall(int i, int j) {
        if(i<0 || j<0 || i>=row || j>=col)
            return false;
        Grid g=grids[i][j];
        return  g.isWall();
    }



    public void setPath(int i, int j) {
        grids[i][j].setWall(false);
// outT(i,j);
        autoPathTime[i][j]++;
    }

    public void setBefore(int i,int j) {
        last.setxIndex(i);
        last.setyIndex(j);
    }

    public  boolean ifRange(int i, int j) {
        if(i<=0||j<=0)
            return false;
        if(i>=row-1||j>=col-1)
            return false;
        return true;
    }

    public  boolean ifautoPath(int i, int j) {
//       if(!ifRange(i,j))
//            return true;
        if(!grids[i][j].isWall())
            return true;
        return false;
    }

    public  boolean ifLegal(int i, int j) {


        //避免邹上一步
//        if(last.getxIndex()==i&&last.getyIndex()==j)
//            return false;

//       if(autoPathTime[i][j]>=10)//避免多次重复走
//            return false;


        //TODO 左上  右上  左下  右下
        if(ifautoPath(i,j-1)&&ifautoPath(i-1,j)&&ifautoPath(i-1,j))
            return false;

        if(ifautoPath(i,j+1)&&ifautoPath(i-1,j)&&ifautoPath(i-1,j+1))
            return false;


        if(ifautoPath(i,j-1)&&ifautoPath(i+1,j)&&ifautoPath(i+1,j-1))
            return false;

        if(ifautoPath(i,j+1)&&ifautoPath(i+1,j)&&ifautoPath(i+1,j+1))
            return false;


        return true;//合法
    }

    public void pathByStart(int startXRow, int startYCol) {
        setPath(startXRow,startYCol);
        int num=200000;
        int begin=0;
        while (begin<num){
            begin++;
            int direction=RandomTool.randomNumber(1, 4);
            if(direction==1){ //TODO 上
                if(ifRange(startXRow-1,startYCol)&&ifLegal(startXRow-1,startYCol)){
                    setBefore(startXRow,startYCol);
                    startXRow--;
                    setPath(startXRow,startYCol);
//                          begin++;
                }
            }
            else if(direction==2){ //TODO 下
                if(ifRange(startXRow+1,startYCol)&&ifLegal(startXRow+1,startYCol)){
                    setBefore(startXRow,startYCol);
                    startXRow++;
                    setPath(startXRow,startYCol);
//                    begin++;
                }
            }
            else if(direction==3){//TODO 左
                if(ifRange(startXRow,startYCol-1)&&ifLegal(startXRow,startYCol-1)){
                    setBefore(startXRow,startYCol);
                    startYCol--;
                    setPath(startXRow,startYCol);
//                    begin++;
                }
            }
            else if(direction==4){ //TODO 右
                if(ifRange(startXRow,startYCol+1)&&ifLegal(startXRow,startYCol+1)){
                    setBefore(startXRow,startYCol);
                    startYCol++;

                    setPath(startXRow,startYCol);
//                    begin++;
                }
            }
        }
        int c=2;
    }

    public  void outT(int i, int j) {
        System.out.println("i"+i+" j"+j);
    }



    public void autoPath(){
        int startXRow=1;
        int startYCol=1;
        int num=200000;
        int begin=0;
        while (begin<num){
            begin++;
            int direction=RandomTool.randomNumber(1, 4);
            if(direction==1){ //TODO 上
                    if(ifRange(startXRow-1,startYCol)&&ifLegal(startXRow-1,startYCol)){
                        setBefore(startXRow,startYCol);
                        startXRow--;
                        setPath(startXRow,startYCol);
//                          begin++;
                    }
            }
            else if(direction==2){ //TODO 下
                if(ifRange(startXRow+1,startYCol)&&ifLegal(startXRow+1,startYCol)){
                    setBefore(startXRow,startYCol);
                    startXRow++;
                    setPath(startXRow,startYCol);
//                    begin++;
                }
            }
           else if(direction==3){//TODO 左
                if(ifRange(startXRow,startYCol-1)&&ifLegal(startXRow,startYCol-1)){
                    setBefore(startXRow,startYCol);
                    startYCol--;
                    setPath(startXRow,startYCol);
//                    begin++;
                }
            }
            else if(direction==4){ //TODO 右
                if(ifRange(startXRow,startYCol+1)&&ifLegal(startXRow,startYCol+1)){
                    setBefore(startXRow,startYCol);
                    startYCol++;

                    setPath(startXRow,startYCol);
//                    begin++;
                }
            }
        }
        int c=2;
    }


    public  boolean move(Role role){
        int x=role.getX()+role.getMoveX();
        int y=role.getY()+role.getMoveY();
       int i,j;
       for (i=0;i<row;i++){
           for (j=0;j<col;j++){
               Grid grid=grids[i][j];
               if(grid.isWall()){
                   if(x+imageSize/2+4<=grid.getX()-mapSize/2){

                   }else if(grid.getX()+mapSize/2<=x-imageSize/2-4){

                   }else if(y+imageSize/2+4<=grid.getY()-mapSize/2){

                   }else if(grid.getY()+mapSize/2<=y-imageSize/2-4){

                   }else{
                        return false;//相交
                   }




               }

           }
       }

        role.move();
        return true;

    }

    public Grid[][] getGrids(){
        return grids;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超维Ai编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值