Class: RubyRobot::Tabletop

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_robot/tabletop.rb

Direct Known Subclasses

NetflixTabletop

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ Tabletop



13
14
15
16
17
18
19
20
21
# File 'lib/ruby_robot/tabletop.rb', line 13

def initialize(width, height)
  # Store position of each piece
  @width = width
  @height = height
  # Actually, this probably isn't necessary
  # @playing_field = Array.new(@width) { Array.new(@height) }
  # Hash with keys as the robot object and values are x/y coords
  @robots = {}
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



11
12
13
# File 'lib/ruby_robot/tabletop.rb', line 11

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



11
12
13
# File 'lib/ruby_robot/tabletop.rb', line 11

def width
  @width
end

Instance Method Details

#calculate_position(orig_position, direction_sym) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_robot/tabletop.rb', line 33

def calculate_position(orig_position, direction_sym)
  case direction_sym
  # These are clockwise from north
  when :north then orig_position[:y] += 1
  when :east  then orig_position[:x] += 1
  when :south then orig_position[:y] -= 1
  when :west  then orig_position[:x] -= 1
  end
  orig_position
end

#height_rangeObject



28
29
30
31
# File 'lib/ruby_robot/tabletop.rb', line 28

def height_range
  # Exclude height since positions are 0-based
  (0...@height)
end

#inspectObject

Human-readable dump of Tabletop, with 2D array index x:0,y:0 in the lower-left corner of the output.



116
117
118
# File 'lib/ruby_robot/tabletop.rb', line 116

def inspect
  @playing_field
end

#move(robot, direction_sym) ⇒ Object

Move the robot in the specified direction.



79
80
81
82
83
84
# File 'lib/ruby_robot/tabletop.rb', line 79

def move(robot, direction_sym)
  raise PlacementError.new "Robot is not on this table" unless placed?(robot)
  new_position = calculate_position(@robots[robot].clone, direction_sym)
  # Move the robot by placing it at its new location
  place(robot, **new_position)
end

#move?(robot, direction_sym) ⇒ Boolean

Can the Robot move in the specified direction w/o falling off? Even though direction is a property of a specific Robot, pass it in here.

Later this could be extended to support multiple Robots on a Tabletop. In that case, this and #move would need to be synchronized…



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby_robot/tabletop.rb', line 64

def move?(robot, direction_sym)
  raise PlacementError.new "Robot is not on this table" unless placed?(robot)
  possible_position = calculate_position(@robots[robot].clone, direction_sym)

  # Is current_position on the board?
  # Check in range (0..width).include?(x) and (0..height).include?(y)
  return false if 
    !width_range.include?(possible_position[:x]) or 
    !height_range.include?(possible_position[:y])
  true
end

#place(robot, x: 0, y: 0) ⇒ Object

Place a robot on this board



103
104
105
106
107
108
109
# File 'lib/ruby_robot/tabletop.rb', line 103

def place(robot, x:0, y:0)
  raise PlacementError.new "Coordinates (#{x},#{y}) are not on this board" if 
    !width_range.include?(x) || !height_range.include?(y)
  # @playing_field[x][y] = robot
  @robots[robot] = {x: x, y: y}
  robot.place(self)
end

#place?(robot, direction_sym) ⇒ Boolean

NYI: Implement for multiple Robots per Tabletop



96
97
98
# File 'lib/ruby_robot/tabletop.rb', line 96

def place?(robot, direction_sym)
  true
end

#placed?(robot) ⇒ Boolean

Is this robot on this Tabletop?



89
90
91
# File 'lib/ruby_robot/tabletop.rb', line 89

def placed?(robot)
  @robots.keys.include?(robot)
end

#position(robot) ⇒ Object

Return hash of x/y coords for a placed Robot, else raise PlacementError. Position state doesn’t mean much to a Robot, by itself: only in relation to a Tabletop.



50
51
52
53
# File 'lib/ruby_robot/tabletop.rb', line 50

def position(robot)
  raise PlacementError.new "Robot is not on this table" unless placed?(robot)
  @robots[robot]
end

#width_rangeObject



23
24
25
26
# File 'lib/ruby_robot/tabletop.rb', line 23

def width_range
  # Exclude width since positions are 0-based
  (0...@width)
end