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ループです。8つのブール値のいずれかが真である限り、それは決して完了しません。

何が起こっているかを確認できるように、ログを追加します。

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 に答える