Пример #1
0
    def generate(self, size: tuple, start_state: tuple,
                 goal_state: tuple) -> list:
        """ Generate a maze with given dimensions and start/goal states """
        maze = list()
        untouchables = dict({start_state: 1, goal_state: 1})
        for row in range(size[0]):
            column = list()
            for col in range(size[1]):
                if (row, col) == start_state:
                    column.append(self.locations['start'])
                elif (row, col) == goal_state:
                    column.append(self.locations['goal'])
                else:
                    column.append(rand.choice(list(self.features.values())))
            maze.append(column)

        while Agent.BFS(maze,
                        start_state,
                        goal_value=self.locations['goal'],
                        wall_value=self.features['wall'])[0] == False:
            location1 = rand.randint(0, size[0] - 1), rand.randint(
                0, size[1] - 1)
            location2 = rand.randint(0, size[0] - 1), rand.randint(
                0, size[1] - 1)

            # It is still possible for l1 to = l2, due to later reassignment
            # The chancees were already low enough that I was questioning this next while loop anyhow
            while location1 == location2:
                location2 = rand.randint(0, size[0] - 1), rand.randint(
                    0, size[1] - 1)

            while location1 in untouchables:
                location1 = rand.randint(0, size[0] - 1), rand.randint(
                    0, size[1] - 1)

            while location2 in untouchables:
                location2 = rand.randint(0, size[0] - 1), rand.randint(
                    0, size[1] - 1)

            maze[location1[0]][location1[1]], maze[location2[0]][
                location2[1]] = maze[location2[0]][location2[1]], maze[
                    location1[0]][location1[1]]
        return maze