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