**
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 block
/* 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.
*/
最佳答案
checkForWin works correctly when tested with just a file in a test class
您的代码的一部分:
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");
如果checkForWin
返回true
,则您的方法必须卡在makeMove(gameFile)
处。这可能陷入了某种无限循环。
关于java - 为什么 checkforWin(File f) 为 true 时不返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18167984/