A simple AI for Tic-Tac-Toe game This is an AI class that takes an array of the board as an argument, and plays the best move after evaluating the board and gets a win rate. I haven't used any java package to do this. How can I make a genetic tree and calculate all possibilities for a given situation

public class AI
{
    int turn,ID;
    AI(int pID)
    {
        ID=pID;
        turn=0;
    }

    int getWinRate(int t[])
    { 
        int comb[][] = {{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}} ;
        int AIwin,oppWin,empty,j,d;
        int WinRate = 0;
        for(int i=0;i&lt;8;i++)  //Count AI win combinations
        {
            AIwin=oppWin=empty=0;
            for(j=0;j&lt;3;j++)
            {
                d= comb[i][j]-1;
                 if(t[d]==2)
                 AIwin++;
                 if(t[d]==1)
                 oppWin++;
                 if(t[d]==0)
                 empty++;
            }
            //Changing win rate according to win conditions
            if(AIwin==3)        
            WinRate+=100;
            if(AIwin==2 &amp;&amp; empty==1)
            WinRate+=3;
            if(oppWin==2 &amp;&amp; empty==1)
            WinRate-=10;
        }
        return WinRate;
    } 
    void play(int t1[])
    {
        int t[]=t1.clone();
        int i,j,c=0;
        for(i=0;i&lt;9;i++){
            if(t[i]==0)
            c++;
        }
        int[] moves=new int[c];
        c=0;
        for(i=0;i&lt;9;i++)
        {
            if(t[i]==0)
           { moves[c++]=i;}
        }
        int rate,boardcopy[]=t.clone();boardcopy[moves[0]]=2;
        int bestMove=moves[0],maxRate=getWinRate(boardcopy);
        for(i=0;i&lt;moves.length;i++)
        {
            boardcopy=t.clone();
            boardcopy[moves[i]]=2;
            rate=getWinRate(boardcopy);
            if(rate&gt;maxRate)
            {
                maxRate=rate;
                bestMove=moves[i];
            }
        }
        System.out.println("The Mighty AI plays : "+(bestMove+1));
        turn++;
        t1[bestMove]=2;
    }
}
Here is some advice for your code.

General recommendations

1) For readability, I suggest that you change the way that you create your arrays; since the "C-style" array declaration is harder to read and less used in Java, in my opinion.

Before

int y[] = {};


After

int[] y = {};


2) In my opinion, I suggest that you wrap the logic of the condition / loop with braces, even if there's only one line of code; this provide consistency and prevent probable bugs if you want to add code to the condition and forget to add the braces.

Before

if (t[i] == 0)
  c++;


After

if (t[i] == 0) {
  c++;
}


getWinRate method

1)  Since the comb is always the same, I sugest that you extract it in a constant.

public class AI {
   //[...]
   private static final int[][] COMB = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, {1, 5, 9}, {3, 5, 7}};
   //[...]
}


2) Instead of using a series of if on a value that can have only one of the values, I suggest that you use the if-else or a switch statement and extract the evaluation of t[d] in a variable.

Before

if (t[d] == 2)
   AIwin++;
if (t[d] == 1)
   oppWin++;
if (t[d] == 0)
   empty++;


if-else

int currentValue = t[d];
if (currentValue == 2)
   AIwin++;
else if (currentValue == 1)
   oppWin++;
else //Zero
   empty++;


or

switch

switch (t[d]) {
   case 2:
      AIwin++;
      break;
   case 1:
      oppWin++;
      break;
   default: //Zero
      empty++;
      break;
}


play method

1) In my opinion, instead of cloning an array, you can use java.util.Arrays#copyOf(int[], int).

   int[] t = Arrays.copyOf(t1, t1.length);


2) The j variable is unused.

3) I suggest that you use better names for your variable, it makes the code harder to read.

4) In my opinion, you can extract some of the logic into sub-methods.

a) Create a method to calculate the number of available moves.

private int calculateNumberOfEmptySpace(int[] game) {
     int nb = 0;

     for (int i = 0; i &lt; 9; i++) {
         if (game[i] == 0) {
             nb++;
         }
     }

     return nb;
 }
 //[...]
void play(int[] t1) {
   int[] copyOfTheGame = Arrays.copyOf(t1, t1.length);
   int numberOfPossibleMoves = calculateNumberOfEmptySpace(t1); 
}


5) The index in the last loop is not used, you can use a for-each

Before

for (i = 0; i &lt; moves.length; i++) {
   boardcopy = t.clone();
   boardcopy[moves[i]] = 2;
   rate = getWinRate(boardcopy);
   if (rate &gt; maxRate) {
     maxRate = rate;
     bestMove = moves[i];
  }
}


After

for (int move : moves) {
   boardcopy = t.clone();
   boardcopy[move] = 2;
   rate = getWinRate(boardcopy);
   if (rate &gt; maxRate) {
     maxRate = rate;
     bestMove = move;
  }
}