Commit 901082fa authored by Josiah Wang's avatar Josiah Wang
Browse files

Added a LeapRobot class

parent 229ae251
......@@ -180,5 +180,38 @@ def _test_robot_init():
assert robot.direction == direction
class LeapingRobot(Robot):
""" Robot that leaps straight to the edge of a grid. """
def leap_forward(self):
""" Make robot leap forward to the edge in the direction it is facing."""
direction = self.direction
row = self.position[0]
col = self.position[1]
if direction == "n":
self.position = (0, col)
elif direction == "s":
self.position = (self.grid.size - 1, col)
elif direction == "w":
self.position = (row, 0)
elif direction == "e":
self.position = (row, self.grid.size - 1)
self.report_leap()
def report_leap(self):
print("Leaping forward to the edge.")
def move_forward_to_wall(self):
""" Move robot forward in the current direction until it hits a wall.
Returns:
tuple : (row, column) coordinate after hitting a wall
"""
self.leap_forward()
self.report_location()
if __name__ == "__main__":
_test_robot_init()
\ No newline at end of file
......@@ -5,7 +5,7 @@ Contains the RobotFactory class for creating a new robot.
import random
from robot import Robot
from robot import Robot, LeapingRobot
class RobotFactory:
def __init__(self, grid, candidate_names=[], prev_id=-1):
......@@ -53,13 +53,21 @@ class RobotFactory:
identifier = self._generate_id()
initial_position = self._generate_position()
initial_direction = self._generate_direction()
return Robot(identifier=identifier,
name=name,
position=initial_position,
direction=initial_direction,
grid=self.grid)
choice = random.randint(0, 1)
if choice == 0:
return Robot(identifier=identifier,
name=name,
position=initial_position,
direction=initial_direction,
grid=self.grid)
else:
return LeapingRobot(identifier=identifier,
name=name,
position=initial_position,
direction=initial_direction,
grid=self.grid)
def _generate_name(self):
""" Select a robot's name at random from a given list
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment