1

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;
}
Kevin
  • 53,822
  • 15
  • 101
  • 132
Ocasta Eshu
  • 841
  • 3
  • 11
  • 23
  • 1
    Can you post the full stack trace? – Mark Byers Nov 26 '11 at 21:59
  • Im not sure what you mean. The full isPlayed() method? or all of the SearchXX methods? – Ocasta Eshu Nov 26 '11 at 22:40
  • A crash in Java produces a list of method names and line numbers, the first of which is the specific line where the crash occurred. This list is called a stack trace. – goto10 Nov 27 '11 at 01:06
  • this did not happen. When run through the GUI the program would hang and i would have to force quit. When run through the command line, it would endlessly run the method isPlayable() and crash the command line without error codes. – Ocasta Eshu Nov 27 '11 at 01:24
  • Hanging is different than crashing. A crash is when the app exits on its own because of an error condition. Hanging is when the program becomes unresponsive and appears "stuck". It's important to be clear about which behavior you are getting because they can have different causes. – goto10 Nov 27 '11 at 01:40
  • ok, then the program is hanging. thank you for clarifying the distinction – Ocasta Eshu Nov 27 '11 at 01:44

1 Answers1

3

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.

goto10
  • 4,370
  • 1
  • 22
  • 33