blob: 3e85ecf85eab4f855a504424e7495d297599c73a (
plain)
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
55
56
57
58
59
60
61
62
63
64
|
class Rover
attr_reader :location
def initialize(heading, coordinates)
@heading = heading
@location = coordinates
end
def heading
@heading.class.name.downcase.to_sym
end
def turn_right
@heading = @heading.turn_right
end
def turn_left
@heading = @heading.turn_left
end
def move_forward(terrain)
@heading.forward(@location, terrain)
end
end
class North
def turn_right
East.new
end
def turn_left
West.new
end
def forward(current_location, terrain)
current_location[:y] = current_location[:y]+1
end
end
class East
def turn_right
South.new
end
def turn_left
North.new
end
def forward(current_location, terrain)
current_location[:x] = current_location[:x]+1
end
end
class West
def turn_right
North.new
end
def turn_left
South.new
end
def forward(current_location, terrain)
current_location[:x] = current_location[:x]-1
end
end
class South
def turn_right
West.new
end
def turn_left
East.new
end
def forward(current_location, terrain)
current_location[:y] = current_location[:y]-1
end
end
|