title: Connect 4 Minimax Algorithm (with Alpha-Beta Pruning) in Java
body: <p>I'm trying to write a connect four game with AI using the minimax algorithm and alpha-beta pruning. I know that the heuristic works OK (at the very least it scores 4 in the row very highly), but for some reason my minimax isn't working. </p>
  
  <p>It seems like the problem is that the current score and the minimax score are always equal, when this obviously should not be the case. </p>
  
  <p>The computer frequently makes stupid decisions like going to a random spot when the human is about to get 4 in a row.</p>
  
  <p>Any and all help would be greatly appreciate.</p>
  
  <pre><code>public class AI {
  
  
  public Move minimax(Board b, int depth, int alpha, int beta, String player, Move move) {
  
      if(depth == 0 || b.boardScore &gt;= 10000) {
          return move;
      }
  
      else if(player.equals("computer")) {
  
          Move maxMove = new Move(100,100);
          maxMove.moveScore = -10000000;
  
          //Do this for every possible column
          for(int x = 0; x &lt; 7; x++) {
  
              //If column is filled, move on.
              while(b.findYSpot(x) == -1) {
                  x++;
                  if(x &gt; 6) {
                      x = 0;
                  }
              }
  
              Move m = new Move(b.findYSpot(x), x); // Create the move
  
              b.putPiece(player, m.xMove); // Put the piece down
              m.moveScore = b.boardScore;//Set the score the move
  
              Move miniMaxMove = minimax(b, depth-1, alpha, beta, "human", m);
  
              // Maximize the score
              if (m.moveScore &gt;= miniMaxMove.moveScore) {
                  if(m.moveScore &gt;= maxMove.moveScore) {
                      maxMove = m;
                  }
              }
              else {
                  if(miniMaxMove.moveScore &gt;= maxMove.moveScore) {
                      maxMove = miniMaxMove;
                  }
              }
  
              b.removePiece(m); //Remove the piece
  
              //Adjust for alpha-beta pruning
              alpha = Math.max(alpha, maxMove.moveScore);
              if (alpha &gt;= beta) {
                  break;
              }
  
          }           
          return maxMove;
      }
  
      else {
          Move minMove = new Move(100, 100);
          minMove.moveScore = 10000000;
  
          //Do this for every possible column
          for(int x = 0; x &lt; 7; x++) {
  
              // If column is filled, move on
              while(b.findYSpot(x) == -1) {
                  x++;
                  if(x&gt;6) {
                      x = 0;
                  }
              }
  
              Move m = new Move(b.findYSpot(x), x); // Make the move
  
              b.putPiece(player, m.xMove); // Put the piece down
              m.moveScore = b.boardScore; //Set the score the move
  
              Move miniMaxMove = minimax(b, depth-1, alpha, beta, "computer", m);
  
              //Minimize the score
              if (m.moveScore &lt;= miniMaxMove.moveScore) {
                  if(m.moveScore &lt;= minMove.moveScore) {
                      minMove = m;
                  }
              }
  
              else {
                  if(miniMaxMove.moveScore &lt;= minMove.moveScore) {
                      minMove = miniMaxMove;
                  }
              }
  
              b.removePiece(m);
  
              //Adjust for alpha-beta pruning
              beta = Math.min(beta, minMove.moveScore);
  
              if(alpha &gt;= beta) {
                  break;
              }
  
          }
          return minMove;
      }
  }
  }
  </code></pre>