java生成迷宫_java怎么生成迷宫地图

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 ArrayList  getMaze() {

ArrayList  maze = new ArrayList  ();

for (int h = 0; h 

for (int w = 0; w 

MazePoint point = new MazePoint(w, h);

maze.add(point);

}

}

return CreateMaze(maze);

}

private ArrayList  CreateMaze(ArrayList  maze) {

int top = 0;

int x = 0;

int y = 0;

ArrayList  team = new ArrayList  ();

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 (times 

int dir = rnd.nextInt(4);

if (val[dir] == dir)

continue;

else

val[dir] = dir;

switch (dir) {

case 0: // 左边

if ((x - 1) >= 0 && maze.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) 

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) >= 0 && maze.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) 

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;

}

}

return maze;

}

}

迷宫

[java] view plain copy

//作者:zhongZw

//邮箱:zhong317@32313133353236313431303231363533e58685e5aeb931333365633966126.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() {

return down;

}

public void setDown() {

this.down = 1;

}

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值