Class: Robot

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/robot.rb,
lib/robot/table.rb,
lib/robot/matrix.rb,
lib/robot/version.rb,
lib/robot/command_centre.rb

Defined Under Namespace

Classes: CommandCentre, Matrix, Table

Constant Summary collapse

MOVEMENT_DIRECTIONS =
{
  north: { x: 0, y: 1 },
  east: { x: 1, y: 0 },
  south: { x: 0, y: -1 },
  west: { x: -1, y: 0 }
}
VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRobot

Returns a new instance of Robot.



18
19
20
# File 'lib/robot.rb', line 18

def initialize
  @table = Table.instance
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



16
17
18
# File 'lib/robot.rb', line 16

def direction
  @direction
end

#tableObject (readonly)

Returns the value of attribute table.



16
17
18
# File 'lib/robot.rb', line 16

def table
  @table
end

Instance Method Details

#leftObject



38
39
40
# File 'lib/robot.rb', line 38

def left
  rotate(:left)
end

#moveObject

trys to move in current direction one place



24
25
26
27
28
29
# File 'lib/robot.rb', line 24

def move
  postition = new_position
  return if table.place(postition[:x], postition[:y])
  puts 'Cannot move off the table'
  CommandCentre.instance.say('Does not compute, I will fall to my death')
end

#place(args) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/robot.rb', line 54

def place(args)
  return unless args
  x, y, face = args.split(',')
  return unless x && y && face
  return unless table.place(x, y)
  self.direction = face.downcase.to_sym
end

#reportObject

outputs the x,y,position of the robot also includes a position matrix



44
45
46
47
48
# File 'lib/robot.rb', line 44

def report
  puts to_s
  puts Matrix.new(table.position, direction).to_s
  say_position
end

#rightObject

rotates compass faces and uses current face as index to retrieve new direction



34
35
36
# File 'lib/robot.rb', line 34

def right
  rotate(:right)
end

#to_sObject



50
51
52
# File 'lib/robot.rb', line 50

def to_s
  "#{table.position[:x]}, #{table.position[:y]} #{direction.upcase}"
end