我正在进行迷宫解决计划(是的,家庭作业),我遇到了一个我似乎无法弄清楚的奇怪问题。基本上,主线程每次运行时都会在同一点退出而不会抛出任何异常或给出任何类型的指示。
一些背景:我们的教授给了我们一个部分完成的java文件,该文件已经有了JFrame,动画,定时代码等。我们唯一需要补充的是解决迷宫的算法。我们必须跟踪(x,y)坐标和人们面对的方向,并递归地导航迷宫。
我使用调试器试图找出它暂停的原因,并且main从正在运行的线程列表中消失而不会抛出异常或错误或任何东西。 GUI保持冻结在同一位置,计时器暂停,直到我关闭JPanel窗口。
以下是代码部分:
private void solve(int x, int y, String facing) {
Graphics2D g2 = (Graphics2D)mazePanel.getGraphics(); //don't mess with the next
while (!timerFired) { // wait for the timer.
try{Thread.sleep(10);} catch(Exception e){}
}
timerFired = false;
currentTime = System.currentTimeMillis();
if((currentTime - startTime) > 50000){
closingMethod();
}
//Do not mess with the above part of this method
if (maze[y][x] != 'F') { //this is if it doesn't find the finish on a turn.........
g2.drawImage(mazeImage, null, 0, 0);
g2.drawImage(printGuy(facing), x*SPRITE_WIDTH, y*SPRITE_HEIGHT, null, null);
mazePanel.setSize(width*SPRITE_WIDTH+10, height*SPRITE_HEIGHT+30);
maze[y][x] = 'X'; // mark this spot as visited. This is how you can keep track of where you've been.
/**
* All of the above code was given, everything below is mine.
* All four direction cases are identical in structure.
*
*/
if(y>0)
northDir = maze[y-1][x];
if(x < maze[0].length)
eastDir = maze[y][x+1];
if(x > 0)
westDir = maze[y][x-1];
if(y < maze.length)
southDir = maze[y+1][x];
switch (facing) {
case "east":
if(!isBlocked(eastDir)){
if(!beenVisited(eastDir))
solve(x+1,y,"east");
else{
if(!isBlocked(northDir)){
if(!beenVisited(northDir))
solve(x,y-1, "north");
}
else if (!isBlocked(southDir)){
if(!beenVisited(southDir))
solve(x,y+1, "south");
}
else
solve(x+1,y,"east");
}
}
else{
if(!isBlocked(northDir) && !isBlocked(southDir)){
if(!beenVisited(northDir) && beenVisited(southDir))
solve(x,y-1,"north");
else //if(beenVisited(northDir) && !beenVisited(southDir))
solve(x,y+1,"south");
}
else if(!isBlocked(northDir))
solve(x,y-1,"north");
else if(!isBlocked(southDir))
solve(x,y+1,"south");
else
solve(x-1,y,"west");
}
break;
//Omitted the other four directions
}
else {
System.out.println("Found the finish!");
//don't mess with the following 4 lines, but you can add stuff below that if you need.
currentTime = System.currentTimeMillis();
long endTime = currentTime - startTime;
long finalTime = endTime / 1000;
System.out.println("Final Time = " + finalTime);
}
如果您需要更多信息或代码,请与我们联系。我感谢任何帮助,因为这非常奇怪。