Class: Robot

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#directionObject

Returns the value of attribute direction.



13
14
15
# File 'lib/r3d3/models/robot.rb', line 13

def direction
  @direction
end

#xObject

Returns the value of attribute x.



13
14
15
# File 'lib/r3d3/models/robot.rb', line 13

def x
  @x
end

#yObject

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

#reportObject



39
40
41
# File 'lib/r3d3/models/robot.rb', line 39

def report
  '%d,%d,%s' % [@x, @y, @direction]
end

#turn_leftObject



31
32
33
# File 'lib/r3d3/models/robot.rb', line 31

def turn_left
  @direction = DIRECTIONS[current_direction_index - 1]
end

#turn_rightObject



35
36
37
# File 'lib/r3d3/models/robot.rb', line 35

def turn_right
  @direction = DIRECTIONS[(current_direction_index + 1) % 4]
end