Пример #1
0
	def test_place(self):
		toyRobot = ToyRobot()
		toyRobot.place(0, 0, Direction.NORTH)
		self.assertEqual(toyRobot.report(), "0, 0, NORTH")

		toyRobot.place(1, 1, Direction.WEST)
		self.assertEqual(toyRobot.report(), "1, 1, WEST")

		toyRobot.place(0, 1, Direction.EAST)
		self.assertEqual(toyRobot.report(), "0, 1, EAST")

		toyRobot.place(1, 2, Direction.SOUTH)
		self.assertEqual(toyRobot.report(), "1, 2, SOUTH")

		toyRobot.place(-1, 2, Direction.SOUTH)
		self.assertNotEqual(toyRobot.report(), "-1, 2, SOUTH")
Пример #2
0
	def test_right(self):
		toyRobot = ToyRobot()

		toyRobot.right() # right must not work before place
		self.assertEqual(toyRobot.report(), None)

		toyRobot.place(0, 0, Direction.NORTH)

		toyRobot.right()
		self.assertEqual(toyRobot.report(), "0, 0, EAST")

		toyRobot.right()
		self.assertEqual(toyRobot.report(), "0, 0, SOUTH")

		toyRobot.right()
		self.assertEqual(toyRobot.report(), "0, 0, WEST")

		toyRobot.right()
		self.assertEqual(toyRobot.report(), "0, 0, NORTH")
Пример #3
0
class Simulation():
    """Simulation
    Represents one simulation in which for a given text one toy_robot is created and
    does the commands specified.
    After executing all the commands the results of each REPORT command are outputed to stdout.
    """
    def __init__(self, command_text):
        self.commands = get_commands_from_input(command_text)
        self.output_texts = []
        self.toy_robot = ToyRobot()
        self.movement_dict = {
            'LEFT': self.toy_robot.rotate_left,
            'RIGHT': self.toy_robot.rotate_right,
            'MOVE': self.toy_robot.move
        }
        self.execute_commands()
        self.output_results()

    def execute_commands(self):
        """execute_commands
        For each command in self.commands call the appropriate ToyRobot action
        """

        for command in self.commands:
            if command in self.movement_dict:
                self.movement_dict[command]()
            elif command == 'REPORT':
                report = self.toy_robot.report()
                if report:
                    self.output_texts.append(report)
            elif command.startswith('PLACE'):
                try:
                    x, y, direction = parse_place_command(command)
                    self.toy_robot.set_position(x, y, direction)
                except InputException:
                    '''Exception
                    Silently ignore it to continue robot movement
                    '''
                    continue

    def output_results(self):
        """output_results
        Output all the texts available in self.output_texts which were filled
        in execute_commands
        """

        for output_text in self.output_texts:
            print(output_text)
Пример #4
0
 def test_report(self):
     robot = ToyRobot(0, 4, "EAST")
     x, y, direction = robot.report()
     self.assertEqual(x, 0)
     self.assertEqual(y, 4)
     self.assertEqual(direction, "EAST")
Пример #5
0
	def test_move(self):
		toyRobot = ToyRobot()

		toyRobot.place(0, 0, Direction.NORTH)

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "1, 0, NORTH")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "2, 0, NORTH")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "3, 0, NORTH")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "4, 0, NORTH")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "4, 0, NORTH")  # validate not going over the edge

		toyRobot.place(0, 0, Direction.EAST)

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 1, EAST")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 2, EAST")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 3, EAST")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 4, EAST")

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 4, EAST")  # validate not going over the edge

		toyRobot.place(0, 0, Direction.WEST)

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 0, WEST")  # validate not going over the edge

		toyRobot.place(0, 0, Direction.SOUTH)

		toyRobot.move()
		self.assertEqual(toyRobot.report(), "0, 0, SOUTH")  # validate not going over the edge