Class: Position
- Inherits:
-
Struct
- Object
- Struct
- Position
- Defined in:
- lib/position.rb
Constant Summary collapse
- NORTH =
:north
- EAST =
:east
- SOUTH =
:south
- WEST =
:west
Instance Attribute Summary collapse
-
#orientation ⇒ Object
Returns the value of attribute orientation.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
Instance Attribute Details
#orientation ⇒ Object
Returns the value of attribute orientation
1 2 3 |
# File 'lib/position.rb', line 1 def orientation @orientation end |
#x ⇒ Object
Returns the value of attribute x
1 2 3 |
# File 'lib/position.rb', line 1 def x @x end |
#y ⇒ Object
Returns the value of attribute y
1 2 3 |
# File 'lib/position.rb', line 1 def y @y end |
Instance Method Details
#advance ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/position.rb', line 7 def advance case orientation when NORTH Position.new(x, y + 1, orientation) when EAST Position.new(x + 1, y, orientation) when SOUTH Position.new(x, y - 1, orientation) when WEST Position.new(x - 1, y, orientation) else self end end |
#left ⇒ Object
22 23 24 |
# File 'lib/position.rb', line 22 def left rotate -1 end |
#right ⇒ Object
26 27 28 |
# File 'lib/position.rb', line 26 def right rotate 1 end |
#to_s ⇒ Object
34 35 36 |
# File 'lib/position.rb', line 34 def to_s "#{x},#{y},#{orientation.to_s.upcase}" end |
#valid?(grid) ⇒ Boolean
30 31 32 |
# File 'lib/position.rb', line 30 def valid?(grid) (0...grid.width).include?(x) && (0...grid.height).include?(y) end |