0
 **

this causes an extra move to made in this gomoku game beyond the winning move and the checkForWin metho after the extra move is the method that detects the win but it should be the checkForWin method immediately after the corresponding makeMove method.

**

import java.io.File;  
boolean hasWinner = false;
File gameFile = new File("src/centralGameFile.txt");

do{
    //player 1
    makeMove(gameFile);
    // check for win
    if (checkForWin(gameFile)){ 
        hasWinner = true;
        break;
    }
    // player 2
    makeMove(gameFile);
    // check for win
    if (checkForWin(gameFile)){
        hasWinner = true;
        break;
    }
}while(hasWinner == false);

System.out.println("somebody has won the game");

 /*this method is located in another class in the same package and is
  called from an instance of the class using the access operator */

protected boolean checkForWin(File f){
//return true if the file  has a winner in it using scanner to look for it
//this method works correctly when tested with just a file in a test class
}

// try/catch blocks omitted for brevity

/* makeMove(File f) method copies the text from f and over writes 
it adding another character; in context this is a gomoku/tic-tac-toe
style game but on a bigger board.
*/

2 Answers2

2
checkForWin works correctly when tested with just a file in a test class

a part of your code:

do{
    //player 1
    makeMove(gameFile);
    // check for win
    if (checkForWin(gameFile)){ 
        hasWinner = true;
        break;
    }
    // player 2
    makeMove(gameFile);
    // check for win
    if (checkForWin(gameFile)){
        hasWinner = true;
        break;
    }
}while(hasWinner == false);

System.out.println("somebody has won the game");

If checkForWin returns true, your method must be hanging at makeMove(gameFile). This is possibly stuck in some infinite loop.

bas
  • 1,678
  • 12
  • 23
  • makeMove(gameFile) is empty, it only exists for an ai that will use it to write its move to the game file as ai can't click the buttons in the game like humans can, does this infinite loop exist as something I have coded or is there something more abstract that I'm not aware of? (beginner here) –  Aug 11 '13 at 09:04
  • This use of a file is not by choice rather its a specification –  Aug 11 '13 at 09:19
  • Could you please put a couple of breakpoints in your code and see where it goes wrong? Literally, if your code fails, either `checkForWin(gameFile);` never returns true, or `makeMove(gameFile);` must be hanging somewhere. – bas Aug 11 '13 at 10:30
1

I would suggest that the cause of your problems is that checkForWin is in fact not working. This could be because:

  • your testing of that method is inadequate, or
  • the code that is updating the file is not doing the right thing (e.g. it isn't closing / flushing the file), or
  • you are calling checkForWin for the wrong file.

Either way, there is not sufficient information in your Question to say what is actually going on here. At a minimum we need to see the code of the checkForWin method, and probably we need to see the code that updates the file as well.


While I have your attention ... there are a couple of minor errors in your code ... but not sufficient to cause the problem you asking about:

  1. Your hasWinner flag is redundant, as is the code that sets it and tests it. The way you have written the loop, the only way you can get to the statements after the loop is if you executed one of the two break statements.

  2. This is bad style ... and potentially dangerous (in other contexts):

     ... while (hasWinner == false);
    

    That should be written as

     ... while (!hasWinner);
    

    Firstly, it is more readable. Every Java programmer should know what the ! operator means, and using ! is the idiomatic way to express this.

    Secondly, your approach is error prone. Consider this:

     ... while (hasWinner = false);
    

    Here you've accidentally written = instead of ==. Unfortunately, the = form is legal Java ... and it means something different to what you intended. If you use the idiomatic version, you can't make this mistake.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Of your three scenarios above I have close methods called for the Scanner used in checkForWin(gameFile); I also have verified that the right file is passed to the method. For testing the method returns true under similar conditions not in this loop. Is it possible that the the Scanner reads the old file before the PrintWriter used for adding moves finishes writing to the file? Or something similar in the I/O that a beginner like me is unaware of? –  Aug 11 '13 at 09:17