1

I'm using the wikipedia pseudocode to base against -

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 β

and here is my java implementation -

private int alphabeta(Node newNode, int depth, int alpha, int beta, boolean Player) {
    Integer[] children;
    if(depth == 0 || newNode.allNodesFull()){
        return (newNode.blacknodes() - newNode.whitenodes());
    }
    if(Player == false){
        children = newNode.findMovesBlack();
        Arrays.sort(children);
        for(Integer child: children){
            nodesGenerated ++;
            alpha = Math.max(alpha, alphabeta(new Node(newNode.move(child), true),
                            depth - 1, alpha, beta, !Player));
            if(beta <= alpha)
                break;
        }return alpha;
    }else{
        children = newNode.findMovesWhite();
        Arrays.sort(children);
        for(Integer child: children){
            nodesGenerated ++;
            beta  = Math.min(beta, alphabeta(new Node(newNode.move(child), false), 
                            depth - 1, alpha, beta, !Player));
            if(beta <= alpha)
                break;
        }return beta;
    }
} 

after some revisions to my code there is no longer a problem with it returning early, but I do have a problem with alpha and beta never changing

I'll give an explanation of what happens, assume they work

findMovesBlack() and findMovesWhite() both return Integer[] arrays that have the possible positions that either player can move regardless of whos turn it is. For the initial position of Reversi, findMovesBlack() would return [19, 26, 37, 44]

allNodesFull() returns a boolean if findMovesBlack() and findMovesWhite() both have lengths of 0.

blacknodes() and whitenodes() return the amount of black or white nodes respectively.

Node.move(int coordinate) returns a String[] array which contains the new position that has been flipped and placed. trust me, it works correctly.

a Node(String[] gameboard, boolean player-to-move) just sets up a new position with the parameters we've found.

I believe that's all you need to see. I've ironed all the kinks out of the back-end.

Sam P
  • 453
  • 6
  • 19
  • 1
    We'd need the `Node` implementation to tell what's going on. The algorithm itself seems fine but what do `allNodesFull()`, `findMovesBlack()`, `move()` etc. do? And what are the parameters for the initial call? – Thomas Apr 19 '12 at 14:05
  • I have added my Node class. I hope you can help me – Sam P Apr 19 '12 at 14:55
  • Also the initial paramaters are as such alphabeta(Node containing the opening position for reversi, 12, 100, -100, false) – Sam P Apr 19 '12 at 15:00
  • Hmm, that's a whole lot of code. Did you try and debug it yourself btw? Just hop through each line and check whether it produces the expected results or nor. You might start at the location of the error, identify what is wrong and then work backwards (stopping earlier in the call hierarchy) until you find the spot where it starts to go wrong. – Thomas Apr 19 '12 at 15:04
  • So complicated? Maybe it is a time to check your algorithm? – AlexTheo Apr 19 '12 at 15:06
  • I've been debugging - don't worry about the Node class, it works as intended. I just need help with the alpha-beta part. – Sam P Apr 19 '12 at 15:10

1 Answers1

0

The answer was in the implementation of the beta and alpha values. I had to mess with the positions relative to the = sign a lot.

Sam P
  • 453
  • 6
  • 19
  • Sorry for the interruption, but I am using the same algorithm from WikiPedia and also have problems. The MinMax works perfectly but alpha-beta keeps making some dummy mistakes.Can you share your successful code ?? – l.vasilev Jun 17 '16 at 17:46