javamultidimensional-arrayreversi

pathfinding search crash if distance is longer than 2


The assignment is to create a game of reversi. I have it working except for moves that involve changing more than one chip

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

, in which case the program crashes. The crash takes place somewhere in isPlayable().

Where am I going wrong?

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

Solution

  • Per your comment, it sounds like you're experiencing a hang, not a crash. When a program hangs, you should look for places where the program can get indefinitely "stuck". The prime suspect in isPlayable is your while loop. As long as any of the eight booleans are true it will never complete.

    I would add some logging so you can see what's happening:

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

    Once you've verified that this is the problem, start looking into your search methods to figure out why its happening.