From f75d0a5c5ff9825398ac4b11644d4cb8c14252a6 Mon Sep 17 00:00:00 2001 From: H.G. Muller Date: Sat, 7 Sep 2013 23:47:42 +0200 Subject: [PATCH] Automatically adapt board format to FEN In some situations (such as loading a game or poition) the board-size overrules are directly set to the values that can be deduced from the FEN. The routine ParseFEN() has an extra parameter autoSize to control that. --- backend.c | 51 ++++++++++++++++++++++++++++++--------------------- backend.h | 2 +- gamelist.c | 2 +- winboard/wclipbrd.c | 3 +-- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/backend.c b/backend.c index 6dae63d..4633377 100644 --- a/backend.c +++ b/backend.c @@ -8564,7 +8564,7 @@ if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats. if(*buf) SetCharTable(pieceToChar, buf); // do again, for it was spoiled by InitPosition } } - ParseFEN(boards[0], &dummy, message+s); + ParseFEN(boards[0], &dummy, message+s, FALSE); DrawPosition(TRUE, boards[0]); startedFromSetupPosition = TRUE; return; @@ -8577,7 +8577,7 @@ if(appData.debugMode) fprintf(debugFP, "nodes = %d, %lld\n", (int) programStats. GameEnds(GameUnfinished, "Engine aborts game", GE_XBOARD); - if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9)) { + if (!ParseFEN(initial_position, &blackPlaysFirst, message + 9, FALSE)) { DisplayError(_("Bad FEN received from engine"), 0); return ; } else { @@ -12081,7 +12081,7 @@ GameContainsPosition (FILE *f, ListGame *lg) for(next = WhitePawn; next>8 ^ random()<<6 ^random()<<20; initDone = TRUE; } - if(lg->gameInfo.fen) ParseFEN(boards[scratch], &btm, lg->gameInfo.fen); + if(lg->gameInfo.fen) ParseFEN(boards[scratch], &btm, lg->gameInfo.fen, FALSE); else CopyBoard(boards[scratch], initialPosition); // default start position if(lg->moves) { turn = btm + 1; @@ -12403,7 +12403,7 @@ LoadGame (FILE *f, int gameNumber, char *title, int useList) if (gameInfo.fen != NULL) { Board initial_position; startedFromSetupPosition = TRUE; - if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen)) { + if (!ParseFEN(initial_position, &blackPlaysFirst, gameInfo.fen, TRUE)) { Reset(TRUE, TRUE); DisplayError(_("Bad FEN position in file"), 0); return FALSE; @@ -12740,7 +12740,7 @@ LoadPosition (FILE *f, int positionNumber, char *title) } if (fenMode) { - if (!ParseFEN(initial_position, &blackPlaysFirst, line)) { + if (!ParseFEN(initial_position, &blackPlaysFirst, line, TRUE)) { DisplayError(_("Bad FEN position in file"), 0); return FALSE; } @@ -16518,7 +16518,7 @@ TypeInDoneEvent (char *move) ChessMove moveType; // [HGM] FENedit - if(gameMode == EditPosition && ParseFEN(board, &n, move) ) { + if(gameMode == EditPosition && ParseFEN(board, &n, move, TRUE) ) { EditPositionPasteFEN(move); return; } @@ -17407,34 +17407,31 @@ PositionToFEN (int move, char *overrideCastling, int moveCounts) } Boolean -ParseFEN (Board board, int *blackPlaysFirst, char *fen) +ParseFEN (Board board, int *blackPlaysFirst, char *fen, Boolean autoSize) { - int i, j; + int i, j, k, w=0; char *p, c; int emptycount, virgin[BOARD_FILES]; ChessSquare piece; p = fen; - /* [HGM] by default clear Crazyhouse holdings, if present */ - if(gameInfo.holdingsWidth) { - for(i=0; i= 0; i--) { j = 0; for (;;) { - if (*p == '/' || *p == ' ' || (*p == '[' && i == 0) ) { - if (*p == '/') p++; + if (*p == '/' || *p == ' ' || *p == '[' ) { + if(j > w) w = j; emptycount = gameInfo.boardWidth - j; while (emptycount--) board[i][(j++)+gameInfo.holdingsWidth] = EmptySquare; + if (*p == '/') p++; + else if(autoSize) { // we stumbled unexpectedly into end of board + for(k=i; k= 10) } else if(*p=='x' || *p=='X') { /* [HGM] X means 10 */ @@ -17474,6 +17471,18 @@ ParseFEN (Board board, int *blackPlaysFirst, char *fen) } while (*p == '/' || *p == ' ') p++; + if(autoSize) appData.NrFiles = w, InitPosition(TRUE); + + /* [HGM] by default clear Crazyhouse holdings, if present */ + if(gameInfo.holdingsWidth) { + for(i=0; igameInfo.fen) ParseFEN(boards[scratch], &btm, currentListGame->gameInfo.fen); + if(currentListGame->gameInfo.fen) ParseFEN(boards[scratch], &btm, currentListGame->gameInfo.fen, FALSE); else CopyBoard(boards[scratch], initialPosition); plyNr = (btm != 0); currentListGame->moves = PackGame(boards[scratch]); diff --git a/winboard/wclipbrd.c b/winboard/wclipbrd.c index d1c5538..08a97bf 100644 --- a/winboard/wclipbrd.c +++ b/winboard/wclipbrd.c @@ -42,7 +42,6 @@ /* Imports from winboard.c */ extern HWND hwndMain; -Boolean ParseFEN(Board b, int *stm, char *FEN); /* File globals */ static char *copyTemp; @@ -305,7 +304,7 @@ VOID PasteGameOrFENFromClipboard() } // [HGM] paste any: make still smarter, to allow pasting of games without tags, recognize FEN in stead - if(!ParseFEN(dummyBoard, &dummy, buf) ) { + if(!ParseFEN(dummyBoard, &dummy, buf, 0) ) { PasteGameFromString( buf ); } else { -- 1.7.0.4