java怎么生成迷宫地图//作者:zhongZw
package cn.zhongZw.model;
import java.util.ArrayList;
import java.util.Random;
public class MazeModel {
private int width = 0;
private int height = 0;
private Random rnd = new Random();
public MazeModel() {
this.width = 50; //迷宫宽度
this.height = 50; //迷宫高度
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public MazeModel(int width, int height) {
super();
this.width = width;
this.height = height;
}
public ArrayListMazePointgetMaze() {
ArrayListMazePointmaze = new ArrayListMazePoint();
for (int h = 0; hheight; h++) {
for (int w = 0; wwidth; w++) {
MazePoint point = new MazePoint(w, h);
maze.add(point);
}
}
return CreateMaze(maze);
}
private ArrayListMazePointCreateMaze(ArrayListMazePointmaze) {
int top = 0;
int x = 0;
int y = 0;
ArrayListMazePointteam = new ArrayListMazePoint();
team.add(maze.get(x + y * width));
while (top = 0) {
int[] val = new int[] {
-1, -1, -1, -1
};
int times = 0;
boolean flag = false;
MazePoint pt = (MazePoint) team.get(top);
x = pt.getX();
y = pt.getY();
pt.visted = true;
ro1: while (times4) {
int dir = rnd.nextInt(4);
if (val[dir] == dir)
continue;
else
val[dir] = dir;
switch (dir) {
case 0: // 左边
if ((x - 1) = 0maze.get(x - 1 + y * width).visted == false) {
maze.get(x + y * width).setLeft();
maze.get(x - 1 + y * width).setRight();
team.add(maze.get(x - 1 + y * width));
top++;
flag = true;
break ro1;
}
break;
case 1: // 右边
if ((x + 1)widthmaze.get(x + 1 + y * width).visted == false) {
maze.get(x + y * width).setRight();
maze.get(x + 1 + y * width).setLeft();
team.add(maze.get(x + 1 + y * width));
top++;
flag = true;
break ro1;
}
break;
case 2: // 上边
if ((y - 1) = 0maze.get(x + (y - 1) * width).visted == false) {
maze.get(x + y * width).setUp();
maze.get(x + (y - 1) * width).setDown();
team.add(maze.get(x + (y - 1) * width));
top++;
flag = true;
break ro1;
}
break;
case 3: // 下边
if ((y + 1)heightmaze.get(x + (y + 1) * width).visted == false) {
maze.get(x + y * width).setDown();
maze.get(x + (y + 1) * width).setUp();
team.add(maze.get(x + (y + 1) * width));
top++;
flag = true;
break ro1;
}
break;
}
times += 1;
}
if (!flag) {
team.remove(top);
top -= 1;
}
}
【迷宫生成java源代码 java实现迷宫程序】return maze;
}
}
迷宫
[java] view plain copy
//作者:zhongZw
//邮箱:zhong317@126.com
package cn.zhongZw.model;
import java.util.*;
import java.lang.*;
public class MazePoint {
private int left = 0;
private int right = 0;
private int up = 0;
private int down = 0;
private int x;
private int y;
public boolean visted;
public MazePoint(int x, int y) {
this.x = x;
this.y = y;
}
public int getLeft() {
return left;
}
public void setLeft() {
this.left = 1;
}
public int getRight() {
return right;
}
public void setRight() {
this.right = 1;
}
public int getUp() {
return up;
}
public void setUp() {
this.up = 1;
}
public int getDown() {
推荐阅读
- 学生卡买什么手机划算,学生卡哪种好
- chatgpt应用方向,CHATGPT会带来怎样的新应用
- 防伪基站有什么cpu,怎样关闭防伪连基站
- 海战休闲益智游戏,经典海战游戏推荐
- php忽略数据库报错 php数据库乱码
- ssm中redis缓存如何配置,redis缓存设计方案
- 国产武器的射击游戏手游,武器最全的射击游戏
- 做直播为什么没人带货,直播带货没有人
- java魂斗罗代码大全 魂斗罗4金手指代码大全