Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Tic Tac Toe negamax实现。_Java_Search_Minimax_Negamax - Fatal编程技术网

Java Tic Tac Toe negamax实现。

Java Tic Tac Toe negamax实现。,java,search,minimax,negamax,Java,Search,Minimax,Negamax,我正在尝试为tic-tac-toe应用程序实现negamax搜索函数,但它不会返回最佳值,相反,它似乎是半随机猜测的。以下是我代码的相关部分: public int negamax(Result result, Token token) { if (result == Result.WIN) { return 1; } else if (result == Result.DRAW) { return 0; } int best =

我正在尝试为tic-tac-toe应用程序实现negamax搜索函数,但它不会返回最佳值,相反,它似乎是半随机猜测的。以下是我代码的相关部分:

public int negamax(Result result, Token token) {
    if (result == Result.WIN) {
        return 1;
    } else if (result == Result.DRAW) {
        return 0;
    }

    int best = -1;

    for (Coordinate move : Board.getAvailableMoves()) {
        Token other = token.getOther();
        Result r = Board.makeMove(move, other);
        int eval = -negamax(r, other);
        Board.unmakeMove(move);

        if (eval > best) {
            best = eval;
        }
    }

    return best;
}

public Coordinate getNegamaxMove(Token token) {
    int score = -1;
    Coordinate bestMove = null;

    for (Coordinate move : Board.getAvailableMoves()) {
        Result result = Board.makeMove(move, token);
        int newScore = negamax(result, token);
        Board.unmakeMove(move);

        if (newScore >= score) {
            score = newScore;
            bestMove = move;
        }
    }

    return bestMove;
}
重要的是要注意,我不是通过董事会作为一个参数,而是通过一个动作的结果,它可以是赢的、平局的、有效的或被占用的(最后2个与当前讨论无关),这些都是不言自明的。坐标类只保存移动的行和列值


非常感谢:)

我已经设法让它工作了,negamax方法有两个问题。首先,令牌应该在循环所有可用移动之前更改,而不是在循环内部。其次,因为我在getNegamaxMove方法中检查了最佳移动,所以在negamax方法中,我必须跟踪最差的移动,而不是最佳移动。以下是注释掉的旧部件的工作实现,以供比较:

public int negamax(Result result, Token token) {
    if (result == Result.WIN) {
        return 1;
    } else if (result == Result.DRAW) {
        return 0;
    }

    int worst = 1;
    // int best = -1

    Token other = token.getOther();
    for (Coordinate move : Board.getAvailableMoves()) {
        // Token other = token.getOther();
        Result r = Board.makeMove(move, other);
        int eval = -negamax(r, other);
        Board.unmakeMove(move);

        // if (eval > best) {
        //     best = eval;
        // }

        if (eval < worst) {
            worst = eval;
        }
    }

    // return best
    return worst;
}
public int negamax(结果、令牌){
if(result==result.WIN){
返回1;
}else if(result==result.DRAW){
返回0;
}
int最差=1;
//最佳整数=-1
Token other=Token.getOther();
对于(坐标移动:Board.getAvailableMoves()){
//Token other=Token.getOther();
结果r=板。移动(移动,其他);
int eval=-negamax(r,其他);
板。取消移动(移动);
//如果(评估>最佳){
//最佳=评估;
// }
如果(评估<最差){
最差=评估;
}
}
//回报最好
回报最差;
}