有哪些算法可以解决Tic Tac Toe?

时间:2012-05-22 15:22:24

标签: tic-tac-toe alpha-beta-pruning

有哪些算法可以解决Tic Tac Toe?特别是电路板尺寸为4 * 4或更大而不是3 * 3?我用Minimax&尝试了4 * 4 alpha-beta修剪但是pc似乎挂起并在堆栈溢出时抛出异常。 我看到这些用javascript编写的源代码,但我不知道它使用的是哪种算法,有人可以为我澄清一下吗? http://js-x.com/page/javascripts__example.html?view=153

1 个答案:

答案 0 :(得分:1)

尝试在一定程度上做截止...我认为你不需要超过4或5的深度才能做出完美的动作。

(带有深度的3 * 3单调板的java):

int minimax(int turn, int depth) { // turn(side to move) is 1 or -1
    int val = -turn; // worst value for that turn 
    int result = NULVAL;
    if (wins()) return turn; // evaluate board (board is a field)
    if (depth == 0) return DRAW;
    for (int i = 0; i < 9; i++) {
        if (board[i] == EMPTY) {
            board[i] = turn; // make move
            result = minimax(-turn, depth - 1);
            board[i] = EMPTY; // delete move
            if (result == turn) 
                return turn; // sees win
            if (result == DRAW)
                val = result;
        }
    }
    // if result keeps NULVAL: couldn't make a move (board is full)
    if (result == NULVAL) return DRAW;
    return val;
}