def minimax(board,depth,alpha,beta,maximizingPlayer): global currentPlayerColor global opponentPlayerColor if depth==0 or not isAnyMovePossible(board, currentPlayerColor) or not isAnyMovePossible(board,opponentPlayerColor): return evaluation(board) if maximizingPlayer: v = -sys.maxint - 1 moves = getAllPossibleMoves(board, opponentPlayerColor) for move in moves: newBoard = deepcopy(board) gamePlay.doMove(newBoard,move) #print "inside max",color #color=getOpponentColor(color) v = max(v,minimax(newBoard, depth-1, alpha, beta, False)) alpha = max(alpha,v) if beta <= alpha: return alpha return v else: v = sys.maxint moves = getAllPossibleMoves(board, currentPlayerColor) for move in moves: newBoard = deepcopy(board) gamePlay.doMove(newBoard,move) #print "inside min",color #color=getOpponentColor(color) v = min(v,minimax(newBoard, depth-1, alpha, beta, True)) beta = min(beta,v) if beta <= alpha: return beta return v
def minimax(board,depth,alpha,beta,maximizingPlayer): global currentPlayerColor global opponentPlayerColor if depth==0 or not isAnyMovePossible(board, currentPlayerColor) or not isAnyMovePossible(board,opponentPlayerColor): if countPieces(board,currentPlayerColor)>7: ''' initial stage and opening moves trying to focus on center and attack ''' return (0.75 * evaluationMovingToCenter(board))+ (0.20 * evaluationAttackFunction(board))+(0.5*evaluationColorVsOpposite(board)) elif countPieces(board,currentPlayerColor)>=6 : ''' middle stage have to be defensive And would also need to be attacktive ''' return (0.75 * evaluationMovingToDefense(board))+ (0.15 * evaluationAttackFunction(board))+(0.5*evaluationColorVsOpposite(board)+(0.5*evaluationMakingItKing(board))) elif countPieces(board,currentPlayerColor)>4 : ''' middle stage have to be get in center amd attack ''' return (0.20 * evaluationMovingToCenter(board))+ (0.70 * evaluationAttackFunction(board))+(0.5*evaluationColorVsOpposite(board)+(0.5*evaluationMakingItKing(board))+evaluationCanBeAttacked(board)) else: ''' when 4 or less than four pieces are remaining ''' return (0.50 * evaluationAttackFunction(board))+(0.30*evaluationColorVsOpposite(board)+(0.20*evaluationMovingToDefense(board))+evaluationCanBeAttacked(board)) if maximizingPlayer: v = -sys.maxint - 1 moves = getAllPossibleMoves(board, opponentPlayerColor) for move in moves: newBoard = deepcopy(board) gamePlay.doMove(newBoard,move) #print "inside max",color #color=getOpponentColor(color) v = max(v,minimax(newBoard, depth-1, alpha, beta, False)) alpha = max(alpha,v) if beta <= alpha: return alpha return v else: v = sys.maxint moves = getAllPossibleMoves(board, currentPlayerColor) for move in moves: newBoard = deepcopy(board) gamePlay.doMove(newBoard,move) #print "inside min",color #color=getOpponentColor(color) v = min(v,minimax(newBoard, depth-1, alpha, beta, True)) beta = min(beta,v) if beta <= alpha: return beta return v
def minimax(board,depth,alpha,beta,maximizingPlayer): #for every alternate player traverse upto depth as specified. #Alternately expand the nodes for the min and max player. #traverse upto the leaf or until the specified depth is reached #Then apply the evaluation function #backtrack for each node till the calling node is reached and return. #in each step alpha-beta prunning is applied and as per conditions the tree is getting prunned . global currentPlayerColor global opponentPlayerColor if depth==0 or not isAnyMovePossible(board, currentPlayerColor) or not isAnyMovePossible(board,opponentPlayerColor): return evaluation(board) if maximizingPlayer: v = -sys.maxint - 1 moves = getAllPossibleMoves(board, opponentPlayerColor) for move in moves: newBoard = deepcopy(board) gamePlay.doMove(newBoard,move) #print "inside max",color #color=getOpponentColor(color) v = max(v,minimax(newBoard, depth-1, alpha, beta, False)) alpha = max(alpha,v) if beta <= alpha: return alpha return v else: v = sys.maxint moves = getAllPossibleMoves(board, currentPlayerColor) for move in moves: newBoard = deepcopy(board) gamePlay.doMove(newBoard,move) #print "inside min",color #color=getOpponentColor(color) v = min(v,minimax(newBoard, depth-1, alpha, beta, True)) beta = min(beta,v) if beta <= alpha: return beta return v
def minimax(board, depth, alpha, beta, maximizingPlayer): global currentPlayerColor global opponentPlayerColor if ( depth == 0 or not isAnyMovePossible(board, currentPlayerColor) or not isAnyMovePossible(board, opponentPlayerColor) ): return evaluation(board) if maximizingPlayer: v = -sys.maxint - 1 moves = getAllPossibleMoves(board, opponentPlayerColor) for move in moves: newBoard = deepcopy(board) gamePlay.doMove(newBoard, move) # print "inside max",color # color=getOpponentColor(color) v = max(v, minimax(newBoard, depth - 1, alpha, beta, False)) alpha = max(alpha, v) if beta <= alpha: return alpha return v else: v = sys.maxint moves = getAllPossibleMoves(board, currentPlayerColor) for move in moves: newBoard = deepcopy(board) gamePlay.doMove(newBoard, move) # print "inside min",color # color=getOpponentColor(color) v = min(v, minimax(newBoard, depth - 1, alpha, beta, True)) beta = min(beta, v) if beta <= alpha: return beta return v
def minimize(board, alpha, beta, depth): global opponentColor global myColor #Return a heurisitc based score once the depth limit is reached if depth <=0 or not gamePlay.isAnyMovePossible(board, opponentColor): return evaluate(board, myColor) score = sys.maxint for move in getAllPossibleMoves(board, myColor): newBoard = deepcopy(board) gamePlay.doMove(newBoard, move) score = min(score, maximize(board, alpha, beta, depth-1)) #alpha cut-off if score <= alpha: return score beta = min(beta, score) return score
def maximize(board, alpha, beta, depth, time): global opponentColor global myColor #Return a heurisitc based score once the depth limit is reached if depth <= 0 or not gamePlay.isAnyMovePossible(board, opponentColor) or time < 7: return evaluate(board, myColor) score = -sys.maxint-1 for move in getAllPossibleMoves(board, opponentColor): newBoard = deepcopy(board) gamePlay.doMove(newBoard, move) score = max(score, minimize(newBoard, alpha, beta, depth-1, time)) #beta cut-off if score >= beta: return score alpha = max(alpha, score) return score
def isTerminal(node, color): return not gamePlay.isAnyMovePossible(node, color)
def minimax(board, depth, alpha, beta, maximizingPlayer): global currentPlayerColor global opponentPlayerColor if depth == 0 or not isAnyMovePossible( board, currentPlayerColor) or not isAnyMovePossible( board, opponentPlayerColor): if countPieces(board, currentPlayerColor) > 7: ''' initial stage and opening moves trying to focus on center and attack ''' return (0.75 * evaluationMovingToCenter(board)) + ( 0.20 * evaluationAttackFunction(board)) + ( 0.5 * evaluationColorVsOpposite(board)) elif countPieces(board, currentPlayerColor) >= 6: ''' middle stage have to be defensive And would also need to be attacktive ''' return (0.75 * evaluationMovingToDefense(board)) + ( 0.15 * evaluationAttackFunction(board)) + ( 0.5 * evaluationColorVsOpposite(board) + (0.5 * evaluationMakingItKing(board))) elif countPieces(board, currentPlayerColor) > 4: ''' middle stage have to be get in center amd attack ''' return (0.20 * evaluationMovingToCenter(board)) + ( 0.70 * evaluationAttackFunction(board)) + ( 0.5 * evaluationColorVsOpposite(board) + (0.5 * evaluationMakingItKing(board)) + evaluationCanBeAttacked(board)) else: ''' when 4 or less than four pieces are remaining ''' return (0.50 * evaluationAttackFunction(board)) + ( 0.30 * evaluationColorVsOpposite(board) + (0.20 * evaluationMovingToDefense(board)) + evaluationCanBeAttacked(board)) if maximizingPlayer: v = -sys.maxint - 1 moves = getAllPossibleMoves(board, opponentPlayerColor) for move in moves: newBoard = deepcopy(board) gamePlay.doMove(newBoard, move) #print "inside max",color #color=getOpponentColor(color) v = max(v, minimax(newBoard, depth - 1, alpha, beta, False)) alpha = max(alpha, v) if beta <= alpha: return alpha return v else: v = sys.maxint moves = getAllPossibleMoves(board, currentPlayerColor) for move in moves: newBoard = deepcopy(board) gamePlay.doMove(newBoard, move) #print "inside min",color #color=getOpponentColor(color) v = min(v, minimax(newBoard, depth - 1, alpha, beta, True)) beta = min(beta, v) if beta <= alpha: return beta return v