Minimax算法不尊重随机化

时间:2016-12-06 19:53:02

标签: java minimax

我正在开发一种minimax算法用于改进的跳棋游戏。在我的评估函数中,每个分数乘以10,然后从中添加/减去1到10之间的随机数(取决于最大或最小节点)。但是,在运行程序时,它总是执行相同的移动序列。我已经检查了评估函数,它肯定会返回具有相同值的节点的随机值,所以我只能假设问题在于minimax函数本身,任何想法?其他函数,generateMoves和simulateMove也可以正常工作。

private int minimax(State state, int depth, int min, int max) {
    ArrayList<Move> moves = generateMoves(state.board, state.colour);
    char opponent = (state.colour == DraughtBoard.WHITE) ? DraughtBoard.BLACK : DraughtBoard.WHITE;

    if (moves.size() == 1)
        nextMove = moves.get(0);

    int bestScore;
    Move bestMove = new Move();
    int score = 0;

    if (depth == 0 || moves.size() == 0) {
        return evaluateBoard(state);
    }

    if (colour == DraughtBoard.WHITE) {
        bestScore = min;
        for (Move move : moves) {
            char[][] temp = state.board.clone();
            boolean scored = simulateMove(move, temp);

            State nextState = new State(temp, opponent, state.whiteScore, state.blackScore);
            if (scored) state.whiteScore++;

            score = minimax(state, depth-1, bestScore, max);

            if (score > bestScore) {
                bestScore = score;
                bestMove = move;
            }
            if (bestScore > max) return max;
        }
        nextMove = bestMove;
        return bestScore;
    } else {
        bestScore = max;
        for (Move move : moves) {
            char[][] temp = state.board.clone();
            boolean scored = simulateMove(move, temp);

            State nextState = new State(temp, opponent, state.whiteScore, state.blackScore);
            if (scored) state.blackScore++;

            score = minimax(state, depth-1, min, bestScore);

            if (score < bestScore) {
                bestScore = score;
                bestMove = move;
            }
            if (bestScore < min) return min;
        }
        nextMove = bestMove;
        return bestScore;
    }
}

1 个答案:

答案 0 :(得分:0)

char[][] temp = state.board.clone();只会执行一个吞下副本(除了你编写自己的clone()方法)

这意味着tempboard具有相同的引用,因此您在调用board时会更改siumlateMove

这可能会导致您的问题。 deep copy of a 2d array