2014-08-28 105 views
1

我的代碼(如下圖所示)正在做它應該做的事情(生成從點a到b的通道,並在兩者之間隨機停止)和它的工作原理。好吧,不是所有的時間。我試圖研究語法問題,並花了數小時尋找一些簡單的數學問題,但我找不到它。迷宮genorator跳過點嗎?

問題在於它大部分時間都會產生一個有效的路徑,但是ocationaly,它從第一點到第二點是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; 
} 

注意:我知道這個代碼可以用方法更小,但這只是對它的功能的粗略測試。以後我會繼續努力。

在此先感謝!

回答

0

我注意到的一個錯誤是在「製作段落」部分。每一個被包裝在一個

if(yPos.size() > 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--) 

換句話說包括列表中的最後一點。

+0

這工作,使它連接大部分時間,但它仍然,ocationaly跳horizo​​ntaly ... – 2014-08-28 02:33:05

+0

感謝您的第二個錯誤,它的工作原理! – 2014-08-28 20:40:09

0

您生成的路徑並不是兩組點之間的整個距離。既然你停在的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); 
    } 

} 

使得點之間的整個距離總是被遍歷。