0

I have created simple Checkers game in Java with minmax AI, but I doesn't work. I use recursive version of minmax, but there must be something wrong with it, because it returns moves, that aren't best, but maybe first generated.

public int minmax(int [][] board, int depth, int curPlayer){
    ArrayList<Move> moves = findMoves(curPlayer, board);

    if (depth == 0 || moves.size() == 0){
        return heurValue(curPlayer, board); 
    }

    int bestVal = 0;
    if (curPlayer == GameCore.BLACK){
        bestVal = Integer.MIN_VALUE;
        curPlayer = GameCore.RED;
    }else{
        bestVal = Integer.MAX_VALUE;
        curPlayer = GameCore.BLACK;
    }

    for(int i = 0; i<moves.size(); i++){
        Move m = moves.get(i);

    int [][] boardNew = makeMove(m, board);

    int value = minmax(boardNew, depth-1, curPlayer);

    board = undoMove(m, boardNew);

    // computer plays as black
    if (curPlayer == GameCore.BLACK){
        if (value < bestVal){
            bestMove = m;
            bestVal = value;
        }
    }else{
        if (value >= bestVal){
            bestMove = m;
            bestVal = value;
        }   
    }
    } 
    return bestVal; 
}

If I call minmax with depth = 1 it should "return 7 values (there are 7 possible moves), but it returns only 1 if I move from 2,4 to 3,3... but when I tried to debug it, ArrayList moves has correct size. So I don't know what is wrong with it. :(

EDIT: By "return" I mistakenly mean that first condition (when depth is 0 or moves are empty) happens only once, but if it was correct it should happen 7 times. Sorry for my bad english.

Do you know some site, where is correct recursive pseudocode for minmax (better with alpha/beta, because I will need to expand it) or could you help me to fix this? It must be only trifle. Thank you!

user2275785
  • 199
  • 1
  • 19
  • I don't if I read your question correctly but `public int minmax(int [][] board, int depth, int curPlayer)` only return one value of the type `int`. If you want to return many value you can return a `Collection` like an `LinkedList` or something similar – Marc-Andre Apr 12 '13 at 20:07
  • Where did you declare boardNew? – Joan Apr 12 '13 at 20:09
  • RED (human) begins game, after his turn (computer plays) minmax is called (for test with depth 1). There are 7 possible moves for computer. MinMax should return 7 values for 7 possible boards, but it won't. Please note, that bestMove is global variable. Sorry for my bad english. – user2275785 Apr 12 '13 at 20:14
  • Sorry it's my fault. I'm translating it from my native language to english and i forget one word. It's here: int [][] boardNew = makeMove(m, board); makeMove returns 2d array with new board config. – user2275785 Apr 12 '13 at 20:16
  • In the code you posted you have: in[][] deskaNew = makeMove – Joan Apr 12 '13 at 20:18
  • Yes, because I have forgotten translate it from my native language. I am sorry about that. – user2275785 Apr 12 '13 at 20:22
  • [minimax-notes](https://www.cs.tcd.ie/Glenn.Strong/3d5/minimax-notes.pdf) this link to documentation of min-max algoristhm – Joan Apr 12 '13 at 20:56
  • @user2275785 I've copy-paste your code and replace method like `makeMove` with dummy return but still, everything look normal to me, so maybe the original code have a problem? – Marc-Andre Apr 12 '13 at 22:56

2 Answers2

0

You've written this to only return the best value, i.e. return bestVal; If you want it to return them all, store them in a List of some sort and change the method signature accordingly.

durron597
  • 31,968
  • 17
  • 99
  • 158
0

EDIT: So it is not a signature problem as I first thought.

I've done a quick search about the MinMax algorythm and this is what i found an article

Quickly what I think is important from this page is :

The values here represents how good a move is. So the MAX player will try to select the move with highest value in the end.

So if I'm rigth, MinMax will only return one move, the one with the bestValue.

Marc-Andre
  • 912
  • 1
  • 15
  • 34
  • No it's caused by my english. I mean this if (depth == 0 || moves.size() == 0){return heurValue(curPlayer, board);} do not occur 7 times. But thank you for your answer anyway. – user2275785 Apr 12 '13 at 20:20
  • No problem. I'm not a native speaker myself so I know how it feels ;) – Marc-Andre Apr 12 '13 at 20:22
  • @user2275785 But correct me if i'm wrong, but returning 1 value is ok, but it should do it 7 times ? – Marc-Andre Apr 12 '13 at 20:23
  • Yes one value 7 time... at least I think it... because there are 7 moves for computer so 7 new boards to evaluate. – user2275785 Apr 12 '13 at 20:33
  • Have you tried to debug it? Cause if i'm rigth, even if `moves` have 7 possible moves it will only return the best value for all the possible moves you have. But i'm not sure cause i don't know anything about the minmax algorythm. – Marc-Andre Apr 12 '13 at 20:37
  • Yes I have tried. It generates 7 moves when it is first called, then in for loop it's called again with new board as param. But now it finds only one move, depth is 0 (1-1) so condition is met and minmax returns value of board and that move. Maybe I do not know how recursion works... I don't get it. – user2275785 Apr 12 '13 at 20:47
  • That should be correct if when minmax returns, it does in the bucle of the caller intance of minmax – Joan Apr 12 '13 at 20:54
  • @user2275785 I've edited my question and found a link with some explanation. But about the "the now it finds only one move" it can be cause by when you re-call the board, the new state has only one move. – Marc-Andre Apr 12 '13 at 20:55
  • I thought that minmax is called 7 times because of `for` loop. Or I don't know. Because when minmax is called from this loop `moves` for new board are generated and it looks like `moves` size is different, so loop is executed different times which cause bad results. (But I have thought that recursion keeps "track" on this and that minmax is called _original_ times (7)). Is there any way how to fix it? – user2275785 Apr 12 '13 at 21:04
  • Yes you are correct in that. (I have posted this comment after yours, so I didn't see it). :) – user2275785 Apr 12 '13 at 21:05
  • Yes it is normal that for each time `minmax` is called `moves` will be different beacause the state of the board is changed so the possible moves has changed too. I think you maybe need to study the concept of recursivity a little bit more, cause what you're describing look like the normal execution of a recursive call. – Marc-Andre Apr 12 '13 at 21:08
  • I thought it evaluates all 7 possible states, but it only evaluate number of states equal to number of new possible moves. In other words: it will make 7 moves to create 7 boards, evaluate them and return the best move (move that create best board). Instead of this it evaluate only 1 board because there is only one possible move. I thought that recursion keeps calling it from original loop (so it's called 6 times after this, generating 6 new boards). But it won't. – user2275785 Apr 12 '13 at 21:19
  • I can' t see what is wrong with your code! I woukd need the code to debug it myself to actually cause rigth now i don't have a clue. Btw, dont you need to change the 'curPlayer' when you call minmax? – Marc-Andre Apr 12 '13 at 21:30