def set_walls(self, matrix): """Set walls and solve the maze. No-op if maze is unsolvable. `matrix`: A 2D matrix with negative values for walls """ mstart = numpy.zeros(matrix.shape + (3,), dtype=numpy.int32) mstart[self.start_point + (0,)] = mstart[(1, 1, 1)] = 1 mstart[self.start_cranny + (2,)] = 1 mstart[self.start_point + (2,)] = 1 for ball in self.balls: tile_pos = self.pixel_to_tile(ball.pos) mstart[int(tile_pos[0]), int(tile_pos[1]), 2] = 1 corridors = matrix >= 0 m = solvemaze(corridors, mstart) if m is None: return False m[self.start_point + (0,)] = m[(1, 1, 1)] = 1 self.bdist = m[:, :, 2] self.dist = m[:, :, 1] m = m[:, :, 0] self.matrix = numpy.select([matrix < 0, True], [matrix, m]) # Clear cache of unsuccessful building attempts self.build_tries = defaultdict(set) return True
def set_walls(self, matrix): """Set walls and solve the maze. No-op if maze is unsolvable. `matrix`: A 2D matrix with negative values for walls """ mstart = numpy.zeros(matrix.shape + (3, ), dtype=numpy.int32) mstart[self.start_point + (0, )] = mstart[(1, 1, 1)] = 1 mstart[self.start_cranny + (2, )] = 1 mstart[self.start_point + (2, )] = 1 for ball in self.balls: tile_pos = self.pixel_to_tile(ball.pos) mstart[int(tile_pos[0]), int(tile_pos[1]), 2] = 1 corridors = matrix >= 0 m = solvemaze(corridors, mstart) if m is None: return False m[self.start_point + (0, )] = m[(1, 1, 1)] = 1 self.bdist = m[:, :, 2] self.dist = m[:, :, 1] m = m[:, :, 0] self.matrix = numpy.select([matrix < 0, True], [matrix, m]) # Clear cache of unsuccessful building attempts self.build_tries = defaultdict(set) return True
def solvemaze(self): """“Solve” the matrix maze; return True if successful """ self.setupMatrix() matrix = self.matrix mstart = numpy.zeros(matrix.shape + (1,), dtype=numpy.int32) mstart[matrix == -1] = 1 corridors = matrix >= -1 m = solvemaze(corridors, mstart, costs=self.costs) if m is None: return False m = m[:, :, 0] self.matrix = numpy.select([matrix < 0, True], [matrix, m]) self.draw() return True
def solvemaze(self): """“Solve” the matrix maze; return True if successful """ self.setupMatrix() matrix = self.matrix mstart = numpy.zeros(matrix.shape + (1, ), dtype=numpy.int32) mstart[matrix == -1] = 1 corridors = matrix >= -1 m = solvemaze(corridors, mstart, costs=self.costs) if m is None: return False m = m[:, :, 0] self.matrix = numpy.select([matrix < 0, True], [matrix, m]) self.draw() return True