Eller算法问题 - 迷宫生成

5

我正在尝试使用Eller's算法创建迷宫。关于这个特定算法在互联网上的信息很少。所以我在使用这个算法时遇到了一些困难,因为有些东西我并不完全理解。但无论如何,这是我现在拥有的:

class Cell:
    def __init__(self, row, col, number, right_wall, bottom_wall):
        self.row = row
        self.col = col
        self.number = number    # defines which set this block is in
        self.right_wall = right_wall
        self.bottom_wall= bottom_wall

# every block includes 5x5 px white space + 1px on it's left and 1px on it's bottom for walls (if the block has ones)
def create_block(row, col, right_wall, bottom_wall):
    for i in range(row-2, row+4):   # since the path is 5px wide
        for j in range(col-2, col+4):   # i go to a central pixel of block's white space and make it thick
            maze[i, j] = [255, 255, 255]    # in other words i draw 2px border around central pixel
    if right_wall:  # if the block has wall on it's right
        create_right_wall(row, col, 1)  # draw right wall with a function
    if bottom_wall: # if the block has wall on it's bottom
        create_bottom_wall(row, col ,1) # draw bottom wall with a function


def create_right_wall(row, col, color):
    if color == 0:  # if color parameter = 0
        for i in range(row-2, row+4):
            maze[i, col+3] = [0, 0, 0]  # I draw (create) black wall
    else:
        for i in range(row-2, row+4):
            maze[i, col+3] = [255, 255, 255]    # I draw white wall (if I need to delete a wall)


def create_bottom_wall(row, col, color):
    if color == 0:
        for i in range(col-2, col+4):
            maze[row+3, i] = [0, 0, 0]
        if row + 4 < maze_height and maze[row+4, col-3][0] == 0:    # sometimes there's 1px gap between bottom walls
            maze[row+3, col-3] = [0, 0, 0]  # so I fill it with black pixel
    else:
        for i in range(col-2, col+4):
            maze[row+3, i] = [255, 255, 255]    # draws white wall (deleting wall)


