示例#1
0
def MinMax(board, depth):  #black turn
    if depth == 0: return [static(board), board, board]
    inv = game.getInverse(board)
    positions = game.generateAdd(inv)
    if not positions:
        return (static(board), board, board)
    else:
        v = [float('inf'), None, None]
        for pos in positions:
            v = min(v, MaxMin(pos, depth - 1)[:-1] + [pos], key=lambda x: x[0])
        return v
def EndMinMax(board,depth,a,b):#black turn
    score = static(board)
    if abs(score)==10000: return [score,board,board]
    inv = game.getInverse(board)
    positions = game.generateHopping(inv)

    if not positions: 
        return [10000,board,board]
    else:
        v = [float('inf'),None,None]
        for pos in positions:
            v = min(v,MidMaxMin(pos,depth-1,a,b)[:-1]+[pos],key = lambda x:x[0])
            if(v[0]<=a): return v
            else: b = min(v[0],b)
        return v
示例#3
0
def static(board):  #flag: 0 white 1 black
    global estTime
    numW, numB = 0, 0
    for b in board:
        if b == 'W':
            numW += 1
        elif b == 'B':
            numB += 1
    estTime += 1
    if numB <= 2: return 10000
    if numW <= 2: return -10000
    inv = game.getInverse(board)
    if numB == 3:
        moves = len(game.generateHopping(inv))
    else:
        moves = len(game.generateMove(inv))
    #print(1000*(numW-numB)-moves,numW,numB,moves)
    return 1000 * (numW - numB) - moves
def MidMinMax(board,depth,a,b):#black turn
    if depth==0:
         return [static(board),board,board]
    flag = checkStep(board)
    if flag==2 or flag==3:
        return EndMinMax(board,depth,a,b)
    inv = game.getInverse(board)
    positions = game.generateMove(inv)
    
    if depth==0 or not positions:
         return [static(board),board,board]
    else:
        v = [float('inf'),None,None]
        for pos in positions:
            v = min(v,MidMaxMin(pos,depth-1,a,b)[:-1]+[pos],key = lambda x:x[0])
            if(v[0]<=a): return v
            else: b = min(v[0],b)
        return v
import sys
import gameLib as game
import MiniMaxOpening

if __name__ == "__main__":
    if len(sys.argv) != 4:
        sys.argv = [sys.argv[0], "board3.txt", "board2.txt", "2"]
    print(sys.argv)
    depth = int(sys.argv[3])
    board = game.readBoard(sys.argv[1])

    # res = MiniMaxOpening.MinMax(board,depth)

    res = MiniMaxOpening.MaxMin(board, depth)
    res[1] = game.getInverse(res[1])
    res[-1] = game.getInverse(res[-1])

    game.writeBoard(res[-1], sys.argv[2])
    #print([ 'x' if val=='x' else (i,val) for i,val in enumerate(board)])
    #print([ 'x' if val=='x' else (i,val) for i,val in enumerate(res[1])])
    print("Board Position:", ''.join(res[-1]))
    print("Position evaluated by static estimation:", MiniMaxOpening.estTime)
    print("MINIMAX esitmate:", res[0])
    #game.show(board)
    #game.show(res[-1])
    #game.show(res[1])
示例#6
0
    if not positions:
        return [10000, board, board]
    else:
        v = [float('inf'), None, None]
        for pos in positions:
            v = min(v,
                    MidMaxMin(pos, depth - 1, a, b)[:-1] + [pos],
                    key=lambda x: x[0])
            if (v[0] <= a): return v
            else: b = min(v[0], b)
        return v


if __name__ == "__main__":
    if len(sys.argv) != 4:
        sys.argv = [sys.argv[0], "board3.txt", "board4.txt", "4"]
    print(sys.argv)
    depth = int(sys.argv[3])
    board = game.readBoard(sys.argv[1])
    res = MidMaxMin(game.getInverse(board), depth, -float('inf'), float('inf'))
    game.writeBoard(res[-1], sys.argv[2])
    # print([ 'x' if val=='x' else (i,val) for i,val in enumerate(board)])
    # print([ 'x' if val=='x' else (i,val) for i,val in enumerate(res[1])])
    print("Board Position:", ''.join(res[-1]))
    print("Position evaluated by static estimation:", estTime)
    print("MINIMAX esitmate:", res[0])
    #game.show(board)
    #game.show(res[-1])
    #game.show(res[1])
示例#7
0
    if board[loc] == 'B':
        for j in game.neighbors(loc):
            if board[j] == 'x':
                locations.append(j)
    return locations


if __name__ == "__main__":
    print("enter difficulty (1~4)")
    depth = int(input())
    board = ['x' for _ in range(23)]
    score = 0
    print("opening game")
    for _ in range(9):
        print("AI turn", score)
        res = ABOpening.MaxMin(game.getInverse(board), depth, -float('inf'),
                               float('inf'))
        score = ABOpening.static(board)
        board = res[-1]
        game.show(board)
        print("Your turn", score)
        loc = int(input())
        board[loc] = 'B'
        game.show(board)
        if game.closeMill(loc, board, turn='B'):
            print("Pick the location to remove")
            loc = int(input())
            board[loc] = 'x'
        score = ABOpening.static(board)
        game.show(board)
示例#8
0
def MinMax(board, depth):  #black turn
    if depth == 0: return [static(board), board, board]
    inv = game.getInverse(board)
    positions = game.generateAdd(inv)
    if not positions:
        return (static(board), board, board)
    else:
        v = [float('inf'), None, None]
        for pos in positions:
            v = min(v, MaxMin(pos, depth - 1)[:-1] + [pos], key=lambda x: x[0])
        return v


if __name__ == "__main__":
    if len(sys.argv) != 4:
        sys.argv = [sys.argv[0], "board1.txt", "board2.txt", "3"]
    print(sys.argv)
    depth = int(sys.argv[3])
    board = game.readBoard(sys.argv[1])

    res = MaxMin(game.getInverse(board), depth)
    game.writeBoard(res[-1], sys.argv[2])
    #print([ 'x' if val=='x' else (i,val) for i,val in enumerate(board)])
    #print([ 'x' if val=='x' else (i,val) for i,val in enumerate(res[1])])
    print("Board Position:", ''.join(res[-1]))
    print("Position evaluated by static estimation:", estTime)
    print("MINIMAX esitmate:", res[0])
    #game.show(board)
    #game.show(res[-1])
    #game.show(res[1])