From: H.G. Muller Date: Wed, 27 Nov 2013 21:02:37 +0000 (+0100) Subject: Implement WB setboard command X-Git-Url: http://winboard.nl/cgi-bin?p=bonanza.git;a=commitdiff_plain;h=e6a997890d6b8163efd00f50fc651b4340f1d68f Implement WB setboard command This uses a FEN parser that converts the FEN to the CSA 'SU' position notation, and hands that to Bonana wit a 'new' command. --- diff --git a/proce.c b/proce.c index 6d63541..a17a5de 100644 --- a/proce.c +++ b/proce.c @@ -139,14 +139,12 @@ int myTime, hisTime, movesPerSession, inc, plyNr; char xboard_mode; int root_pos[nsquare]; -void -xboard_to_CSA( tree_t * restrict ptree, char *in, char *out ) +int +bonanza_piece( char p ) { - char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4]; - int piece=0; - if(fromY == '@') { // drop (contains all info needed to convert it) - if(fromX >= 'a') fromX += 'A' - 'a'; - switch(fromX) { // encode piece + int piece = 0; + if(p >= 'a') p += 'A' - 'a'; + switch(p) { // encode piece case 'P': piece = pawn; break; case 'L': piece = lance; break; case 'N': piece = knight; break; @@ -154,7 +152,60 @@ xboard_to_CSA( tree_t * restrict ptree, char *in, char *out ) case 'G': piece = gold; break; case 'B': piece = bishop; break; case 'R': piece = rook; break; + case 'K': piece = king; break; + } + return piece; +} + +void +read_fen( char *p ) +{ + static char fen[128]; + int r, f, gote = 1; + char *q = fen, *start = p; + strncpy( fen, p+9 , 127); + strcpy( p, "new SU" ); p += 6; + for ( r = 1; r <= 9; r++) { + for ( f = 0; *q; q++ ) { + int promoted = 0; + if( *q == '+' ) promoted = promote, q++; + if( isdigit( *q ) ) f += atoi( q ); else + if( isalpha( *q ) ) { + int piece = bonanza_piece( *q ); + if( ( *q >= 'a' && *q <= 'z' ) != gote ) { + gote = !gote; // switch color + *p++ = ( gote ? '-' : '+' ); + } + sprintf( p, "%d%d%s", 9-f, r, astr_table_piece[piece + promoted] ); + p += 4; f++; + } else { q++; break; } } + } + while( q[-1] == ' ' ) q++; + if( q[-1] == '[' ) { // holdings + while( isalpha( *q ) ) { + int piece = bonanza_piece( *q ); + if( ( *q >= 'a' && *q <= 'z' ) != gote ) { + gote = !gote; // switch color + *p++ = ( gote ? '-' : '+' ); + } + sprintf( p, "00%s", astr_table_piece[piece] ); + p += 4; q++; + } + if( *q == '-' ) q++; + if( *q == ']' ) q++; + while( *q == ' ' ) q++; + } else q--; + sprintf(p, " %c", *q == 'w' ? '+' : '-' ); // side to move +} + +void +xboard_to_CSA( tree_t * restrict ptree, char *in, char *out ) +{ + char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4]; + int piece=0; + if(fromY == '@') { // drop (contains all info needed to convert it) + piece = bonanza_piece( fromX ); sprintf(out, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]); } else { // board move (need to figure out moved piece) int from = ('9' - fromY)*9 + (fromX - 'a'); @@ -253,7 +304,7 @@ Out("# command = '%s'\n", line); IF("analyze") { return 0; } IF("exit") { return 0; } IF("variant") { /* ignore, since it must be Shogi */; } - IF("setboard") { ; } + IF("setboard") { forceMode = plyNr = 0; read_fen( line ); return 0; } IF("option") { if(sscanf(line+7, "MultiPV=%d", &value) == 1) { sprintf(line, "mpv num %d", value); return 0; } if(sscanf(line+7, "centi-Pawn margin=%d", &value) == 1) { sprintf(line, "mpv width %d", value); return 0; }