示例#1
0
        private PointField findClosed(LinkedList <AStarSolver <GameField> .PathNode> nodeList)
        {
            PointField returnList = new PointField(currentlyChecking.value);

            int minX = int.MaxValue;
            int maxX = int.MinValue;
            int minY = int.MaxValue;
            int maxY = int.MinValue;

            foreach (AStarSolver <GameField> .PathNode node in nodeList)
            {
                if (node.X < minX)
                {
                    minX = node.X;
                }

                if (node.X > maxX)
                {
                    maxX = node.X;
                }

                if (node.Y < minY)
                {
                    minY = node.Y;
                }

                if (node.Y > maxY)
                {
                    maxY = node.Y;
                }
            }

            int middleX = (int)Math.Ceiling(((maxX - minX) / 2f)) + minX;
            int middleY = (int)Math.Ceiling(((maxY - minY) / 2f)) + minY;
            //Console.WriteLine("Middle: x:[{0}]  y:[{1}]", middleX, middleY);

            Point        current;
            List <Point> toFill       = new List <Point>();
            List <Point> checkedItems = new List <Point>();

            checkedItems.Add(new Point(currentlyChecking.x, currentlyChecking.y));
            Point toAdd;

            toFill.Add(new Point(middleX, middleY));
            int x;
            int y;

            while (toFill.Count > 0)
            {
                current = toFill[0];
                x       = current.X;
                y       = current.Y;

                if (x < minX)
                {
                    return(null);//OOB
                }
                if (x > maxX)
                {
                    return(null);//OOB
                }
                if (y < minY)
                {
                    return(null);//OOB
                }
                if (y > maxY)
                {
                    return(null); //OOB
                }
                if (this[y - 1, x] && currentField[y - 1, x] == 0)
                {
                    toAdd = new Point(x, y - 1);
                    if (!toFill.Contains(toAdd) && !checkedItems.Contains(toAdd))
                    {
                        toFill.Add(toAdd);
                    }
                }
                if (this[y + 1, x] && currentField[y + 1, x] == 0)
                {
                    toAdd = new Point(x, y + 1);
                    if (!toFill.Contains(toAdd) && !checkedItems.Contains(toAdd))
                    {
                        toFill.Add(toAdd);
                    }
                }
                if (this[y, x - 1] && currentField[y, x - 1] == 0)
                {
                    toAdd = new Point(x - 1, y);
                    if (!toFill.Contains(toAdd) && !checkedItems.Contains(toAdd))
                    {
                        toFill.Add(toAdd);
                    }
                }
                if (this[y, x + 1] && currentField[y, x + 1] == 0)
                {
                    toAdd = new Point(x + 1, y);
                    if (!toFill.Contains(toAdd) && !checkedItems.Contains(toAdd))
                    {
                        toFill.Add(toAdd);
                    }
                }
                if (getValue(current) == 0)
                {
                    returnList.add(current);
                }
                checkedItems.Add(current);
                toFill.RemoveAt(0);
            }

            return(returnList);
        }
示例#2
0
        private PointField findClosed(LinkedList<AStarSolver<GameField>.PathNode> nodeList)
        {
            PointField returnList = new PointField(currentlyChecking.value);

            int minX = int.MaxValue;
            int maxX = int.MinValue;
            int minY = int.MaxValue;
            int maxY = int.MinValue;

            foreach (AStarSolver<GameField>.PathNode node in nodeList)
            {
                if (node.X < minX)
                    minX = node.X;

                if(node.X > maxX)
                    maxX = node.X;

                if (node.Y < minY)
                    minY = node.Y;

                if (node.Y > maxY)
                    maxY = node.Y;

            }

            int middleX = (int)Math.Ceiling(((maxX - minX) / 2f)) + minX;
            int middleY = (int)Math.Ceiling(((maxY - minY) / 2f)) + minY;
            //Console.WriteLine("Middle: x:[{0}]  y:[{1}]", middleX, middleY);

            Point current;
            List<Point> toFill = new List<Point>();
            List<Point> checkedItems = new List<Point>();
            checkedItems.Add(new Point(currentlyChecking.x, currentlyChecking.y));
            Point toAdd;
            toFill.Add(new Point(middleX,middleY));
            int x;
            int y;
            while(toFill.Count > 0)
            {
                current = toFill[0];
                x = current.X;
                y = current.Y;

                if (x < minX)
                    return null;//OOB
                if (x > maxX)
                    return null;//OOB
                if (y < minY)
                    return null;//OOB
                if (y > maxY)
                    return null; //OOB

                if (this[y - 1, x] && currentField[y - 1, x] == 0)
                {
                    toAdd = new Point(x, y - 1);
                    if (!toFill.Contains(toAdd) && !checkedItems.Contains(toAdd))
                        toFill.Add(toAdd);
                }
                if (this[y + 1, x] && currentField[y + 1, x] == 0 )
                {
                    toAdd = new Point(x, y + 1);
                    if (!toFill.Contains(toAdd) && !checkedItems.Contains(toAdd))
                        toFill.Add(toAdd);
                }
                if (this[y, x - 1] && currentField[y, x - 1] == 0)
                {
                    toAdd = new Point(x - 1, y);
                    if (!toFill.Contains(toAdd) && !checkedItems.Contains(toAdd))
                        toFill.Add(toAdd);
                }
                if (this[y, x + 1] && currentField[y, x + 1] == 0)
                {
                    toAdd = new Point(x + 1, y);
                    if (!toFill.Contains(toAdd) && !checkedItems.Contains(toAdd))
                        toFill.Add(toAdd);
                }
                if (getValue(current) == 0)
                    returnList.add(current);
                checkedItems.Add(current);
                toFill.RemoveAt(0);

            }

            return returnList;
        }