Results 1 to 2 of 2
  1. #1
    shahrukh1 is offline Member
    Join Date
    Apr 2012
    Posts
    7
    Rep Power
    0

    Default 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
    Java 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

    Java 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

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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?

Similar Threads

  1. Pruning?? (N-Queens problem)
    By n00neimp0rtant in forum New To Java
    Replies: 1
    Last Post: 02-14-2010, 06:41 AM
  2. minmax with Alpha - Beta Pruning
    By Zosden in forum Advanced Java
    Replies: 6
    Last Post: 05-02-2008, 08:40 AM
  3. Alpha-beta algorithm, game tree
    By ljaf_1985 in forum New To Java
    Replies: 0
    Last Post: 03-28-2008, 04:21 PM
  4. SableCC 4-alpha.3
    By levent in forum Java Software
    Replies: 0
    Last Post: 07-26-2007, 07:59 PM
  5. MegaBlock 0.0.1 alpha
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-14-2007, 08:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •