-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.rb
94 lines (87 loc) · 2.53 KB
/
route.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class Route
attr_reader :get
attr_reader :distance
attr_reader :routeArray
# Take User Obj and Coordinate
def initialize(user, destination)
pointer = []
route = []
pointer.push(*user.coordinate.get)
res = ["- Start at #{pointer}"]
## This will generate the array of string
## For the history that will be put into the history file
# 1
if user.coordinate.x < destination.x
(destination.x - user.coordinate.x).times do
pointer[0] = pointer[0] += 1
res.push("- Go to #{pointer}")
route.push([pointer[0],pointer[1]])
end
# 2
if user.coordinate.y < destination.y
res.push("- Turn left")
(destination.y - user.coordinate.y).times do
pointer[1] = pointer[1] += 1
res.push("- Go to #{pointer}")
route.push([pointer[0],pointer[1]])
end
elsif user.coordinate.y > destination.y
res.push("- Turn Right")
(user.coordinate.y - destination.y).times do
pointer[1] = pointer[1] -= 1
res.push("- Go to #{pointer}")
route.push([pointer[0],pointer[1]])
end
else
end
# 1
elsif user.coordinate.x > destination.x
(user.coordinate.x - destination.x).times do
pointer[0] = pointer[0] -= 1
res.push("- Go to #{pointer}")
route.push([pointer[0],pointer[1]])
end
#2
if user.coordinate.y < destination.y
res.push("- Turn Right")
(destination.y - user.coordinate.y).times do
pointer[1] = pointer[1] += 1
res.push("- Go to #{pointer}")
route.push([pointer[0],pointer[1]])
end
elsif user.coordinate.y > destination.y
res.push("- Turn Left")
(user.coordinate.y - destination.y).times do
pointer[1] = pointer[1] -= 1
res.push("- Go to #{pointer}")
route.push([pointer[0],pointer[1]])
end
else
end
else
if user.coordinate.y < destination.y
(destination.y - user.coordinate.y).times do
pointer[1] = pointer[1] += 1
res.push("- Go to #{pointer}")
route.push([pointer[0],pointer[1]])
end
elsif user.coordinate.y > destination.y
(user.coordinate.y - destination.y).times do
pointer[1] = pointer[1] -= 1
res.push("- Go to #{pointer}")
route.push([pointer[0],pointer[1]])
end
else
end
end
# 1
res.push("- Finish at #{pointer}")
@get = res
@distance = generateDistance(user, destination)
@routeArray = route
end
# This will generate the distance based on the route
def generateDistance(u, d)
(u.coordinate.x - d.x).abs + (u.coordinate.y - d.y).abs
end
end