def creating():
    current_point = [3, 3]  # where the top-left block appears ([3,3] is for the block's center)

    set_count = 1   # to have unique set numbers, it increases every time after it has been used

    for row in range(height):   # going from top to bottom
        # I print some unnecessary information just to know what's happening
        print("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - row", row) # current row being created
        if row == 0:    # if it's the first row
            for col in range(width):
                create_block(current_point[0], current_point[1], False, False)

                blocks[row].append(Cell(current_point[0], current_point[1], set_count, False, False))
                set_count += 1  # since the set number has been used, the next block will have another one

                current_point[1] += 6   # the center of the next block is 6px away from the center of the current one

        elif row == height - 1: # if it's the last row
            for i in range(width):
                create_block(current_point[0], current_point[1], False, False)
                blocks[row].append(Cell(current_point[0], current_point[1], blocks[row-1][i].number, False, True))

                current_point[1] += 6

                # I don't know why I do this. Just trying to create the last line correctly
                if (not blocks[row-1][i].bottom_wall and not blocks[row-1][i + 1].bottom_wall) and \
                        (blocks[row-1][i].number == blocks[row-1][i + 1].number):
                    create_right_wall(blocks[row][i].row, blocks[row][i].col, 0)
            break   # since it's the last row, don't do anything else



        else:
            for col in range(width):
                create_block(current_point[0], current_point[1], False, False)

                print("block on top has set:", blocks[row-1][col].number, end=" ")

                if blocks[row-1][col].bottom_wall:  # if upper block has bottom wall
                    blocks[row].append(Cell(current_point[0], current_point[1], set_count, False, False))
                    print("upper block has a bottom wall, so set for the current block is", set_count)
                    set_count += 1
                else:   # if not, the current block's set will be the same as for the upper one
                    blocks[row].append(Cell(current_point[0], current_point[1],
                                            blocks[row-1][col].number, False, False))
                    print("current block set", blocks[row-1][col].number)

                current_point[1] += 6

            # just to show set numbers for current row
            for i in blocks[row]:
                print(i.number, end=" ")


        # putting a wall between blocks of the same set (we don't want to have loops in our maze)
        for i in range(len(blocks[row]) - 1):
            if blocks[row][i].number == blocks[row][i+1].number:
                blocks[row][i].right_wall = True
                create_right_wall(blocks[row][i].row, blocks[row][i].col, 0)
                print("put a wall between", i+1, "и", i+2, "because", blocks[row][i].number, "=",\
                      blocks[row][i+1].number)


        for i in range(len(blocks[row]) - 1):
            if random.choice([0, 1]) == 0 and blocks[row][i].number != blocks[row][i+1].number:
                blocks[row][i + 1].number = blocks[row][i].number
                print("connect block", i + 1, "and", i + 2)
            else:
                blocks[row][i].right_wall = True
                create_right_wall(blocks[row][i].row, blocks[row][i].col, 0)


        # to know what set nu,bers we have in the current row
        sets_in_row = []
        for i in blocks[row]:
            print(i.number, end=" ")
            sets_in_row.append(i.number)

        sets_in_row = sorted(set(sets_in_row), key=lambda x: sets_in_row.index(x))
        print(sets_in_row)


        current_bl = 0  # current block in a
        for mn in sets_in_row:  # for every set number in a row
            current_mn_length = sum([p.number == mn for p in blocks[row]])  # how many blocks has this set number
            if current_mn_length > 1: # if the current set has more than 1 block
                quantity = random.randrange(1, current_mn_length)   # random number of bottom walls
                # whick blocks in the current set will have a bottom wall
                bloxxxx = random.sample(list(range(current_bl, current_bl + current_mn_length)), quantity)

                # just to know how it's going
                print("\nblock:")
                for y in range(current_bl, current_bl + current_mn_length):
                    print("pos:", y + 1, end=" ")
                    print("  num:", blocks[row][y].number,)


                print("bottom walls for")
                for i in bloxxxx:
                    print(i+1, end=" ")

                print()

                for b in bloxxxx:
                    blocks[row][b].bottom_wall = True
                    create_bottom_wall(blocks[row][b].row, blocks[row][b].col, 0)

                current_bl += current_mn_length

            else:
                print("\n set length of", current_bl + 1, "=", current_mn_length, "so no bottom wall\n")
                current_bl += current_mn_length

        current_point[0] += 6   # go to the center of the next row block
        current_point[1] = 3    # go to the center of the first block of the next row


while True:
    width = int(input("Width: "))
    height = int(input("height: "))

    maze_width = width * 6 + 1
    maze_height = height * 6 + 1

    maze = np.full((maze_height, maze_width, 3), 0, dtype=np.uint8)
    paths = []
    all_positions = width * height
    break

blocks = [[] for h in range(height)]

creating()

for h in range(maze_height):
    maze[h][maze_width-1] = [0, 0, 0]

for w in range(maze_width):
    maze[maze_height-1][w] = [0, 0, 0]

img = Image.fromarray(maze, 'RGB')
img.save('maze.png')
img.show()

我尝试解释了每一行代码,这样你就可以理解我的代码在做什么。我知道有很多不必要的代码片段,那是因为我尝试了很多方法来生成一个正确的迷宫。
问题在于迷宫并不总是被正确地生成。例如,这个迷宫有循环。但它不应该有。

enter image description here

请看这个在这里输入图片描述 这个迷宫有循环和隔离块。这是因为设置了数字。在这个迷宫中,第二行有以下集合:

在这里输入图片描述 在我随机连接块之后,一排2被22隔开了。所以第二个块不是唯一的2号集合。

请帮我修复我的代码。如果您对我的代码有任何问题,我会尽力提供更多信息。


我没有时间筛选你的所有代码,但如果你需要提示,看起来最后一个代码中的违规是在创建底部墙壁时发生的。从提供的链接中可以看到:“如果一个单元格是其集合中唯一的成员,则不要创建底部墙壁”。而你却为集合22中的单元格创建了底部墙壁。找出原因。 - glibdud
1
当您随机水平连接单元格时,您只是更改右侧单元格的集合编号以匹配左侧单元格的编号。您必须更改该集合中的所有单元格,而不仅仅是其中一个。 - jasonharper
1个回答

