示例#1
0
    public MazePath Branch()
    {
        if (points.Count < 3)
        {
            return(null);
        }

        var index = rand.Next(points.Count - 2) + 1;

        var nx = points[index].x;
        var ny = points[index].y;

        while (nx % 2 == 1 || ny % 2 == 1)
        {
            index = rand.Next(points.Count - 2) + 1;
            nx    = points[index].x;
            ny    = points[index].y;
        }

        var dirNew = Direction.UP;
        var gUp    = new GridPoint(nx, ny - 1);
        var gDown  = new GridPoint(nx, ny + 1);
        var gLeft  = new GridPoint(nx - 1, ny);
        var gRight = new GridPoint(nx + 1, ny);

        // Path is vertical
        if ((gUp.Equals(points[index - 1]) && gDown.Equals(points[index + 1])) ||
            (gUp.Equals(points[index + 1]) && gDown.Equals(points[index - 1])))
        {
            var roll = rand.Next(2);

            nx     = (roll == 0 ? nx + 1 : nx - 1);
            dirNew = (roll == 0 ? Direction.RIGHT : Direction.LEFT);

            return(new MazePath(nx, ny, dirNew));
        }

        // Path is horizontal
        if ((gLeft.Equals(points[index - 1]) && gRight.Equals(points[index + 1])) ||
            (gLeft.Equals(points[index + 1]) && gRight.Equals(points[index - 1])))
        {
            var roll = rand.Next(2);

            ny     = (roll == 0 ? ny + 1 : ny - 1);
            dirNew = (roll == 0 ? Direction.DOWN : Direction.UP);

            return(new MazePath(nx, ny, dirNew));
        }

        // Corner, upper right
        if ((gUp.Equals(points[index - 1]) && gRight.Equals(points[index + 1])) ||
            (gUp.Equals(points[index + 1]) && gRight.Equals(points[index - 1])))
        {
            var roll = rand.Next(2);

            nx     = (roll == 0 ? nx - 1 : nx);
            ny     = (roll == 0 ? ny : ny + 1);
            dirNew = (roll == 0 ? Direction.LEFT : Direction.DOWN);

            return(new MazePath(nx, ny, dirNew));
        }

        // Corner, upper left
        if ((gUp.Equals(points[index - 1]) && gLeft.Equals(points[index + 1])) ||
            (gUp.Equals(points[index + 1]) && gLeft.Equals(points[index - 1])))
        {
            var roll = rand.Next(2);

            nx     = (roll == 0 ? nx + 1 : nx);
            ny     = (roll == 0 ? ny : ny + 1);
            dirNew = (roll == 0 ? Direction.RIGHT : Direction.DOWN);

            return(new MazePath(nx, ny, dirNew));
        }

        // Corner, lower right
        if ((gDown.Equals(points[index - 1]) && gRight.Equals(points[index + 1])) ||
            (gDown.Equals(points[index + 1]) && gRight.Equals(points[index - 1])))
        {
            var roll = rand.Next(2);

            nx     = (roll == 0 ? nx - 1 : nx);
            ny     = (roll == 0 ? ny : ny - 1);
            dirNew = (roll == 0 ? Direction.LEFT : Direction.UP);

            return(new MazePath(nx, ny, dirNew));
        }

        // Corner, lower left
        if ((gDown.Equals(points[index - 1]) && gLeft.Equals(points[index + 1])) ||
            (gDown.Equals(points[index + 1]) && gLeft.Equals(points[index - 1])))
        {
            var roll = rand.Next(2);

            nx     = (roll == 0 ? nx + 1 : nx);
            ny     = (roll == 0 ? ny : ny - 1);
            dirNew = (roll == 0 ? Direction.RIGHT : Direction.UP);

            return(new MazePath(nx, ny, dirNew));
        }

        return(null);
    }