寻路 - StackOverflowError
Pathfinding - StackOverflowError
我的寻路有问题:
起点在 0,0,终点在右下角 "C"。它在
处抛出一个 exeption java.lang.WhosebugError
if(getPath(x, y+1)==true){
return true;
}
和
if(getPath(x, y-1)==true){
return true;
}
方法在这里
public static boolean getPath(int x,int y, int num){
if(x==startX-1|| y==startY-1 || x==endX+1 || y==endY+1){
return false;
}
if (" C".equals(array[x][y])){
return true;
}
if ("# ".equals(array[x][y])||"x".equals(array[x][y])){
return false;
}
if ("[]".equals(array[x][y])){
array[x][y]="+";
}
if(getPath(x, y+1,num)==true){ //vpravo
return true;
}
if(getPath(x+1, y,num)==true){ //dolu
return true;
}
if(getPath(x-1, y,num)==true){ //nahoru
return true;
}
if(getPath(x, y-1,num)==true){ //vlevo
return true;
}
if("+".equals(array[x][y])){
array [x][y]="x";
}
return false;
}
}
我使用的价值观
public static Random r = new Random();
public static int i1 = r.nextInt(4) + 4;
public static int startX=0;
public static int startY=0;
public static int endX=i1-1;
public static int endY=i1-1;
public static int rows = i1;
public static int columns = i1;
public static String[][] array = new String[rows][columns];
我有 i3-i14 用于随机障碍物,它们是这样定义的
public static int i3 = r.nextInt(4) ;
...
public static int i14 = r.nextInt(4) ;
在我更改比较后它仍然抛出异常
编辑:我想我在 IF 比较“+”并将其替换为 "x"
时找到了 problem.It
if("+".equals(array[x][y])){
array [x][y]="x";
你能帮我解决这个问题吗?谢谢
首先,您需要使用 .equals(.)
进行字符串比较,而不是 ==
。
试试这个老栗子:
How do I compare strings in Java?
在那之后,您避免重新跟踪步骤的逻辑可能会开始起作用,并且您可以避免失控的递归。
给定的代码很难说,但我想到了三件事:
- 您确定添加了 C 和所有其他数据吗?
- “C”和“#”周围的额外空格应该存在吗?
- 为什么要将
[]
转换为 +
,然后再转换为 x
?我猜这是为了标记你已经尝试过这个单元格,如果是这样,你不能在 4 getPath
调用之前将它设置为 x
吗?在 4 次调用之后设置它意味着您可能会继续原地打转,因此 Whosebug。
我的寻路有问题:
起点在 0,0,终点在右下角 "C"。它在
处抛出一个exeption java.lang.WhosebugError
if(getPath(x, y+1)==true){
return true;
}
和
if(getPath(x, y-1)==true){
return true;
}
方法在这里
public static boolean getPath(int x,int y, int num){
if(x==startX-1|| y==startY-1 || x==endX+1 || y==endY+1){
return false;
}
if (" C".equals(array[x][y])){
return true;
}
if ("# ".equals(array[x][y])||"x".equals(array[x][y])){
return false;
}
if ("[]".equals(array[x][y])){
array[x][y]="+";
}
if(getPath(x, y+1,num)==true){ //vpravo
return true;
}
if(getPath(x+1, y,num)==true){ //dolu
return true;
}
if(getPath(x-1, y,num)==true){ //nahoru
return true;
}
if(getPath(x, y-1,num)==true){ //vlevo
return true;
}
if("+".equals(array[x][y])){
array [x][y]="x";
}
return false;
}
}
我使用的价值观
public static Random r = new Random();
public static int i1 = r.nextInt(4) + 4;
public static int startX=0;
public static int startY=0;
public static int endX=i1-1;
public static int endY=i1-1;
public static int rows = i1;
public static int columns = i1;
public static String[][] array = new String[rows][columns];
我有 i3-i14 用于随机障碍物,它们是这样定义的
public static int i3 = r.nextInt(4) ;
...
public static int i14 = r.nextInt(4) ;
在我更改比较后它仍然抛出异常
编辑:我想我在 IF 比较“+”并将其替换为 "x"
时找到了 problem.Itif("+".equals(array[x][y])){
array [x][y]="x";
你能帮我解决这个问题吗?谢谢
首先,您需要使用 .equals(.)
进行字符串比较,而不是 ==
。
试试这个老栗子:
How do I compare strings in Java?
在那之后,您避免重新跟踪步骤的逻辑可能会开始起作用,并且您可以避免失控的递归。
给定的代码很难说,但我想到了三件事:
- 您确定添加了 C 和所有其他数据吗?
- “C”和“#”周围的额外空格应该存在吗?
- 为什么要将
[]
转换为+
,然后再转换为x
?我猜这是为了标记你已经尝试过这个单元格,如果是这样,你不能在 4getPath
调用之前将它设置为x
吗?在 4 次调用之后设置它意味着您可能会继续原地打转,因此 Whosebug。