我的代碼(如下圖所示)正在做它應該做的事情(生成從點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;
}
注意:我知道這個代碼可以用方法更小,但這只是對它的功能的粗略測試。以後我會繼續努力。
在此先感謝!
這工作,使它連接大部分時間,但它仍然,ocationaly跳horizontaly ... – 2014-08-28 02:33:05
感謝您的第二個錯誤,它的工作原理! – 2014-08-28 20:40:09