【问题标题】:Maze genorator skips spots?迷宫生成器会跳过景点?
【发布时间】:2014-08-28 01:52:18
【问题描述】:

我的代码(如下所示)正在做它应该做的事情(生成从 a 点到 b 点的通道,其间有随机停靠点)并且它有效。好吧,不是所有的时间。我试图研究语法问题,并花了几个小时寻找一些简单的数学问题,但我找不到。

问题是它在大多数情况下生成一条有效路径,但有时,它从第一个点到第二个点有 3 个点。有谁知道是什么问题?

    public static int[][] genLayer(int enterX, int enterY) {

    // Initiate Variables and arrays
    ArrayList<Integer> xPos = new ArrayList<Integer>(); // Array of x
                                                        // positions
    ArrayList<Integer> yPos = new ArrayList<Integer>(); // Array of y
                                                        // positions

    int[][] layer = new int[20][20]; // The 2D array of the layer to be
                                        // returned to the caller

    // Generates the points for the passageway to go thru.
    int point1X = rand.nextInt(20); // The first point's x
    int point1Y = rand.nextInt(20); // The first point's y
    int point2X = rand.nextInt(20); // The second point's x
    int point2Y = rand.nextInt(20); // The second point's y
    int point3X = rand.nextInt(20); // The third point's x
    int point3Y = rand.nextInt(20); // The third point's y

    layer[enterX][enterY] = 4; // Set the cords of enter X and Y to 4, the
                                // number representing the up stairs

    // Enter To Point 1:

    // Generate the first set of x points for the layer's passages
    if (enterX > point1X) {

        for (int x = enterX - 1; x > point1X; x--) {
            xPos.add(x);
        }

    } else if (enterX < point1X) {

        for (int x = enterX + 1; x < point1X; x++) {
            xPos.add(x);
        }

    }

    // Generate the first set of y points for the layer's passages
    if (enterY > point1Y) {

        for (int y = enterY - 1; y > point1Y; y--) {
            yPos.add(y);
        }

    } else if (enterY < point1Y) {

        for (int y = enterY + 1; y < point1Y; y++) {
            yPos.add(y);
        }

    }

    // Make Passages
    if (yPos.size() > 0) {
        if (rand.nextBoolean() & xPos.size() > 0) { // Chose randomly
                                                    // whether to
                                                    // make the passage up
                                                    // then
                                                    // sideways or sideways
                                                    // then
                                                    // up.
                                                    //
                                                    // Then, decide if there
                                                    // is
                                                    // any horizontal or
                                                    // vertical passages to
                                                    // generate
            // x then y

            for (int i = 0; i < xPos.size(); i++) {

                layer[xPos.get(i)][enterY] = 1; // make the horizontal
                                                // passage

            }

            for (int i = 0; i < yPos.size(); i++) {

                layer[xPos.get(xPos.size() - 1)][yPos.get(i)] = 1; // make
                                                                    // the
                                                                    // vertical
                                                                    // passage

            }

        } else {

            // y then x

            for (int i = 0; i < yPos.size(); i++) {

                layer[enterX][yPos.get(i)] = 1; // make the vertical passage

            }

            for (int i = 0; i < xPos.size(); i++) {

                layer[xPos.get(i)][yPos.get(yPos.size() - 1)] = 1; // make
                                                                    // the
                                                                    // horizontal
                                                                    // passage

            }

        }
    }
    // Set point 1 to the last xPos and yPos to make up for unknown
    // calculation errors
    if (xPos.size() > 0)
        point1X = xPos.get(xPos.size() - 1);
    if (yPos.size() > 0)
        point1Y = yPos.get(yPos.size() - 1);

    // Flush the values of xPos and yPos
    xPos.clear();
    yPos.clear();

    // Point 1 To Point 2:

    // Generate the second set of x points for the layer's passages
    if (point1X > point2X) {

        for (int x = point1X - 1; x > point2X; x--) {
            xPos.add(x);
        }

    } else if (point1X < point2X) {

        for (int x = point1X + 1; x < point2X; x++) {
            xPos.add(x);
        }

    }

    // Generate the second set of y points for the layer's passages
    if (point1Y > point2Y) {

        for (int y = point1Y - 1; y > point2Y; y--) {
            yPos.add(y);
        }

    } else if (point1Y < point2Y) {

        for (int y = point1Y + 1; y < point2Y; y++) {
            yPos.add(y);
        }

    }

    // Make Passages
    if (yPos.size() > 0) {
        if (rand.nextBoolean() & xPos.size() > 0) { // Chose randomly
                                                    // whether to
                                                    // make the passage up
                                                    // then
                                                    // sideways or sideways
                                                    // then
                                                    // up.
                                                    //
                                                    // Then, decide if there
                                                    // is
                                                    // any horizontal or
                                                    // vertical passages to
                                                    // generate
            // x then y

            for (int i = 0; i < xPos.size(); i++) {

                layer[xPos.get(i)][point1Y] = 1; // make the horizontal
                                                    // passage

            }

            for (int i = 0; i < yPos.size(); i++) {

                layer[xPos.get(xPos.size() - 1)][yPos.get(i)] = 1; // make
                                                                    // the
                                                                    // vertical
                                                                    // passage

            }

        } else {

            // y then x

            for (int i = 0; i < yPos.size(); i++) {

                layer[point1X][yPos.get(i)] = 1; // make the vertical
                                                    // passage

            }

            for (int i = 0; i < xPos.size(); i++) {

                layer[xPos.get(i)][yPos.get(yPos.size() - 1)] = 1; // make
                                                                    // the
                                                                    // horizontal
                                                                    // passage

            }

        }
    }
    // Set point 2 to the last xPos and yPos to make up for unknown
    // calculation errors
    if (xPos.size() > 0)
        point2X = xPos.get(xPos.size() - 1);
    if (yPos.size() > 0)
        point2Y = yPos.get(yPos.size() - 1);

    // Flush the values of xPos and yPos
    xPos.clear();
    yPos.clear();

    // Point 2 To Point 3:

    // Generate the third set of x points for the layer's passages
    if (point2X > point3X) {

        for (int x = point2X - 1; x > point3X; x--) {
            xPos.add(x);
        }

    } else if (point2X < point3X) {

        for (int x = point2X + 1; x < point3X; x++) {
            xPos.add(x);
        }

    }

    // Generate the third set of y points for the layer's passages
    if (point2Y > point3Y) {

        for (int y = point2Y - 1; y > point3Y; y--) {
            yPos.add(y);
        }

    } else if (point2Y < point3Y) {

        for (int y = point2Y + 1; y < point3Y; y++) {
            yPos.add(y);
        }

    }

    // Make Passages
    if (yPos.size() > 0) {
        if (rand.nextBoolean() & xPos.size() > 0) { // Chose randomly
                                                    // whether to
                                                    // make the passage up
                                                    // then
                                                    // sideways or sideways
                                                    // then
                                                    // up.
                                                    //
                                                    // Then, decide if there
                                                    // is
                                                    // any horizontal or
                                                    // vertical passages to
                                                    // generate
            // x then y

            for (int i = 0; i < xPos.size(); i++) {

                layer[xPos.get(i)][point2Y] = 1; // make the horizontal
                                                    // passage

            }

            for (int i = 0; i < yPos.size(); i++) {

                layer[xPos.get(xPos.size() - 1)][yPos.get(i)] = 1; // make
                                                                    // the
                                                                    // vertical
                                                                    // passage

            }

        } else {

            // y then x

            for (int i = 0; i < yPos.size(); i++) {

                layer[point2X][yPos.get(i)] = 1; // make the vertical
                                                    // passage

            }

            for (int i = 0; i < xPos.size(); i++) {

                layer[xPos.get(i)][yPos.get(yPos.size() - 1)] = 1; // make
                                                                    // the
                                                                    // horizontal
                                                                    // passage

            }

        }
    }
    // Set point 3 to the last xPos and yPos to make up for unknown
    // calculation errors
    if (xPos.size() > 0)
        point3X = xPos.get(xPos.size() - 1);
    if (yPos.size() > 0)
        point3Y = yPos.get(yPos.size() - 1);

    // Flush the values of xPos and yPos
    xPos.clear();
    yPos.clear();

    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 20; j++) {
            System.out.print(" " + layer[i][j]);
        }
        System.out.println();
    }
    return layer;
}

