-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposition.rb
More file actions
52 lines (44 loc) · 888 Bytes
/
Copy pathposition.rb
File metadata and controls
52 lines (44 loc) · 888 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Represents the absolute position
# of an object identified by (x,y)
# and direction.
require './direction'
include Direction
module Position
class PositionClass
def initialize(x,y,d)
@x_val = x
@y_val = y
@dir = DirectionClass.new(d)
end
def to_s
return @x_val.to_s+" "+@y_val.to_s+" "+@dir.to_s
end
def move_forward()
case @dir.to_s
when "N"
@y_val += 1
when "S"
@y_val -= 1
when "E"
@x_val += 1
when "W"
@x_val -= 1
end
end
def turn_left()
@dir.switch_to_left
end
def turn_right()
@dir.switch_to_right
end
def fell_off?(edge_x,edge_y)
if @x_val < 0 || @x_val > edge_x
return true
end
if @y_val < 0 || @y_val > edge_y
return true
end
return false
end
end
end