java写的迷宫代码



迷宫代码:
截图如下:

package
com.zxl.maze; /* * 抽象类表示选择不同的算法*/ public abstract class AbstractMap { /* * 得到数据*/ public abstract boolean[][] getData(int m,int n); /* * 重置*/ public abstract void reset(int m,int n); } package com.zxl.maze; /* *深度优先,生成迷宫*/ import java.awt.Point; import java.util.Random; import java.util.Stack; public class DFSMap extends AbstractMap { private int m = 30, n = 30; private boolean a[][]; private int currX, currY; private Stack<Point> 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 }; public DFSMap() { stack = new Stack<Point>(); } @Override public boolean[][] getData(int m,int n)// 得到数据 { // TODO Auto-generated method stub reset(m, n); return a; } @Override public 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 void goRandPosition() { int i, 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(new Point(i, j)); currX = i; currY = j; break; } } } private void start()// 具体操作 { while (!check()) { go(); } a[m - 2][n - 1] = true; } private void go() { 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(new Point(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 = new Random(); 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; } return a; } private boolean isSafe(int x, int y) { if (x < 0 || x >= m || y < 0 || y >= n || a[x][y] == true) { return false; } return true; } } package com.zxl.maze; /* * 随机prim算法的核心是(翻译的维基): 1、让迷宫全是墙 2、选一个格作为迷宫的通路,然后把它的邻墙放入列表 3、当列表里还有墙时 ——1、从列表里随机选一个墙,如果它对面的格子不是迷宫的通路 ————1、把墙打通,让对面的格子成为迷宫的通路 ————2、把那个格子的邻墙加入列表 ——2、如果对面的格子已经是通路了,那就从列表里移除这面墙*/ import java.awt.Point; import java.util.ArrayList; import java.util.List; import java.util.Random; public class PriMap extends AbstractMap { private int m = 0, n = 0; private boolean a[][]; private int currX, currY;// 记录当前格子位置 private List<Point> set;// 用作队列,存放墙 private int[] X_GO1 = { 1, 0, -1, 0 }; private int[] Y_GO1 = { 0, -1, 0, 1 }; public void reset(int m, int n) { 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 void go() { while (set.size() > 0) { if (choiceWall())// 选择墙 { addWall(currX, currY);// 将墙的临墙加入队列 } } } private boolean choiceWall() { // TODO Auto-generated method stub Random r = new Random(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, int y) { // 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, int y) { if (x % 2 == 0) return 1;// 竖线 else if (y % 2 == 0) return 2;// 横线 else return 0;// 出错了 } private boolean isSafe(int x, int y) { if (x <= 0 || x >= m || y <= 0 || y >= n || a[x][y] == true) { return false; } return true; } @Override public boolean[][] getData(int m,int n) { // TODO Auto-generated method stub set = new ArrayList<Point>(); reset(m, n); return a; } } package com.zxl.maze; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JPanel; public class MainPanel extends JPanel { int x = 0; int y = 0; boolean b[][]=null; public MainPanel() { this.setSize(400, 400); } public void setMap(boolean b[][]) { this.b = b; } @Override public Dimension getPreferredSize() { // TODO Auto-generated method stub return new Dimension(400,400); } public void paintMap(int x, int y) { this.x = x; this.y = y; repaint(); } @Override public void paintComponent(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); } } } } } package com.zxl.maze; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class MainFrame extends JFrame { //JButton resetButton; Container con; boolean b[][]; JMenu menu; JMenuBar bar; JMenuItem item; MainPanel pan=new MainPanel(); public MainFrame() { this.setSize(600,550); this.setDefaultCloseOperation(EXIT_ON_CLOSE); //resetButton=new JButton("重 绘"); con=this.getContentPane(); con.setLayout(new BorderLayout()); //con.add(resetButton,BorderLayout.SOUTH); menu=new JMenu("菜单"); item=new JMenuItem("绘制地图"); bar=new JMenuBar(); bar.add(menu); menu.add(item); this.add(pan); this.setJMenuBar(bar); this.setVisible(true); //setMap(); item.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub setMap(); Thread t=new Thread(new DrawHandler(pan)); t.start(); } }); } private void setMap() { // TODO Auto-generated method stub this.requestFocus(); AbstractMap map=new DFSMap(); b=map.getData(20, 20); pan.setMap(b); /*for(int i=0;i<b.length;i++) { for(int j=0;j<b[0].length;j++) { pan.paintMap(i, j); try { Thread.sleep(10); }catch(Exception ex) { ex.printStackTrace(); } } }*/ } class DrawHandler implements Runnable{ private MainPanel pan; public DrawHandler(MainPanel pan){ this.pan=pan; } public void run(){ for(int i=0;i<b.length;i++) { for(int j=0;j<b[0].length;j++) { pan.paintMap(i, j); try { Thread.sleep(10); }catch(Exception ex) { ex.printStackTrace(); } } } } } } package com.zxl.maze; public class Test { public static void main(String []args) { new MainFrame(); } } package com.zxl.maze; import java.util.Random; /* *深度优先,生成迷宫*/ public class TreeMap extends AbstractMap { private int m = 0, n = 0; private boolean a[][]; private int currX, 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 }; @Override public boolean[][] getData(int m,int n)// 得到数据 { // TODO Auto-generated method stub reset(m,n); return a; } @Override public 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()// 后退一步 { int i, 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 void go() { 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 = new Random(); 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; } return a; } private boolean isSafe(int x, int y) { if (x < 0 || x >= m || y < 0 || y >= n || a[x][y] == true) { return false; } return true; } }

 

posted @ 2014-05-02 08:19  路痕  阅读(8203)  评论(1编辑  收藏  举报