0

I made this maze generation program following this post

The top answer has some algorithm-style code which I tried to implement in python using this code, but for some reason it creates "loops" around 1 wide tiles, which I don't want to happen

Sample maze generated by my program

My code:

import numpy as np
import random
from gridmaker import Grid # my custom class
xlim = 50
ylim = 50
image = Grid(xlim, ylim)
fronts = []

def test_if_wall(x, y):
    global image

def scan(x, y):
    '''scanning'''
    global image
    coords = []

    if x == 1:
        x == 3
    elif y == 1:
        y == 3
    elif x == 49:
        x == 47
    elif y == 49:
        y == 47

    if image.test_colour(x + 2, y) == False:
        coords.append([x + 2, y])

    if image.test_colour(x - 2, y) == False:
        coords.append([x - 2, y])

    if image.test_colour(x, y + 2) == False:
        coords.append([x, y + 2])

    if image.test_colour(x, y - 2) == False:
        coords.append([x, y - 2])

    return coords

def check_neighbours(x, y):
    global image
    coords = []

    if x == 1:
        x == 3
    elif y == 1:
        y == 3
    elif x == 49:
        x == 47
    elif y == 49:
        y == 47 

    if image.test_colour(x + 2, y) == True:
        coords.append([x + 2, y])

    if image.test_colour(x - 2, y) == True:
        coords.append([x - 2, y])

    if image.test_colour(x, y + 2) == True:
        coords.append([x, y + 2])

    if image.test_colour(x, y - 2) == True:
        coords.append([x, y - 2])
    
    return coords

def join_to_rneighbour(x, y):
    global image
    
    nb = check_neighbours(x,y)
    #print(nb)
    rand = random.choice(nb)
    if rand == [x + 2, y]:
        image.set_white(x + 1, y)
        image.set_white(x, y)

    elif rand == [x - 2, y]:
        image.set_white(x - 1, y)
        image.set_white(x, y)

    elif rand == [x, y + 2]:
        image.set_white(x, y + 1)
        image.set_white(x, y)

    elif rand == [x, y - 2]:
        image.set_white(x, y - 1)
        image.set_white(x, y)

    


fronts = []

def logic(x, y):
    global image, fronts
    _fronts = scan(x, y)
    for item in _fronts:
        fronts.append(item)
    image.set_white(x, y)
    while fronts != []:
        rand = random.choice(fronts)
        xp = rand[0]
        yp = rand[1]
        join_to_rneighbour(xp, yp)
        fronts.remove([xp, yp])
        logic(xp, yp)

logic(3, 3)
print(image.test_colour(0,0))
image.refresh()

(I'm writing this in an IPython notebook)

class Grid() makes a matplotlib graph like the one in the image and it can:

  1. Set a block to white or black: image.set_white(x, y) sets cell to 1, image.set_black(x, y) sets cell to 0
  2. Test colour: image.test_colour(x, y) returns True if there is a passage there and False if there is a wall there
  3. Refresh graph: image.refresh()
  4. Grid.grid is a numpy.zeros array of specified size 50, 50

I've also tried switching the function in join_to_rneighbour() to scan() instead of check_neighbours() but it then creates "loops" where there are walls on all four sides of a cell.

leaves
  • 111
  • 1
  • 12
  • What is `x == 3` supposed to do? – Kaz Aug 14 '21 at 06:41
  • @Kaz It was an attempt to stop the maze from going off the boundaries while having a 1 wide border around the maze, but it failed so I decided to create a 1 wide boundary around the maze after generation. – leaves Aug 14 '21 at 06:45
  • `==` is doing a test. if `x == 1: x == 3` will do nothing `x == 3` is `False`. I imagine you want `x = 3` – mozway Aug 14 '21 at 06:52
  • @mozway I'll try that, but my main concern at the moment is why my maze is making loops. – leaves Aug 14 '21 at 07:05
  • @leaves You seem to have coded that. `check_neighbors` looks for white neighbors: i.e. cells containing passages. You then dig tunnels to those neighbors. That creates loops. To generate a maze, find frontier cells that have not been visited yet, and dig tunnels to those cells in one integrated step. – Kaz Aug 14 '21 at 18:49
  • @Kaz Isn't that what the code is already doing by finding a neighboring passage cell from a frontier wall and digging to it? Could you elaborate more on your solution and explain on how I could implement it? – leaves Aug 15 '21 at 05:20

0 Answers0