Ejemplo n.º 1
0
        private void wavefront(Point start, Point goal, int val)
        {
            if (start.x < 0 || start.x > 7 || start.y < 0 || start.y > 3)
            {
                return;
            }
            int cell = getCell(start);

            if (cell < 0 || (cell > 0 && cell < val))
            {
                return;
            }

            setCell(start, val);
            //Console.Out.WriteLine("x: " + start.x + " y: " + start.y + " val: " + getCell(start));
            if (start.Equals1(goal))
            {
                return;
            }
            wavefront(new Point(start.x - 1, start.y), goal, val + 1);
            wavefront(new Point(start.x + 1, start.y), goal, val + 1);
            wavefront(new Point(start.x, start.y - 1), goal, val + 1);
            wavefront(new Point(start.x, start.y + 1), goal, val + 1);
            wavefront(new Point(start.x - 1, start.y-1), goal, val + 1);
            wavefront(new Point(start.x + 1, start.y-1), goal, val + 1);
            wavefront(new Point(start.x-1, start.y - 1), goal, val + 1);
            wavefront(new Point(start.x+1, start.y + 1), goal, val + 1);
        }
Ejemplo n.º 2
0
        private Stack<Point> getPath(Point start, Point goal)
        {
            Stack<Point> path = new Stack<Point>();
            wavefront(start, goal, 1);
            Point current = new Point(goal.x, goal.y);
            while (!current.Equals1(start))
            {
                path.Push(current);
                int val = getCell(current);

                Point left = new Point(current.x - 1, current.y);
                Point right = new Point(current.x + 1, current.y);
                Point up = new Point(current.x, current.y + 1);
                Point down = new Point(current.x, current.y - 1);
                Point leftup = new Point(current.x-1, current.y + 1);
                Point rightup = new Point(current.x+1, current.y + 1);
                Point leftdown = new Point(current.x-1, current.y - 1);
                Point rightdown = new Point(current.x+1, current.y - 1);

                int leftVal = getCell(left);
                int rightVal = getCell(right);
                int upVal = getCell(up);
                int downVal = getCell(down);
                int leftUpVal = getCell(leftup);
                int rightUpVal = getCell(rightup);
                int leftDownVal = getCell(leftdown);
                int rightDownVal = getCell(rightdown);
                int[] adjVals = new int[] { leftVal, leftDownVal, downVal, leftUpVal, rightUpVal, rightVal, rightDownVal, upVal };
                Point[] adjPoints = new Point[] { left, leftdown, down, leftup, rightup, right, rightdown, up };

                for (int i = 0; i < 8; i++)
                {
                    if (adjVals[i] >= 0 && adjVals[i] < val)
                    {
                        current = adjPoints[i];

                        break;
                    }
                }
            }
            return path;
        }