Class: Pointer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#directionObject

Returns the value of attribute direction.



4
5
6
# File 'lib/pointer.rb', line 4

def direction
  @direction
end

#trampolineObject

Returns the value of attribute trampoline.



4
5
6
# File 'lib/pointer.rb', line 4

def trampoline
  @trampoline
end

#xObject

Returns the value of attribute x.



4
5
6
# File 'lib/pointer.rb', line 4

def x
  @x
end

#yObject

Returns the value of attribute y.



4
5
6
# File 'lib/pointer.rb', line 4

def y
  @y
end

Instance Method Details

#stepObject



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