简体   繁体   English

如果之后绘制停止工作

[英]Draw stops working after if

I'm trying to create a maze with Union Find, but am unable to remove walls.我正在尝试使用 Union Find 创建一个迷宫,但无法移除墙壁。 This is what I have got so far.这是我到目前为止所得到的。

 private void createMaze (int cells, Graphics g) {
     s = new int[cells*cells]; //No unions yet setting all to -1
     for(int i = 0; i < cells*cells; ++i){
         s[i] = -1;
     }
     g.setColor(Color.yellow);
     random = new Random();

     while(breaker){
         g.setColor(Color.yellow);
         int innerWall = random.nextInt(4)+0;
         int randomCellX = random.nextInt(cells-1)+0;
         int randomCellY = random.nextInt(cells)+0;

         if(randomCellX==cells&&innerWall==2||
            randomCellX==0&&innerWall==0||
            randomCellY==cells-1&&innerWall==3||
            randomCellY==0&&innerWall==1){
             continue; 
         }           
         else{

             int location = randomCellX+(randomCellY*cells);
             int neighbour = 0;
             if(innerWall==0){
                 neighbour =location-1;
             }
             else if(innerWall==1){
                 neighbour =location-cells;
             }
             else if(innerWall==2){
                 neighbour =location+1;
             }
             else if(innerWall==3){
                 neighbour =location+cells;
             }
             int locationRoot =find(location);
             int neighbourRoot =find(neighbour);

             if(locationRoot==neighbourRoot){
                 breaker = checkIfDone(s);
                }
             union(location,neighbour);
             drawWall(randomCellX,randomCellY,innerWall,g);


         }


     }
}

If I remove the如果我删除

if(randomCellX==cells&&innerWall==2||
            randomCellX==0&&innerWall==0||
            randomCellY==cells-1&&innerWall==3||
            randomCellY==0&&innerWall==1){
             continue; 
         }

It removes the lines fine,but when it is added the walls are not removed.它可以很好地去除线条,但是添加时不会去除墙壁。 The method is called but doesn't do anything.该方法被调用但不执行任何操作。

It seems some logical mistake have been done obviously.似乎显然已经犯了一些逻辑错误。 But can not spot specifically as you haven't given the explanation of the logic you have done.但不能具体发现,因为你没有给出你所做的逻辑的解释。 Only can help showing the path where the problem might be happening.只能帮助显示问题可能发生的路径。

Since you can not reach the else block anyway, it is obvious that one or more of these conditions in if are always true .由于无论如何都无法到达else块,因此很明显if中的一个或多个条件始终为true

(randomCellX == cells && innerWall == 2)
(randomCellX == 0 && innerWall == 0)
(randomCellY == cells - 1 && innerWall == 3)
(randomCellY == 0 && innerWall == 1)

That's why you are getting true for the if condition and continuing the loop without doing anything.这就是为什么您对 if 条件变得真实并继续循环而不做任何事情的原因。 Ensure the conditions are okay.确保条件良好。

If these conditions are right, the next suspect might be these lines:如果这些条件是正确的,下一个嫌疑人可能是这些行:

 int innerWall = random.nextInt(4)+0;
 int randomCellX = random.nextInt(cells-1)+0;
 int randomCellY = random.nextInt(cells)+0;

Check whether these random values range are exactly what you want or not.检查这些随机值范围是否正是您想要的。 For example: you are taking random values for randomCellX from 0 to cells-2, but for randomCellY the range is 0 to cells-1.例如:您为randomCellX从 0 到 cell-2 的随机值,但对于randomCellY ,范围是 0 到 cell-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM