Beispiel #1
0
def alfabeta(board, depth, alfa, beta):
    boardstring = board.tostr() + str(board.team) + str(depth)
    # print boardstring , board.value()
    if boardstring in memo:
        return memo[boardstring]
    if depth == 0 or board.is_final():
        return (board.value(), None)
    ret = None
    bestValue = -INF
    childNodes = board.movimentos()
    if not childNodes:
        return (0, None)
    for child in childNodes:
        newboard = Board(board.s)
        newboard.move(child)
        (val, x) = alfabeta(newboard, depth - 1, -beta, -alfa)
        val = -val
        if val > bestValue:
            ret = child
            bestValue = val
        if val > alfa:
            alfa = val
        if alfa >= beta:
            break
    memo[boardstring] = (bestValue, ret)
    return (bestValue, ret)