Class: Model::Compass

Inherits:
Object
  • Object
show all
Defined in:
lib/karel/model/compass.rb

Constant Summary collapse

DIRECTIONS =
{
  up: [0, 1],
  left: [-1, 0],
  down: [0, -1],
  right: [1, 0]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(direction) ⇒ Compass

Returns a new instance of Compass.



14
15
16
# File 'lib/karel/model/compass.rb', line 14

def initialize(direction)
  @direction = direction
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



12
13
14
# File 'lib/karel/model/compass.rb', line 12

def direction
  @direction
end

Instance Method Details

#translate_location(location) ⇒ Object



18
19
20
21
22
23
# File 'lib/karel/model/compass.rb', line 18

def translate_location(location)
  Location.new(
    location.x + DIRECTIONS[direction][0],
    location.y + DIRECTIONS[direction][1]
  )
end

#turnObject



25
26
27
28
29
30
# File 'lib/karel/model/compass.rb', line 25

def turn
  dir_index = DIRECTIONS.keys.index(direction)
  dir_index += 1
  dir_index %= DIRECTIONS.length
  self.class.new(DIRECTIONS.keys[dir_index])
end