Class: ToyRobotSim::Robot
- Inherits:
-
Object
- Object
- ToyRobotSim::Robot
- Defined in:
- lib/toy_robot_sim/robot.rb
Constant Summary collapse
- DIRECTIONS =
%w(NORTH EAST SOUTH WEST)
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize(table) ⇒ Robot
constructor
A new instance of Robot.
- #left ⇒ Object
- #move ⇒ Object
- #place(location, direction) ⇒ Object
- #report ⇒ Object
- #right ⇒ Object
Constructor Details
#initialize(table) ⇒ Robot
Returns a new instance of Robot.
9 10 11 |
# File 'lib/toy_robot_sim/robot.rb', line 9 def initialize(table) @table = table if table.is_a?(ToyRobotSim::Table) end |
Instance Attribute Details
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
5 6 7 |
# File 'lib/toy_robot_sim/robot.rb', line 5 def direction @direction end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
5 6 7 |
# File 'lib/toy_robot_sim/robot.rb', line 5 def location @location end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
5 6 7 |
# File 'lib/toy_robot_sim/robot.rb', line 5 def table @table end |
Instance Method Details
#left ⇒ Object
29 30 31 |
# File 'lib/toy_robot_sim/robot.rb', line 29 def left turn(-1) end |
#move ⇒ Object
22 23 24 25 26 27 |
# File 'lib/toy_robot_sim/robot.rb', line 22 def move if placed? new_location = @location.send(@direction.downcase) @location = new_location if valid_move?(new_location, @direction) end end |
#place(location, direction) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/toy_robot_sim/robot.rb', line 15 def place(location, direction) if valid_move?(location, direction) @location = location @direction = direction end end |
#report ⇒ Object
37 38 39 |
# File 'lib/toy_robot_sim/robot.rb', line 37 def report "#{@location.x},#{@location.y},#{@direction}" if placed? end |
#right ⇒ Object
33 34 35 |
# File 'lib/toy_robot_sim/robot.rb', line 33 def right turn(1) end |