Sha256: fe130b1c1691578fe3b23454836dc7c91efa8b6af90f09c0aca38fc582be54a6

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

# 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 ==(other)
      point == other.point && direction == other.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

4 entries across 4 versions & 1 rubygems

Version Path
robot_rea-0.1.9 lib/robot/position.rb
robot_rea-0.1.8 lib/robot/position.rb
robot_rea-0.1.7 lib/robot/position.rb
robot_rea-0.1.6 lib/robot/position.rb