def __init__(self, board, broadth=None): self._board = board self._moveSortHandler = MoveSortHandler(board, broadth) return
def __init__(self,board,broadth=None): self._board = board self._moveSortHandler = MoveSortHandler(board,broadth) return
class SearchHandler: """ MiniMax search algorithm with alpha-beta pruning. """ def __init__(self, board, broadth=None): self._board = board self._moveSortHandler = MoveSortHandler(board, broadth) return def getBestMove(self, depth=4): self._board.stackClear() (best_move, value) = self.alphaBeta(True, depth, -float("inf"), float("inf")) return best_move def alphaBeta(self, isMax, depth, alpha, beta): """ return (best_move,alpha/beta) """ # Max = Computer = black # Because computer is doing the search, so it tries to maximize its winning possiblity # print " now the depth is ",depth if depth == 0 or self._board.checkOver(): # reach the search depth limit or terminal node # print " now returning the last_move and calcValue",self._board._lastMove,self._board.calcValue() return (self._board._lastMove, self._board.calcValue()) legalMoves = self._board.allLegalMoves( isMax) # isMax = Computer =Black side sortedLegalMoves = self._moveSortHandler.sortLegalMoves(legalMoves) best_move = None if isMax: for move in sortedLegalMoves: self._board.makeMove(move) # print "Black: Search after makeMove" # self._board.printBoard() (childMove, childValue) = self.alphaBeta(False, depth - 1, alpha, beta) self._board.unmakeMove(move) # print "Black: Search after UNmakeMove" # self._board.printBoard() if alpha < childValue: # (best_move,alpha) = (childMove,childValue) alpha = childValue best_move = move if alpha >= beta: # print "alpha cutoff pruning here" break return (best_move, alpha) else: #Red side - Player for move in sortedLegalMoves: self._board.makeMove(move) # print "Red: Search after makeMove" # self._board.printBoard() (childMove, childValue) = self.alphaBeta(True, depth - 1, alpha, beta) self._board.unmakeMove(move) # print "Red: Search after UNmakeMove" # self._board.printBoard() if beta > childValue: # (best_move,beta) = (childMove,childValue) beta = childValue best_move = move if alpha >= beta: #******* print "beta cutoff pruning here" break return (best_move, beta)
class SearchHandler: """ MiniMax search algorithm with alpha-beta pruning. """ def __init__(self,board,broadth=None): self._board = board self._moveSortHandler = MoveSortHandler(board,broadth) return def getBestMove(self,depth=4): self._board.stackClear() (best_move,value) = self.alphaBeta(True,depth,-float("inf"),float("inf")) return best_move def alphaBeta(self,isMax,depth,alpha,beta): """ return (best_move,alpha/beta) """ # Max = Computer = black # Because computer is doing the search, so it tries to maximize its winning possiblity # print " now the depth is ",depth if depth == 0 or self._board.checkOver(): # reach the search depth limit or terminal node # print " now returning the last_move and calcValue",self._board._lastMove,self._board.calcValue() return (self._board._lastMove,self._board.calcValue()) legalMoves = self._board.allLegalMoves(isMax)# isMax = Computer =Black side sortedLegalMoves = self._moveSortHandler.sortLegalMoves(legalMoves) best_move = None if isMax: for move in sortedLegalMoves: self._board.makeMove(move) # print "Black: Search after makeMove" # self._board.printBoard() (childMove,childValue) = self.alphaBeta(False,depth-1,alpha,beta) self._board.unmakeMove(move) # print "Black: Search after UNmakeMove" # self._board.printBoard() if alpha < childValue: # (best_move,alpha) = (childMove,childValue) alpha = childValue best_move = move if alpha >=beta: # print "alpha cutoff pruning here" break return (best_move,alpha) else: #Red side - Player for move in sortedLegalMoves: self._board.makeMove(move) # print "Red: Search after makeMove" # self._board.printBoard() (childMove,childValue) = self.alphaBeta(True,depth-1,alpha,beta) self._board.unmakeMove(move) # print "Red: Search after UNmakeMove" # self._board.printBoard() if beta > childValue: # (best_move,beta) = (childMove,childValue) beta = childValue best_move = move if alpha >=beta: #******* print "beta cutoff pruning here" break return (best_move,beta)