Exemple #1
0
def alphabeta(pos, depth, alpha, beta, me, stop_flag):
    if depth == 0 or stop_flag() or get_score(pos) > WIN_SCORE or get_score(
            pos) < -WIN_SCORE or bb.is_tie(pos):
        return get_score(pos)

    if me:
        best = -WIN_SCORE
        children = get_children(pos, True)[0:10]
        for n_pos in children:
            val = alphabeta(n_pos, depth - 1, alpha, beta, False, stop_flag)
            mmx_scores[n_pos] = val
            if val > best:
                best = val
            if best > alpha:
                alpha = best
            if alpha >= beta:
                #childrens[pos].sort(key=lambda c: scores[c] if c not in mmx_scores else mmx_scores[c], reverse=me)
                return best
        #childrens[pos].sort(key=lambda c: scores[c] if c not in mmx_scores else mmx_scores[c], reverse=me)
        return best
    else:
        best = WIN_SCORE
        children = get_children(pos, False)[0:10]
        for n_pos in children:
            val = alphabeta(n_pos, depth - 1, alpha, beta, True, stop_flag)
            mmx_scores[n_pos] = val
            if val < best:
                best = val
            if best < beta:
                beta = best
            if beta <= alpha:
                #childrens[pos].sort(key=lambda c: scores[c] if c not in mmx_scores else mmx_scores[c], reverse=me)
                return best
        #childrens[pos].sort(key=lambda c: scores[c] if c not in mmx_scores else mmx_scores[c], reverse=me)
        return best
Exemple #2
0
def minimax(pos, depth, alpha, beta, me):
    if bitboard.is_tie(pos):
        return 0
    score = get_score(pos)
    if score > WIN_SCORE:
        return score
    elif score < -WIN_SCORE:
        return score
    elif depth == 0:
        return score

    if me:
        best = -WIN_SCORE
        children = get_children(pos, True, 9)
        for n_pos in children:
            val = minimax(n_pos, depth - 1, alpha, beta, False)
            minimax_scores[n_pos] = val
            best = val if val > best else best
            alpha = best if best > alpha else alpha
            if alpha >= beta:
                # re sort childrens with minimax evaluation
                childrens[pos].sort(
                    key=lambda c: get_score(c)
                    if c not in minimax_scores else 100 * minimax_scores[c],
                    reverse=me)
                return best
        childrens[pos].sort(
            key=lambda c: get_score(c)
            if c not in minimax_scores else 100 * minimax_scores[c],
            reverse=me)
        return best
    else:
        best = WIN_SCORE
        children = get_children(pos, False, 9)
        for n_pos in children:
            val = minimax(n_pos, depth - 1, alpha, beta, False)
            minimax_scores[n_pos] = val
            best = val if val < best else best
            beta = best if best < beta else beta
            if alpha <= beta:
                # re sort childrens with minimax evaluation
                childrens[pos].sort(
                    key=lambda c: get_score(c)
                    if c not in minimax_scores else 100 * minimax_scores[c],
                    reverse=me)
                return best
        childrens[pos].sort(
            key=lambda c: get_score(c)
            if c not in minimax_scores else 100 * minimax_scores[c],
            reverse=me)
        return best
Exemple #3
0
def quick_alphabeta(pos, depth, alpha, beta, me):
    if depth == 0 or pos not in childrens or scores[pos] > WIN_SCORE or scores[
            pos] < -WIN_SCORE or bb.is_tie(pos):
        return scores[pos]

    if me:
        best = -WIN_SCORE
        for n_pos in childrens[pos]:
            val = quick_alphabeta(n_pos, depth - 1, alpha, beta, False)
            best = val if val > best else best
            alpha = best if best > alpha else alpha
            if alpha >= beta:
                return best
        return best
    else:
        best = WIN_SCORE
        for n_pos in childrens[pos]:
            val = quick_alphabeta(n_pos, depth - 1, alpha, beta, True)
            best = val if val < best else best
            beta = best if best < beta else beta
            if beta <= alpha:
                return best
        return best