Java 迷宫创造者跳过斑点?
我的代码(如下所示)正在做它应该做的事情(生成一个从a点到b点的通道,中间有随机的停止点),它可以工作。嗯,不是所有的时候。我试着研究语法问题,花了几个小时寻找一些简单的数学问题,但我找不到 问题是它大部分时间都会生成有效路径,但从位置上看,从第一个点到第二个点有3个点。有人知道问题是什么吗Java 迷宫创造者跳过斑点?,java,arraylist,generator,multidimensional-array,maze,Java,Arraylist,Generator,Multidimensional Array,Maze,我的代码(如下所示)正在做它应该做的事情(生成一个从a点到b点的通道,中间有随机的停止点),它可以工作。嗯,不是所有的时候。我试着研究语法问题,花了几个小时寻找一些简单的数学问题,但我找不到 问题是它大部分时间都会生成有效路径,但从位置上看,从第一个点到第二个点有3个点。有人知道问题是什么吗 public static int[][] genLayer(int enterX, int enterY) { // Initiate Variables and arrays
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;
}
publicstaticint[]genLayer(intenterx,intentery){
//启动变量和数组
ArrayList xPos=new ArrayList();//x的数组
//职位
ArrayList yPos=新的ArrayList();//y的数组
//职位
int[][]layer=new int[20][20];//要创建的层的2D数组
//返回给打电话的人
//生成通道要通过的点。
int point1X=rand.nextInt(20);//第一个点的x
int point1Y=rand.nextInt(20);//第一个点的y
int point2X=rand.nextInt(20);//第二个点的x
int point2Y=rand.nextInt(20);//第二个点的y
int point3X=rand.nextInt(20);//第三个点的x
int point3Y=rand.nextInt(20);//第三个点的y
层[enterX][enterY]=4;//将enter X和Y的跳线设置为4,则
//代表楼上的数字
//进入第1点:
//为层的通道生成第一组x点
如果(输入X>point1X){
对于(int x=enterX-1;x>point1X;x--){
xPos.add(x);
}
}否则如果(输入x点1Y){
对于(int y=entry-1;y>point1Y;y--){
添加(y);
}
}否则如果(输入<点1Y){
对于(int y=entry+1;y0){
如果(rand.nextBoolean()&xPos.size()>0){//随机选择
//是否
//把文章补上
//然后
//横着还是横着
//然后
//起来。
//
//然后,决定是否有
//是
//任何水平或垂直的
//通往
//产生
//x然后y
对于(int i=0;i0)
point1X=xPos.get(xPos.size()-1);
如果(yPos.size()>0)
point1Y=yPos.get(yPos.size()-1);
//刷新xpo和ypo的值
xPos.clear();
yPos.clear();
//第1点至第2点:
//为层的通道生成第二组x点
如果(点1X>点2X){
对于(int x=point1X-1;x>point2X;x--){
xPos.add(x);
}
}否则如果(点1X<点2X){
对于(int x=point1X+1;x点2Y){
对于(int y=point1Y-1;y>point2Y;y--){
添加(y);
}
}else if(点1y<点2y){
对于(int y=point1Y+1;y0){
如果(rand.nextBoolean()&xPos.size()>0){//随机选择
//是否
//把文章补上
//然后
//横着还是横着
//然后
//起来。
//
//然后,决定是否有
//是
p2 p3
p1
1 1 1 1
0 0 0 0
4 0 0 0
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 0 0 P2
1 1 1 0
1 0 0 0
P1 0 0 0
// 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);
}
}