Я пытаюсь создать лабиринт с помощью рекурсивного возврата, но я не могу правильно вызвать create_maze (). Из моего главного меню я вызываю класс лабиринта следующим образом:
maze = Maze.create_maze (NoOfRows, NoOfColumns)
Однако я получаю сообщение об ошибке аргумента от create_maze, в котором говорится, что мне не хватает дополнительного " y "или мой self.path (0 отсутствует дополнительно y
Где я ошибаюсь?
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