注意:我知道这段代码可以使用方法变得非常小,但这只是对其功能的粗略测试。我稍后会处理这个问题。

提前致谢!

【问题讨论】:

    标签: java arraylist generator multidimensional-array maze


    【解决方案1】:

    我注意到的一个错误是在“制作段落”部分。每一个都包裹在一个

    if(yPos.size() &gt; 0)

    有条件的,但不考虑xPos.size() 大于零的情况。基本上,如果Y 没有变化,但X 有变化,那么它将跳过创建该部分的段落。

    例子:

    p2    p3
    
    p1
    

    结果

    1 1 1 1
    0 0 0 0
    4 0 0 0
    

    下一个错误: 如果其中一个变量只差一,那么它生成的点列表的大小将为 0,因此它不会连接两者。例如enterX 等于 10 和point1X 等于 9 将不会连接它们。

    p3
    
    p2
       p1
    

    结果

    1  0
    1  0
    0  0
    0  4
    

    要解决这个问题,我建议更改表单的所有循环

    for (int x = enterX - 1; x > point1X; x--)
    

    for (int x = enterX - 1; x >= point1X; x--)
    

    换句话说,包括列表中的最后一点。

    【讨论】:

    • 这行得通,使它大部分时间都连接起来,但它仍然,ocationaly 水平跳跃......
    【解决方案2】:

    您生成的路径不会走完两点之间的整个距离。由于您在 pX - 1 和 pY - 1 处停止生成路径(在正方向遍历的情况下,以及在负方向遍历期间的 pX + 1 和 pY + 1 的情况下),您会得到这种设计:

    0  0 0 P2
    1  1 1 0
    1  0 0 0
    P1 0 0 0
    

    请注意,路径实际上并未到达 P2。尝试将这些部分和类似的部分更改为

    // Generate the first set of x points for the layer's passages
    if (enterX > point1X) {
    
        for (int x = enterX - 1; x >= point1X; x--) { // > becomes >=
            xPos.add(x);
        }
    
    } else if (enterX < point1X) {
    
        for (int x = enterX + 1; x <= point1X; x++) { // < becomes <=
            xPos.add(x);
        }
    
    }
    

    以便始终遍历点之间的整个距离。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      相关资源
      最近更新 更多