迷宫代码:
截图如下:
packagecom.zxl.maze;/** 抽象类表示选择不同的算法*/
public abstract classAbstractMap
{/** 得到数据*/
public abstract boolean[][] getData(int m,intn);/** 重置*/
public abstract void reset(int m,intn);
}packagecom.zxl.maze;/**深度优先,生成迷宫*/
importjava.awt.Point;importjava.util.Random;importjava.util.Stack;public class DFSMap extendsAbstractMap
{private int m = 30, n = 30;private booleana[][];private intcurrX, currY;private Stack stack;//保存已走路径
private int[] X_GO1 = { -2, 2, 0, 0};private int[] Y_GO1 = { 0, 0, 2, -2};private int[] X_GO2 = { -1, 1, 0, 0};private int[] Y_GO2 = { 0, 0, 1, -1};publicDFSMap()
{
stack= new Stack();
}
@Overridepublic boolean[][] getData(int m,int n)//得到数据
{//TODO Auto-generated method stub
reset(m, n);returna;
}
@Overridepublic void reset(int m, int n)//重置
{//TODO Auto-generated method stub
a= new boolean[2 * m + 1][2 * n + 1];this.m = 2 * m + 1;this.n = 2 * n + 1;for (int i = 0; i < this.m; i++)for (int j = 0; j < this.n; j++)
a[i][j]= false;
currX= 1;
currY= 1;
stack.clear();
stack.push(new Point(1, 1));
a[1][0] = true;
a[1][1] = true;
start();
}private boolean check()//判断是否全部走过。。。
{for (int i = 1; i < m; i += 2)
{for (int j = 1; j < n; j += 2)
{if (a[i][j] == false)return false;
}
}return true;
}private void forward()//后退一步
{if (stack.size() > 0)
{
Point p=stack.pop();
currX= (int) p.getX();
currY= (int) p.getY();
}else{
goRandPosition();
}
}private voidgoRandPosition()
{inti, j;while (true)
{
i= (int) (Math.random() *m);
j= (int) (Math.random() *n);if (a[i][j] == true && i % 2 == 1 && j % 2 == 1)
{
stack.push(newPoint(i, j));
currX=i;
currY=j;break;
}
}
}private void start()//具体操作
{while (!check())
{
go();
}
a[m- 2][n - 1] = true;
}private voidgo()
{int orders[] = getOrder(4);for (int i = 0; i < orders.length; i++)
{if (isSafe(currX + X_GO1[orders[i]], currY + Y_GO1[orders[i]]))//上
{
goNext(orders[i]);return;
}
}
forward();
}private void goNext(int i)//下一步
{//TODO Auto-generated method stub
a[currX + X_GO1[i]][currY + Y_GO1[i]] = true;
a[currX+ X_GO2[i]][currY + Y_GO2[i]] = true;
stack.push(newPoint(currX, currY));
currX+=X_GO1[i];
currY+=Y_GO1[i];
}private int[] getOrder(int i)//产生随机序列
{//TODO Auto-generated method stub
int a[] = new int[i];
Random ran= newRandom();for (int j = 0; j < i; j++)
a[j]=j;for (int k = 0; k < i; k++)
{int r1 =ran.nextInt(i);int r2 =ran.nextInt(i);int b =a[r1];
a[r1]=a[r2];
a[r2]=b;
}returna;
}private boolean isSafe(int x, inty)
{if (x < 0 || x >= m || y < 0 || y >= n || a[x][y] == true)
{return false;
}return true;
}
}packagecom.zxl.maze;/** 随机prim算法的核心是(翻译的维基):
1、让迷宫全是墙
2、选一个格作为迷宫的通路,然后把它的邻墙放入列表
3、当列表里还有墙时
——1、从列表里随机选一个墙,如果它对面的格子不是迷宫的通路
————1、把墙打通,让对面的格子成为迷宫的通路
————2、把那个格子的邻墙加入列表
——2、如果对面的格子已经是通路了,那就从列表里移除这面墙*/
importjava.awt.Point;importjava.util.ArrayList;importjava.util.List;importjava.util.Random;public class PriMap extendsAbstractMap
{private int m = 0, n = 0;private booleana[][];private int currX, currY;//记录当前格子位置
private List set;//用作队列,存放墙
private int[] X_GO1 = { 1, 0, -1, 0};private int[] Y_GO1 = { 0, -1, 0, 1};public void reset(int m, intn)
{this.m = 2 * m + 1;this.n = 2 * n + 1;
a= new boolean[this.m][this.n];for (int i = 0; i < this.m; i++)for (int j = 0; j < this.n; j++)
a[i][j]= false;
currX= 1;
currY= 1;
set.clear();
set.add(new Point(1, 2));
set.add(new Point(2, 1));
a[1][0] = true;
a[1][1] = true;
go();
a[this.m - 2][this.n - 1] = true;
}public voidgo()
{while (set.size() > 0)
{if (choiceWall())//选择墙
{
addWall(currX, currY);//将墙的临墙加入队列
}
}
}private booleanchoiceWall()
{//TODO Auto-generated method stub
Random r = newRandom(System.currentTimeMillis());
Point p=set.remove(r.nextInt(set.size()));int x = (int) p.getX();int y = (int) p.getY();if (test(x, y) == 1)
{if (isSafe(x - 1, y) && a[x - 1][y] == false)
{
a[x][y]= true;
currX= x - 1;
currY=y;
a[currX][currY]= true;return true;
}else if (isSafe(x + 1, y) && a[x + 1][y] == false)
{
a[x][y]= true;
currX= x + 1;
currY=y;
a[currX][currY]= true;return true;
}
}else if (test(x, y) == 2)
{if (isSafe(x, y - 1) && a[x][y - 1] == false)
{
a[x][y]= true;
currX=x;
currY= y - 1;
a[currX][currY]= true;return true;
}else if (isSafe(x, y + 1) && a[x][y + 1] == false)
{
a[x][y]= true;
currX=x;
currY= y + 1;
a[currX][currY]= true;return true;
}
}return false;
}private void addWall(int x, inty)
{//TODO Auto-generated method stub
for (int i = 0; i < X_GO1.length; i++)
{if (isSafe(currX + X_GO1[i], currY +Y_GO1[i]))
{if (!set.contains(new Point(currX + X_GO1[i], currY +Y_GO1[i])))
{
set.add(new Point(currX + X_GO1[i], currY +Y_GO1[i]));
}
}
}
}private int test(int x, inty)
{if (x % 2 == 0)return 1;//竖线
else if (y % 2 == 0)return 2;//横线
else
return 0;//出错了
}private boolean isSafe(int x, inty)
{if (x <= 0 || x >= m || y <= 0 || y >= n || a[x][y] == true)
{return false;
}return true;
}
@Overridepublic boolean[][] getData(int m,intn)
{//TODO Auto-generated method stub
set = new ArrayList();
reset(m, n);returna;
}
}packagecom.zxl.maze;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Graphics;importjavax.swing.JPanel;public class MainPanel extendsJPanel
{int x = 0;int y = 0;boolean b[][]=null;publicMainPanel()
{this.setSize(400, 400);
}public void setMap(booleanb[][])
{this.b =b;
}
@OverridepublicDimension getPreferredSize()
{//TODO Auto-generated method stub
return new Dimension(400,400);
}public void paintMap(int x, inty)
{this.x =x;this.y =y;
repaint();
}
@Overridepublic voidpaintComponent(Graphics g)
{//TODO Auto-generated method stub
if (x == 0&&y==0)
{
g.clearRect(0, 0, this.getWidth(), this.getHeight());
}else{for(int i=0;i<=x;i++)for(int j=0;j<=y;j++)
{if (b[i][j] == true)
{
g.setColor(Color.blue);
g.fillRect(50 + i * 10, 50 + j* 10, 10, 10);
}else{
g.setColor(Color.red);
g.fillRect(50 + i * 10, 50 + j * 10, 10, 10);
}
}
}
}
}packagecom.zxl.maze;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Container;importjava.awt.Graphics;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;public class MainFrame extendsJFrame
{//JButton resetButton;
Container con;booleanb[][];
JMenu menu;
JMenuBar bar;
JMenuItem item;
MainPanel pan=newMainPanel();publicMainFrame()
{this.setSize(600,550);this.setDefaultCloseOperation(EXIT_ON_CLOSE);//resetButton=new JButton("重 绘");
con=this.getContentPane();
con.setLayout(newBorderLayout());//con.add(resetButton,BorderLayout.SOUTH);
menu=new JMenu("菜单");
item=new JMenuItem("绘制地图");
bar=newJMenuBar();
bar.add(menu);
menu.add(item);this.add(pan);this.setJMenuBar(bar);this.setVisible(true);//setMap();
item.addActionListener(newActionListener()
{
@Overridepublic voidactionPerformed(ActionEvent e)
{//TODO Auto-generated method stub
setMap();
Thread t=new Thread(newDrawHandler(pan));
t.start();
}
});
}private voidsetMap()
{//TODO Auto-generated method stub
this.requestFocus();
AbstractMap map=newDFSMap();
b=map.getData(20, 20);
pan.setMap(b);/*for(int i=0;i
{
for(int j=0;j
{
pan.paintMap(i, j);
try
{
Thread.sleep(10);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}*/}class DrawHandler implementsRunnable{privateMainPanel pan;publicDrawHandler(MainPanel pan){this.pan=pan;
}public voidrun(){for(int i=0;i
{for(int j=0;j
{
pan.paintMap(i, j);try{
Thread.sleep(10);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
}
}packagecom.zxl.maze;public classTest
{public static voidmain(String []args)
{newMainFrame();
}
}packagecom.zxl.maze;importjava.util.Random;/**深度优先,生成迷宫*/
public class TreeMap extendsAbstractMap
{private int m = 0, n = 0;private booleana[][];private intcurrX, currY;private int[] X_GO1 = { -2, 2, 0, 0};private int[] Y_GO1 = { 0, 0, 2, -2};private int[] X_GO2 = { -1, 1, 0, 0};private int[] Y_GO2 = { 0, 0, 1, -1};
@Overridepublic boolean[][] getData(int m,int n)//得到数据
{//TODO Auto-generated method stub
reset(m,n);returna;
}
@Overridepublic void reset(int m, int n)//重置
{//TODO Auto-generated method stub
a = new boolean[2 * m + 1][2 * n + 1];this.m = 2 * m + 1;this.n = 2 * n + 1;for (int i = 0; i < this.m; i++)for (int j = 0; j < this.n; j++)
a[i][j]= false;
currX= 1;
currY= 1;
a[1][0] = true;
a[1][1] = true;
start();
}private boolean check()//判断是否全部走过。。。
{for (int i = 1; i < m; i += 2)
{for (int j = 1; j < n; j += 2)
{if (a[i][j] == false)return false;
}
}return true;
}private void forward()//后退一步
{inti, j;while (true)
{
i= (int) (Math.random() *m);
j= (int) (Math.random() *n);if (a[i][j] == true && i % 2 == 1 && j % 2 == 1)
{
currX=i;
currY=j;return;
}
}
}private void start()//具体操作
{while (!check())
{
go();
}
a[m- 2][n - 1] = true;
}private voidgo()
{int orders[] = getOrder(4);for (int i = 0; i < orders.length; i++)
{if (isSafe(currX + X_GO1[orders[i]], currY + Y_GO1[orders[i]]))//上
{
goNext(orders[i]);return;
}
}
forward();
}private void goNext(int i)//下一步
{//TODO Auto-generated method stub
a[currX + X_GO1[i]][currY + Y_GO1[i]] = true;
a[currX+ X_GO2[i]][currY + Y_GO2[i]] = true;
currX+=X_GO1[i];
currY+=Y_GO1[i];
}private int[] getOrder(int i)//产生随机序列
{//TODO Auto-generated method stub
int a[] = new int[i];
Random ran= newRandom();for (int j = 0; j < i; j++)
a[j]=j;for (int k = 0; k < i; k++)
{int r1 =ran.nextInt(i);int r2 =ran.nextInt(i);int b =a[r1];
a[r1]=a[r2];
a[r2]=b;
}returna;
}private boolean isSafe(int x, inty)
{if (x < 0 || x >= m || y < 0 || y >= n || a[x][y] == true)
{return false;
}return true;
}
}