Class: Robot::Simulator::Robot
- Inherits:
-
Object
- Object
- Robot::Simulator::Robot
- Defined in:
- lib/robot/simulator/robot.rb
Overview
Robot class corresponds to robot which can move around on the table.
Instance Attribute Summary collapse
-
#current_track ⇒ Object
readonly
Returns the value of attribute current_track.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize(table) ⇒ Robot
constructor
A new instance of Robot.
- #move ⇒ Object
- #place(coordinate, direction) ⇒ Object
- #turn_left ⇒ Object
- #turn_right ⇒ Object
Constructor Details
#initialize(table) ⇒ Robot
Returns a new instance of Robot.
7 8 9 |
# File 'lib/robot/simulator/robot.rb', line 7 def initialize(table) @table = table end |
Instance Attribute Details
#current_track ⇒ Object
Returns the value of attribute current_track.
5 6 7 |
# File 'lib/robot/simulator/robot.rb', line 5 def current_track @current_track end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
5 6 7 |
# File 'lib/robot/simulator/robot.rb', line 5 def table @table end |
Instance Method Details
#move ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/robot/simulator/robot.rb', line 17 def move return false unless is_on_table? newX = current_track.coordinate.x newX += 1 if current_track.facing == Direction::EAST newX -= 1 if current_track.facing == Direction::WEST newY = current_track.coordinate.y newY += 1 if current_track.facing == Direction::NORTH newY -= 1 if current_track.facing == Direction::SOUTH new_coordinate = Coordinate.new(newX, newY) return false unless is_valid_coordinate? new_coordinate self.current_track = Track.new new_coordinate, current_track.facing end |
#place(coordinate, direction) ⇒ Object
11 12 13 14 15 |
# File 'lib/robot/simulator/robot.rb', line 11 def place(coordinate, direction) return false unless is_valid_coordinate?(coordinate) self.current_track = Track.new coordinate, direction end |