1

I'm trying to create a maze through recursive backtracking but I can't seem to call create_maze() properly. From my main menu I call the maze class like this

maze = Maze.create_maze(NoOfRows, NoOfColumns)

However, I get an argument error from create_maze saying I'm missing an additional "y" or my self.path(0 is missing an additional y

Where am I going wrong?

from numpy.random import random_integers as rand
from Generate import ascii_representation
from constants import *
import numpy as np

WALL_TYPE = np.int8
WALL = 0
EMPTY = 1
RED = 2
BLUE = 3


class Maze:
    def __init__(self, Width, Height):
        self.Width = Width
        self.Height = Height
        self.board = np.zeros((Width, Height), dtype=WALL_TYPE)
        self.board.fill(EMPTY)

    def set_borders(self):
        self.board[0, :] = self.board[-1, :] = WALL
        self.board[:, 0] = self.board[:, -1] = WALL

    def is_wall(self, x, y):
        return self.board[x][y] == WALL

    def set_wall(self, x, y):
        self.board[x][y] = WALL

    def remove_wall(self, x, y):
        self.board[x][y] = EMPTY

    def in_maze(self, x, y):
        return 0 <= x < self.Width and 0 <= y < self.Height

    def write_to_file(self, filename):
        f = open(filename, 'w')
        f.write(ascii_representation(self))
        f.close()

    def set_path(self, x, y):
       self.board[y][x] = False

    @staticmethod
    def load_from_file(filename):
        with open(filename, 'r') as f:
            content = f.readlines()

        # remove whitespace characters like `\n` at the end of each line
        content = [x.strip() for x in content]

        xss = []
        for line in content:
            xs = []

            for c in line:
                if c == ' ':
                    xs.append(EMPTY)
                elif c == 'X':
                    xs.append(WALL)
                else:
                    raise ValueError('unexpected character found: ' + c)

            xss.append(xs)

        maze = Maze(len(xss), len(xss[0]))

        for xs in xss:
            assert len(xs) == maze.Height

        for i in range(maze.Width):
            for j in range(maze.Height):
                if xss[i][j] == EMPTY:
                    maze.remove_wall(i, j)
                else:
                    maze.set_wall(i, j)

        return maze

    @staticmethod
    def complete_maze(Width, Height):
        maze = Maze(Width, Height)

        for i in range(Width):
            for j in range(Height):
                maze.board[i][j] = WALL

        return maze


    def create_maze(x, y):
       Maze.set_path(x, y)
       all_directions = [[1, 0], [-1, 0], [0, 1], [0, -1]]
       random.shuffle(all_directions)

       while len(all_directions) > 0:
           direction_to_try = all_directions.pop()
           node_x = x + (direction_to_try[0] * 2)
           node_y = y + (direction_to_try[1] * 2)

           if Maze.is_wall(node_x, node_y):
               link_cell_x = x + direction_to_try[0]
               link_cell_y = y + direction_to_try[1]
               self.set_path(link_cell_x, link_cell_y)
               self.create_maze(node_x, node_y)
       return

1 Answers1

1

Take a look at the definition for set_path:

def set_path(self, x, y):

This expects three parameters. When you call it with Maze.set_path(x,y), you're only giving it two: x and y. Python expects three and in the order self, then x, then y. Python is interpreting the x you give to be self, then the y to be x and then gives an error that y isn't given. But what is actually missing is self!

To fix this, you need to change this call to self.set_path(x, y) (which is a shortcut for Maze.set_path(self, x, y)). You'll also need to pass self to create_maze as well by changing its definition to def create_maze(self, x, y). And finally, change the way you call create_maze() to something like

maze = Maze(NoOfRows, NoOfCols).create_maze(NoOfRows, NoOfColumns)

As an aside, it seems you never use self.Width or self.Height. You should either remove them from __init__ and just take the x and y passed in create_maze(), or change create_maze() to use self.Width and self.Height instead of taking x and y as parameters.

Kexus
  • 659
  • 2
  • 6
  • 13
  • Thank you! Now I get a different error for self path when I pass 50 and 50 for x and y , line 35, in set_path self.board[y][x] = False IndexError: index 50 is out of bounds for axis 0 with size 50 – LordLightingXII Mar 05 '20 at 17:55
  • That's because your board is x by y in size, meaning its indices range from (0 to x-1) by (0 to y-1). Accessing board[x][y] is thus out of bounds. You'll need to start from a different position, and include some checks in your functions to avoid going out of bounds. – Kexus Mar 05 '20 at 18:02