Click to See Complete Forum and Search --> : Alpha-beta pruning


jchoi1009
February 3rd, 2009, 03:15 PM
Hi,

I am having trouble implementing alpha-beta pruning algorithm for an AI. I have followed various pseudo codes available but none worked, so I assume I am doing something wrong implementing it.

Here is a simplified version of my code:

int AlphaBeta(char *successor, int depth, bool max, int alpha, int beta){
int val = heuristic_function(successor);
if (terminal_node(val))
return val;

char **successors = generate_successors(successor);
if (max) val = INT_MIN;
else val = INT_MAX;
for (n = 0 ; n < num_successors ; n++){
int tmp = AlphaBeta(successors[n], depth-1, !max, alpha, beta);
if (max){
val = MAX(val, tmp);
if (val >= beta)
return val;
alpha = MAX(alpha, val);
}
else{
val = MIN(val, tmp);
if (val <= alpha)
return val;
beta = MIN(beta, val);
}
}
return val;
}

max is a boolean variable which is true when it is Max's turn, and false when it is Min's turn. The function is initially called with max=true.

I know other parts of my program are correctly written because it works great with Minimax algorithm. But with Alpha-beta pruning, it seems that branches that shouldn't be pruned ARE pruned, thus the AI agent becoming incredibly stupid.

Could anybody help me with this?
Thanks alot!

ahoodin
February 3rd, 2009, 05:00 PM
int AlphaBeta(char *successor, int depth, bool man, int alpha, int beta){

should bool man be bool max?

jchoi1009
February 3rd, 2009, 05:16 PM
Yes, that's a typo, properly written in the actual code.