java - 为什么 checkforWin(File f) 为 true 时不返回 true?

标签 java methods gomoku

 **

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/

相关文章:

java - 确保方法在 x 秒内每个输入仅调用一次

java - 使用 BinaryTree 将字符编码为二进制

java - 新手: Maven execution exception

c++ - 自定义类函数引起的内存错误

java - Gomoku 中一个好的 Minimax 表示?

c++ - 五子棋最先进的技术

Java Wicket 1.5.6 : File name on download is url encoded, 将空格转换为加号

methods - 如何在 Rust 中使用方法作为函数指针

java - 尝试做一个公共(public)方法

java - 单击时在 JButton 上绘制一个椭圆