Exemplo n.º 1
0
 public bool isEqual(node other)
 {
     return (xPos == other.xPos && yPos == other.yPos);
 }
Exemplo n.º 2
0
        private void aStar()
        {
            List<node> open = new List<node>();
            List<node> closed = new List<node>();
            node active = new node();
            active.xPos = startX;
            active.yPos = startY;
            open.Add(active);

            while ((active.xPos != destX || active.yPos != destY))
            {

                for (int i = 0; i < 8; i++)
                {
                    int x = 0;
                    int y = 0;
                    //west
                    if (i == 0)
                    {
                        x = -1;
                        y = 0;
                    }
                    //northwest
                    if (i == 1)
                    {
                        x = -1;
                        y = -1;
                    }
                    //north
                    if (i == 2)
                    {
                        x = 0;
                        y = -1;
                    }
                    //northeast
                    if (i == 3)
                    {
                        x = 1;
                        y = -1;
                    }
                    //east
                    if (i == 4)
                    {
                        x = 1;
                        y = 0;
                    }
                    //southeast
                    if (i == 5)
                    {
                        x = 1;
                        y = 1;
                    }
                    //south
                    if (i == 6)
                    {
                        x = 0;
                        y = 1;
                    }
                    //southwest
                    if (i == 7)
                    {
                        x = -1;
                        y = 1;
                    }

                    node temp = new node();
                    temp.parent = active;
                    temp.xPos = temp.parent.xPos + x;
                    temp.yPos = temp.parent.yPos + y;
                    if ((temp.xPos >= 0 && temp.xPos< 172) && (temp.yPos >= 0 && temp.yPos < 120))
                    {

                        //calculates F G and H values
                        if (Math.Abs(x) == Math.Abs(y))
                            temp.G = 14;
                        else
                            temp.G = 10;
                        temp.G = temp.G + temp.parent.G;
                        temp.H = (Math.Abs(temp.xPos - destX) + Math.Abs(temp.yPos - destY)) * 10;
                        temp.F = temp.G + temp.H;

                        //checks if node is in open
                        bool containsOpen = false;
                        bool containsClosed = false;
                        if (map[temp.xPos, temp.yPos] != 1)
                        {
                            for (int k = 0; k < open.Count; k++)
                            {
                                if (open.ElementAt(k).isEqual(temp))
                                {
                                    containsOpen = true;
                                    if (active.G + temp.G < open.ElementAt(k).G)
                                    {
                                        open.ElementAt(k).parent = active;
                                        temp.G = active.G + open.ElementAt(k).G;
                                        temp.F = temp.G + temp.H;
                                    }
                                }
                            }
                            for (int k = 0; k < closed.Count; k++)
                            {
                                if (closed.ElementAt(k).isEqual(temp))
                                {
                                    containsClosed = true;
                                }
                            }

                            if (!containsOpen && !containsClosed)
                            {
                                open.Add(temp);
                                map[temp.xPos, temp.yPos] = 2;
                            }
                        }

                    }
                }

                //finds lowest F value
                int min = 1;
                for (int k = 1; k < open.Count; k++)
                {

                    if (open.ElementAt(k).F <= open.ElementAt(min).F)
                        min = k;
                }

                closed.Add(active);

                map[open.ElementAt(min).xPos, open.ElementAt(min).yPos] = 5;

                active = open.ElementAt(min);
                open.RemoveAt(min);

            }
            while (active.xPos != startX || active.yPos != startY)
            {
                map[active.xPos,active.yPos] = 6;
                active = active.parent;
            }
        }