Tic Tac Toe negamax implementation.

时间:2015-05-12 22:31:33

标签: java search minimax negamax

I'm trying to implement the negamax search function for a tic-tac-toe application, but it does not return the optimal values, instead it appears to guess semi-randomly. Here's the relevant part of my code:

<platform name="android">
    <supports-screen xlargeScreens="false"/>
</platform>

It's important to note that I don't pass the board as a parameter, but rather the outcome of a move, which can be either WIN, DRAW, VALID or OCCUPIED (the last 2 are not relevant to the current discussion) which are all self-explanatory. The Coordinate class just holds the row and column values of the move.

Thank you very much :)

1 个答案:

答案 0 :(得分:1)

我已经设法让它工作,negamax方法有2个问题。首先,在循环遍历所有可用移动之前,应该更改令牌,而不是在循环内部。其次,因为我检查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;
}