class MauxRobot::Position
This class represents the Robot
position on the table
Constants
- POSSIBLE_DIRECTIONS
- POSSIBLE_MOVEMENTS
Attributes
face[R]
x[R]
y[R]
Public Class Methods
new(x, y, face)
click to toggle source
# File lib/maux_robot/position.rb, line 18 def initialize(x, y, face) # rubocop:disable Naming/UncommunicativeMethodParamName @x = x.to_i @y = y.to_i @face = face&.downcase&.to_sym || :invalid end
Public Instance Methods
==(other)
click to toggle source
# File lib/maux_robot/position.rb, line 55 def ==(other) other.x == x && other.y == y && other.face == face end
forward_position()
click to toggle source
# File lib/maux_robot/position.rb, line 42 def forward_position return self unless valid_direction? x = @x + POSSIBLE_MOVEMENTS[@face][:x] y = @y + POSSIBLE_MOVEMENTS[@face][:y] Position.new(x, y, @face) end
left()
click to toggle source
# File lib/maux_robot/position.rb, line 28 def left return self unless valid_direction? next_direction_index = (POSSIBLE_DIRECTIONS.index(@face) + 1) % 4 @face = POSSIBLE_DIRECTIONS[next_direction_index] end
report(format_type = :csv)
click to toggle source
# File lib/maux_robot/position.rb, line 50 def report(format_type = :csv) formatter = MauxRobot::Formatter.from(format_type) puts formatter.generate(self) end
right()
click to toggle source
# File lib/maux_robot/position.rb, line 35 def right return self unless valid_direction? next_direction_index = (POSSIBLE_DIRECTIONS.index(@face) - 1) @face = POSSIBLE_DIRECTIONS[next_direction_index] end
valid_direction?()
click to toggle source
# File lib/maux_robot/position.rb, line 24 def valid_direction? POSSIBLE_DIRECTIONS.include?(@face) end