コード例 #1
0
 def test_validate_place_command(self):
     toy_robot_obj = ToyRobot()
     self.assertEqual(toy_robot_obj.validate_place_command(0,1,"NORTH"), True)
     self.assertEqual(toy_robot_obj.validate_place_command(0,1,"North"), True)
     self.assertEqual(toy_robot_obj.validate_place_command(0,1,"NOTH"), False)
     self.assertEqual(toy_robot_obj.validate_place_command(5,1,"NORTH"), False)
     self.assertEqual(toy_robot_obj.validate_place_command(0,1,"SOUTH-WEST"), False)
     self.assertEqual(toy_robot_obj.validate_place_command(5,1,"NORTH"), False)
コード例 #2
0
ファイル: app.py プロジェクト: vijayrohin/toy_robot_simulator
def execute_main():
    # install_all_packages(["readchar"])
    toy_robot_obj = ToyRobot()
    is_simulator_running = True
    print(
        "-------------------------------------------------------------------------------"
    )
    print(
        "-------------------------Toy Robot Simulator ( 5 X 5 )-------------------------"
    )
    print(
        "-------------------------------------------------------------------------------"
    )
    print(
        "=========================Available Commands and Syntax========================="
    )
    print(
        "-------------------------------------------------------------------------------"
    )
    print(
        "(1) PLACE [x_position],[y_position],[face_direction] - To place it on the table"
    )
    print("*Parameters definitions:*")
    print("**[x_position] -> between 0 and 4")
    print("**[y_position] -> between 0 and 4")
    print("**[face_direction] -> NORTH,SOUTH,EAST,WEST")
    print("(2) MOVE - To move robot one step forward in its facing direction")
    print("(3) RIGHT - To turn robot 90 degrees to its right side")
    print("(4) LEFT - To turn robot 90 degrees to its left side")
    print("(5) REPORT - To print current positions and facing direction")
    print("(6) EXIT - To exit the simulator")
    print(
        "==============================================================================="
    )
    print("Type the input commands for the robot...")
    while is_simulator_running:  # Infinte loop - stops only when user types in EXIT Command
        given_command = input(
            "New Command: "
        )  # Getting input from the user, single command at one instance
        orig_command = given_command
        ansi_escape = re.compile(
            r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])'
        )  # Removing any escape sequence characters included in raw string
        given_command = ansi_escape.sub('', given_command)
        given_command = given_command.strip()
        given_command_split_up = given_command.split(
            " ")  # Splitting up to get the intended command
        invoked_command = (given_command_split_up[0]).upper()
        if invoked_command == "EXIT" and len(
                given_command_split_up) == 1:  # To check if its exit command
            is_simulator_running = False
            print("Thanks for playing toy robot simulator!!!")
        elif invoked_command == TOY_MAIN_COMMANDS[0] and check_space_present(
                given_command) and parse_place_command(
                    given_command
                ) is not None:  # To check if its PLACE command
            parsed_dict = parse_place_command(
                given_command)  # Parsing parameters
            is_placement_valid = toy_robot_obj.validate_place_command(
                parsed_dict["x_pos"], parsed_dict["y_pos"],
                parsed_dict["direction"]
            )  # Validating parameter values for PLACE command
            if is_placement_valid:  # if valid perform assignment for placing toy robot on the table
                toy_robot_obj.set_placement_position(parsed_dict["x_pos"],
                                                     parsed_dict["y_pos"],
                                                     parsed_dict["direction"])
            else:
                print(
                    "Warning: Place command not recognized. Please enter a valid Place command (Refer from list above)"
                )
        elif invoked_command == TOY_MAIN_COMMANDS[1] and len(
                given_command_split_up) == 1:  # To check if its MOVE command
            return_val, comment = toy_robot_obj.move_forward()
            if not return_val:
                print(comment)
        elif invoked_command == TOY_MAIN_COMMANDS[2] and len(
                given_command_split_up) == 1:  # To check if its REPORT command
            result = toy_robot_obj.report_overall_position()
            if result != "":
                print(result)
            else:
                print(
                    "Warning: Toy Robot is not placed on the table. Please use a valid Place command to place it on table."
                )
        elif invoked_command == TOY_MAIN_COMMANDS[3] and len(
                given_command_split_up) == 1:  # To check if its LEFT command
            return_val, comment = toy_robot_obj.turn_position(invoked_command)
            if not return_val:
                print(comment)
        elif invoked_command == TOY_MAIN_COMMANDS[4] and len(
                given_command_split_up) == 1:  # To check if its RIGHT command
            return_val, comment = toy_robot_obj.turn_position(invoked_command)
            if not return_val:
                print(comment)
        else:
            if 'x' in repr(orig_command):
                print(
                    "Warning: Command not recognized. Please enter a valid command (Refer from list above)"
                )
            else:
                print(
                    "Warning: Command not recognized. Please enter a valid command (Refer from list above)"
                )