Class: Direction

Inherits:
TypesafeEnum::Base
  • Object
show all
Defined in:
lib/software_challenge_client/direction.rb

Overview

Access them as Direction::RIGHT for example.

Class Method Summary collapse

Class Method Details

.from_to(from_direction, to_direction) ⇒ Turn

Returns The Turn action to get from from_direction to to_direction.

Returns:

  • (Turn)

    The Turn action to get from from_direction to to_direction.



31
32
33
34
35
36
37
# File 'lib/software_challenge_client/direction.rb', line 31

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

.get_turn_direction(direction, turns) ⇒ Direction

Returns The direction when turning the given number of steps from the given direction.

Parameters:

  • direction (Direction)

    starting direction

  • turns (Integer)

    number of turns (positive means turning counterclockwise).

Returns:

  • (Direction)

    The direction when turning the given number of steps from the given direction.



25
26
27
28
# File 'lib/software_challenge_client/direction.rb', line 25

def self.get_turn_direction(direction, turns)
  # order of directions is equal to counterclockwise turning
  Direction.find_by_ord((direction.ord + turns) % 6)
end