def minimax(node, depth, alpha, beta, maxnode, p_board, p_block, flag1, flag2, best_row, best_col): if depth==0: utility = check(p_board,p_block) if flag1 == 'o': return (-utility,best_row,best_col) return (utility,best_row,best_col) else: children_list = compute_cells(p_board,p_block,node) for child in children_list: if maxnode: p_board[child[0]][child[1]] = flag1 else: p_board[child[0]][child[1]] = flag2 if maxnode: score = minimax (child,depth-1,alpha,beta,False,p_board,p_block,flag1,flag2,best_row,best_col) if (score[0] > alpha): alpha = score[0] best_row = child[0] best_col = child[1] else: score = minimax (child,depth-1,alpha,beta,True,p_board,p_block,flag1,flag2,best_row,best_col) if (score[0] < beta): beta = score[0] best_row = child[0] best_col = child[1] p_board[child[0]][child[1]] = '-' if (alpha >= beta): break if maxnode: return (alpha, best_row, best_col) else: return(beta, best_row, best_col)
def mminimax(node, depth, mini, maxi, maxnode, p_board, p_block, flag1, flag2, best_row, best_col): #global best_row, best_col if depth==0: p_board[node[0]][node[1]] = flag2 utility = check(p_board,p_block) #print p_board p_board[node[0]][node[1]] = '-' #print 'utility = ' + str(utility) if flag1 == 'o': return (-utility,best_row,best_col) return (utility,best_row,best_col) if maxnode: v = mini #print 'v =' + str(v) p_board[node[0]][node[1]] = flag2 #print p_board children_list = compute_cells(p_board,p_block,node) #print node #print children_list for child in children_list: v1 = minimax (child,depth-1,v,maxi,False,p_board,p_block,flag1,flag2,best_row,best_col) print 'max', maxi if(depth != 4): p_board[node[0]][node[1]] = '-' #print 'v1 = ' + str(v1) if (v1[0] >= v): v = v1[0] best_row = child[0] best_col = child[1] if (v >= maxi): return (maxi,best_row,best_col) return (v,best_row,best_col) else: v = maxi p_board[node[0]][node[1]] = flag1 #print p_board #print 'min', #print node children_list = compute_cells(p_board,p_block,node) #print children_list for child in children_list: v1 = minimax (child,depth-1,mini,v,True,p_board,p_block,flag1,flag2,best_row,best_col) print 'min',mini p_board[node[0]][node[1]] = '-' #print 'v1 = ' + str(v1) if (v1[0] <= v): v = v1[0] best_row = child[0] best_col = child[1] if (v <= mini): return (mini,best_row,best_col) return (v,best_row,best_col)