Example #1
0
def main():
    inFileName, outFileName, depth = sys.argv[1], sys.argv[2], int(sys.argv[3])
    root = MorrisGame.readFromFile(inFileName)

    result = MaxMinABMidEnd(root, depth, float('-inf'), float('inf'))

    print('Initial position:', root.position, 'Output position: ', result.position)
    print('Position evaluated by static estimation: ', MorrisGame.numEvaluate)
    print('MINIMAX estimate: ', result.value)

    MorrisGame.write2File(outFileName, result.position)
def main():
    inFileName, outFileName, depth = sys.argv[1], sys.argv[2], int(sys.argv[3])
    root = MorrisGame.readFromFile(inFileName)

    result = MiniMaxOpeningBlack(root, depth)

    print('Initial position:', root.position, 'Output position: ',
          result.position)
    print('Position evaluated by static estimation: ', MorrisGame.numEvaluate)
    print('MINIMAX estimate: ', result.value)

    MorrisGame.write2File(outFileName, result.position)
def MiniMaxGameBlack(root, depth):
    """ use MINIMAX algorithm to generate a move for black """

    # reverse the board
    root.position = MorrisGame.reverse(root.position)

    result = MaxMinMidEnd(root, depth)

    # reverse the result
    result.position = MorrisGame.reverse(result.position)
    root.position = MorrisGame.reverse(root.position)

    return result
def MiniMaxOpeningBlack(root, depth):
    """ use MINIMAX algorithm to generate a move for black in the opening"""

    # reverse the board
    root.position = MorrisGame.reverse(root.position)

    result = MaxMinOpening(root, depth)

    # reverse the result
    result.position = MorrisGame.reverse(result.position)
    root.position = MorrisGame.reverse(root.position)

    return result
Example #5
0
def doubleMills(position):
    num = 0
    length = len(position)
    for i in range(length):
        if position[i] == 'W':
            if MorrisGame.closeMill(i, position):
                neList = MorrisGame.neighbor[i]
                for n in neList:
                    if position[n] == 'x':
                        pos = position[:n] + 'W' + position[n + 1:]
                        if MorrisGame.closeMill(n, pos):
                            num += 1
    return num
Example #6
0
def main():
    # inFileName, outFileName, depth = 'board1.txt', 'board2.txt', 5    # for test

    inFileName, outFileName, depth = sys.argv[1], sys.argv[2], int(sys.argv[3])
    root = MorrisGame.readFromFile(inFileName)

    result = MaxMinOpening(root, depth)

    print('Initial position:', root.position, 'Output position: ',
          result.position)
    print('Position evaluated by static estimation: ', MorrisGame.numEvaluate)
    print('MINIMAX estimate: ', result.value)

    MorrisGame.write2File(outFileName, result.position)
Example #7
0
def main():
    inFileName, outFileName, depth = sys.argv[1], sys.argv[2], int(sys.argv[3])
    # inFileName, outFileName, depth = 'board3.txt', '1.txt', 1
    root = MorrisGame.readFromFile(inFileName)

    # print(MorrisGame.staticMidEnd(root.position))

    result = MaxMinMidEnd(root, depth)
    # result = MinMaxMidEnd(root, depth)

    print('Initial position:', root.position, 'Output position: ',
          result.position)
    print('Position evaluated by static estimation: ', MorrisGame.numEvaluate)
    print('MINIMAX estimate: ', result.value)

    MorrisGame.write2File(outFileName, result.position)
Example #8
0
def MinMaxMidEndImproved(board, depth):
    numBlack = MorrisGame.countNums(board.position)[1]
    if depth == 0 or numBlack < 3:
        board.value = staticMidEndImproved(board.position, depth)
        MorrisGame.numEvaluate += 1
        return board
    else:
        board.child = MorrisGame.genMoveMidEndBlack(board.position)

        minValue = float('inf')
        retBoard = None
        for child in board.child:
            result = MaxMinMidEndImproved(child, depth - 1)
            if minValue > result.value:
                minValue = result.value
                retBoard = child
                retBoard.value = minValue
        return retBoard
