我的代码(如下所示)正在做他们应该做的事情(从a点到b点生成一条通道,中间有随机停留)并且它有效。好吧,不是所有的时间。我试图研究语法问题,花了几个小时寻找一些简单的数学问题,但我找不到它。
Probolem是它在大多数时间生成一个有效的路径,但是从第一个点到第二个点,它是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 :(得分:0)
我注意到的一个错误是在#34; Make Passages&#34;部分。每一个都包裹在
中 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,等于9的point1X
将不会连接它们。
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--)
换句话说,包括列表中的最后一点。
答案 1 :(得分: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);
}
}
以便始终遍历点之间的整个距离。