示例#1
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            int winner = 0;

            for (int i = 0; i < openSet.Count; i++)
            {
                if (openSet[i].f < openSet[winner].f)
                {
                    winner = i;
                }
            }

            Spot current = openSet[winner];
            Spot temp    = current;

            if (openSet[winner] == end)
            {
                path.Add(temp);
                while (temp.previous != null)
                {
                    path.Add(temp.previous);
                    temp = temp.previous;
                }

                Debug.WriteLine("DONE!");
                timer1.Enabled = false;
            }

            removeFromArray(openSet, current);
            closeSet.Add(current);

            List <Spot> neighbors = current.neighbors;

            for (int i = 0; i < neighbors.Count; i++)
            {
                Spot neighbor = neighbors[i];

                if (!closeSet.Exists(u => u == neighbor) && !neighbor.wall)
                {
                    double tempG = current.g + 1;

                    if (openSet.Exists(u => u == neighbor))
                    {
                        if (tempG < neighbor.g)
                        {
                            neighbor.g = tempG;
                        }
                    }
                    else
                    {
                        neighbor.g = tempG;
                        openSet.Add(neighbor);
                    }

                    neighbor.h        = heuristic(neighbor, end);
                    neighbor.f        = neighbor.g + neighbor.h;
                    neighbor.previous = current;
                }
            }



            foreach (Spot item in openSet)
            {
                item.Fill(g, Brushes.Green);
            }

            foreach (Spot item in closeSet)
            {
                item.Fill(g, Brushes.Red);
            }

            foreach (Spot item in path)
            {
                item.Fill(g, Brushes.Blue);
            }
        }
示例#2
0
        public void FindPath(Object obj)
        {
            try
            {
                KeyValuePair <Point, Spot> FirstPair = Spots.First();

                FirstPair.Value.GScore = 0;

                FirstPair.Value.HScore = PointFunctions.DistanceTo(FirstPair.Value.Point, End);

                OpenSet.Add(FirstPair);

                while (OpenSet.Count > 0)
                {
                    Spot Current = OpenSet.ToList().OrderBy(p => p.Value.FScore).First().Value;

                    Console.WriteLine("Current point is: " + Current.Point.ToString());

                    Invalidate();

                    if (Current.Point == End)
                    {
                        Current.Color = Color.Blue;

                        bool Searching = true;

                        while (Searching)
                        {
                            if (Current.CameFrom.Value != null)
                            {
                                Current.CameFrom.Value.Color = Color.Blue;

                                Current.CameFrom.Value.OnBestPath = true;

                                Current = Current.CameFrom.Value;
                            }
                            else
                            {
                                Searching = false;
                            }
                        }


                        Spots.ToList().ForEach(s =>
                        {
                            if (!s.Value.OnBestPath && !s.Value.IsObstacle)
                            {
                                s.Value.Color = Color.White;
                                Invalidate();
                            }
                        });

                        Invalidate();

                        Console.WriteLine("Done!");

                        break;
                    }

                    ClosedSet.Add(Current.Point, Current);

                    OpenSet.Remove(Current.Point);

                    int tempG = 0;

                    foreach (KeyValuePair <Point, Spot> neighbour in Current.Neibhours)
                    {
                        if (ClosedSet.Contains(neighbour) || neighbour.Value.IsObstacle)
                        {
                            continue;
                        }

                        tempG = Current.GScore + PointFunctions.DistanceTo(Current.Point, neighbour.Key);

                        if (!OpenSet.Contains(neighbour))
                        {
                            OpenSet.Add(neighbour);
                        }
                        else if (tempG >= neighbour.Value.GScore)
                        {
                            continue;
                        }

                        neighbour.Value.GScore   = tempG;
                        neighbour.Value.HScore   = PointFunctions.DistanceTo(neighbour.Key, End);
                        neighbour.Value.CameFrom = new KeyValuePair <Point, Spot>(Current.Point, Current);
                    }

                    Invalidate();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);

                System.Diagnostics.Debugger.Break();

                throw;
            }
        }