Class: RubyRobot::Tabletop
- Inherits:
-
Object
- Object
- RubyRobot::Tabletop
- Defined in:
- lib/ruby_robot/tabletop.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #calculate_position(orig_position, direction_sym) ⇒ Object
- #height_range ⇒ Object
-
#initialize(width, height) ⇒ Tabletop
constructor
A new instance of Tabletop.
-
#inspect ⇒ Object
Human-readable dump of Tabletop, with 2D array index x:0,y:0 in the lower-left corner of the output.
-
#move(robot, direction_sym) ⇒ Object
Move the robot in the specified direction.
-
#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.
-
#place(robot, x: 0, y: 0) ⇒ Object
Place a robot on this board.
-
#place?(robot, direction_sym) ⇒ Boolean
NYI: Implement for multiple Robots per Tabletop.
-
#placed?(robot) ⇒ Boolean
Is this robot on this Tabletop?.
-
#position(robot) ⇒ Object
Return hash of x/y coords for a placed Robot, else raise PlacementError.
- #width_range ⇒ Object
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
#height ⇒ Object (readonly)
Returns the value of attribute height.
11 12 13 |
# File 'lib/ruby_robot/tabletop.rb', line 11 def height @height end |
#width ⇒ Object (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_range ⇒ Object
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 |
#inspect ⇒ Object
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 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_range ⇒ Object
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 |