Class: LocoBot::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/loco_bot/table.rb

Overview

Representation of a table on which robots can be placed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width = 5, height = 5) ⇒ Table

Returns a new instance of Table

Parameters:

  • width (Integer) (defaults to: 5)

    The width of the Table

  • height (Integer) (defaults to: 5)

    The height of the 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

#heightFixnum (readonly)

Determines the farthest accessible point on the Table's y-axis.

Returns:

  • (Fixnum)

    height of the table



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

def height
  @height
end

#widthFixnum (readonly)

Determines the farthest accessible point on the Table's x-axis.

Returns:

  • (Fixnum)

    width of the Table



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.

Parameters:

  • robot (Robot)

    the Robot to place on the Table

  • x (Integer)

    the x-axis coordinate

  • y (Integer)

    the y-axis coordinate

  • direction (Direction)

    the facing Direction

Returns:

  • (Boolean)

    true if placing was successful, false otherwise.



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.

Parameters:

  • x (Integer)

    the x-axis coordinate

  • y (Integer)

    the y-axis coordinate

Returns:

  • (Boolean)

    true if a Robot can be placed at the given coordinates, false otherwise.



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.

Parameters:

  • robot (Robot)

    the Robot to remove from the Table

Returns:

  • (Boolean)

    true if removing was successful, false otherwise.



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

#robotsArray<Robot>

The collection of Robots currently placed on the Table.

Returns:

  • (Array<Robot>)

    a collection of Robots



72
73
74
# File 'lib/loco_bot/table.rb', line 72

def robots
  @robots.dup
end