def deploy_rover(self):
        prompt = "\nWhere should the rover be deployed?\n" if len(self.rovers) == 0 \
            else "\nWhere should the next rover be deployed?\n"
        while True:
            try:
                coordinates = input(prompt).split()
                x = int(coordinates[0])
                y = int(coordinates[1])
                d = coordinates[2].upper()
                if d not in ['N', 'S', 'E', 'W']:
                    MissionComms.print_warn(
                        f"Not a valid direction, options are N, S, E, and W")
                    continue
                if x > self.max_x or y > self.max_y:
                    MissionComms.print_warn(
                        f"Not a valid position, try and keep it in bounds")
                    continue
                rover = Rover("Rover", x, y, d)
                self.collision_avoidance(rover, deployment=True)
                break
            except (ValueError, IndexError):
                MissionComms.print_warn(
                    f"Expecting 2 numbers and a direction in the format: 0 0 N"
                )
                continue
            except RoverError:
                MissionComms.print_warn(
                    f"A rover is already deployed there, try again.")
                continue

        MissionComms.print_info(
            f"{rover.name} deployed at position:\t {rover.get_rover_position()}"
        )
        self.rovers.append(rover)
        self._drive_rover(rover)
    def drive_rovers(self):
        print("")
        print(
            "Which rover would you like to give orders to? (choose a number)")
        MissionComms.print_bold("----------------------------------")
        MissionComms.print_bold(f"Nr \t| Name  \t| Position")
        MissionComms.print_bold("----------------------------------")
        for index, rover in enumerate(self.rovers):
            MissionComms.print_bold(
                f"{index} \t| {rover.name}  \t| {rover.get_rover_position()}")

        while True:
            try:
                index = int(input())
                if 0 <= index < len(self.rovers):
                    break
            except ValueError:
                pass

            MissionComms.print_warn(
                f"Please provide a valid number between 0 and {len(self.rovers) - 1}"
            )

        rover = self.rovers[index]
        commands = input(f"\nPlease enter commands for {rover.name}:\n")
        self._manage_rover(rover, commands)
 def _manage_rover(self, rover: Rover, commands: str):
     for command in list(commands):
         try:
             self._action_command(command, rover)
         except CommandAbort:
             break
     MissionComms.print_info(
         f"{rover.name} is now at position: {rover.get_rover_position()}")
    def deploy_rovers(self):
        max_rovers = 4 if self.max_x > 4 else self.max_x
        while True:
            number_of_rovers = input(
                f"\nNumber of rovers (1-{max_rovers}) to deploy?\n")
            if number_of_rovers.isdigit():
                if int(number_of_rovers) > max_rovers:
                    MissionComms.print_fail(
                        f"Can't deploy more than {max_rovers} rovers!")
                    continue
                if int(number_of_rovers) <= 0:
                    MissionComms.print_fail(
                        "We need some rovers, choose a number between 1 and 4")
                    continue
                break
            MissionComms.print_fail(
                f"Expecting a number between 1 and {max_rovers}")
            continue

        for r in range(int(number_of_rovers)):
            rover = Rover(ROVER_NAMES[r], r, 0, 'N')
            self.rovers.append(rover)
            MissionComms.print_info(
                f"Rover {rover.name} deployed at position:\t {rover.get_rover_position()}"
            )
 def _action_command(self, command: str, rover: Rover) -> None:
     command = command.upper()
     if command == 'L':
         rover.rotate_left()
     elif command == 'R':
         rover.rotate_right()
     elif command == 'M':
         try:
             self.collision_avoidance(rover)
             rover.move_forward()
         except RoverError:
             raise CommandAbort("Aborting commands")
     else:
         MissionComms.print_warn(
             f"{command} is not a valid command, ignoring this command.")
Exemplo n.º 6
0
def brief() -> None:
    print("-------------------------------------------------------")
    MissionComms.print_bold("""
        Welcome to the Mission to mars!
        
        Brief:
        ------
            You are to deploy rovers to mars and journey the unknown.
        
        Instructions:
        -------------
            -  First you will define the size of the plateau by giving the top x and y coordinates.
                The minimum x and y coordinates are both 0.
            -  To deploy a rover you specify it's coordinates and direction, 
                for example `1 2 N` will deploy a rover at position x = 1 and y = 2 facing North.
            - Valid directions are N, S, E and W.
            - To move a rover you have one of three options:
                - `M` will move the rover forward in the direction it is facing.
                    e.g. Given rover position `0 0 N`: `M` will result in rover position `0 1 N`
                - `R` will rotate the rover 90 degrees clockwise but NOT move it from it's current position.
                    e.g. Given rover position `0 0 N`: `R` will result in rover position `0 0 E`
                - `L` will rotate the rover 90 degrees anti-clockwise but NOT move it from it's current position.
                    e.g. Given rover position `0 0 N`: `L` will result in rover position `0 0 W`
            - Moves can be combined to move the rover, for example:                
                Given rover position `0 0 N`: `MMRMMLM` will result in rover position `2 3 N`
        
        Note on Mission Levels:
        -----------------------
            Two levels were created for this mission, namely Beginner and Advanced.
            - Beginner is the basic assignment use case:
                You deploy a rover at a position by giving it coordinates. 
                Once the first rover is moved you move on to the second one. 
                Only 2 rovers will be deployed. 
            - Advanced is making things more interesting and interactive. 
                You decide how many rovers you deploy. The maximum is dependent on the size of the plateau.
                All rovers are deployed at the bottom of the plateau, next to each other. 
                Once deployed you select the rover you want to move, move it and repeat.
                To quit you just hit Ctrl-C.
                
        Last words:
        -----------
            Try not to fall off of the plateau and don't crash into other rovers.
            May the terminal be with you. 
    """)
    print("-------------------------------------------------------")
 def scout_plateau(self):
     while True:
         size = input(f"\nHow big is the plateau?\nFor example: `5 5`\n")
         max_coordinates = size.split()
         try:
             self.max_x = int(max_coordinates[0])
             self.max_y = int(max_coordinates[1])
             if self.max_x == 0 or self.max_y == 0:
                 MissionComms.print_warn(
                     "We can do better than that, make the plateau bigger than 0"
                 )
                 continue
             MissionComms.print_info(
                 f"Plateau confirmed to be {self.max_x}x{self.max_y}")
             break
         except (IndexError, ValueError):
             MissionComms.print_warn(
                 "Expecting 2 coordinates separated by a space, for example: 5 5"
             )
             continue
Exemplo n.º 8
0
    def __init__(self, message):
        MissionComms.print_fail(message)

        super().__init__(message)
Exemplo n.º 9
0
                Once deployed you select the rover you want to move, move it and repeat.
                To quit you just hit Ctrl-C.
                
        Last words:
        -----------
            Try not to fall off of the plateau and don't crash into other rovers.
            May the terminal be with you. 
    """)
    print("-------------------------------------------------------")


if __name__ == '__main__':
    brief()

    print("Mission options: (choose a number)")
    MissionComms.print_bold("----------------------------------")
    MissionComms.print_bold(f"Nr \t| Level \t| Description")
    MissionComms.print_bold("----------------------------------")
    MissionComms.print_bold(
        f"0 \t| Beginner \t| Deploy one rover at a time (The assignment use case)"
    )
    MissionComms.print_bold(
        f"1 \t| Advanced \t| Deploy multiple rovers at once (I got carried away...)"
    )

    while True:
        try:
            l = input()
            level = int(l)
            if 0 <= level < 2:
                break