Sha256: 71b35bcc8d34d815dcd750cddbab801f07852a57865522c89ff7cfb99d443607

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 KB

Contents

# encoding: UTF-8

require 'typesafe_enum'
# A direction on the hexagonal board of the game. Possible directions are:
# * RIGHT
# * UP_RIGHT
# * UP_LEFT
# * LEFT
# * DOWN_LEFT
# * DOWN_RIGHT
# Access them as Direction::RIGHT for example.
class Direction < TypesafeEnum::Base
  new :RIGHT
  new :UP_RIGHT
  new :UP_LEFT
  new :LEFT
  new :DOWN_LEFT
  new :DOWN_RIGHT

  # @param direction [Direction] starting direction
  # @param turns [Integer] number of turns (positive means turning
  #                        counterclockwise).
  # @return [Direction] The direction when turning the given number of steps
  #                     from the given direction.
  def self.get_turn_direction(direction, turns)
    # order of directions is equal to counterclockwise turning
    Direction.find_by_ord((direction.ord + turns) % 6)
  end

  # @return [Turn] The Turn action to get from from_direction to to_direction.
  def self.from_to(from_direction, to_direction)
    distance = (to_direction.ord - from_direction.ord + 6) % 6
    if distance > 3
      distance = distance - 6
    end
    Turn.new(distance)
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
software_challenge_client-0.3.4 lib/software_challenge_client/direction.rb
software_challenge_client-0.3.3 lib/software_challenge_client/direction.rb
software_challenge_client-0.3.2 lib/software_challenge_client/direction.rb
software_challenge_client-0.3.1 lib/software_challenge_client/direction.rb
software_challenge_client-0.3.0 lib/software_challenge_client/direction.rb
software_challenge_client-0.2.0 lib/software_challenge_client/direction.rb