# Alpha Beta pruning help!

Printable View

• 04-22-2012, 11:46 PM
shahrukh1
Alpha Beta pruning help!
Im writing an engine for a game I'm developing
I'm trying to follow the pseudo code given on Wikipedia
Code:

```function alphabeta(node, depth, α, β, Player)            if  depth = 0 or node is a terminal node         return the heuristic value of node     if  Player = MaxPlayer         for each child of node             α := max(α, alphabeta(child, depth-1, α, β, not(Player) ))                if β ≤ α                 break                            (* Beta cut-off *)         return α     else         for each child of node             β := min(β, alphabeta(child, depth-1, α, β, not(Player) ))                if β ≤ α                 break                            (* Alpha cut-off *)         return β (* Initial call *) alphabeta(origin, depth, -infinity, +infinity, MaxPlayer)```

for some reason my code does not work, so I'm wondering if a implemented the pseudo code into java correctly
the game has only 2 players, and it is a zero sum game

Code:

```private int calculate(Game template, int depth, int alpha, int beta, byte player) {         int score; //i'll refer to this later                 if (depth == 0) {             return Evaluation.evaluate(template, player); //value of the current board         }                 //starts the alpha/beta pruning         if (player == 1) {    //were 1 is player 1 of 2, am i supposed to do that?                         score = alpha;  //used in my own "min/max" function                         Game[] children = pMoves(template);  //this generates the possible moves, and creates the "children" of the first node             for (byte i = 0; i < 9; i++) {  //loops through all the possible moves                                 if (children[i] != null) { //this is just a legal check, u may ignore                                         score = calculate(children[i], depth - 1, alpha, beta, (byte) 2);  //gets the value from the child                                         //this is the equivalent of min/max function?                     if (score > alpha) { //compares child                         alpha = score; //we found a better move                         Xmove = i;  //i have to store the best move somehow, ignore                                             } else if (score < alpha) {                         return score; //cuts off the bad move                     }                 }             }             return alpha; //this is the best move for player 1?         } else {             score = beta;             Game[] Children = pMoves(template);             for (byte i = 0; i < 9; i++) {                 if (Children[i] != null) {                     score = -1 * calculate(Children[i], depth - 1, alpha, beta, (byte) 1);                     if (score < beta) {                         beta = score;                         Ymove = i;                     } else if (score > beta) {                         return score;                      }                 }             }             return beta; //this is the best move for player 2?         }     }```
any clarifications needed, just ask
• 04-23-2012, 12:04 PM
Sierra
Re: Alpha Beta pruning help!
If you do not post what exactly is your problem probably no one will help you... "does not work" is not very accurate - what exactly is the wrong output / behavior?