#๐Ÿ”’ Maze Help

8 messages ยท Page 1 of 1 (latest)

sharp canyon
#

Can someone please help me troubleshoot this issue. I can't figure out why it's behaving as it is. I've got a maze generation script. I want the maze to generate on a grid, leaving borders on the top, bottom, left, and right - each 1-unit thick. The problem is that the the walls on the right and bottom sides are generating 2 units thick. Here is the main part of the maze generation script:

def get_neighbors(x, y):
    neighbors = []
    for direction in DIRECTIONS:
        nx, ny = x + direction[0] * 2, y + direction[1] * 2
        # Ensure that we are not going out of bounds and respect the maze's borders
        if 1 <= nx <= COLS - 1 and 1 <= ny <= ROWS - 1 and not visited[ny][nx]:
            neighbors.append((nx, ny))
    random.shuffle(neighbors)
    return neighbors

def generate_maze():
    x, y = 1, 1
    stack.append((x, y))
    visited[y][x] = True
    maze[y][x] = 0

    while stack:
        draw_maze()
        pygame.display.update()
        clock.tick(60)

        x, y = stack[-1]
        neighbors = get_neighbors(x, y)

        if neighbors:
            nx, ny = neighbors[0]
            stack.append((nx, ny))
            visited[ny][nx] = True

            # Remove the wall between the current cell and the chosen cell
            maze[(y + ny) // 2][(x + nx) // 2] = 0
            maze[ny][nx] = 0
        else:
            stack.pop()

# Initialize maze grid with all walls, leaving borders intact
for row in range(ROWS):
    for col in range(COLS):
        if row == 0 or row == ROWS - 1 or col == 0 or col == COLS - 1:
            maze[row][col] = 1  # Borders remain as walls
        else:
            maze[row][col] = 1  # Inside cells initially walls but will be carved
inland steepleBOT
#

Hey @sharp canyon!

It looks like you incorrectly specified a language for your code block.

Make sure you put your code on a new line following py. There must not be any spaces after py.

Here is an example of how it should look:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
inland steepleBOT
#

@sharp canyon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sharp canyon
#

The primary part of the maze generation script is the part that finds neighbors:

def get_neighbors(x, y):
    neighbors = []
    for direction in DIRECTIONS:
        nx, ny = x + direction[0] * 2, y + direction[1] * 2
        # Ensure that we are not going out of bounds and respect the maze's borders
        if 1 <= nx <= COLS - 1 and 1 <= ny <= ROWS - 1 and not visited[ny][nx]:
            neighbors.append((nx, ny))
    random.shuffle(neighbors)
    return neighbors

When I change the <= COLS - 1 to be < COLS - 1 it removes the border completely. But with the = it makes it 2 thick

#

I can't figure out why

#

Nevermind. I figured it out. It's because of the * 2 making the path always snap to multiples of 2n + 1

inland steepleBOT
#

@sharp canyon

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.