a-star 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/A_Star.rb +14 -18
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0cfec0ca5ab1dcc9074403c04a15c8b882dbca2
|
4
|
+
data.tar.gz: 64180ed20f03c68ab30dae600b10182dc4cab98d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 962f22ba62929fd10049410198e868949792610c01c0a5af0edda89b04991e2e8b5ed2c11ed48b411ee89c08b065fb4f1226d3ca9349de395d33ca3293d1ff14
|
7
|
+
data.tar.gz: 65a972cc63d057b5aa1e74474748aae1efb28c1db33c3f5a1431609a56a3a8924c63c130920d08639903374983dd2a343d8a96e132bbee403ddaaab21987e94b
|
data/lib/A_Star.rb
CHANGED
@@ -32,8 +32,8 @@ class FromTo
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
def node x, y, index, cost,
|
36
|
-
return [x, y, index, cost,
|
35
|
+
def node x, y, index, cost, heuristic, totalCost
|
36
|
+
return [x, y, index, cost, heuristic, totalCost]
|
37
37
|
end
|
38
38
|
|
39
39
|
class AStar
|
@@ -47,7 +47,7 @@ class AStar
|
|
47
47
|
@mazeLabel = mazeName.split( /()\s|\./)[0]
|
48
48
|
@mazeFileType = "." + mazeName.split(/\s|\./)[1]
|
49
49
|
|
50
|
-
@firstNode = node start[0], start[1], -1, -1, -1, -1 # [x, y, index,
|
50
|
+
@firstNode = node start[0], start[1], -1, -1, -1, -1 # [x, y, index, cost, heuristic, totalCost]
|
51
51
|
@destNode = node dest[0], dest[1], -1, -1, -1, -1
|
52
52
|
|
53
53
|
@height = maze.dimension.height
|
@@ -61,7 +61,7 @@ class AStar
|
|
61
61
|
|
62
62
|
def solve
|
63
63
|
|
64
|
-
|
64
|
+
until @visited.empty? do
|
65
65
|
minIndex = 0
|
66
66
|
0.upto @visited.length - 1 do |i|
|
67
67
|
if @visited[i][5] < @visited[minIndex][5]
|
@@ -75,14 +75,14 @@ class AStar
|
|
75
75
|
if here[0] == @destNode[0] && here[1] == @destNode[1]
|
76
76
|
path = [@destNode]
|
77
77
|
puts "We're here! Final node at: (x: #{here[0]}, y: #{here[1]})"
|
78
|
-
|
78
|
+
until here[2] == -1 do
|
79
79
|
here = @unvisited[here[2]]
|
80
80
|
path.unshift here
|
81
81
|
end
|
82
|
-
puts "The entire path from #{@start} to #{@dest}
|
82
|
+
puts "The entire path from node #{@start} to node #{@dest} are the nodes: \n#{path}"
|
83
83
|
|
84
84
|
hue = 0
|
85
|
-
hueCoeff = 360.0 / path.length
|
85
|
+
hueCoeff = 360.0 / path.length # when * by path.length (the end of the arr) it would be 360, so one complete rainbow
|
86
86
|
|
87
87
|
(1..path.length).each do |n|
|
88
88
|
@solvedMaze[ path[n - 1][0], path[n - 1][1] ] = ChunkyPNG::Color.from_hsl(hue, 1, 0.6)
|
@@ -100,7 +100,7 @@ class AStar
|
|
100
100
|
horizontalFriend = friendNodes[j][0]
|
101
101
|
verticalFriend = friendNodes[j][1]
|
102
102
|
|
103
|
-
if passable? horizontalFriend, verticalFriend || (horizontalFriend
|
103
|
+
if passable? horizontalFriend, verticalFriend || (horizontalFriend == @destNode[0] && verticalFriend == @destNode[1])
|
104
104
|
onUnvisited = false
|
105
105
|
0.upto @unvisited.length - 1 do |k|
|
106
106
|
unvisitedNode = @unvisited[k]
|
@@ -124,7 +124,7 @@ class AStar
|
|
124
124
|
friendHeuristics << heuristic(friendNodes[k], @dest)
|
125
125
|
end
|
126
126
|
lowestHeuristic = friendHeuristics.min
|
127
|
-
|
127
|
+
unless onVisited && heuristic(friendNodes[j], @dest) != lowestHeuristic # If you're somwhere new and is fastest
|
128
128
|
newNode = node horizontalFriend, verticalFriend, @unvisited.length - 1, -1, -1, -1
|
129
129
|
newNode[3] = here[3] + cost(here, newNode)
|
130
130
|
newNode[4] = heuristic newNode, @destNode
|
@@ -192,13 +192,14 @@ class AStar
|
|
192
192
|
|
193
193
|
def draw
|
194
194
|
puts "Solving..."
|
195
|
-
go = Time.new
|
196
195
|
|
197
|
-
|
196
|
+
go = Time.new # Start the time
|
197
|
+
path = solve # Here we go!
|
198
|
+
finish = Time.new # Take the finish time
|
199
|
+
|
198
200
|
unless path.empty?
|
199
|
-
finish = Time.new
|
200
201
|
puts "\n\nTime taken to solve: " + (finish - go).to_s + " seconds!"
|
201
|
-
minutes = ((finish - go) / 60).round
|
202
|
+
minutes = ((finish - go) / 60.0).round
|
202
203
|
if minutes > 0
|
203
204
|
if minutes > 1
|
204
205
|
puts "Circa " + minutes.to_s + " Minutes."
|
@@ -210,11 +211,6 @@ class AStar
|
|
210
211
|
puts "No solution found, solve function returned empty array for path!\nPlease make sure your maze is solvable!"
|
211
212
|
end
|
212
213
|
|
213
|
-
#startColour = "#ff3c5e"
|
214
|
-
#destColour = "#68ff9f"
|
215
|
-
#@solvedMaze[@start[0], @start[1]] = ChunkyPNG::Color.from_hex startColour
|
216
|
-
#@solvedMaze[@dest[0], @dest[1]] = ChunkyPNG::Color.from_hex destColour
|
217
|
-
|
218
214
|
@solvedMaze.save(@mazeLabel + "-solved" + @mazeFileType)
|
219
215
|
end
|
220
216
|
end
|