Problem implementing alpha-beta pruning for chess engine

215 views Asked by At

I have been working on a chess engine recently, and I am ready to implement some kind of AI to actually play the game (search positions). I have written an alpha-beta pruning algorithm, but it doesn't return the best moves when i test it.

The code for the alpha-beta search is:

float Search::alphabeta(S_BOARD* pos, S_SEARCHINFO *info, int depth, float alpha, float beta){
if (depth == 0) {
    info->nodes++;
    return eval::staticEval(pos);
}

info->nodes++;

S_MOVELIST list;
MoveGeneration::validMoves(pos, list);

float value = 0;
S_MOVE bestMove;
bestMove.move = NOMOVE;
bestMove.score = 0;

float prevBound = (pos->whitesMove == WHITE) ? alpha : beta;

int pvMove = TT::probeMove(pos);
if (pvMove != NOMOVE) {
    for (int i = 0; i < list.count; i++) {
        if (list.moves[i].move == pvMove) {
            list.moves[i].score = 20000000;
            break;
        }
    }
}


if (pos->whitesMove == WHITE) {
    value = -INFINITE;
    for (int moveNum = 0; moveNum < list.count; moveNum++) {
        pickNextMove(moveNum, &list);
        MoveGeneration::makeMove(*pos, list.moves[moveNum].move);

        value = max(value, alphabeta(pos, info, depth - 1, alpha, beta));

        MoveGeneration::undoMove(*pos);

        if (value > alpha) {
            if (value >= beta) {
                if (moveNum == 0) {
                    info->fhf++;
                }
                info->fh++;
                break;
            }
            alpha = value;
            bestMove = list.moves[moveNum];

        }

    }

    if (pos->is_checkmate) {
        return -MATE + pos->ply;
    }
    else if (pos->is_stalemate) {
        return 0;
    }

    if (alpha != prevBound) {
        TT::storePvMove(pos, bestMove);
    }

    return value;
}

else {
    value = INFINITE;

    for (int moveNum = 0; moveNum < list.count; moveNum++) {
        pickNextMove(moveNum, &list);
        MoveGeneration::makeMove(*pos, list.moves[moveNum].move);

        value = min(value, alphabeta(pos, info, depth - 1, alpha, beta));

        MoveGeneration::undoMove(*pos);

        if (value < beta){
            if (beta <= alpha) {
                if (moveNum == 0) {
                    info->fhf++;
                }
                info->fh++;
                break;
            }
            beta = value;
            bestMove = list.moves[moveNum];
        }


    }

    if (pos->is_checkmate) {
        return MATE - pos->ply;
    }
    else if (pos->is_stalemate) {
        return 0;
    }

    if (beta != prevBound) {
        TT::storePvMove(pos, bestMove);
    }

    return value;
}

(MoveGeneration is a namespace, so it isn't a problem with calling functions outside of object instances.)

I run the function inside an iterative deepening function, which is as follows:

float Search::searchPosition(S_BOARD* pos, S_SEARCHINFO *info){

clearForSearch(pos, info);
float score = -INFINITE;
int bestMove = NOMOVE;
int pvMoves = 0;

// Iterative deepening.
for (int currDepth = 1; currDepth <= info->depth; currDepth++){
    auto start = std::chrono::high_resolution_clock::now();
    score = alphabeta(pos, info, currDepth, -INFINITE, INFINITE);
    auto end = std::chrono::high_resolution_clock::now();
    pvMoves = TT::getPvLine(pos, currDepth);
    bestMove = pos->pvArray[0];
    
    std::chrono::duration<double> elapsed = end - start;
    std::cout << "[+] Depth: " << currDepth << " score: " << score << " move: " << printMove(bestMove)
    << " nodes: " << info->nodes << " kN/s: " << (info->nodes/elapsed.count())/1000 << std::endl;
    
    std::cout << "pv";
    for (int i = 0; i < pvMoves; i++){
        std::cout << " " << printMove(pos->pvArray[i]);
    }
    std::cout << std::endl;
    
    std::cout << "Ordering: " << info->fhf/info->fh << std::endl;
    
}


return score;}

Can anyone please help me by pointing out potential errors i have made?

Thank you for your help, and just tell me if i need to upload more of my code.

1

There are 1 answers

0
eligolf On

it doesn't return a move since you are only returning the value at the bottom of your alpha/beta function. In the original call, put this:

move, score = alphabeta(pos, info, currDepth, -INFINITE, INFINITE);

In your depth == 0, checkmate and stalemate you return:

return None, eval

At the end of your two player functions (minimizing and maximizing player) you return:

return move, value

Finally, when you make the recursive calls from your two player functions, you need to only obtain the value. I am not sure about your programming language, but in e.g. Python you put a [1] at the end to just get the value and not the move, something like this:

value = max(value, alphabeta(pos, info, depth - 1, alpha, beta))[1]