Beispiel #1
0
    def execute(self, searchMethod, granularity, trajectory, saveImage,
                saveMaze):
        self.initialize()
        if not self.running:
            print("Program init failed")
            raise SystemExit

        currAngle = [0, 0, 0]
        for i in range(len(self.arm.getArmAngle())):
            currAngle[i] = self.arm.getArmAngle()[i]
        self.gameLoop()

        if not self.__human:
            print("Transforming a map configuration to a maze...")
            maze = transformToMaze(self.arm, self.goals, self.obstacles,
                                   self.window, granularity)
            print("Done!")
            print("Searching the path...")
            path, num_explored = search(maze, searchMethod)
            for i in range(len(path)):
                self.arm.setArmAngle(path[i])
                if (trajectory > 0) and (i % trajectory == 0):
                    self.trajectory.append(self.arm.getArmPos())
                self.gameLoop()
            print("Done!")

        while self.running:
            pygame.event.pump()
            keys = pygame.key.get_pressed()

            if (keys[K_ESCAPE]):
                self.running = False

            if self.__human:
                alpha, beta, gamma = currAngle
                if (keys[K_z]):
                    alpha += granularity if isValueInBetween(
                        self.armLimits[ALPHA], alpha + granularity) else 0

                if (keys[K_x]):
                    alpha -= granularity if isValueInBetween(
                        self.armLimits[ALPHA], alpha - granularity) else 0

                if (keys[K_a]):
                    beta += granularity if isValueInBetween(
                        self.armLimits[BETA], beta + granularity) else 0

                if (keys[K_s]):
                    beta -= granularity if isValueInBetween(
                        self.armLimits[BETA], beta - granularity) else 0

                if (keys[K_q]):
                    gamma += granularity if isValueInBetween(
                        self.armLimits[GAMMA], gamma + granularity) else 0

                if (keys[K_w]):
                    gamma -= granularity if isValueInBetween(
                        self.armLimits[GAMMA], gamma - granularity) else 0

                newAngle = (alpha, beta, gamma)
                tempArm = copy.deepcopy(self.arm)
                tempArm.setArmAngle(newAngle)
                armEnd = tempArm.getEnd()
                armPos = tempArm.getArmPos()

                if doesArmTouchObstacles(
                        armPos, self.obstacles) or not isArmWithinWindow(
                            armPos, self.window):
                    continue

                if not doesArmTouchGoals(armEnd,
                                         self.goals) and doesArmTouchObstacles(
                                             armPos, self.goals):
                    continue

                self.arm.setArmAngle(newAngle)
                self.gameLoop()
                currAngle = copy.deepcopy(newAngle)

                if doesArmTouchGoals(armEnd, self.goals):
                    self.gameLoop()
                    print("SUCCESS")
                    raise SystemExit

        if saveImage:
            pygame.image.save(self.displaySurface, saveImage)

        if saveMaze and not self.__human:
            print(saveMaze)
            maze.saveToFile(saveMaze)
        print(saveMaze)
Beispiel #2
0
def build_maze_basic(configfile, map_name):
	config = configparser.ConfigParser()

    # set 'data/' to you config directory
	config.read('data/' + configfile)
	window = eval(config.get(map_name, 'Window'))
	armBase = eval(config.get(map_name, 'ArmBase'))
	armLinks = eval(config.get(map_name, 'ArmLinks'))

	arm1 = Arm(armBase, armLinks)
	obstacles = eval(config.get(map_name, 'Obstacles'))
	goals = eval(config.get(map_name, 'Goals'))

	return arm1, goals, obstacles, window

# modify configfile to the path of actual config file
# To test if your extra credit code run with autograder, put it in the same folder with your code and run debug.py. If all goes well, the program should print out the path found.
configfile, map_name, granularity = "test_config_part4.txt", "Test1", 10
# configfile, map_name, granularity = "test_config.txt", "Test2", 5

arm, goals, obstacles, window = build_maze_basic(configfile, map_name)
arm_student = copy.deepcopy(arm)
student_maze = transform_student.transformToMaze(
    arm_student, goals, obstacles, window, granularity
)

student_path = search_student.search(student_maze, "bfs")

print(student_path)