深度优先迷宫生成类

来源:互联网 发布:java 创建自定义菜单 编辑:程序博客网 时间:2024/05/04 19:00
package maze.eng;

import java.util.Random;

public class mapBuilderChilderOne extends mapBuilderFarther
{
    boolean set = true;
    int xNum;
    int yNum;
    int count;
    int xEnd;
    int yEnd;
    int keeper;
    int longNum;
    long time;
    
    manage theManage;
    
    public mapBuilderChilderOne()
    {
        
        xNum = 2;
        yNum = 2;
        count = 0;
        xEnd = 2;
        yEnd = 2;
        keeper = 0;
    }
    
    public manage getMap(int myIn)
    {   
        count = 0;
        keeper = 0;
        longNum = myIn;
        set = true;
        
        theManage = new manage(myIn);
        
        time = System.currentTimeMillis();
        
        
        while(set)
        {
            xNum = 2;
            yNum = 2;
            count = 0;
            xEnd = 2;
            yEnd = 2;
            keeper = 0;
            time++;
            set = false;
            
            theManage.renew();
            Random rand = new Random(time);
        
            building(rand, xNum, yNum, theManage);
        }
        
        theManage.setManage(xEnd, yEnd, '4');
        theManage.setManage(xNum, yNum, '3');
        
        theManage.setX(xNum);
        theManage.setY(yNum);
        
        return theManage;
    }
    
    private void building(Random rand, int x, int y, manage myIn)
    {
        keeper++;
        if(count == 0 || count > longNum - 2)
        {
            myIn.setManage(x, y, '2');
            
            int buiDateIntOne = rand.nextInt() % 4;
            while(buiDateIntOne < 0)
            {
                buiDateIntOne = rand.nextInt() % 4;
            }

            int buiContIntOne = 0;

            while(buiContIntOne < 4)
            {
                if(buiDateIntOne == 0)
                {
                    if(myIn.judgeBuild(x - 1, y))
                    {
                        building(rand, x - 1, y, myIn);
                    }
                    buiContIntOne++;
                    buiDateIntOne++;
                    buiDateIntOne %= 4;
                }
                else if(buiDateIntOne == 1)
                {
                    if(myIn.judgeBuild(x + 1, y))
                    {
                        building(rand, x + 1, y, myIn);
                    }
                    buiContIntOne++;
                    buiDateIntOne++;
                    buiDateIntOne %= 4;
                }
                else if(buiDateIntOne == 2)
                {
                    if(myIn.judgeBuild(x, y - 1))
                    {
                        building(rand, x, y - 1, myIn);
                    }
                    buiContIntOne++;
                    buiDateIntOne++;
                    buiDateIntOne %= 4;
                }
                else if(buiDateIntOne == 3)
                {
                    if(myIn.judgeBuild(x, y + 1))
                    {
                        building(rand, x, y + 1, myIn);
                    }
                    buiContIntOne++;
                    buiDateIntOne++;
                    buiDateIntOne %= 4;
                }
            }

            if(count == 0)
            {
                xEnd = x;
                yEnd = y;
            }
        }
        
        if(keeper < longNum)
        {
            count = longNum;
            set = true;
        }
        
        count++;
    }
}