Minimax游戏状态

时间:2014-12-14 17:06:07

标签: java algorithm minimax

我正在尝试实现一个minimax算法,我有算法的jist,但似乎我需要'撤消'在算法中的某个点移动。我找不到它应该在哪里。如果有人能告诉我在哪里以及为什么,我们将不胜感激。

private int minimax(Player[][] state, int r, int c, Player player) {
    if (getWinner(state, r, c) == player) return 10; //returns 10 if player is winner
    Player opponent = (player == Player.PLAYERX ? Player.PLAYERO : Player.PLAYERX);
    if (getWinner(state, r , c) == opponent) return -10; //returns -10 if opponent is winner

    if (getPlays(state, player) > getPlays(state, opponent)) state[r][c] = opponent; //Puts opponent in current spot if player has played more times than opponent
    else state[r][c] = player; //Puts player in current spot if opponent has played more times than player

    for (int column = 0; column < GRID_WIDTH; column++) {
        int row = top(state, column);
        if (row >= 0) {
            return minimax(state, row, column, player);
        }
    }
    return 0; //minimax will only ever return this if there are no plays left to be made, meaning that the scenario resulted in a draw
}

1 个答案:

答案 0 :(得分:1)

您需要在制作hypothetical时制作州的副本,否则需要制作&#34;试用版&#34;磁盘仍保留在实际state

Player[][] hypothetical = state;

应该是

Player[][] hypothetical = (Player[][])state.clone();

或者您可以使用state代替hypothetical,但在循环后添加磁盘删除:

if (getPlays(state, player) > getPlays(state, opponent))
    state[r][c] = opponent; //Puts opponent in current spot if player has played more times than opponent
else
    state[r][c] = player; //Puts player in current spot if opponent has played more times than player
int best = -10;
for (int column = 0; column < GRID_WIDTH; column++) {
    int row = top(hypothetical, column);
    if (row >= 0) {
        int tmp = minimax(hypothetical, row, column, player);
        if (tmp > best) best = tmp;
    }
}
state[r][c] = null;
return best;