-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirection.rb
More file actions
54 lines (48 loc) · 1.02 KB
/
Copy pathdirection.rb
File metadata and controls
54 lines (48 loc) · 1.02 KB
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
52
53
54
# Represents a direction instance
# and methods to manipulate the
# current direction
module Direction
$dirs = [:north,:east,:south,:west]
$dir_values = {
north:0,
east: 1,
south:2,
west: 3
}
class DirectionClass
def initialize(cur)
case cur
when "N"
@current = :north
when "E"
@current = :east
when "W"
@current = :west
when "S"
@current = :south
else
raise RuntimeError, "Invalid direction value"
end
end
def to_s
case @current
when :north
"N"
when :south
"S"
when :east
"E"
when :west
"W"
end
end
def switch_to_left()
req_direction = ($dir_values[@current]-1)%($dir_values.size)
@current = $dirs[req_direction]
end
def switch_to_right()
req_direction = ($dir_values[@current]+1)%($dir_values.size)
@current = $dirs[req_direction]
end
end
end