def getTileDelta(self, x, y, direction, spaces=1): """Get tile <spaces> away in direction from (x, y) >>> m = Maze() >>> m.getTileDelta(3, 4, RIGHT).hasWall(RIGHT) False >>> m.setTile(4, 4, RIGHT, True) >>> m.getTileDelta(3, 4, RIGHT).hasWall(RIGHT) True >>> m.getTileDelta(2, 4, RIGHT, 2).hasWall(RIGHT) True >>> m.getTileDelta(4, 2, UP, 2).hasWall(RIGHT) True >>> m.getTileDelta(6, 4, LEFT, 2).hasWall(RIGHT) True >>> m.getTileDelta(4, 6, DOWN, 2).hasWall(RIGHT) True """ delta = getDelta(direction) dx, dy = delta dx *= spaces dy *= spaces assert x + dx >= 0, "X coordinate less than 0" assert x + dx < self.width, "X coordinate greater than width" assert y + dy >= 0, "Y coordinate less than 0" assert y + dy < self.height, "Y coordinate greater than height" return self._grid[x + dx][y + dy]
def generateMaze(self): # TODO """Generate maze using Prim's algorithm""" import random center_x = Maze.width // 2 center_y = Maze.height // 2 center_tile = self.getTile(center_x, center_y) # Add center tile and center's walls cells = [self.getTile(center_x, center_y)] walls = [(center_x, center_y, direction) for direction in [RIGHT, UP, LEFT, DOWN]] while walls != []: # Choose a random wall wall = walls[random.randint(0, len(walls) - 1)] x, y = wall[0], wall[1] direction = wall[2] try: # Get wall's neighboring tile newTile = self.getTileDelta(x, y, direction) # If neighbor tile is not already in cells, add it and its walls if newTile not in cells: # Do some magic that I can't be bothered to explain nx, ny = x + getDelta(direction)[0], y + getDelta( direction)[1] self.setTile(x, y, direction, False) cells.append(newTile) for d in [RIGHT, UP, LEFT, DOWN]: newWall = (nx, ny, d) newWallAlt = (nx + getDelta(d)[0], ny + getDelta(d)[1], getOpposite(d)) if (newWall not in walls) and (newWallAlt not in walls): walls.append(newWall) else: walls.remove(wall) except AssertionError: # If the wall's neighbor is outside of the grid, remove it walls.remove(wall) pass
def generateMaze(self): # TODO """Generate maze using Prim's algorithm""" import random center_x = Maze.width // 2 center_y = Maze.height // 2 center_tile = self.getTile(center_x, center_y) # Add center tile and center's walls cells = [self.getTile(center_x, center_y)] walls = [(center_x, center_y, direction) for direction in [RIGHT, UP, LEFT, DOWN]] while walls != []: # Choose a random wall wall = walls[random.randint(0, len(walls) - 1)] x, y = wall[0], wall[1] direction = wall[2] try: # Get wall's neighboring tile newTile = self.getTileDelta(x, y, direction) # If neighbor tile is not already in cells, add it and its walls if newTile not in cells: # Do some magic that I can't be bothered to explain nx, ny = x + getDelta(direction)[0], y + getDelta(direction)[1] self.setTile(x, y, direction, False) cells.append(newTile) for d in [RIGHT, UP, LEFT, DOWN]: newWall = (nx, ny, d) newWallAlt = (nx + getDelta(d)[0], ny + getDelta(d)[1], getOpposite(d)) if (newWall not in walls) and (newWallAlt not in walls): walls.append(newWall) else: walls.remove(wall) except AssertionError: # If the wall's neighbor is outside of the grid, remove it walls.remove(wall) pass