2

我刚遇到了同样的问题!

经过长时间思考,我得出结论:在线上可用的所有教程都没有正确解释“连接集合”的方面!这就是创建循环的方式,就像你展示的那样。

让我在这里尝试简化算法步骤:

  1. 使用右侧墙将一行分成不同的集合。
  2. 为每个集合至少制作一个底部开口。
  3. 使用相同的集合分隔单元格以防止循环。
  4. 从步骤1开始。

现在有一个关键因素需要考虑:一个集合是标记有相同“数字”(表示一个集合)的一行中所有单元格的总和。

    | 1   1   1 | 2 | 1 |

集合 1 包括所有值为 1 的单元格,包括最后一个单元格。因此,如果您想向下打开单个单元格,也可以选择像这样打开最后一个单元格的底部:

    | 1   1   1 | 2 | 1 |
    |-----------| ↓ | ↓ |

这个很好,因为集合1至少有一个开口。

现在问题是当你连接集合时,循环会出现问题:

    | 1   1   1 | 2 | 1 |
    | ↓ |-------| ↓ | ↓ | ← let's make 2 down openings for set 1
    | 1   4   5 |(2   1)| ← let's say you want to merge set 1 and 2
    | 2   4   5 | 2   2 | ← also change the set of the first cell!

你需要更改与替换后的单元格相同的行中所有单元格的集合!这在教程中从未显示过,但在算法说明中已经说明!这样,当你走迷宫时,“第一个单元格向上连接最后两个单元格”的信息将会被保留。如果集合2向下扩散到第三个单元格,你将被提示放置一堵墙来保留这样的循环。
    | 1   1   1   1   1 |
    | ↓ | ↓ | ↓ |---| ↓ |
    | 1 | 1 | 1 | 2 | 1 |
    | ↓ |-------| ↓ | ↓ |
    | 1   4   4 |(2   1)|
    | 2 | 4   4 | 2   2 |
    | ↓ |---| ↓ | ↓ |---|
    |(2   5)  4 | 2   6 |
    | 2   2 | 4 | 2 | 6 |
    |---| ↓ | ↓ | ↓ | ↓ |
    | 7  (2   4)|(2   6)|
    | 7 | 2   2 | 2   2 |
    | ↓ |---| ↓ | ↓ |---|
    |(7   8)  2  (2   9)| ← here you HAVE to put a wall between the two 2!
    | 7   7 | 2 | 2   2 |
    |---| ↓ | ↓ |-------|
    | a   7   2   b   c | ← last row connect all sets
    | a   a   a   a   a |

生成的迷宫将会是这样的:
    _____________________
    |                   |
    |   |   |   |---|   |
    |   |   |   |   |   |
    |   |-------|   |   |
    |   |       |       |
    |   |---|   |   |---|
    |       |   |   |   |
    |---|   |   |   |   |
    |   |       |       |
    |   |---|   |   |---|
    |       | Y |     X |
    |---|   |   |-------|
    |                   |
    |-------------------|

看到这里没有循环,X仍然与迷宫的其余部分连接在一起,因为在最后一步中,我们向集合2中的Y单元格开了一个向下的出口!

如果我们没有正确合并集合,结果会变成这样:

    _____________________
    |                   |
    |   |   |   |---|   |
    |   |   |   |   |   |
    |   |-------|   |   |
    |   |       |       |
    |   |---|   |   |---|
    |       |   |   |   |
    |---|   |   |   |   |
    |   |       |       |
    |   |---|   |   |---|
    |       | 1   2   2 | ← 1 and 2 are actually in the same set!
    |---|   |   |-------|
    |                   |
    |-------------------|

希望这篇晚来的翻译能对您有所帮助!

1
谢谢您指出算法中正确的集合合并方式,这让我省去了几天的头疼。 - disable1992

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接