Class: Upwords::Cursor
- Inherits:
-
Object
- Object
- Upwords::Cursor
- Defined in:
- lib/upwords/cursor.rb
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #down ⇒ Object
-
#initialize(max_y, max_x, init_y = 0, init_x = 0) ⇒ Cursor
constructor
A new instance of Cursor.
- #left ⇒ Object
- #move(dy, dx) ⇒ Object
- #pos ⇒ Object
- #right ⇒ Object
- #up ⇒ Object
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
#x ⇒ Object (readonly)
Returns the value of attribute x.
3 4 5 |
# File 'lib/upwords/cursor.rb', line 3 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
3 4 5 |
# File 'lib/upwords/cursor.rb', line 3 def y @y end |
Instance Method Details
#down ⇒ Object
18 19 20 |
# File 'lib/upwords/cursor.rb', line 18 def down move(1, 0) end |
#left ⇒ Object
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 |
#pos ⇒ Object
35 36 37 |
# File 'lib/upwords/cursor.rb', line 35 def pos [y, x] end |
#right ⇒ Object
26 27 28 |
# File 'lib/upwords/cursor.rb', line 26 def right move(0, 1) end |
#up ⇒ Object
14 15 16 |
# File 'lib/upwords/cursor.rb', line 14 def up move(-1, 0) end |