2014-08-28 7 views
1

내 코드 (아래 그림)가 할 일을하고 있습니다 (중간에서 임의의 정지 점을 사용하여 점 a에서 점 b까지 통로를 생성합니다). 음, 모든 시간은 아닙니다. 나는 문법 문제를 연구하려고 노력했으며, 몇 시간 만에 간단한 수학 문제를 찾는데 시간을 보냈지 만 그것을 찾을 수는 없다.미로 genorator가 스팟을 건너 뜁니까?

Probolem은 대부분의 경우 유효한 경로를 생성하지만 실제로는 첫 번째 지점에서 두 번째 지점까지 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()가 0보다 클 때의 경우를 고려하지 않습니다. 기본적으로 Y에 변경 사항이 없지만 X에 변경 사항이 있으면 해당 섹션을 만드는 것을 건너 뜁니다.

예 :

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

다음 버그에

p2 p3 

p1 

결과 변수 중 하나는, 단지 오프 하나 포인트리스트의 다음의 크기 인 경우는 생성 0 것 , 그래서 그것들을 연결하지 않을 것입니다. 예를 들어 enterX은 10과 같고 point1X은 9와 연결되지 않습니다.

1 0 
1 0 
0 0 
0 4 

에서

p3 

p2 
    p1 

결과는 내가

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

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

에 양식의 루프의 모든 변화 좋을 것,이 문제를 해결하려면 목록의 마지막 지점을 포함하여

+0

이것은 잘 작동하여 거의 모든 시간을 연결하지만, 여전히 ocationaly는 수평으로 점프합니다 ... –

+0

두 번째 버그를 보내 주셔서 감사합니다! –

0

생성하는 경로가 두 점 집합 사이의 전체 거리를 벗어나지 않습니다. 1 평 - - 당신은 픽셀로 경로를 생성 정지 이후 1 (양의 방향 탐색의 경우와 pX의 + 1과 음의 방향으로 통과하는 동안 평 + 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); 
    } 

} 

등의 점 사이의 전체 거리를 항상 통과하도록하십시오.