Example #9
0
def MaxMinMidEndImproved(board, depth):
    numWhite = MorrisGame.countNums(board.position)[0]
    if depth == 0 or numWhite < 3:
        board.value = staticMidEndImproved(board.position, depth)
        MorrisGame.numEvaluate += 1
        return board
    else:
        board.child = MorrisGame.genMoveMidEnd(board.position)

        maxValue = float('-inf')
        retBoard = None
        for child in board.child:
            result = MinMaxMidEndImproved(child, depth - 1)
            if maxValue < result.value:
                maxValue = result.value
                retBoard = child
                retBoard.value = maxValue
        return retBoard
Example #10
0
def MinMaxOpening(board, depth):
    """ use MINIMAX algorithm to choose the move for 'MIN' """
    if depth == 0:
        board.value = MorrisGame.openingStatic(board.position)
        MorrisGame.numEvaluate += 1
        return board
    else:
        board.child = MorrisGame.genMoveOpeningBlack(board.position)

        minValue = float('inf')
        retBoard = None
        for child in board.child:
            result = MaxMinOpening(child, depth - 1)
            if minValue > result.value:
                retBoard = result  # for test
                minValue = result.value
                retBoard = child
                retBoard.value = minValue
        return retBoard
Example #11
0
def MinMaxMidEnd(board, depth):
    """ use MINIMAX algorithm to choose the move for 'MIN' """

    numBlack = MorrisGame.countNums(board.position)[1]
    if depth == 0 or numBlack < 3:
        board.value = MorrisGame.staticMidEnd(board.position)
        MorrisGame.numEvaluate += 1
        return board
    else:
        board.child = MorrisGame.genMoveMidEndBlack(board.position)

        minValue = float('inf')
        retBoard = None
        for child in board.child:
            result = MaxMinMidEnd(child, depth - 1)
            if minValue > result.value:
                # retBoard = result   # for test

                minValue = result.value
                retBoard = child
                retBoard.value = minValue
        return retBoard
Example #12
0
def MaxMinMidEnd(board, depth):
    """ use MINIMAX algorithm to choose the move for 'MAX' """

    numWhite = MorrisGame.countNums(board.position)[0]
    if depth == 0 or numWhite < 3:
        board.value = MorrisGame.staticMidEnd(board.position)
        MorrisGame.numEvaluate += 1
        return board
    else:
        board.child = MorrisGame.genMoveMidEnd(board.position)

        maxValue = float('-inf')
        retBoard = None
        for child in board.child:
            result = MinMaxMidEnd(child, depth - 1)
            if maxValue < result.value:
                # retBoard = result   # for test

                maxValue = result.value
                retBoard = child
                retBoard.value = maxValue
        return retBoard
Example #13
0
def openingStaticImproved(position):
    """ improve the static estimation by adding the number of potential mills and number of double mills """

    piecesDiff = MorrisGame.openingStatic(position)
    num2PiecesConf = count2PiecesConf(position)
    if num2PiecesConf >= 2:
        num3PiecesConf = num2PiecesConf - 1
    else:
        num3PiecesConf = 0

    # staticEs = 6 * piecesDiff + 12 * num2PiecesConf + 7 * num3PiecesConf + 26 * countMills(position)
    # staticEs = piecesDiff + 6 * num2PiecesConf
    staticEs = piecesDiff + 2 * num2PiecesConf + 10 * num3PiecesConf
    return staticEs
Example #14
0
def MinMaxABOpening(board, depth, alpha, beta):
    """ use Alpha-Beta pruning to choose the move for 'MIN' """

    if depth == 0:
        board.value = MorrisGame.openingStatic(board.position)
        MorrisGame.numEvaluate += 1
        return board
    else:
        board.child = MorrisGame.genMoveOpeningBlack(board.position)

        minValue = float('inf')
        retBoard = None
        for child in board.child:
            result = MaxMinABOpening(child, depth - 1, alpha, beta)
            if minValue > result.value:
                minValue = result.value
                retBoard = child
                retBoard.value = minValue
            if minValue <= alpha:
                return retBoard
            elif minValue < beta:
                beta = minValue
        return retBoard
