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