CCCPaste

549331278dc01

private int minimax(bool flag)
        {
            int max_val=-1000,min_val=1000;
            int i,j,value = 1;
            if(checkVictory(computer) == 1)
            {
                return 10;
            }
            else if(checkVictory(human) == 1)
            {
                return -10;
            }
            else if(isFull()== 1)
            {
                return 0;
            }
            int[] score = {1,1,1,1,1,1,1,1,1};//if score[i]=1 then it is empty
        
            for(i=0;i<9;i++)
            {
                if(oriBoard[i] == "*")
                {
                    if(min_val>max_val) // reverse of pruning condition.....
                    {
                        if(flag == true)
                        {
                            oriBoard[i] = "X";
                            value = minimax(false);
                        }
                        else
                        {
                            oriBoard[i] = "O";
                            value = minimax(true);
                        }
                        oriBoard[i] = "*";
                        score[i] = value;
                    }
                }
            }
            if(flag == true)
            {
                max_val = -1000;
                for(j=0;j<9;j++)
                {
                    if(score[j] > max_val && score[j] != 1)
                    {
                        max_val = score[j];
                        bestMove = j;
                    }
                }
                return max_val;
            }
            if(flag == false)
            {
                min_val = 1000;
                for(j=0;j<9;j++)
                {
                    if(score[j] < min_val && score[j] != 1)
                    {
                        min_val = score[j];
                        bestMove = j;
                    }
                }
                return min_val;
            }
        }