void ChessBoard::makeZobristKey() {
    zobristKey = 0;
    int i = 0;

    for ( u64 c : chessboard ) {
        while ( c ) {
            int position = BITScanForward ( c );
            updateZobristKey ( i, position );
            c &= NOTPOW2[position];
        }

        i++;
    }

    if ( enpassantPosition != NO_ENPASSANT ) {
        updateZobristKey ( 13, enpassantPosition );
    }

    u64 x2 = rightCastle;

    while ( x2 ) {
        int position = BITScanForward ( x2 );
        updateZobristKey ( 14, position );
        x2 &= NOTPOW2[position];
    }
}
Example #2
0
int GTB::getDtm(const int xside,
                const bool doPrint,
                const _Tchessboard &chessboard,
                const int depth) const {

    const int side = xside ^1;
    const uchar rightCastle = chessboard[RIGHT_CASTLE_IDX];
    unsigned int ws[17];    /* list of squares for white */
    unsigned int bs[17];    /* list of squares for black */
    unsigned char wp[17];    /* what white pieces are on those squares */
    unsigned char bp[17];    /* what black pieces are on those squares */
    unsigned info = tb_UNKNOWN;    /* default, no tbvalue */
    unsigned pliestomate = 0;
    int count = 0;
    //white
    for (int piece = 1; piece < 12; piece += 2) {
        for (u64 b = chessboard[piece]; b; RESET_LSB(b)) {
            int position = BITScanForward(b);
            ws[count] = DECODE_POSITION[position];
            wp[count] = DECODE_PIECE[piece];
            count++;
        }
    }
    ws[count] = tb_NOSQUARE;    /* it marks the end of list */
    wp[count] = tb_NOPIECE;    /* it marks the end of list */
    //black
    count = 0;
    for (int piece = 0; piece < 12; piece += 2) {
        for (u64 b = chessboard[piece]; b; RESET_LSB(b)) {
            int position = BITScanForward(b);
            bs[count] = DECODE_POSITION[position];
            bp[count] = DECODE_PIECE[piece];
            count++;
        }
    }
    bs[count] = tb_NOSQUARE;
    bp[count] = tb_NOPIECE;
    unsigned int tb_castling = 0;
    tb_castling = rightCastle & ChessBoard::RIGHT_QUEEN_CASTLE_WHITE_MASK ? tb_WOOO : 0;
    tb_castling |= rightCastle & ChessBoard::RIGHT_KING_CASTLE_WHITE_MASK ? tb_WOO : 0;
    tb_castling |= rightCastle & ChessBoard::RIGHT_KING_CASTLE_BLACK_MASK ? tb_BOO : 0;
    tb_castling |= rightCastle & ChessBoard::RIGHT_QUEEN_CASTLE_BLACK_MASK ? tb_BOOO : 0;
    int tb_available = 0;
    if (depth > 8) {
        tb_available = tb_probe_hard(side, tb_NOSQUARE, tb_castling, ws, bs, wp, bp, &info, &pliestomate);
    } else if (depth >= probeDepth) {
        tb_available = tb_probe_soft(side, tb_NOSQUARE, tb_castling, ws, bs, wp, bp, &info, &pliestomate);
    }
    return extractDtm(side, doPrint, tb_available, info, pliestomate);
}
int ChessBoard::loadFen ( string fen ) {
    if ( fen.empty() ) { return loadFen(); }

    istringstream iss ( fen );
    string pos, castle, enpassant, side;
    iss >> pos;
    iss >> side;
    iss >> castle;
    iss >> enpassant;
    int ix = 0;
    int s[64];

    for ( unsigned ii = 0; ii < pos.length(); ii++ ) {
        uchar ch = pos.at ( ii );

        if ( ch != '/' ) {
            if ( INV_FEN[ch] != 0xFF ) {
                s[ix++] = INV_FEN[ch];
            }
            else if ( ch > 47 && ch < 58 ) {
                for ( int t = 0; t < ch - 48; t++ ) {
                    s[ix++] = SQUARE_FREE;
                }
            }
            else {
                cout << "Bad FEN position format (" << ( char ) ch << ") " << fen << endl;
                return sideToMove;
            };
        }
    }

    if ( ix != 64 ) {
        cout << "Bad FEN position format " << fen << endl;
        return sideToMove;
    }

    if ( side == "b" ) {
        sideToMove = BLACK;
    }
    else if ( side == "w" ) {
        sideToMove = WHITE;
    }
    else {
        cout << "Bad FEN position format " << fen << endl;
        return sideToMove;
    }

    memset ( chessboard, 0, sizeof ( chessboard ) );

    for ( int i = 0; i < 64; i++ ) {
        int p = s[63 - i];

        if ( p != SQUARE_FREE ) {
            chessboard[p] |= POW2[i];
        }
    };

    rightCastle = 0;

    for ( unsigned e = 0; e < castle.length(); e++ ) {
        switch ( castle.at ( e ) ) {
            case 'K':
                rightCastle |= RIGHT_KING_CASTLE_WHITE_MASK;
                break;

            case 'k':
                rightCastle |= RIGHT_KING_CASTLE_BLACK_MASK;
                break;

            case 'Q':
                rightCastle |= RIGHT_QUEEN_CASTLE_WHITE_MASK;
                break;

            case 'q':
                rightCastle |= RIGHT_QUEEN_CASTLE_BLACK_MASK;
                break;

            default:
                ;
        };
    };

    friendKing[WHITE] = BITScanForward ( chessboard[KING_WHITE] );
    friendKing[BLACK] = BITScanForward ( chessboard[KING_BLACK ] );
    enpassantPosition = NO_ENPASSANT;

    for ( int i = 0; i < 64; i++ ) {
        if ( enpassant == BOARD[i] ) {
            enpassantPosition = i;

            if ( sideToMove ) {
                enpassantPosition -= 8;
            }
            else {
                enpassantPosition += 8;
            }

            break;
        }
    }

    makeZobristKey();
    return sideToMove;
}
Example #4
0
int Endgame::getEndgameValue(const int N_PIECE, const int side) {
    ASSERT(N_PIECE != 999);
    ASSERT_RANGE(side, 0, 1);

    int result = INT_MAX;
    int winnerSide = -1;
    switch (N_PIECE) {
        case 4 :
            if (chessboard[QUEEN_BLACK]) {
                if (chessboard[PAWN_WHITE]) {
                    result = KQKP(WHITE, BITScanForward(chessboard[KING_BLACK]), BITScanForward(chessboard[KING_WHITE]), BITScanForward(chessboard[PAWN_WHITE]));
                    winnerSide = BLACK;
                } else if (chessboard[ROOK_WHITE]) {
                    result = KQKR(BITScanForward(chessboard[KING_BLACK]), BITScanForward(chessboard[KING_WHITE]));
                    winnerSide = BLACK;
                }
            } else if (chessboard[QUEEN_WHITE]) {
                if (chessboard[PAWN_BLACK]) {
                    result = KQKP(BLACK, BITScanForward(chessboard[KING_WHITE]), BITScanForward(chessboard[KING_BLACK]), BITScanForward(chessboard[PAWN_BLACK]));
                    winnerSide = WHITE;
                } else if (chessboard[ROOK_BLACK]) {
                    result = KQKR(BITScanForward(chessboard[KING_WHITE]), BITScanForward(chessboard[KING_BLACK]));
                    winnerSide = WHITE;
                }
            } else if (chessboard[ROOK_BLACK]) {
                if (chessboard[PAWN_WHITE]) {
                    result = KRKP<WHITE>(side == BLACK, BITScanForward(chessboard[KING_BLACK]), BITScanForward(chessboard[KING_WHITE]), BITScanForward(chessboard[ROOK_BLACK]), BITScanForward(chessboard[PAWN_WHITE]));
                    winnerSide = BLACK;
                } else if (chessboard[BISHOP_WHITE]) {
                    result = KRKB(BITScanForward(chessboard[KING_WHITE]));
                    winnerSide = BLACK;
                } else if (chessboard[KNIGHT_WHITE]) {
                    result = KRKN(BITScanForward(chessboard[KING_WHITE]), BITScanForward(chessboard[KNIGHT_WHITE]));
                    winnerSide = BLACK;
                }
            } else if (chessboard[ROOK_WHITE]) {
                if (chessboard[PAWN_BLACK]) {
                    result = KRKP<BLACK>(side == WHITE, BITScanForward(chessboard[KING_WHITE]), BITScanForward(chessboard[KING_BLACK]), BITScanForward(chessboard[ROOK_WHITE]), BITScanForward(chessboard[PAWN_BLACK]));
                    winnerSide = WHITE;
                } else if (chessboard[BISHOP_BLACK]) {
                    result = KRKB(BITScanForward(chessboard[KING_BLACK]));
                    winnerSide = WHITE;
                } else if (chessboard[KNIGHT_BLACK]) {
                    result = KRKN(BITScanForward(chessboard[KING_WHITE]), BITScanForward(chessboard[KNIGHT_BLACK]));
                    winnerSide = WHITE;
                }
            } else if ((chessboard[BISHOP_BLACK] && chessboard[KNIGHT_BLACK])) {
                result = KBNK(BITScanForward(chessboard[KING_BLACK]), BITScanForward(chessboard[KING_WHITE]));
                winnerSide = BLACK;
            } else if (chessboard[BISHOP_WHITE] && chessboard[KNIGHT_WHITE]) {
                result = KBNK(BITScanForward(chessboard[KING_WHITE]), BITScanForward(chessboard[KING_BLACK]));
                winnerSide = WHITE;
            }
            break;
        case 5:
            if (chessboard[KNIGHT_WHITE] && bitCount(chessboard[BISHOP_BLACK]) == 2) {
                result = KBBKN(BITScanForward(chessboard[KING_BLACK]), BITScanForward(chessboard[KING_WHITE]), BITScanForward(chessboard[KNIGHT_WHITE]));
                winnerSide = BLACK;
            } else if (chessboard[KNIGHT_BLACK] && bitCount(chessboard[BISHOP_WHITE]) == 2) {
                result = KBBKN(BITScanForward(chessboard[KING_WHITE]), BITScanForward(chessboard[KING_BLACK]), BITScanForward(chessboard[KNIGHT_BLACK]));
                winnerSide = WHITE;
            }
            break;
        default:
            break;
    }
    if (winnerSide == -1)return INT_MAX;
    return winnerSide == side ? result : -result;
}