Exemplo n.º 1
0
def min_value(board, a, b):
    """ Returns the minimum value for thr board.
    """
    # initialize v = +infinity
    v = 1000000

    # Check state
    result = common.game_status(board)

    # Check results
    if result == common.constants.X:
        return 1

    elif result == common.constants.O:
        return -1

    elif check_tie(board):
        return 0

    # If game is not finished - check the value of v
    for y in range(TICTACTOE_HEIGHT):
        for x in range(TICTACTOE_WIDTH):
            if common.get_cell(board, y, x) == common.constants.NONE:
                new_board = board[:]
                common.set_cell(new_board, y, x, common.constants.O)
                v = min(v, max_value(new_board, a, b))

                # For abprun_tictactoe()
                if (a is not None) and (b is not None):
                    if v <= a:
                        return v
                    b = min(b, v)
    return v
Exemplo n.º 2
0
def abminval(board, turn, a, b):
    val = 2  # has to be less than owin
    # check whose turn it is...
    if turn == common.constants.O:
        next_ = common.constants.X
    else:
        next_ = common.constants.O

    # Check if the game is already finished or not...
    gstat = common.game_status(board)
    if gstat == common.constants.O:
        val = -1
    elif gstat == common.constants.X:
        val = 1
    elif common.constants.NONE not in board:
        val = 0
    else:
        for i in range(3):
            for j in range(3):
                cval = common.get_cell(board, i, j)
                if cval == common.constants.NONE:
                    newboard = board[:]
                    common.set_cell(newboard, i, j, turn)
                    val = min(val, abmaxval(newboard, next_, a, b))
                    if val <= a:
                        return val
                    b = min(val, b)
    return val
def min_val(board):
    v = common.constants.X
    for i in range(0, 3):
        for j in range(0, 3):
            if common.get_cell(board, i, j) == common.constants.NONE:
                common.set_cell(board, i, j, common.constants.O)
                v = min(v, minmax_tictactoe(board, common.constants.X))
                common.set_cell(board, i, j, common.constants.NONE)
    return v
def min_val_ABPrun(board, alpha, beta):
    v = common.constants.X
    for i in range(0, 3):
        for j in range(0, 3):
            if common.get_cell(board, i, j) == common.constants.NONE:
                common.set_cell(board, i, j, common.constants.O)
                v = min(
                    v,
                    abprun_tictactoe_helper(board, common.constants.X, alpha,
                                            beta))
                common.set_cell(board, i, j, common.constants.NONE)
                if v == min(v, alpha):
                    return v
                beta = min(beta, v)
    return v