forked from unorthodoxgeek/pre-ruby-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.rb
65 lines (53 loc) · 1.18 KB
/
robot.rb
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
65
class Robot
@@DIRECTIONS_ARR = [:north, :east, :south, :west]
@@DIRECTIONS_HASH = Hash[@@DIRECTIONS_ARR.map.with_index.to_a]
def initialize(board)
@board = board
end
def place(x, y, direction)
if (@board.valid?(x, y))
@x = x.to_i
@y = y.to_i
@direction = direction.downcase.to_sym
end
end
def isPlaceInitialized
@x and @y and @direction
end
def report
if (isPlaceInitialized)
puts [@x, @y, @direction].join(",").upcase
[@x, @y, @direction]
end
end
def move
if (isPlaceInitialized)
send(@direction)
end
end
def left
if (isPlaceInitialized)
arr_new_index = (@@DIRECTIONS_HASH[@direction] - 1) % @@DIRECTIONS_ARR.length;
@direction = @@DIRECTIONS_ARR[arr_new_index]
end
end
def right
if (isPlaceInitialized)
arr_new_index = (@@DIRECTIONS_HASH[@direction] + 1) % @@DIRECTIONS_ARR.length;
@direction = @@DIRECTIONS_ARR[arr_new_index]
end
end
private
def north
place(@x, @y +1, @direction)
end
def south
place(@x, @y -1, @direction)
end
def east
place(@x + 1, @y, @direction)
end
def west
place(@x -1, @y, @direction)
end
end