Question

I have followed the pseducode on the wikipedia article, and I think I got it working. However, it returns the score, and that doesn't exactly help when I want to know what move I want to make.

I tried what I think would be a way to get the best move, but I don't think it is working as when I actually try to play against it (chess), the AI makes somewhat retarded moves with a depth level of 3.

Here is my function:

public static function alphaBeta(node, depth, alph, beta, team, tellTheMove:Boolean = false):* {
        var pointer:ChessMove;
        if (depth == 0) {
            return scoreOf(node);
        }
        var childrenOf:Vector.<ChessMove >  = returnPossibleMoves(node,team);
        if (childrenOf.length == 0) {
            return scoreOf(node);
        }
        if (team == 0) {
            for (var i in childrenOf) {
                var that:Number = alphaBeta(childrenOf[i],depth - 1,alph,beta,1);
                if(tellTheMove){
                }
                if (that > alph) {
                    alph = that;
                    if(tellTheMove){
                        pointer = childrenOf[i];
                    }
                }
                if (beta <= alph) {
                    break;
                }
            }
            if(tellTheMove){
                return pointer; //Returns the move that's score last exceeded alpha.
            }
            return alph;
        } else {
            for (var j in childrenOf) {
                var that2:Number = alphaBeta(childrenOf[j],depth - 1,alph,beta,0);
                if (that2 < beta) {
                    beta = that2;
                }
                if (beta <= alph) {
                    break;
                }
            }
            return beta;
        }
    }
Was it helpful?

Solution

Depth 3 is very little for a problem like chess. At this depth most of the power depends on your final evaluation function. This evaluation function is very hard to do in way that it can predict the value of the board efficiently.

Try something simpler, which can be solved efficiently at a lower depth. Tic-Tac-Toe is a very good game for a first attempt at Min-Max. This is because the final outcome is well known. If you get your algorithm correctly you should not be able to beat it at all. If you do Tic-Tac-Toe and the algorithm is loosing, you know that you have a mistake.

Also note that in some cases Min-Max plays optimal, but still will look retarded to a human opponent. For example if there is no chance at winning, Min-Max will start to play randomly and do very dumb moves. This is the case , because Min-Max expects the opponent to also play perfect, which is usually not the case with humans. There are some simple changes that can be done to the algorithm to change this behavior and have min-max play "less retarded" in such cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top