gpt4 book ai didi

javascript - 迷宫(递归除法)算法设计

转载 作者:搜寻专家 更新时间:2023-11-01 04:17:03 28 4
gpt4 key购买 nike

我目前正在开发一个随机迷宫生成器,它将迷宫存储在一个名为 grid 的二维数组中。这将在稍后用于生成一个真正的 3D 迷宫,用户随后可以穿过该迷宫。

在做了一些研究之后,我尝试使用递归除法算法创建这个迷宫生成器,但是由于迷宫格式的性质,这对我来说并不是很有效。

据我了解,递归除法不会将墙壁视为单元

例如,我的网格看起来像这样:

  a b c d e f g h
1 - - - - - - - -
2 | | | | |
3 | | |
4 | - - | - |
5 | | | |
6 | - | - |
7 x |
8 - - - - - - - -

我在这里试图表达的意思是,我试图创建的网格将像这样表示:

w w w w w w w w
w w w w w
w w w
w w w w w w
w w w w
w w w w w
g w
w w w w w w w w

其中“w”是墙,“g”是入口/导出。所以墙壁被放置在网格中,例如网格[1][2] == 'w'

递归除法算法的问题在于,墙没有被视为单元格的成员。所有“单元格”基本上都包含空白,并且墙壁将放置在它们周围。

所以当我尝试在我的情况下实现这个算法时,我得到了这样的结果:(黑色方 block 是墙,白色方 block 是空的,红色方 block 是入口)enter image description here

My JSFiddle is located here.

基本上,用户将从红色方 block 开始,必须穿过迷宫并找到可以打开门(红色方 block )逃脱的 key ,因此必须可以访问迷宫中的所有空白区域.

有人知道如何重写此算法以确保从红色方 block 到迷宫中任何其他空间始终有一条路径吗?理想情况下,路径的宽度永远不会超过一平方。

代码:

var grid;

function generate(dimensions, numDoors) {
//numDoors is unused right now

grid = new Array();
for (var i = 0; i < dimensions; i++) {
grid[i] = new Array();

for (var j = 0; j < dimensions; j++) {
grid[i][j] = "";
}
}

addOuterWalls();
var ent = addEntrance();
addInnerWalls(true, 1, grid.length - 2, 1, grid.length - 2, ent);
}

function addOuterWalls() {
for (var i = 0; i < grid.length; i++) {
if (i == 0 || i == (grid.length - 1)) {
for (var j = 0; j < grid.length; j++) {
grid[i][j] = "w";
}
} else {
grid[i][0] = "w";
grid[i][grid.length - 1] = "w";
}
}
}

function addEntrance() {
var x = randomNumber(1, grid.length - 1);
grid[grid.length - 1][x] = "g";
return x;
}

function addInnerWalls(h, minX, maxX, minY, maxY, gate) {
if (h) {

if (maxX - minX < 2) {
return;
}

var y = randomNumber(minY, maxY);
addHWall(minX, maxX, y);

addInnerWalls(!h, minX, maxX, minY, y-1, gate);
addInnerWalls(!h, minX, maxX, y + 1, maxY, gate);
} else {
if (maxY - minY < 2) {
return;
}

var x = randomNumber(minX, maxX);
addVWall(minY, maxY, x);

addInnerWalls(!h, minX, x-1, minY, maxY, gate);
addInnerWalls(!h, x + 1, maxX, minY, maxY, gate);
}
}

function addHWall(minX, maxX, y) {
var hole = randomNumber(minX, maxX);

for (var i = minX; i <= maxX; i++) {
if (i == hole) grid[y][i] = "";
else grid[y][i] = "w";
}
}

function addVWall(minY, maxY, x) {
var hole = randomNumber(minY, maxY);

for (var i = minY; i <= maxY; i++) {
if (i == hole) grid[i][x] = "";
else grid[i][x] = "w";
}
}

function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

function display() {
document.getElementById("cnt").innerHTML = "";

for (var i = 0; i < grid.length; i++) {
var output = "<div>";
for (var j = 0; j < grid.length; j++) {
output += "<b " + grid[i][j] + "></b>";
}
output += "</div>";
document.getElementById("cnt").innerHTML += output;
}
}
generate(30, 1, 1);
display();

最佳答案

仅在偶数单元格中放置墙壁,在奇数单元格中放置门,并使“尺寸”为奇数。 http://jsfiddle.net/tPm3s/1/

Code:
var grid;

function generate(dimensions, numDoors) {
grid = new Array();
for (var i = 0; i < dimensions; i++) {
grid[i] = new Array();

for (var j = 0; j < dimensions; j++) {
grid[i][j] = "";
}
}

addOuterWalls();
var ent = addEntrance();
addInnerWalls(true, 1, grid.length - 2, 1, grid.length - 2, ent);
}

function addOuterWalls() {
for (var i = 0; i < grid.length; i++) {
if (i == 0 || i == (grid.length - 1)) {
for (var j = 0; j < grid.length; j++) {
grid[i][j] = "w";
}
} else {
grid[i][0] = "w";
grid[i][grid.length - 1] = "w";
}
}
}

function addEntrance() {
var x = randomNumber(1, grid.length - 1);
grid[grid.length - 1][x] = "g";
return x;
}

function addInnerWalls(h, minX, maxX, minY, maxY, gate) {
if (h) {

if (maxX - minX < 2) {
return;
}

var y = Math.floor(randomNumber(minY, maxY)/2)*2;
addHWall(minX, maxX, y);

addInnerWalls(!h, minX, maxX, minY, y-1, gate);
addInnerWalls(!h, minX, maxX, y + 1, maxY, gate);
} else {
if (maxY - minY < 2) {
return;
}

var x = Math.floor(randomNumber(minX, maxX)/2)*2;
addVWall(minY, maxY, x);

addInnerWalls(!h, minX, x-1, minY, maxY, gate);
addInnerWalls(!h, x + 1, maxX, minY, maxY, gate);
}
}

function addHWall(minX, maxX, y) {
var hole = Math.floor(randomNumber(minX, maxX)/2)*2+1;

for (var i = minX; i <= maxX; i++) {
if (i == hole) grid[y][i] = "";
else grid[y][i] = "w";
}
}

function addVWall(minY, maxY, x) {
var hole = Math.floor(randomNumber(minY, maxY)/2)*2+1;

for (var i = minY; i <= maxY; i++) {
if (i == hole) grid[i][x] = "";
else grid[i][x] = "w";
}
}

function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

function display() {
document.getElementById("cnt").innerHTML = "";

for (var i = 0; i < grid.length; i++) {
var output = "<div>";
for (var j = 0; j < grid.length; j++) {
output += "<b " + grid[i][j] + "></b>";
}
output += "</div>";
document.getElementById("cnt").innerHTML += output;
}
}
generate(31, 1, 1);
display();

关于javascript - 迷宫(递归除法)算法设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23530756/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com