def noncapturewin(pieces):

    # If a lion is already captured, then this function reverts to being
    # the capturewin function.

    lioncapture = capturewin(pieces)
    if lioncapture is not None : return lioncapture

    # If Player A's lion has reached the furthest rank.

    if pieces['lion'][0][1][0] == 1:
        selectedactions = selection(b, pieces)
        for action in selectedactions:
            state = perform(action, pieces)
            if state is not None:
                if capturewin(state) == b:
                    return b
        return a

    # If Player B's lion has reached the furthest rank.

    if pieces['lion'][1][1][0] == 4:
        selectedactions = selection(a, pieces)
        for action in selectedactions:
            state = perform(action, pieces)
            if state is not None:
                if capturewin(state) == a:
                    return a
        return b
Esempio n. 2
0
def alphabeta(pieces, depth, alpha=-math.inf, beta=+math.inf):

    if depth <= 0: return

    selected = selection(b, pieces)
    # Randomly permute the children of the root node before alpha-beta
    # pruning begins.
    random.shuffle(selected)
    possibilities = []

    # Determine all candidate moves for Player B.
    for action in selected:
        possibilities.append(perform(action, pieces))

    possibilities = [i for i in possibilities if i is not None]

    # Apply alpha-beta pruning to the root node.
    maximinlist = []

    for possibility in possibilities:
        value = minimizer(possibility, depth - 1, alpha, beta)
        maximinlist.append(value)
        alpha = max(alpha, value)

    maxvalue = max(maximinlist)
    maxindex = maximinlist.index(maxvalue)

    return possibilities[maxindex]
Esempio n. 3
0
def enter(side, command, pieces):
    try:
        action = translate(side, command, pieces)
    except:
        print("Press enter to exit the game.")
        input()
        quit()
    return perform(action, pieces)
Esempio n. 4
0
def maximizer(pieces, depth, alpha, beta):

    # Skew the score if one side has at least two more pieces.
    skew = 0
    majority = piecemajority(pieces)
    if majority == a: skew = -0.01
    elif majority == b: skew = 0.01

    # If one side wins, provide a score. Otherwise, if the depth
    # limit has been reached, provide the score skew.
    if capturewin(pieces) == b: return 1
    elif noncapturewin(pieces) == b: return 1
    elif capturewin(pieces) == a: return -1
    elif noncapturewin(pieces) == a: return -1
    elif depth == 0: return skew

    # Determine all candidate moves for Player B at this node.
    selected = selection(b, pieces)
    possibilities = []

    for action in selected:
        possibilities.append(perform(action, pieces))

    possibilities = [i for i in possibilities if i is not None]

    # Apply alpha-beta pruning to this node.
    maximinlist = []

    for possibility in possibilities:
        value = minimizer(possibility, depth - 1, alpha, beta)
        maximinlist.append(value)
        alpha = max(alpha, value)
        if beta <= alpha:
            break

    return max(maximinlist) + skew