Sha256: 3c416a10cf1fc2b18af696589abf5699f9b0f125a589daf5c9f6d4d9a7f41e9f

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

# Position value object

module Robot
  class Position
    attr_reader :point, :direction

    def initialize(point: Point.new(x: 0, y: 0), direction: Robot::Directions::NORTH)
      @point = point
      @direction = direction
    end

    def north
      Position.new(point: point.north, direction: direction)
    end

    def south
      Position.new(point: point.south, direction: direction)
    end

    def east
      Position.new(point: point.east, direction: direction)
    end

    def west
      Position.new(point: point.west, direction: direction)
    end

    def left
      Position.new(point: point, direction: lefts[direction] )
    end

    def right
      Position.new(point: point, direction: rights[direction] )
    end

    def ==(position)
      point == position.point && direction == position.direction
    end

    def to_s
      "#{point}, #{direction}"
    end

    private

    def lefts
      {
        Robot::Directions::EAST  => Robot::Directions::NORTH,
        Robot::Directions::NORTH => Robot::Directions::WEST,
        Robot::Directions::WEST  => Robot::Directions::SOUTH,
        Robot::Directions::SOUTH => Robot::Directions::EAST,
      }.freeze
    end

    def rights
      {
        Robot::Directions::EAST  => Robot::Directions::SOUTH,
        Robot::Directions::SOUTH => Robot::Directions::WEST,
        Robot::Directions::WEST  => Robot::Directions::NORTH,
        Robot::Directions::NORTH => Robot::Directions::EAST,
      }.freeze
    end
  end
end


Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
robot_rea-0.1.5 lib/robot/position.rb
robot_rea-0.1.4 lib/robot/position.rb