1

I am currently writing a chess engine and have progressed decently far, I have however run into a problem and would like a few opinions on the manner. Ok, my problem is the fact that my chess AI doesnt make the "best" move, It seems to fail to see simple things like the fact that its piece may be taken back or something. My alpha beta prunning goes as follows.

int Search (TREE *tree, int ply, int wtm, int alpha, int beta) { 
    if (0 == ply) { 
        return quiesce(tree, ply, wtm, alpha, beta); 
    } 

    movePointer = GenCaptures(tree, MAXPLY, wtm, moves); 
    movePointer = GenNonCaptures(tree, wtm, movePointer); 
    for (move = &moves[0]; move < movePointer; move++) { 
        MakeMove(tree, &tree->movePath[ply], wtm); 
        score = -Search(tree, ply - 1, Flip(wtm), -beta, -alpha); 
        UnmakeMove(tree, &tree->movePath[ply], wtm); 
        tree->movePath[ply].move = 0; 
        if (score >= beta) { 
            return beta; 
         } 
         if (score > alpha) { 
             alpha = score; 
         } 
    } 

I think my alpha beta pruning works well enough because it does return reasonable moves, I think the problem is when I try to obtain my rootMove. Im trying to get the root move by (

int searchRoot( TREE *tree, int ply, int wtm ) { 
    //int depth = 1; 
    int moves[220]; 
    int *movePointer = 0; 
    int *move = 0;
    int rootAlpha = -MATE - 1; 
    int rootValue = -MATE - 1; 
    int rootBeta = MATE + 1; 
    MOVE currentMove; 
    currentMove.move = 0; 
    currentMove.capture = 0; 
    movePointer = GenCaptures( tree, MAXPLY, wtm, moves ); 
    movePointer = GenNonCaptures( tree, wtm, movePointer ); 
    for ( move = &moves[0]; move < movePointer; move++ ) { 
        currentMove.move = *move; 
        tree->movePath[MAXPLY] = currentMove; 
        MakeMove( tree, &currentMove, wtm ); 
        int lastValue = -Search( tree, MAXPLY -1, Flip(wtm), rootAlpha, rootBeta ); 
        UnmakeMove( tree, &currentMove, wtm ); 
        if ( lastValue > rootValue ) { 
            tree->rootMove = *move; rootValue = lastValue; 
        } 
    } 
} 

Any ideas would be helpful, thank you.

Mayur Birari
  • 5,837
  • 8
  • 34
  • 61
Ben Purdy
  • 45
  • 1
  • 7
  • How deep do you get - what is MAXPLY? A shallow search just doesn't play very well. – Bo Persson Apr 24 '12 at 17:23
  • Normally you'd want to handle `alpha` and `beta` the same way in the root node as in other nodes. Here you're not updating alpha in the root node, which will make your search slower (it won't change the results though). – interjay Apr 24 '12 at 17:27
  • 2
    You should ask this question on a specialist forum like TalkChess where it might draw the attention of Bob Hyatt or equivalent: http://www.talkchess.com/forum/index.php – trojanfoe May 01 '12 at 06:32

1 Answers1

0

Your searchRoot does not correctly implement the negamax idea. Your line

int lastValue = -Search( tree, MAXPLY -1, Flip(wtm), rootAlpha, rootBeta );

should read

int lastValue = -Search( tree, MAXPLY -1, Flip(wtm), -rootBeta, -rootAlpha ); 
TemplateRex
  • 69,038
  • 19
  • 164
  • 304