def test_add_rover(self): planet = Planet(2, 2) rover = Rover(1, 2, 'N', planet) self.assertEqual(len(planet.rovers), 0) planet.add_rover(rover) self.assertEqual(len(planet.rovers), 1)
def test_update_grid(self): planet = Planet(2, 2) self.assertEqual(planet.grid[1][2], 0) rover = Rover(1, 2, 'N', planet) planet.add_rover(rover) planet.update_grid() self.assertEqual(planet.grid[1][2], 1)
def test_launching_of_rover(self): planet = Planet(2, 2) self.assertIsInstance(launch_rover('1 2 N', planet), Rover) rover = Rover(1, 1, 'S', planet) planet.add_rover(rover) planet.update_grid() # send second one with same coordinates self.assertFalse(launch_rover('1 1 W', planet))
def test_get_rover_on_position(self): planet = Planet(5, 5) rover = Rover(2, 1, 'N', planet) planet.add_rover(rover) planet.update_grid() self.assertIsInstance(planet.get_rover_on_position(2, 1), Rover) self.assertEqual(planet.get_rover_on_position(2, 1), rover) rover2 = Rover(3, 5, 'S', planet) planet.add_rover(rover2) planet.update_grid() self.assertEqual(planet.get_rover_on_position(3, 5), rover2)
def main(): file_path = raw_input( 'Please insert the path to the text file that will be used to start the program. If blank, the file used will be the default one.\n' ) # Opening the file with input specified content = open_file(file_path) # If the path is incorrect, keep asking or get a blank to use the default while content == None: file_path = raw_input( "The path given is incorrect or th file tracked has the wrong format. If blank, the file used will be the default one.\n" ) content = open_file(file_path) # Grid size received from input grid_size = content[0] # Creating the planet if validation_grid_size(grid_size): planet = Planet(grid_size[0], grid_size[2]) else: print 'Size of grid given is incorrect' print '{} rovers are about to be launched.'.format(len(content) / 2) # Launch every rover on the instructions read before for i in range(1, len(content), 2): initiation_instructions = content[i] rover_path = content[i + 1] if validation_instructions(initiation_instructions): rover = launch_rover(initiation_instructions, planet) if rover: planet.add_rover(rover) if validation_path(rover_path): if move_rover(rover, rover_path): print "The rover number {} has finished its movements. It's final position is: {}".format( (i + 1) / 2, rover.print_status()) else: print "Ups! looks like the rover numer {} can't keep moving on the direction given. I'ts final position is: {}".format( (i + 1) / 2, rover.print_status()) else: print 'Path given for the movement of rover is incorrect' planet.update_grid() else: print 'A rover is already on the launching position. Rover {} not launched.'.format( (i + 1) / 2) else: print 'Instructions given for rover launching are incorrect' print 'Final map of the planet is:\n' planet.paint_grid()