+void
+build_BPFEN_string(const Board *board,char **bpfen, int* bpfen_len)
+{ static int bits[256]; /* Max six bits per piece, 32 blanks, and then some. */
+ int bit_pos = 0;
+ Rank rank;
+ int ix = 0;
+ for(rank = LASTRANK; rank >= FIRSTRANK; rank--){
+ Col col;
+ for(col = FIRSTCOL; col <= LASTCOL; col++){
+ int coloured_piece = board->board[RankConvert(rank)]
+ [ColConvert(col)];
+ if(coloured_piece == EMPTY){
+ bits[bit_pos++] = 0;
+ continue;
+ }
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = (EXTRACT_COLOUR(coloured_piece) == WHITE) ? 1 : 0;
+ switch(EXTRACT_PIECE(coloured_piece)) {
+ case PAWN:
+ bits[bit_pos++] = 0;
+ break;
+ case KNIGHT:
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 0;
+ bits[bit_pos++] = 0;
+ break;
+ case BISHOP:
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 0;
+ bits[bit_pos++] = 1;
+ break;
+ case ROOK:
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 0;
+ break;
+ case QUEEN:
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 0;
+ break;
+ case KING:
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = 1;
+ break;
+ }
+ }
+ }
+ /* Pad board. */
+ while ((bit_pos % 8) != 0) {
+ bits[bit_pos++] = 0;
+ }
+
+ /* Non-board information. */
+ bits[bit_pos++] = (board->to_move == WHITE) ? 0 : 1;
+ bits[bit_pos++] = board->WKingCastle ? 1 : 0;
+ bits[bit_pos++] = board->WQueenCastle ? 1 : 0;
+ bits[bit_pos++] = board->BKingCastle ? 1 : 0;
+ bits[bit_pos++] = board->BQueenCastle ? 1 : 0;
+
+ if(board->EnPassant){
+ int col_index = board->ep_col - COLBASE;
+ bits[bit_pos++] = 1;
+ bits[bit_pos++] = (col_index >> 2) & 1;
+ bits[bit_pos++] = (col_index >> 1) & 1;
+ bits[bit_pos++] = col_index & 1;
+ }
+ else{
+ bits[bit_pos++] = 0;
+ }
+
+ /* Pad non-board. */
+ while ((bit_pos % 8) != 0) {
+ bits[bit_pos++] = 0;
+ }
+
+ /* Convert from bits to binary form. */
+ *bpfen = (char *)malloc(bit_pos / 8);
+ *bpfen_len = bit_pos / 8;
+
+ for (ix = 0; ix < bit_pos / 8; ++ix) {
+ unsigned char ch = 0;
+ int jx;
+
+ for (jx = 0; jx < 8; ++jx) {
+ ch |= (bits[ix * 8 + jx] << jx);
+ }
+
+ (*bpfen)[ix] = ch;
+ }
+}
+