我正在使用Prim的算法创建迷宫。我已经成功地做到了,但是现在我试图通过更改选择要添加到迷宫中的潜在细胞的方式来使其变得“更难”。在我看来,“艰难”介于两个极端之间:
Extreme#1是潜在代代表中细胞的完全随机选择,其中每个分支以近似相等的速度发育。它有很多不同的分支,但是一旦到达原点,您几乎可以沿着一条直线指向期望的位置。这是显示此方法的图片:
极端2是选择添加到列表中的最后一个内容的地方,从而创建了一个冗长,乏味,容易的迷宫。当您仅选择放置在潜在段落列表中的最后一个项目时,它就会形成。这是显示此方法的图片:
我试图通过优先处理最近放置的单元格来平衡这一点,但是很难产生分支,如在第一个中可以看到的那样,但是仍然有一条通向整个迷宫的路径。 / p>
尝试执行此操作的最有趣的方法是,当我尝试添加50%的机会添加最后一个块时,然后尝试在下一个失败的情况下添加50%的机会,依此类推。但是,我搞砸了,并尝试先做[-0]的索引,使第一个块被添加的概率为50%,然后是最后一个,然后是第二个,依此类推。这创建了一个有趣的迷宫,但是当我“修复”它时,迷宫看起来很像第二个极端。
我尝试的另一种方法是在我的代码中使用的方法:
for i in range(1, len(potential_passage_list) + 1):
if randint(0, int(len(passage_list) / 50)) == 0:
maze_passage(potential_passage_list[-i][0], potential_passage_list[-i][1])
这是尝试并有合理的可能性将一个块添加到更早的potential_passage_list中。
所以,我的问题是,如何创建一个“硬”迷宫,其中包含许多分支,但模式不可预测?可以使用什么算法来做到这一点?
我正在使用python 3和pygame库来显示所有内容。
如果可以理解,这是我的代码:
import pygame
from random import shuffle, randint
# variables
######
# changeable variables
cell_size = 7 # cannot be less than 3
maze_length = 160 * cell_size + 1
maze_height = 100 * cell_size + 1
######
# colours
black = (0, 0, 0)
white = (245, 245, 245)
red = (255, 0, 0)
blue = (0, 0, 255)
# other variables
passage_list = []
potential_passage_list = []
impossible_passage = []
random_cell = []
done = False
# initialize pygame and display screen
pygame.init()
screen = pygame.display.set_mode((maze_length, maze_height))
pygame.display.flip()
def one_connection(cell_x, cell_y):
# ensure that it will only touch one passage
count = 0
if [cell_x + cell_size, cell_y] in passage_list:
count += 1
if [cell_x - cell_size, cell_y] in passage_list:
count += 1
if [cell_x, cell_y + cell_size] in passage_list:
count += 1
if [cell_x, cell_y - cell_size] in passage_list:
count += 1
if count <= 1:
return True
else:
return False
def valid_cell(cell_x, cell_y):
# check if already in potential_passage_list
if [cell_x, cell_y] in potential_passage_list:
impossible_passage.append([cell_x, cell_y])
# check if in impossible list
elif [cell_x, cell_y] in impossible_passage:
impossible_passage.append([cell_x, cell_y])
# check if out of boundary
elif cell_x < 0 or cell_x >= maze_length - cell_size or cell_y < 0 or cell_y >= maze_height - cell_size:
impossible_passage.append([cell_x, cell_y])
# ensure that it will only touch one passage
elif not one_connection(cell_x, cell_y):
impossible_passage.append([cell_x, cell_y])
# check if it isolates any walls / cut off unconnected corners
elif (([cell_x + cell_size, cell_y + cell_size] in passage_list and [cell_x + cell_size, cell_y] not in
passage_list and [cell_x, cell_y + cell_size] not in passage_list) or
([cell_x + cell_size, cell_y - cell_size] in passage_list and [cell_x + cell_size, cell_y] not in
passage_list and [cell_x, cell_y - cell_size] not in passage_list) or
([cell_x - cell_size, cell_y + cell_size] in passage_list and [cell_x - cell_size, cell_y] not in
passage_list and [cell_x, cell_y + cell_size] not in passage_list) or
([cell_x - cell_size, cell_y - cell_size] in passage_list and [cell_x - cell_size, cell_y] not in
passage_list and [cell_x, cell_y - cell_size] not in passage_list)):
impossible_passage.append([cell_x, cell_y])
# check if already in passage_list
elif [cell_x, cell_y] not in passage_list:
return True
# functions
def maze_passage(cell_x, cell_y):
# reset block_passage_list
block_passage_list = []
# remove from list so it does not interfere with valid_cell procedure
potential_passage_list.remove([cell_x, cell_y])
if valid_cell(cell_x, cell_y):
# display rectangle
pygame.draw.rect(screen, white, [cell_x, cell_y, cell_size, cell_size])
pygame.display.update()
passage_list.append([cell_x, cell_y])
# add valid walls to block_passage_list
if valid_cell(cell_x + cell_size, cell_y):
block_passage_list.append([cell_x + cell_size, cell_y])
if valid_cell(cell_x - cell_size, cell_y):
block_passage_list.append([cell_x - cell_size, cell_y])
if valid_cell(cell_x, cell_y + cell_size):
block_passage_list.append([cell_x, cell_y + cell_size])
if valid_cell(cell_x, cell_y - cell_size):
block_passage_list.append([cell_x, cell_y - cell_size])
shuffle(block_passage_list)
for j in block_passage_list:
potential_passage_list.append(j)
# create initial cell
start_cell = [randint(0, int(maze_height / cell_size))*cell_size, randint(0, int(maze_height / cell_size))*cell_size]
potential_passage_list.append([start_cell[0], start_cell[1]])
# loop for creating maze
while not done:
for event in pygame.event.get():
# exit screen when exit pressed in pygame
if event.type == pygame.QUIT:
done = True
# select cell
for i in range(1, len(potential_passage_list) + 1):
if randint(0, int(len(passage_list) / 50)) == 0:
maze_passage(potential_passage_list[-i][0], potential_passage_list[-i][1])
break
# check if maze completion finished
if not potential_passage_list:
# create start and end
passage_list.sort()
pygame.draw.rect(screen, red, [passage_list[0][0] + 1, passage_list[0][1] + 1, cell_size - 2, cell_size - 2])
pygame.draw.rect(screen, blue, [passage_list[-1][0] + 1, passage_list[-1][1] + 1, cell_size - 2, cell_size - 2])
pygame.display.update()
可以随意获取我的代码,对其进行处理,并分享发现的效果。
谢谢!
答案 0 :(得分:0)
我喜欢使用Kruskal算法并指定不同的选择权重来去除不同配置中的边缘,而不是优先考虑新旧单元。
这使您可以创建具有多种不同特征的迷宫。您可以在此处尝试一个演示:https://mtimmerm.github.io/webStuff/maze.html
如果您喜欢扩展现有路径的选项(滑块1、2和3),则会使迷宫更加困难。