Class: LocoBot::Table
- Inherits:
-
Object
- Object
- LocoBot::Table
- Defined in:
- lib/loco_bot/table.rb
Overview
Representation of a table on which robots can be placed.
Instance Attribute Summary collapse
-
#height ⇒ Fixnum
readonly
Determines the farthest accessible point on the Table’s y-axis.
-
#width ⇒ Fixnum
readonly
Determines the farthest accessible point on the Table’s x-axis.
Instance Method Summary collapse
-
#initialize(width = 5, height = 5) ⇒ Table
constructor
A new instance of Table.
-
#place_robot(robot, x, y, direction) ⇒ Boolean
Places the given Robot on the Table at the given coordinates, facing the given Direction.
-
#position_valid?(x, y) ⇒ Boolean
Determines whether a Robot can be placed at the given coordinates.
-
#remove_robot(robot) ⇒ Boolean
Removes the given Robot from the Table.
-
#robots ⇒ Array<Robot>
The collection of Robots currently placed on the Table.
Constructor Details
#initialize(width = 5, height = 5) ⇒ Table
Returns a new instance of Table.
16 17 18 19 20 21 22 |
# File 'lib/loco_bot/table.rb', line 16 def initialize(width = 5, height = 5) @width = width @height = height @robots = [] validate_dimensions end |
Instance Attribute Details
#height ⇒ Fixnum (readonly)
Determines the farthest accessible point on the Table’s y-axis.
12 13 14 |
# File 'lib/loco_bot/table.rb', line 12 def height @height end |
#width ⇒ Fixnum (readonly)
Determines the farthest accessible point on the Table’s x-axis.
7 8 9 |
# File 'lib/loco_bot/table.rb', line 7 def width @width end |
Instance Method Details
#place_robot(robot, x, y, direction) ⇒ Boolean
Places the given Robot on the 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 current.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/loco_bot/table.rb', line 31 def place_robot(robot, x, y, direction) return false unless position_valid?(x, y) robot.remove if robot.table robot.instance_variable_set(:@table, self) robot.instance_variable_set(:@x, x) robot.instance_variable_set(:@y, y) robot.instance_variable_set(:@direction, direction) @robots.push(robot) unless robots.include?(robot) true end |
#position_valid?(x, y) ⇒ Boolean
Determines whether a Robot can be placed at the given coordinates.
66 67 68 |
# File 'lib/loco_bot/table.rb', line 66 def position_valid?(x, y) position_within_bounds?(x, y) && position_free?(x, y) end |
#remove_robot(robot) ⇒ Boolean
Removes the given Robot from the Table. Position attributes of the Robot are set to nil.
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/loco_bot/table.rb', line 49 def remove_robot(robot) return false unless robots.include?(robot) robot.instance_variable_set(:@table, nil) robot.instance_variable_set(:@x, nil) robot.instance_variable_set(:@y, nil) robot.instance_variable_set(:@direction, nil) @robots.delete(robot) true end |
#robots ⇒ Array<Robot>
The collection of Robots currently placed on the Table.
72 73 74 |
# File 'lib/loco_bot/table.rb', line 72 def robots @robots.dup end |