Class: Upwords::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/upwords/cursor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_y, max_x, init_y = 0, init_x = 0) ⇒ Cursor

Returns a new instance of Cursor.



5
6
7
8
9
10
11
12
# File 'lib/upwords/cursor.rb', line 5

def initialize(max_y, max_x, init_y = 0, init_x = 0)
  @max_y = max_y
  @max_x = max_x

  # HACK: Force init_y, init_x to be in bounds
  @y = init_y % @max_y
  @x = init_x % @max_x 
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



3
4
5
# File 'lib/upwords/cursor.rb', line 3

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



3
4
5
# File 'lib/upwords/cursor.rb', line 3

def y
  @y
end

Instance Method Details

#downObject



18
19
20
# File 'lib/upwords/cursor.rb', line 18

def down
  move(1, 0)
end

#leftObject



22
23
24
# File 'lib/upwords/cursor.rb', line 22

def left
  move(0, -1)
end

#move(dy, dx) ⇒ Object



30
31
32
33
# File 'lib/upwords/cursor.rb', line 30

def move(dy, dx)
  @y = (y + dy) % @max_y
  @x = (x + dx) % @max_x 
end

#posObject



35
36
37
# File 'lib/upwords/cursor.rb', line 35

def pos
  [y, x]
end

#rightObject



26
27
28
# File 'lib/upwords/cursor.rb', line 26

def right
  move(0, 1)
end

#upObject



14
15
16
# File 'lib/upwords/cursor.rb', line 14

def up
  move(-1, 0)
end