def testGolden7x9Maze(self): maze = labmaze.RandomMaze(height=7, width=9, random_seed=12345) expected_mazes = { ('*********\n' '*********\n' '*********\n' '*** ***\n' '*** ***\n' '*** ***\n' '*********\n'), # libstdc++ ('*********\n' '*********\n' '*********\n' '* ***\n' '* ***\n' '* ***\n' '*********\n'), # libc++ } actual_maze = str(maze.entity_layer) self.assertIn(actual_maze, expected_mazes) np.testing.assert_array_equal(maze.entity_layer, labmaze.TextGrid(actual_maze)) expected_variations = actual_maze.replace('*', '.').replace(' ', 'A') self.assertEqual(str(maze.variations_layer), expected_variations) np.testing.assert_array_equal(maze.variations_layer, labmaze.TextGrid(expected_variations))
def array_to_textgrid(array): str_rpr = [] for line in array: for c in line: str_rpr.append(c) str_rpr.append('\n') return lbm.TextGrid(''.join(str_rpr))
def testAssignment(self): original_string = 'abcd\nefgh\nijkl\n' maze = labmaze.TextGrid(original_string) maze[:2, :2] = '#' expected_string = '##cd\n##gh\nijkl\n' expected_array = [['#', '#', 'c', 'd'], ['#', '#', 'g', 'h'], ['i', 'j', 'k', 'l']] self.assertEqual(str(maze), expected_string) np.testing.assert_array_equal(maze, expected_array)
def testCopying(self): original_string = '1234\n5678\n' maze = labmaze.TextGrid(original_string) copied = copy.copy(maze) self.assertEqual(type(maze), type(copied)) self.assertEqual(str(maze), str(copied)) np.testing.assert_array_equal(maze, copied) deepcopied = copy.deepcopy(maze) self.assertEqual(type(maze), type(deepcopied)) self.assertEqual(str(maze), str(deepcopied)) np.testing.assert_array_equal(maze, deepcopied)
def regenerate(self): """Regenerates the maze if required.""" level = next(self._level_iterator) self._entity_layer = labmaze.TextGrid(_ascii_to_text_grid_level(level)) self._variation_layer = self._entity_layer.copy() self._variation_layer[:] = '.' self._num_boxes = (self._entity_layer == BOX_CHAR).sum() num_targets = (self._entity_layer == TARGET_CHAR).sum() if num_targets != self._num_boxes: raise ValueError( 'Number of targets {} should equal number of boxes {}.'.format( num_targets, self._num_boxes))
def testNoOverlappingWalls(self): maze_string = """..** .*** .*** """.replace(' ', '') walls = covering.make_walls(labmaze.TextGrid(maze_string)) surface = 0 for wall in walls: size_x = wall.end.x - wall.start.x size_y = wall.end.y - wall.start.y surface += size_x * size_y self.assertEqual(surface, 8)
def testConstruction(self): original_string = 'abcd\nefgh\nijkl\n' maze = labmaze.TextGrid(original_string) self.assertEqual(str(maze), original_string) self.assertEqual(maze.shape, (3, 4)) expected_array = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l']] np.testing.assert_array_equal(maze, expected_array) # Test that iteration works. for actual_row, expected_row in zip(maze, expected_array): for actual_col, expected_col in zip(actual_row, expected_row): self.assertEqual(actual_col.strip(), expected_col.strip()) # Test that indexed access works. height = len(expected_array) width = len(expected_array[0]) for y in range(height): for x in range(width): self.assertEqual(maze[y, x].strip(), expected_array[y][x].strip())