Question

 **

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.
*/

No correct solution

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top