beefsack / go-astar

Go implementation of the A* search algorithm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Returns path in wrong direction

avivey opened this issue · comments

While defined as func Path(from, to Pather) ..., the resulting path actually starts at to node and ends at from.

This can be seen using this patch to pather_test.go:

diff --git pather_test.go pather_test.go
index b9d78bd..331ee0d 100644
--- pather_test.go
+++ pather_test.go
@@ -159,18 +159,19 @@ func (w World) RenderPath(path []Pather) string {
                return ""
        }
        height := len(w[0])
-       pathLocs := map[string]bool{}
-       for _, p := range path {
+       pathLocs := map[string]rune{}
+       for i, p := range path {
                pT := p.(*Tile)
-               pathLocs[fmt.Sprintf("%d,%d", pT.X, pT.Y)] = true
+               nr := []rune("0123456789abc")[i%12] // "clever" rendering of node index in path.
+               pathLocs[fmt.Sprintf("%d,%d", pT.X, pT.Y)] = nr
        }
        rows := make([]string, height)
        for x := 0; x < width; x++ {
                for y := 0; y < height; y++ {
                        t := w.Tile(x, y)
                        r := ' '
-                       if pathLocs[fmt.Sprintf("%d,%d", x, y)] {
-                               r = KindRunes[KindPath]
+                       if nr := pathLocs[fmt.Sprintf("%d,%d", x, y)]; nr != 0 {
+                               r = nr
                        } else if t != nil {
                                r = KindRunes[t.Kind]
                        }

which renders 0 on T and 9 on F:

=== RUN   TestStraightLine
    path_test.go:14: Input world
        .....~......
        .....MM.....
        .F........T.
        ....MMM.....
        ............
    path_test.go:19: Resulting path
        .....~......
        .....MM.....
        .9876543210.
        ....MMM.....
        ............
--- PASS: TestStraightLine (0.01s)

Hi @avivey, thanks for the issue and the investigation. This could potentially be a breaking change for users, so will have to be fixed in a major version.