-1
public  int minimax(Board b) { //player1 is AI

  //depth+=10;
  System.out.println("Current board is  " );
  System.out.println("To play now on this board is  " + b.whoseTurn.getId() );
  b.display();
  if( (GameController.hasWon(b.whoseTurn,b)) 
    || (GameController.hasWon(b.whoseNotInTurn, b)) 
    || (GameController.isDraw(b.whoseTurn,b,b.whoseNotInTurn))) {           
    return evaluate(b);     
  }
  ArrayList<Integer> possibleMoves = generateMoves(b);

  if(b.whoseTurn.getId() == "AI") {

    bestScore = -1000000;

    for(int i = 0; i < possibleMoves.size(); i++) {
      int move = possibleMoves.get(i);
      b.myBoard.get(move).setSymbol(b.whoseTurn.getSymbol());

      swapPlayers(b);
      score = minimax (b);      

      if(score > bestScore) {

        bestScore = score;
        bestMove = move;
        System.out.println("In maximum Move is " + move + "BESTScore is" + score);

      }
      unmakeMove(b.myBoard.get(move));

    }

    return bestMove;
  }
  else {
    bestScore = 1000000;
    for(int i = 0; i < possibleMoves.size(); i++) {
      int move = possibleMoves.get(i);
      b.myBoard.get(move).setSymbol(b.whoseTurn.getSymbol());

      swapPlayers(b);
      score = minimax(b);

      if(score < bestScore) {
        myMoves.add(move);
        bestScore = score;
        bestMove = move;
        System.out.println("In minimum Move is " + move + "Score is" + score);

      }
    unmakeMove(b.myBoard.get(move));

  }
  return bestMove;          
}

Where am I making a mistake here? Here is a link to the entire code.

rudolph9
  • 8,021
  • 9
  • 50
  • 80
Abhijay
  • 191
  • 4
  • 18
  • 1
    What does it do that it shouldn't? Code dumps aren't the best approach to SO questions. – Dave Newton Apr 25 '14 at 14:41
  • What is wrong with this code? – Anubian Noob Apr 25 '14 at 14:45
  • It helps if you isolate the code sample down to just the part that's causing the problem. It also helps if it's something we can just drop and run with out needing other classes. See [SSCCE](http://meta.stackexchange.com/questions/22754/sscce-how-to-provide-examples-for-programming-questions) – crownjewel82 Apr 25 '14 at 14:46
  • `b.whoseTurn.getId() == "AI"` should be `b.whoseTurn.getId().equals("AI")` – tehlexx Apr 25 '14 at 14:48
  • I modified whoseTurn.getId().equals("AI").It worked for some cases. However, the code is not working for all the cases. When am I exactly suppose to swap the players? I am swapping them after I play a move and after I undo it. Thanks for your help! – Abhijay Apr 25 '14 at 15:01
  • Please go through the entire code once. I have posted the link.I am unable to find any mistake! But the output doesn't seem to match. There seems to be a logical flaw somewhere. What am I doing wrong? – Abhijay Apr 25 '14 at 15:16

1 Answers1

1

Let's try this: You return evaluate(b) at the end of the recursion to get the score, that's good. But all the levels of recursion above are going to return bestMove aren't they? So you lose your bestScore in the recursion because you don't propagate it properly to the top.

Victor D.
  • 149
  • 2
  • 9
  • Yes. That's true. What could be a possible way out? I have been breaking me head over this for quite some time now. – Abhijay Apr 25 '14 at 15:06
  • I tried printing bestScore all throughout. That too seems to give a wrong answer. Is there any problem with the logic? Please do let me know. – Abhijay Apr 25 '14 at 15:13
  • I would return bestScore instead of bestMove for now and try to print it like you did to see if it's okay. Fix this and then get the bestMove by setting it in your board object for example, don't return it. – Victor D. Apr 25 '14 at 15:19
  • I tried to figure that out. But no success. Where do you think I could be possibly making a mistake? – Abhijay Apr 25 '14 at 15:29
  • Well I can't really test for you and you should provide more details! What is the variable depth in evaluate? It looks like it doesn't change overtime. – Victor D. Apr 25 '14 at 15:34
  • I am not using that variable. I realised that there is one end position of game for which the program is not evaluating the score. I have pasted the link to the entire code, in case you would like to see what the evaluate function is doing – Abhijay Apr 25 '14 at 15:38
  • I know, I have looked at it. Your evaluate function will return 100 or -100 depending on the player and nothing else. You should take a look at that, I can't help you much more than I already did, sorry :) Don't forget to accept the answer if it helped though! – Victor D. Apr 25 '14 at 15:41