Class: Robot
- Inherits:
-
Object
- Object
- Robot
- 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
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize ⇒ Robot
constructor
A new instance of Robot.
- #left ⇒ Object
-
#move ⇒ Object
trys to move in current direction one place.
- #place(args) ⇒ Object
-
#report ⇒ Object
outputs the x,y,position of the robot also includes a position matrix.
-
#right ⇒ Object
rotates compass faces and uses current face as index to retrieve new direction.
- #to_s ⇒ Object
Constructor Details
Instance Attribute Details
#direction ⇒ Object
Returns the value of attribute direction.
16 17 18 |
# File 'lib/robot.rb', line 16 def direction @direction end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
16 17 18 |
# File 'lib/robot.rb', line 16 def table @table end |
Instance Method Details
#left ⇒ Object
38 39 40 |
# File 'lib/robot.rb', line 38 def left rotate(:left) end |
#move ⇒ Object
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 |
#report ⇒ Object
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 |
#right ⇒ Object
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_s ⇒ Object
50 51 52 |
# File 'lib/robot.rb', line 50 def to_s "#{table.position[:x]}, #{table.position[:y]} #{direction.upcase}" end |