Advertisement

Help with AB code

Started by February 22, 2006 04:46 PM
0 comments, last by Nokturnal 18 years, 8 months ago
Hello, i have great problems with my source code to build an really correct alpha beta algorithm. Here is the alphabeta algorithm.

int alphabeta(int depth, int alpha, int beta, gameboard,player)
{

        if(player == black)
                curPlayer = blackplayer;
        else curPlayer = whiteplayer;

        if(depth == 0) return evalfunc();

        generatePossibleMoves(possible_moves);
        for (i = 0; i<possible_moves.size();i++)
        {
                if(timeleft() <= 0 || gameIsOver() != 0) break;
                applyMove(possible_moves);
                value = -alphabeta(depth-1,-beta,-alpha, gameboard, player);
                myGame.undoMove(possible_moves);
                if(value >= beta) return value;
                if(value > alpha)
                {
                        bestMoveTmp = i;
                        alpha = value;
                }
                if (globalDepth == depth) bestMove = bestMoveTmp;
        }
        return alpha;
}
bestMove is a global variable which stores the index within the possible_moves and reads it out in another function where the move is done. I don't know if the algorithm work correct because i switch the players in the first part. Is this necessary ? How does a AB Algorithm work if each time the player has to be switched because of the MIN/MAX theory? It is necessary because in one depth i have to compute my own moves and in the next depth the moves from the opponent.... Thanks a lot
Hi , looks like you are using negamax variation of minimax.Here is a small pseudo code for minimax with pruning

Minimax(in game board, in int depth, in int max_depth, out score chosen_score, out score chosen_move ,int turn,int beta, int alpha)begin     if (depth = max_depth) or (generate_moves() =0)then        chosen_score = evaluation(board);          return chosen_score; 	    else         moves_list = generate_moves(board);            for (i = 1 to moves_list.length) do                best_score = default;                new_board = board;                apply_move(new_board, moves_list);	the_score=Minimax(new_board, depth+1, max_depth, chosen_score, chosen_move ,-turn,int beta, int alpha)       		if(alphas_turn)		best_score = the_score;	 	if(best_score &gt; alpha)			if( best_score &lt; beta)				assign best move ,best score & alpha;	endif	if(betas_turn)		best_score = the_score;	 	if(best_score &lt; beta)			if( best_score &gt;alpha)				assign best move , best score & beta ;	endif	             endfor	      endifreturn chosen_scoreend



and here is pure minimax

Minimax(in game board, in int depth, in int max_depth, out score chosen_score, out score chosen_move ,int turn)begin     if (depth = max_depth) or (generate_moves() =0)then        chosen_score = evaluation(board);       return chosen_score;    else         moves_list = generate_moves(board);            for (i = 1 to moves_list.length) do                best_score =default;                new_board = board;                apply_move(new_board, moves_list);    the_score =   minimax(new_board, depth+1, max_depth, the_score, the_move,-turn)	if (turn = max)	if (the_score &gt; best_score) 		best_score = the_score		best_move=the_move	endif	endif	if(the_score &lt; best_score)		best_score = the_score		best_move=the_move	               	 endif		endif	endfor  chosen_score = best_score;            chosen_move = best_move;return chosen_scoreend


and yes , switching players is necessary as based on the current players perspective , highest of all the children (MAX) or lowest of all the children (MIN) will be chosen.
"I think there is a world market for maybe five computers." -- Thomas Watson, Chairman of IBM, 1943

This topic is closed to new replies.

Advertisement