Class: LocoBot::Robot
- Inherits:
-
Object
- Object
- LocoBot::Robot
- 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
-
#direction ⇒ Direction
readonly
The Direction the Robot is currently facing on the Table.
-
#table ⇒ Table
readonly
The Table the Robot is currently placed on.
-
#x ⇒ Integer
readonly
The x-axis coordinate the Robot is currently at on the Table.
-
#y ⇒ Integer
readonly
The y-axis coordinate the Robot is currently at on the Table.
Instance Method Summary collapse
-
#hodor! ⇒ void
Outputs a friendly greating.
-
#move ⇒ Boolean
Moves the robot one unit forward in the direction it is currently facing.
-
#next_position ⇒ Hash
Returns a Hash containing the next x and y coordinates of the Robot if it moves facing its current direction.
-
#next_position_valid? ⇒ Boolean
Determines if the next position position of the Robot is valid according to its Table.
-
#place(table, x, y, direction) ⇒ Boolean
Places the Robot on the given Table at the given coordinates, facing the given Direction.
-
#remove ⇒ Boolean
Removes the Robot from its current Table.
-
#report ⇒ Hash
Returns a Hash containing the x, y and direction of the robot.
-
#turn_left ⇒ Boolean
Rotates the robot 90 degrees to the left without changing its position.
-
#turn_right ⇒ Boolean
Rotates the robot 90 degrees to the right without changing its position.
Instance Attribute Details
#direction ⇒ Direction (readonly)
Returns the Direction the Robot is currently facing on the Table.
20 21 22 |
# File 'lib/loco_bot/robot.rb', line 20 def direction @direction end |
#table ⇒ Table (readonly)
Returns the Table the Robot is currently placed on.
8 9 10 |
# File 'lib/loco_bot/robot.rb', line 8 def table @table end |
#x ⇒ Integer (readonly)
Returns the x-axis coordinate the Robot is currently at on the Table.
12 13 14 |
# File 'lib/loco_bot/robot.rb', line 12 def x @x end |
#y ⇒ Integer (readonly)
Returns the y-axis coordinate the Robot is currently at on the Table.
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 |
#move ⇒ Boolean
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_position ⇒ Hash
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.
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 |
#remove ⇒ Boolean
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 |
#report ⇒ Hash
Returns a Hash containing the x, y and direction of the robot.
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_left ⇒ Boolean
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_right ⇒ Boolean
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 |