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
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
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
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
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