Python:适用于Connect 4的Alpha Beta Minimax

时间:2013-01-01 23:31:48

标签: python alpha-beta-pruning

我正在尝试使用Python中的minimax实现一个AI for Connect 4作为个人项目。目前我有这个。

def alphaBeta(myBoard, column, depth, alpha, beta, player):
    parent = board()
    for r in range(ROWS):
        for c in range(COLUMNS):
            parent.board[r][c] = myBoard.board[r][c]
    parent.move(column, player)
    if parent.isFull() or parent.isWon()[0] or depth <= 0:
        if parent.isFull(): return 0
        if parent.isWon()[1] == player: return float('inf')
        elif parent.isWon()[0]: return -1*float('inf')
        else: return statScore(parent, player)
    if player == "O":
        for child in range(COLUMNS):
            alpha = max(alpha, alphaBeta(parent, child, depth-1, alpha, beta, "X"))
            if beta <= alpha:
                break
        return alpha
    else:
        for child in range(COLUMNS):
            beta = min(beta, alphaBeta(parent, child, depth-1, alpha, beta, "O"))
            if beta <= alpha:
                break
        return beta

def ai(myBoard, depth):
    output = []
    bestScore = float('inf')
    for column in range(COLUMNS):
        if myBoard.isValid(column):
            score = alphaBeta(myBoard, column, depth, -1*float('inf'), float('inf'), "O")
            if score < bestScore:
                output = [column]
                bestScore = score
            elif score == bestScore:
                output.append(column)
    move = random.choice(output)
    return move

然而,由于某种原因,计算机不会成功。例如。鉴于这个电路板,计算机应该在第4列中播放,以获得胜利。但是,它实际上在第3栏中播放。

 0 1 2 3 4 5 6
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | |O| | |
| | |X|O|O| | |
|X|X|O|X|O|X|X|
---------------
 0 1 2 3 4 5 6

0 个答案:

没有答案