user2275785
user2275785

Reputation: 199

MinMax AI for Checkers

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!

Upvotes: 0

Views: 3248

Answers (2)

Marc-Andre
Marc-Andre

Reputation: 912

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.

Upvotes: 0

durron597
durron597

Reputation: 32343

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.

Upvotes: 0

Related Questions