-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaStar.go
53 lines (50 loc) · 1.07 KB
/
aStar.go
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
package main
import (
"sort"
"time"
)
func solveAStar(grid [DIM][DIM]*cell, start, finish *cell) {
defer func() {
lock = false
}()
resetPathfindingValues(grid)
var openList, closeList cellList
start.h = 0
start.f = 0
start.g = 0
openList = append(openList, start)
for len(openList) > 0 {
sort.Sort(openList)
current := openList[0]
openList = openList[1:]
closeList = append(closeList, current)
for _, v := range current.getNeighbours(grid) {
x := v.x
y := v.y
fromGrid := grid[x][y]
if fromGrid.isDestination(finish) {
fromGrid.parent = current
printTrace(fromGrid)
return
}
if fromGrid.isInList(closeList) {
continue
}
gNew := current.g + 1
hNew := fromGrid.getHScore(finish)
fNew := gNew + hNew
if fromGrid.f == INF || fromGrid.f > fNew {
fromGrid.g = gNew
fromGrid.h = hNew
fromGrid.f = fNew
fromGrid.parent = current
fromGrid.hit = true
if !fromGrid.isInList(openList) {
openList = append(openList, fromGrid)
}
}
}
time.Sleep(20 * time.Millisecond)
}
println("Not found")
}