Exemplo n.º 1
0
        public Form1()
        {
            InitializeComponent();

            this.DoubleBuffered = true;

            this.BackColor = Color.Black;

            w = (Width) / Cols;

            h = (Height) / Rows;

            this.WindowState = FormWindowState.Maximized;

            Spots = new Dictionary <Point, Spot>();

            OpenSet = new OpenSet(Color.Green);

            ClosedSet = new ClosedSet(Color.Red);

            MainPath = new List <Spot>();

            Random Ran = new Random(10000000);

            for (int i = 0; i < Cols; i++)
            {
                for (int j = 0; j < Rows; j++)
                {
                    Spot spot = new Spot(i, j, w, h, Color.White, Ran);

                    KeyValuePair <Point, Spot> pair = new KeyValuePair <Point, Spot>(spot.Point, spot);

                    Spots.Add(spot.Point, spot);

                    if (i == Cols - 1 && j == Rows - 1)
                    {
                        End = spot.Point;
                    }
                }
            }

            //Spot s = Spots.Last().Value;

            foreach (KeyValuePair <Point, Spot> item in Spots)
            {
                item.Value.FindNeighbours(Spots, Cols, Rows);
            }

            Astar algorithm = new Astar(Spots, End);

            algorithm.InvalidateCaller += RefreshForm;

            ThreadPool.QueueUserWorkItem(new WaitCallback(algorithm.FindPath));
        }
Exemplo n.º 2
0
 static void Main(string[] args)
 {
     //地图
     matrix Matrix = new matrix(7, 3);
     //开始点
     Node startNode = new Node(2,2,0,new Node());
     //结束点
     Node endNode = new Node(6, 2, 0, new Node());
     //阻挡点
     Node obstalNode1 = new Node(4, 1, 0, new Node());
     Node obstalNode2 = new Node(4, 2, 0, new Node());
     Node obstalNode3 = new Node(4, 3, 0, new Node());
     Dictionary<string, Node> obstalDic = new Dictionary<string,Node>();
     obstalDic.Add(obstalNode1.getKey(), obstalNode1);
     obstalDic.Add(obstalNode2.getKey(), obstalNode1);
     obstalDic.Add(obstalNode3.getKey(), obstalNode1);
     Astar astar = new Astar(startNode, endNode, Matrix, obstalDic);
     astar.AStar();
     astar.getShortCutRoute();
     Console.Read();
 }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            int[][] map = CreateMap();

            Action <Node <int> > populateChildren = node =>
            {
                int        locX = node.locationX;
                int        locY = node.locationY;
                Node <int> child;

                if (locX + 1 < map.Length)
                {
                    if (map[locX + 1][locY] == 0)
                    {
                        child = Node <int> .CreateNode();

                        child.locationX = locX + 1;
                        child.locationY = locY;
                        node.Children.Add(child);
                    }
                }
                if (locX - 1 >= 0)
                {
                    if (map[locX - 1][locY] == 0)
                    {
                        child = Node <int> .CreateNode();

                        child.locationX = locX - 1;
                        child.locationY = locY;
                        node.Children.Add(child);
                    }
                }
                if (locY + 1 < map[locX].Length)
                {
                    if (map[locX][locY + 1] == 0)
                    {
                        child = Node <int> .CreateNode();

                        child.locationX = locX;
                        child.locationY = locY + 1;
                        node.Children.Add(child);
                    }
                }
                if (locY - 1 >= 0)
                {
                    if (map[locX][locY - 1] == 0)
                    {
                        child = Node <int> .CreateNode();

                        child.locationX = locX;
                        child.locationY = locY - 1;
                        node.Children.Add(child);
                    }
                }
            };
            Func <Node <int>, Node <int>, int> heuristic = (start, goal) =>
            {
                return(Math.Abs(start.locationX - goal.locationX) + Math.Abs(start.locationY - goal.locationY));
            };

            Astar <int> pathFinding = new Astar <int>(heuristic, populateChildren);
            Node <int>  s           = Node <int> .CreateNode();

            Node <int> g = Node <int> .CreateNode();

            bool found = false;
            int  z     = 0;
            int  q     = 0;

            while (!found)
            {
                if (map[z][q] == 0)
                {
                    s.locationX = z;
                    s.locationY = q;
                    found       = true;
                    break;
                }
                q++;
                if (q == 5)
                {
                    z++;
                    q = 0;
                }
            }
            found = false;
            z     = map.Length - 1;
            q     = map.Length - 1;
            while (!found)
            {
                if (map[z][q] == 0)
                {
                    g.locationX = z;
                    g.locationY = q;
                    found       = true;
                    break;
                }
                q--;
                if (q == -1)
                {
                    z--;
                    q = 4;
                }
            }

            List <Node <int> > path = pathFinding.FindPath(s, g);

            if (path != null && path.Count > 0)
            {
                Console.WriteLine("Path Found");
                foreach (Node <int> x in path)
                {
                    x.printNode();
                }
            }
            else
            {
                Console.WriteLine("Path doesn't exist!");
            }

            PrintMap(map);

            Console.ReadLine();
        }