Class: Robot
- Inherits:
-
Object
- Object
- Robot
- Defined in:
- lib/r3d3/models/robot.rb
Constant Summary collapse
- DIRECTIONS =
%w(NORTH EAST SOUTH WEST)
- STEP_DIRECTIONS =
{ NORTH: [0, 1], EAST: [1, 0], SOUTH: [0, -1], WEST: [-1, 0] }
Instance Attribute Summary collapse
-
#direction ⇒ Object
Returns the value of attribute direction.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
-
#initialize(x = '0', y = '0', direction = 'NORTH') ⇒ Robot
constructor
A new instance of Robot.
- #move(board) ⇒ Object
- #place(x, y, direction) ⇒ Object
- #report ⇒ Object
- #turn_left ⇒ Object
- #turn_right ⇒ Object
Constructor Details
#initialize(x = '0', y = '0', direction = 'NORTH') ⇒ Robot
Returns a new instance of Robot.
15 16 17 |
# File 'lib/r3d3/models/robot.rb', line 15 def initialize(x = '0', y = '0', direction = 'NORTH') place(x, y, direction) end |
Instance Attribute Details
#direction ⇒ Object
Returns the value of attribute direction.
13 14 15 |
# File 'lib/r3d3/models/robot.rb', line 13 def direction @direction end |
#x ⇒ Object
Returns the value of attribute x.
13 14 15 |
# File 'lib/r3d3/models/robot.rb', line 13 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
13 14 15 |
# File 'lib/r3d3/models/robot.rb', line 13 def y @y end |
Instance Method Details
#move(board) ⇒ Object
26 27 28 29 |
# File 'lib/r3d3/models/robot.rb', line 26 def move(board) return if board.forbid_move?(*attempted_move) update_position(attempted_move) end |
#place(x, y, direction) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/r3d3/models/robot.rb', line 19 def place(x, y, direction) @x = x.to_i @y = y.to_i @direction = direction raise 'Direction is not valid' unless DIRECTIONS.include? @direction end |
#report ⇒ Object
39 40 41 |
# File 'lib/r3d3/models/robot.rb', line 39 def report '%d,%d,%s' % [@x, @y, @direction] end |
#turn_left ⇒ Object
31 32 33 |
# File 'lib/r3d3/models/robot.rb', line 31 def turn_left @direction = DIRECTIONS[current_direction_index - 1] end |
#turn_right ⇒ Object
35 36 37 |
# File 'lib/r3d3/models/robot.rb', line 35 def turn_right @direction = DIRECTIONS[(current_direction_index + 1) % 4] end |