Class: Position

Inherits:
Struct
  • Object
show all
Defined in:
lib/position.rb

Constant Summary collapse

NORTH =
:north
EAST =
:east
SOUTH =
:south
WEST =
:west

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#orientationObject

Returns the value of attribute orientation

Returns:

  • (Object)

    the current value of orientation



1
2
3
# File 'lib/position.rb', line 1

def orientation
  @orientation
end

#xObject

Returns the value of attribute x

Returns:

  • (Object)

    the current value of x



1
2
3
# File 'lib/position.rb', line 1

def x
  @x
end

#yObject

Returns the value of attribute y

Returns:

  • (Object)

    the current value of y



1
2
3
# File 'lib/position.rb', line 1

def y
  @y
end

Instance Method Details

#advanceObject



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

#leftObject



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

def left
  rotate -1
end

#rightObject



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

def right
  rotate 1
end

#to_sObject



34
35
36
# File 'lib/position.rb', line 34

def to_s
  "#{x},#{y},#{orientation.to_s.upcase}"
end

#valid?(grid) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/position.rb', line 30

def valid?(grid)
  (0...grid.width).include?(x) && (0...grid.height).include?(y)
end