DEV Community

Cover image for Chess Engine in C #5: Assertions
Victor Chigbo
Victor Chigbo

Posted on

Chess Engine in C #5: Assertions

In the last article, I talked about converting FEN strings to board positions. Today, I will talk about assertions.

The assertions are used in the CheckBoard function. Here is the function below:

int CheckBoard(const S_BOARD *pos) {
    int temp_pieceNum[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int temp_bigPieces[2] = {0, 0};
    int temp_majorPieces[2] = {0, 0};
    int temp_minorPieces[2] = {0, 0};
    int temp_material[2] = {0, 0};

    int sq64, temp_piece, temp_piece_num, sq120, colour, piece_count;

    U64 temp_pawns[3] = {0ULL, 0ULL, 0ULL};

    temp_pawns[WHITE] = pos->pawns[WHITE];
    temp_pawns[BLACK] = pos->pawns[BLACK];
    temp_pawns[BOTH] = pos->pawns[BOTH];

    // check piece lists
    for (temp_piece = wP; temp_piece <= bK; temp_piece++) {
        for (temp_piece_num = 0; temp_piece_num < pos->pieceNum[temp_piece]; temp_piece_num++) {
            sq120 = pos->pieceList[temp_piece][temp_piece_num];
            ASSERT(pos->pieces[sq120] == temp_piece);
        }
    }

    // check piece counts and other counters
    for (sq64 = 0; sq64 < 64; sq64++) {
        sq120 = SQ120(sq64);
        temp_piece = pos->pieces[sq120];
        temp_pieceNum[temp_piece]++;
        colour = PieceColour[temp_piece];

        if (PieceBig[temp_piece] == TRUE) {
            temp_bigPieces[colour]++;
        }

        if (PieceMinor[temp_piece] == TRUE) {
            temp_minorPieces[colour]++;
        }

        if (PieceMajor[temp_piece] == TRUE) {
            temp_majorPieces[colour]++;
        }

        temp_material[colour] += PieceValue[temp_piece];
    }

    for (temp_piece = wP; temp_piece <= bK; temp_piece++) {
        ASSERT(temp_pieceNum[temp_piece] == pos->pieceNum[temp_piece]);
    }

    // check bitboards count
    piece_count = COUNT(temp_pawns[WHITE]);
    ASSERT(piece_count == pos->pieceNum[wP]);
    piece_count = COUNT(temp_pawns[BLACK]);
    ASSERT(piece_count == pos->pieceNum[bP]);
    piece_count = COUNT(temp_pawns[BOTH]);
    ASSERT(piece_count == pos->pieceNum[bP] + pos->pieceNum[wP]);

    // check bitboards squares
    while (temp_pawns[WHITE]) {
        sq64 = POP(&temp_pawns[WHITE]);
        ASSERT(pos->pieces[SQ120(sq64)] == wP);
    }

    while (temp_pawns[BLACK]) {
        sq64 = POP(&temp_pawns[BLACK]);
        ASSERT(pos->pieces[SQ120(sq64)] == bP);
    }

    while (temp_pawns[BOTH]) {
        sq64 = POP(&temp_pawns[BOTH]);
        ASSERT(pos->pieces[SQ120(sq64)] == wP || pos->pieces[SQ120(sq64)] == bP);
    }

    // sanity checks
    ASSERT(temp_material[WHITE] == pos->material[WHITE] && temp_material[BLACK] == pos->material[BLACK]);
    ASSERT(temp_bigPieces[WHITE] == pos->bigPieces[WHITE] && temp_bigPieces[BLACK] == pos->bigPieces[BLACK]);
    ASSERT(temp_majorPieces[WHITE] == pos->majorPieces[WHITE] && temp_majorPieces[BLACK] == pos->majorPieces[BLACK]);
    ASSERT(temp_minorPieces[WHITE] == pos->minorPieces[WHITE] && temp_minorPieces[BLACK] == pos->minorPieces[BLACK]);

    ASSERT(pos->side == WHITE || pos->side == BLACK);
    ASSERT(GeneratePosKey(pos) == pos->posKey);

    ASSERT(pos->enPassant == NO_SQ || (RanksBoard[pos->enPassant] == RANK_6 && pos->side == WHITE) || (RanksBoard[pos->enPassant] == RANK_3 && pos->side == BLACK));

    ASSERT(pos->pieces[pos->KingSquare[WHITE]] == wK);
    ASSERT(pos->pieces[pos->KingSquare[BLACK]] == bK);

    return TRUE;
}
Enter fullscreen mode Exit fullscreen mode

If you haven't read the previous article, click here so that you can follow along and if you want to see the code, you can click here to view the Github repo.

Until the next commit....bye 👋

Top comments (0)