如果距离大于2,则路径查找搜索崩溃

时间:2011-11-26 21:56:00

标签: java multidimensional-array reversi

任务是创造一个逆转游戏。除了涉及更换多个芯片的移动外,我还有它工作

       x o
       o o
     o o o
       @        <-- the @ is x's move that crashes the game.

,在这种情况下程序崩溃。崩溃发生在isPlayable()

的某个地方

我哪里错了?

//there are 7 more such methods for different directions 
// (down, left, right, diagonals).       
public void searchN(int x, int y, int increment){
 if(x-increment>=0){
  if(this.isPlayed(x-increment,y)){                         
    if(increment>1 && chips[x-increment][y]==turnColor){
          //turnColor is an int value 1 or 2 
          // (0 represents unplayed space in chips[][]) 
          // 1 corresponding to white, 2 corresponding to black.
      playable[0] = true;
      leads[0] = false;
    } else {
      if(chips[x-increment][y]!=turnColor){
        leads[0]=true;
      }
    }
  }else
    leads[0]=false;

}else
  leads[0]=false;
}

public boolean isPlayable(int x, int y){
  this.searchN(x,y,1);  //7 other directions are searched

  while(leads[0]||leads[1]||leads[2]||leads[3]||leads[4]
                ||leads[5]||leads[6]||leads[7]){
    int i = 2;
    if(leads[0])  // 7 other directions are searched given that their marker is true.
      this.searchN(x,y,i);      
  }
  if(playable[0]||playable[1]||playable[2]||playable[3]||playable[4]
                ||playable[5]||playable[6]||playable[7])
    return true;
  else
    return false;
}

1 个答案:

答案 0 :(得分:3)

根据你的评论,听起来你正在经历一次悬挂,而不是崩溃。当一个程序挂起时,你应该找到程序无限期“卡住”的地方。 isPlayable中的主要嫌疑人是您的while循环。只要八个布尔中的任何一个都是真的,它就永远无法完成。

我会添加一些日志记录,以便您可以看到发生了什么:

while(leads[0]||leads[1]||leads[2]||leads[3]||leads[4]
            ||leads[5]||leads[6]||leads[7]){
    System.out.println("leads[0]: " + leads[0]);
    System.out.println("leads[1]: " + leads[1]);
    // etc.

    int i = 2;
    if(leads[0])  // 7 other directions are searched given that their marker is true.
        this.searchN(x,y,i);    
}

一旦您确认这是问题所在,请开始查看您的搜索方法,找出