https://github.com/shanghuiyang/astar
A-star algorithm implemented with Go.
https://github.com/shanghuiyang/astar
astar-algorithm astar-pathfinding
Last synced: about 2 months ago
JSON representation
A-star algorithm implemented with Go.
- Host: GitHub
- URL: https://github.com/shanghuiyang/astar
- Owner: shanghuiyang
- License: mit
- Created: 2020-10-07T16:06:42.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-12-31T04:59:52.000Z (over 4 years ago)
- Last Synced: 2023-07-27T22:36:55.112Z (almost 3 years ago)
- Topics: astar-algorithm, astar-pathfinding
- Language: Go
- Homepage:
- Size: 71.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# A-Star
[](https://github.com/shanghuiyang/astar/actions/workflows/ci.yml)
[](https://github.com/shanghuiyang/astar/blob/master/LICENSE)
A-Star algorithm implemented with Go.
## Usage
see the [example/main.go](example/main.go) for complete usage.
```go
package main
import (
"fmt"
"github.com/shanghuiyang/astar"
"github.com/shanghuiyang/astar/tilemap"
)
// a map with 10 rows and 20 cols
const strmap = `
####################
# #
# #
# ######### #
# #
# ####### #
# #
# #
# #
####################
`
func main() {
// build a map from string
m := tilemap.BuildFromStr(strmap)
// define the origin and destination
org := &astar.Point{X: 7, Y: 2}
des := &astar.Point{X: 1, Y: 16}
// find the path using a-star algorithm
a := astar.New(m)
path, err := a.FindPath(org, des)
if err != nil {
fmt.Printf("error: %v\n", err)
return
}
// draw the tilemap with the path
a.Draw()
fmt.Printf("path: %v\n\n", path)
}
```
output,
```
####################
# .B #
# . #
# #########. #
# ........ #
# . ####### #
# . #
# A #
# #
####################
path: (7, 2) (6, 3) (5, 4) (4, 5) (4, 6) (4, 7) (4, 8) (4, 9) (4, 10) (4, 11) (4, 12) (3, 13) (2, 14) (1, 15) (1, 16)
```
## More Cases
```
################ ################
# # # #
# A # # A #
# ## # # . ## #
# # # . #
# B # # ........B #
################ ################
----------------------------------------
################ ################
# # # #
# A # # A.... #
####### ######## #######.########
# # # . #
# B # # ....B #
################ ################
----------------------------------------
################ ################
# # # .. #
# A # # # A . #. #
# # # # .. # . #
# # # # # . #
# # B # # # ..B #
################ ################
----------------------------------------
################
# # #
# A # #
# # # no way!
# # #
# # B #
################
----------------------------------------
################
# #
# A #
# # no way!
# #####
# # B #
################
```