From 46c262574b049e1792dcf53e806e26ccc8b51b8f Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Tue, 20 Jun 2023 11:17:40 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20A*=20=E7=A4=BA=E4=BE=8B=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/astar/astar_example_test.go | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 utils/astar/astar_example_test.go diff --git a/utils/astar/astar_example_test.go b/utils/astar/astar_example_test.go new file mode 100644 index 0000000..14244fb --- /dev/null +++ b/utils/astar/astar_example_test.go @@ -0,0 +1,60 @@ +package astar_test + +import ( + "fmt" + "github.com/kercylan98/minotaur/utils/astar" + "github.com/kercylan98/minotaur/utils/geometry" +) + +type Graph struct { + geometry.FloorPlan +} + +func (slf Graph) Neighbours(point geometry.Point[int]) []geometry.Point[int] { + neighbours := make([]geometry.Point[int], 0, 4) + for _, direction := range geometry.DirectionUDLR { + np := geometry.GetDirectionNextWithCoordinateArray(direction, point) + if slf.IsFree(np) { + neighbours = append(neighbours, np) + } + } + + return neighbours +} + +func ExampleFind() { + graph := Graph{ + FloorPlan: geometry.FloorPlan{ + "===========", + "X XX X X", + "X X XX X", + "X XX X", + "X XXX X", + "X XX X X", + "X XX X X", + "===========", + }, + } + + paths := astar.Find[geometry.Point[int], int](graph, geometry.NewPoint(1, 1), geometry.NewPoint(8, 6), func(a, b geometry.Point[int]) int { + return geometry.CalcDistance(geometry.DoublePointToCoordinate(a, b)) + }, func(a, b geometry.Point[int]) int { + return geometry.CalcDistance(geometry.DoublePointToCoordinate(a, b)) + }) + + for _, path := range paths { + graph.Put(path, '.') + } + + fmt.Println(graph) + + // Output: + // =========== + // X.XX X X + // X. X XX X + // X.XX......X + // X.... XXX.X + // X XX X ..X + // X XX X . X + // =========== +}