package _4;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
/**
- 迷宫生成算法和迷宫寻路算法
-
@author ygch
*/
public class Maze {
private int height;
private int width;
private Random r; //随机位置生成器
private boolean blocked[][];
boolean[][] visited;//寻路过程中判断某个位置是否已经被访问过
private Stack path;//寻路过程中保存的路径
private final int[][] direction={{0, -1}, {1, 0}, {0, 1}, {-1, 0}};/**
- 重构equals函数,方便后面的比较操作 */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Maze other = (Maze) obj; return height == other.height && width == other.width; }
public Maze(int height, int width) {
super();
this.height = height;
this.width = width;
//让整个迷宫的大小为奇数,这样可以构造一行路一行墙的迷宫
if(this.height%2==0) this.height+=1;
if(this.width%2==0) this.width+=1;
r = new Random();
path= new Stack<>();//给迷宫四周加上墙 blocked = new boolean[this.height + 2][this.width + 2]; visited=new boolean[this.height+2][this.width+2]; // 最左和最右两列置1 for (int i = 0; i < this.height + 2; i++) { blocked[i][0] = true; blocked[i][this.width + 1] = true; } // 最上和最下两行置1 for (int i = 0; i < this.width + 2; i++) { blocked[0][i] = true; blocked[this.height + 1][i] = true; }
}
public int getHeight()
{
return height;
}public int getWidth()
{
return width;
}
部分如上