title: minimax algorithm to find bestmove for tictactoe
body: <p>I'm trying to create a TicTacToe game in android using the minimax Algorithm to find the best possible move for the computer. But it returns the next available spot on the board every time. I tried searching for tutorials and solutions for this problem but still no answer.</p>
  
  <p>This is my code, what I tried.</p>
  
  <pre><code>private void compMove() {
      final String pos =bestMove();
  
      matrix[turn][turn2] = 'O';
      vars.get(pos).setImageDrawable(getDrawable(R.drawable.zero)); //put O in position
      whosmov="pm";
      getWinner(checkWinner());               //check if O has won
      }
  
      //finding best possible move for computer
      private String bestMove(){
          int bestscore=100,a=0,b=0;
          for(int i=0;i&lt;3;i++){
              for(int j=0;j&lt;3;j++){
                  if(matrix[i][j]==0){
                      matrix[i][j]='O';
                      int score=miniMax(matrix,0,true);
                      matrix[i][j]=0;
                      if(score&lt;bestscore){
                          a=i;
                          b=j;
                          bestscore=score;
                      }
                  }
              }
          }
          turn=a;         //position x for matrix
          turn2=b;        //position y for matrix
          return a+""+b;
      }
  
  private int miniMax(char[][] matrix,int depth,boolean ismax) {
      int result=checkWinner();
  
      if(result!=0){
          return result;
      }
  
      if(ismax){
          int best = -100;
          for(int i=0;i&lt;3;i++){
              for(int j=0;j&lt;3;j++){
                  if(matrix[i][j]==0){
                      matrix[i][j]='X';
                    int score = miniMax(matrix,depth+1, false);
                    best=Math.max(score,best);
                      matrix[i][j]=0;
                  }
              }
          }
          return best;
      }
      else{
          int best = 100;
          for(int i=0;i&lt;3;i++){
              for(int j=0;j&lt;3;j++){
                  if(matrix[i][j]==0){
                      matrix[i][j]='O';
                      int score = miniMax(matrix,depth+1, true);
  
                      best=Math.min(score,best);
                      matrix[i][j]=0;
                  }
              }
          }
          return best;
      }
  }
  
  private int checkWinner() {
          //horizontal check
          for(int i=0;i&lt;3;i++){
              if(matrix[i][0]==matrix[i][1] &amp;&amp; matrix[i][1]==matrix[i][2]){
                  if(matrix[i][0]=='X'){
                      return 1;
                  }
                  if(matrix[i][0]=='O'){
                      return -1;
                  }
              }
          }
          //vertical check
      for(int i=0;i&lt;3;i++){
          if(matrix[0][i]==matrix[1][i] &amp;&amp; matrix[2][i]==matrix[0][i]){
              if(matrix[0][i]=='X'){
                  return 1;
              }
              if(matrix[0][i]=='O'){
                  return -1;
              }
          }
      }
      // Diagonal check
      if(matrix[0][0]==matrix[1][1] &amp;&amp; matrix[1][1]== matrix[2][2]){
          if(matrix[0][0]=='X'){
              return 1;
          }
          if(matrix[0][0]=='O'){
              return -1;
          }
      }
      if(matrix[2][0]==matrix[1][1] &amp;&amp; matrix[1][1]== matrix[0][2]){
          if(matrix[2][0]=='X'){
              return 1;
          }
          if(matrix[2][0]=='O'){
              return -1;
          }
  
      }
  
      return 0;
  
  }
  </code></pre>
  
  <p>I think something is wrong in the minimax function. Please help me.</p>