Python 递归回溯创建迷宫
Python Recurisve Backtracking to create a maze
我正在尝试通过递归回溯创建一个迷宫,但我似乎无法正确调用 create_maze()。在我的主菜单中,我这样称呼迷宫 class
迷宫 = 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
看看 set_path 的定义:
def set_path(self, x, y):
这需要三个参数。当你用 Maze.set_path(x,y)
调用它时,你只给了它两个:x
和 y
。 Python 需要三个,顺序是 self
,然后是 x
,然后是 y
。 Python 正在将您给出的 x
解释为 self
,然后将 y
解释为 x
,然后给出一个错误,即 y
不是给定的。但实际上缺少的是 self
!
要解决此问题,您需要将此调用更改为 self.set_path(x, y)
(这是 Maze.set_path(self, x, y)
的快捷方式)。您还需要将 self
传递给 create_maze
,方法是将其定义更改为 def create_maze(self, x, y)
。最后,将您调用 create_maze()
的方式更改为
maze = Maze(NoOfRows, NoOfCols).create_maze(NoOfRows, NoOfColumns)
顺便说一句,您似乎从未使用过 self.Width
或 self.Height
。您应该将它们从 __init__
中删除并仅采用 create_maze()
中传递的 x
和 y
,或者将 create_maze()
更改为使用 self.Width
和 self.Height
而不是将 x
和 y
作为参数。
我正在尝试通过递归回溯创建一个迷宫,但我似乎无法正确调用 create_maze()。在我的主菜单中,我这样称呼迷宫 class
迷宫 = 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
看看 set_path 的定义:
def set_path(self, x, y):
这需要三个参数。当你用 Maze.set_path(x,y)
调用它时,你只给了它两个:x
和 y
。 Python 需要三个,顺序是 self
,然后是 x
,然后是 y
。 Python 正在将您给出的 x
解释为 self
,然后将 y
解释为 x
,然后给出一个错误,即 y
不是给定的。但实际上缺少的是 self
!
要解决此问题,您需要将此调用更改为 self.set_path(x, y)
(这是 Maze.set_path(self, x, y)
的快捷方式)。您还需要将 self
传递给 create_maze
,方法是将其定义更改为 def create_maze(self, x, y)
。最后,将您调用 create_maze()
的方式更改为
maze = Maze(NoOfRows, NoOfCols).create_maze(NoOfRows, NoOfColumns)
顺便说一句,您似乎从未使用过 self.Width
或 self.Height
。您应该将它们从 __init__
中删除并仅采用 create_maze()
中传递的 x
和 y
,或者将 create_maze()
更改为使用 self.Width
和 self.Height
而不是将 x
和 y
作为参数。