Class: Pointer
- Inherits:
-
Object
- Object
- Pointer
- Defined in:
- lib/pointer.rb
Overview
This class is responsible for keeping track of it’s location on the code map and moving in a direction every iteration.
Constant Summary collapse
- DIRECTIONS =
[:w, :e, :n, :s]
- DIRECTIONS_MAP =
Instance Attribute Summary collapse
-
#direction ⇒ Object
Returns the value of attribute direction.
-
#trampoline ⇒ Object
Returns the value of attribute trampoline.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
-
#initialize(direction = :e) ⇒ Pointer
constructor
A new instance of Pointer.
- #step ⇒ Object
Constructor Details
#initialize(direction = :e) ⇒ Pointer
Returns a new instance of Pointer.
11 12 13 14 15 |
# File 'lib/pointer.rb', line 11 def initialize(direction = :e) @direction = direction @x, @y = 0, 0 @trampoline = false end |
Instance Attribute Details
#direction ⇒ Object
Returns the value of attribute direction.
4 5 6 |
# File 'lib/pointer.rb', line 4 def direction @direction end |
#trampoline ⇒ Object
Returns the value of attribute trampoline.
4 5 6 |
# File 'lib/pointer.rb', line 4 def trampoline @trampoline end |
#x ⇒ Object
Returns the value of attribute x.
4 5 6 |
# File 'lib/pointer.rb', line 4 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
4 5 6 |
# File 'lib/pointer.rb', line 4 def y @y end |
Instance Method Details
#step ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/pointer.rb', line 17 def step diff = DIRECTIONS_MAP[@direction] multiplier = @trampoline ? 2 : 1 self.x = x + diff.first * multiplier self.y = y + diff.last * multiplier self.x = x % 80 self.y = y % 20 @trampoline = false end |