class Position

Constants

EAST
NORTH
SOUTH
WEST

Public Instance Methods

advance() click to toggle source
# 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() click to toggle source
# File lib/position.rb, line 22
def left
  rotate -1
end
right() click to toggle source
# File lib/position.rb, line 26
def right
  rotate 1
end
to_s() click to toggle source
# File lib/position.rb, line 34
def to_s
  "#{x},#{y},#{orientation.to_s.upcase}"
end
valid?(grid) click to toggle source
# File lib/position.rb, line 30
def valid?(grid)
  (0...grid.width).include?(x) && (0...grid.height).include?(y)
end

Private Instance Methods

orientations() click to toggle source
# File lib/position.rb, line 46
def orientations
  [NORTH, EAST, SOUTH, WEST]
end
rotate(by) click to toggle source
# File lib/position.rb, line 40
def rotate(by)
  facing_index = orientations.index(orientation)
  facing       = orientations.rotate(facing_index).rotate(by).first
  Position.new(x, y, facing)
end