Example #15
0
def MinMaxABMidEnd(board, depth, alpha, beta):
    """ use Alpha-Beta pruning to choose the move for 'MIN' """

    numBlack = MorrisGame.countNums(board.position)[1]
    if depth == 0 or numBlack < 3:
        board.value = MorrisGame.staticMidEnd(board.position)
        MorrisGame.numEvaluate += 1
        return board
    else:
        board.child = MorrisGame.genMoveMidEndBlack(board.position)

        minValue = float('inf')
        retBoard = None
        for child in board.child:
            result = MaxMinABMidEnd(child, depth - 1, alpha, beta)
            if minValue > result.value:
                minValue = result.value
                retBoard = child
                retBoard.value = minValue
            if minValue <= alpha:
                return retBoard
            elif minValue < beta:
                beta = minValue
        return retBoard
Example #16
0
def MaxMinABMidEnd(board, depth, alpha, beta):
    """ use Alpha-Beta pruning to choose the move for 'MAX' """

    numWhite = MorrisGame.countNums(board.position)[0]
    if depth == 0 or numWhite < 3:
        board.value = MorrisGame.staticMidEnd(board.position)
        MorrisGame.numEvaluate += 1
        return board
    else:
        board.child = MorrisGame.genMoveMidEnd(board.position)

        maxValue = float('-inf')
        retBoard = None
        for child in board.child:
            result = MinMaxABMidEnd(child, depth - 1, alpha, beta)
            if maxValue < result.value:
                maxValue = result.value
                retBoard = child
                retBoard.value = maxValue
            if maxValue >= beta:
                return retBoard
            elif maxValue > alpha:
                alpha = maxValue
        return retBoard
Example #17
0
def MaxMinABOpening(board, depth, alpha, beta):
    """ use Alpha-Beta pruning to choose the move for 'MAX' """

    if depth == 0:
        board.value = MorrisGame.openingStatic(board.position)
        MorrisGame.numEvaluate += 1
        return board
    else:
        board.child = MorrisGame.genMoveOpening(board.position)

        maxValue = float('-inf')
        retBoard = None
        for child in board.child:
            result = MinMaxABOpening(child, depth - 1, alpha, beta)
            if maxValue < result.value:
                maxValue = result.value
                retBoard = child
                retBoard.value = maxValue
            if maxValue >= beta:
                return retBoard
            else:
                if maxValue > alpha:
                    alpha = maxValue
        return retBoard
Example #18
0
def staticMidEndImproved(position, depth):
    """ improved the static estimation by adding the number of potential mills and number of double mills """
    static = MorrisGame.staticMidEnd(position)

    num2PiecesConf = count2PiecesConf(position)
    if num2PiecesConf >= 2:
        num3PiecesConf = num2PiecesConf - 1
    else:
        num3PiecesConf = 0
    if static == 10000:
        static *= (depth + 1)
    # # staticEs = static + 200 * num2PiecesConf
    # # staticEs = static + 200 * countMills(position) + 200 * num2PiecesConf + 100 * num3PiecesConf + 400 * doubleMills(position)
    staticEs = static + 100 * countMills(position) + 300 * doubleMills(
        position)
    # staticEs = static + 200 * num2PiecesConf + 300 * num3PiecesConf
    return staticEs
Example #19
0
def MaxMinOpeningImproved(board, depth):
    """ use MINIMAX algorithm and an improved estimation to choose the move for 'MAX' """

    if depth == 0:
        board.value = openingStaticImproved(board.position)
        MorrisGame.numEvaluate += 1
        return board
    else:
        board.child = MorrisGame.genMoveOpening(board.position)

        maxValue = float('-inf')
        retBoard = None
        for child in board.child:
            result = MinMaxOpeningImproved(child, depth - 1)
            if maxValue < result.value:
                maxValue = result.value
                retBoard = child
                retBoard.value = maxValue
        return retBoard