Ejemplo n.º 1
0
    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))
Ejemplo n.º 2
0
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))
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
 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))
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
    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())