Class: LocoBot::Robot

Inherits:
Object
  • Object
show all
Defined in:
lib/loco_bot/robot.rb,
lib/loco_bot/robot/direction.rb,
lib/loco_bot/robot/direction/east.rb,
lib/loco_bot/robot/direction/west.rb,
lib/loco_bot/robot/direction/north.rb,
lib/loco_bot/robot/direction/south.rb

Overview

Representation of a robot, placeable on tables.

Defined Under Namespace

Modules: Direction

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#directionDirection (readonly)



20
21
22
# File 'lib/loco_bot/robot.rb', line 20

def direction
  @direction
end

#tableTable (readonly)



8
9
10
# File 'lib/loco_bot/robot.rb', line 8

def table
  @table
end

#xInteger (readonly)



12
13
14
# File 'lib/loco_bot/robot.rb', line 12

def x
  @x
end

#yInteger (readonly)



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

def y
  @y
end

Instance Method Details

#hodor!void

This method returns an undefined value.

Outputs a friendly greating.



85
86
87
# File 'lib/loco_bot/robot.rb', line 85

def hodor!
  puts 'HODOR HODOR !'
end

#moveBoolean

Moves the robot one unit forward in the direction it is currently facing.



43
44
45
46
47
48
49
50
# File 'lib/loco_bot/robot.rb', line 43

def move
  return false unless next_position_valid?

  @x = next_position[:x]
  @y = next_position[:y]

  true
end

#next_positionHash

Returns a Hash containing the next x and y coordinates of the Robot if it moves facing its current direction. The Hash will be empty if the Robot has not been placed.

Examples:

robot.place(1, 2, Direction::West)
robot.next_position # => {x: 0, y: 2}


95
96
97
98
99
# File 'lib/loco_bot/robot.rb', line 95

def next_position
  return {} if table.nil?

  direction.vector(x, y)
end

#next_position_valid?Boolean

Determines if the next position position of the Robot is valid according to its Table.



103
104
105
106
107
# File 'lib/loco_bot/robot.rb', line 103

def next_position_valid?
  return false if table.nil?

  table.position_valid?(next_position[:x], next_position[:y])
end

#place(table, x, y, direction) ⇒ Boolean

Places the Robot on the given Table at the given coordinates, facing the given Direction. If the Robot was previously on another Table, it is removed from it before being placed on the given.



29
30
31
# File 'lib/loco_bot/robot.rb', line 29

def place(table, x, y, direction)
  table.place_robot(self, x, y, direction)
end

#removeBoolean

Removes the Robot from its current Table. Position attributes of the Robot are set to nil.



35
36
37
38
39
# File 'lib/loco_bot/robot.rb', line 35

def remove
  return false if table.nil?

  table.remove_robot(self)
end

#reportHash

Returns a Hash containing the x, y and direction of the robot.

Examples:

robot.place(1, 2, Direction::West)
robot.report # => {x: 1, y: 2, direction: LocoBot::Robot::Direction::West}


77
78
79
80
81
# File 'lib/loco_bot/robot.rb', line 77

def report
  return {} if table.nil?

  { x: x, y: y, direction: direction }
end

#turn_leftBoolean

Rotates the robot 90 degrees to the left without changing its position.



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

def turn_left
  return false if table.nil?

  @direction = direction.left

  true
end

#turn_rightBoolean

Rotates the robot 90 degrees to the right without changing its position.



64
65
66
67
68
69
70
# File 'lib/loco_bot/robot.rb', line 64

def turn_right
  return false if table.nil?

  @direction = direction.right

  true
end