-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.rb
159 lines (136 loc) · 4.04 KB
/
map.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require "./node.rb"
$w = 5
$h = 7
$map = Array.new($w){Array.new($h)}
$start_point
$end_point
$openlist = Hash.new
$closedlist = []
def create_map
$w.times do |x|
$h.times do |y|
$map[x][y] = Node.new
$map[x][y].x = x
$map[x][y].y = y
end
end
end
def update_neighbors
$w.times do |x|
$h.times do |y|
if x > 0
$map[x][y].neighbor.push($map[x-1][y])
end
if x < $w-1
$map[x][y].neighbor.push($map[x+1][y])
end
if y > 0
$map[x][y].neighbor.push($map[x][y-1])
end
if y < $h-1
$map[x][y].neighbor.push($map[x][y+1])
end
if x > 0 && y > 0
$map[x][y].neighbor.push($map[x-1][y-1])
end
if x > 0 && y < $h-1
$map[x][y].neighbor.push($map[x-1][y+1])
end
if x < $w-1 && y > 0
$map[x][y].neighbor.push($map[x+1][y-1])
end
if x < $w-1 && y < $h-1
$map[x][y].neighbor.push($map[x+1][y+1])
end
end
end
end
def edit_map
$map[1][3].passable = false
$map[2][3].passable = false
$map[3][3].passable = false
end
def print_out_map
$map.each do |row|
row.each do |node|
print "|#{node.x} #{node.y} "
if node.passable
print "."
else
print "#"
end
end
puts " "
end
end
def find_path()
$start_point = $map[0][0]
$end_point = $map[4][4]
$openlist[$start_point] = 0
while $openlist.length > 0 do
current = $openlist.key($openlist.values.min)
if(current == $end_point)
break;
end
$closedlist << current
$openlist.delete(current)
for neighbor in current.neighbor
if($closedlist.include?(neighbor) || neighbor.passable == false)
puts "neighbor #{neighbor.x} #{neighbor.y} is on closed list or is not walkable"
else
if($openlist.include?(neighbor) == false)
puts "neighbor #{neighbor.x} #{neighbor.y} is not on the open list, adding it!"
neighbor.parent = current
neighbor.gscore = neighbor.parent.gscore + 10
if(neighbor.x != current.x && neighbor.y != current.y)
neighbor.gscore = neighbor.parent.gscore + 14
end
neighbor.hscore = calculate_h_score(neighbor.x,neighbor.y,$end_point.x,$end_point.y)
neighbor.fscore = neighbor.gscore + neighbor.hscore
$openlist[neighbor] = neighbor.fscore
puts "my hscore is #{neighbor.hscore}"
puts "my gscore is #{neighbor.gscore}"
puts "my fscore is #{neighbor.fscore}"
elsif($openlist.include?(neighbor))
puts "neighbor #{neighbor.x} #{neighbor.y} is on the open list"
tempG = neighbor.parent.gscore + 10
if(neighbor.x != current.x && neighbor.y != current.y)
tempG = neighbor.parent.gscore + 14
end
puts "temp #{tempG}"
puts "neighbor parent g score #{neighbor.parent.gscore}"
if(tempG < neighbor.gscore)
neighbor.gscore = tempG
neighbor.hscore = calculate_h_score(neighbor.x,neighbor.y,$end_point.x,$end_point.y)
neighbor.fscore = neighbor.gscore + neighbor.hscore
$openlist[neighbor] = neighbor.fscore
puts "tempG was #{tempG}"
puts "my hscore is #{neighbor.hscore}"
puts "my gscore is #{neighbor.gscore}"
puts "my fscore is #{neighbor.fscore}"
end
end
end
end
end
end
def calculate_h_score(startx,starty,endx,endy)
return ((startx-endx).abs + (starty-endy).abs)*10
end
def path()
parent = $map[4][4].parent
parent2 = parent.parent
parent3 = parent2.parent
parent4 = parent3.parent
parent5 = parent4.parent
puts "parent #{parent.x} #{parent.y}"
puts "parent2 #{parent2.x} #{parent2.y}"
puts "parent3 #{parent3.x} #{parent3.y}"
puts "parent4 #{parent4.x} #{parent4.y}"
end
create_map()
edit_map()
update_neighbors()
find_path()
print_out_map()
path