Class: Model::Compass
- Inherits:
-
Object
- Object
- Model::Compass
- 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
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
Instance Method Summary collapse
-
#initialize(direction) ⇒ Compass
constructor
A new instance of Compass.
- #translate_location(location) ⇒ Object
- #turn ⇒ Object
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
#direction ⇒ Object (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 |
#turn ⇒ Object
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 |