1

任务是创建一个黑白棋游戏。除了涉及更改多个芯片的动作外,我可以正常工作

       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;
}
4

1 回答 1

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);    
}

一旦您确认这是问题所在,请开始研究您的搜索方法以找出问题发生的原因

于 2011-11-27T01:47:46.627 回答