Ejemplo n.º 1
0
 def test_avoid_same_cell(self):
     robot = ToyRobot.Robot()
     robot.clear_obstacles()
     robot.avoid(3,3)
     robot.avoid(3,3)
     expected = [[3,3]]#avoid duplicates
     self.assertEqual(expected, robot.report_obstacles())
Ejemplo n.º 2
0
 def test_avoid_obstacle(self):
     robot = ToyRobot.Robot()
     robot.clear_obstacles()
     robot.avoid(3,3)
     robot.place(3,2,'NORTH')
     robot.move()
     expected = "3,2,NORTH"
     self.assertEqual(expected, robot.report())
Ejemplo n.º 3
0
 def test_example_c(self):
     robot = ToyRobot.Robot()
     robot.place(1, 2, 'EAST')
     robot.move()
     robot.move()
     robot.rotate('LEFT')
     robot.move()
     expected = "3,3,NORTH"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 4
0
 def test_move_north_when_at_top_of_grid(self):
     robot = ToyRobot.Robot()
     robot.place(3, 5, 'NORTH')
     robot.move()
     expected = "3,5,NORTH"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 5
0
 def test_example_a(self):
     robot = ToyRobot.Robot()
     robot.place(0, 0, 'NORTH')
     robot.move()
     expected = "0,1,NORTH"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 6
0
 def test_example_b(self):
     robot = ToyRobot.Robot()
     robot.place(0, 0, 'NORTH')
     robot.rotate('LEFT')
     expected = "0,0,WEST"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 7
0
 def test_success_place(self):
     robot = ToyRobot.Robot()
     robot.clear_obstacles()
     robot.avoid(3,3)
     robot.place(2,2,'NORTH')
     self.assertEqual(robot.place_flag, True)
Ejemplo n.º 8
0
 def test_clear_obstacles(self):
     robot = ToyRobot.Robot()
     robot.avoid(3,3)
     robot.avoid(2,2)
     robot.clear_obstacles()
     self.assertEqual(robot.report_obstacles(), [])
Ejemplo n.º 9
0
 def test_place_without_direction_and_not_placed(self):
     robot = ToyRobot.Robot()
     robot.place(5, 5)
     self.assertEqual(robot.report_placed(), False)
Ejemplo n.º 10
0
 def test_initial_place(self):
     robot = ToyRobot.Robot()
     robot.place(2, 2, 'NORTH')
     expected = "2,2,NORTH"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 11
0
 def test_rotate_right_from_west(self):
     robot = ToyRobot.Robot()
     robot.place(2, 0, 'WEST')
     robot.rotate('RIGHT')
     expected = "2,0,NORTH"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 12
0
 def test_rotate_left_from_west(self):
     robot = ToyRobot.Robot()
     robot.place(2, 0, 'WEST')
     robot.rotate('LEFT')
     expected = "2,0,SOUTH"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 13
0
 def test_rotate_right_from_north(self):
     robot = ToyRobot.Robot()
     robot.place(2, 0, 'NORTH')
     robot.rotate('RIGHT')
     expected = "2,0,EAST"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 14
0
 def test_rotate_left_from_north(self):
     robot = ToyRobot.Robot()
     robot.place(2, 0, 'NORTH')
     robot.rotate('LEFT')
     expected = "2,0,WEST"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 15
0
 def test_move_east_when_at_north_east_of_grid(self):
     robot = ToyRobot.Robot()
     robot.place(5, 5, 'EAST')
     robot.move()
     expected = "5,5,EAST"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 16
0
 def test_move_south_when_at_bottom_of_grid(self):
     robot = ToyRobot.Robot()
     robot.place(1, 0, 'SOUTH')
     robot.move()
     expected = "1,0,SOUTH"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 17
0
 def test_place_without_direction_and_already_placed(self):
     robot = ToyRobot.Robot()
     robot.place(3, 3, 'NORTH')
     robot.place(5, 5)
     expected = "5,5,NORTH"
     self.assertEqual(expected, robot.report())
Ejemplo n.º 18
0
 def test_non_initial_place(self):
     robot = ToyRobot.Robot()
     robot.place(4, 4, 'SOUTH')
     robot.place(0, 0, 'EAST')
     expected = "0,0,EAST"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 19
0
import re
import ToyRobot

# Entry point of program. This file is where user input functionality is provided

command = ''
robot = ToyRobot.Robot()
is_placed = False
regex = re.compile(r'^PLACE\s[0-5][,][0-5][,](NORTH|SOUTH|EAST|WEST)')  # uses regex to enforce valid PLACE input
regex_placed = re.compile(r'^PLACE\s[0-5][,][0-5]')

while True:
    command = input("Enter an instruction for robot to execute: ")
    if command == 'QUIT':
        break
    if not is_placed:
        if regex.match(command):  # If the command matches the format PLACE X,X,DIRECTION
            is_placed = True  # Flag allows other commands to be executed
            coords = re.findall('\d+', command)  # Retrieve the coordinates X,X from the command input
            start_direction = command.rsplit(',', 1)[1]  # Retrieve the starting DIRECTION from the command input
            robot.place(int(coords[0]), int(coords[1]), start_direction)
        else:
            print("Invalid input. Use the place command to place robot"
                  " with the format 'PLACE X,X,DIRECTION' where X is between 0 and 5 inclusive and DIRECTION is one of "
                  "NORTH/SOUTH/EAST/WEST")
            continue
    else:
        if regex.match(command):
            coords = re.findall('\d+', command)
            start_direction = command.rsplit(',', 1)[1]
            robot.place(int(coords[0]), int(coords[1]), start_direction)
Ejemplo n.º 20
0
 def test_invalid_place(self):
     robot = ToyRobot.Robot()
     robot.place(4, 4, 'SOUTH')
     robot.place(10, 10, 'NORTH')
     expected = "4,4,SOUTH"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 21
0
 def test_avoid_place(self):
     robot = ToyRobot.Robot()
     robot.clear_obstacles()
     robot.avoid(4, 4)
     robot.place(4, 4, 'NORTH')
     self.assertEqual(robot.place_flag, False)
Ejemplo n.º 22
0
 def test_move_south(self):
     robot = ToyRobot.Robot()
     robot.place(1, 1, 'SOUTH')
     robot.move()
     expected = "1,0,SOUTH"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 23
0
 def test_move_east(self):
     robot = ToyRobot.Robot()
     robot.place(0, 0, 'EAST')
     robot.move()
     expected = "1,0,EAST"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 24
0
 def test_move_west(self):
     robot = ToyRobot.Robot()
     robot.place(2, 0, 'WEST')
     robot.move()
     expected = "1,0,WEST"
     self.assertEqual(robot.report(), expected)
Ejemplo n.º 25
0
 def test_avoid_two_cells(self):
     robot = ToyRobot.Robot()
     robot.avoid(4, 4)
     robot.avoid(2, 2)
     expected = [[4,4], [2,2]]
     self.assertEqual(expected, robot.report_obstacles())
Ejemplo n.º 26
0
 def test_move_west_when_at_south_west_of_grid(self):
     robot = ToyRobot.Robot()
     robot.place(0, 0, 'WEST')
     robot.move()
     expected = "0,0,WEST"
     self.assertEqual(robot.report(), expected)