From eb0cc399a0f464254a164c391e40345081e33059 Mon Sep 17 00:00:00 2001 From: H.G. Muller Date: Fri, 22 Apr 2011 12:47:24 +0200 Subject: [PATCH] Refactor code for loading games and positions in match The code in NextMatchGame and MatchEvent was nearly the same, (MatchEvent did a little more error checking, but that is never harmful), and could be combined. In stead of determining the load index incrementally, it is now calculated in an absolute way from the game number, so it can be used more easily in tournament mode. --- backend.c | 74 ++++++++++++++++++++++++++++--------------------------------- 1 files changed, 34 insertions(+), 40 deletions(-) diff --git a/backend.c b/backend.c index 9ecc707..138422a 100644 --- a/backend.c +++ b/backend.c @@ -1231,6 +1231,38 @@ InitBackEnd2() } } +int +CalculateIndex(int index, int gameNr) +{ // [HGM] autoinc: absolute way to determine load index from game number (taking auto-inc and rewind into account) + int res; + if(index > 0) return index; // fixed nmber + if(index == 0) return 1; + res = (index == -1 ? gameNr : (gameNr-1)/2 + 1); // autoinc + if(appData.rewindIndex > 0) res = (res-1) % appData.rewindIndex + 1; // rewind + return res; +} + +int +LoadGameOrPosition(int gameNr) +{ // [HGM] taken out of MatchEvent and NextMatchGame (to combine it) + if (*appData.loadGameFile != NULLCHAR) { + if (!LoadGameFromFile(appData.loadGameFile, + CalculateIndex(appData.loadGameIndex, gameNr), + appData.loadGameFile, FALSE)) { + DisplayFatalError(_("Bad game file"), 0, 1); + return 0; + } + } else if (*appData.loadPositionFile != NULLCHAR) { + if (!LoadPositionFromFile(appData.loadPositionFile, + CalculateIndex(appData.loadPositionIndex, gameNr), + appData.loadPositionFile)) { + DisplayFatalError(_("Bad position file"), 0, 1); + return 0; + } + } + return 1; +} + void MatchEvent(int mode) { // [HGM] moved out of InitBackend3, to make it callable when match starts through menu @@ -1242,25 +1274,7 @@ MatchEvent(int mode) } matchMode = mode; matchGame = 1; - if (*appData.loadGameFile != NULLCHAR) { - int index = appData.loadGameIndex; // [HGM] autoinc - if(index<0) lastIndex = index = 1; - if (!LoadGameFromFile(appData.loadGameFile, - index, - appData.loadGameFile, FALSE)) { - DisplayFatalError(_("Bad game file"), 0, 1); - return; - } - } else if (*appData.loadPositionFile != NULLCHAR) { - int index = appData.loadPositionIndex; // [HGM] autoinc - if(index<0) lastIndex = index = 1; - if (!LoadPositionFromFile(appData.loadPositionFile, - index, - appData.loadPositionFile)) { - DisplayFatalError(_("Bad position file"), 0, 1); - return; - } - } + if(!LoadGameOrPositionFile(1)) return; first.matchWins = second.matchWins = 0; // [HGM] match: needed in later matches TwoMachinesEvent(); } @@ -9233,7 +9247,6 @@ StartChessProgram(cps) } } - void TwoMachinesEventIfReady P((void)) { @@ -9254,27 +9267,8 @@ TwoMachinesEventIfReady P((void)) void NextMatchGame P((void)) { - int index; /* [HGM] autoinc: step load index during match */ Reset(FALSE, TRUE); - if (*appData.loadGameFile != NULLCHAR) { - index = appData.loadGameIndex; - if(index < 0) { // [HGM] autoinc - lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1; - if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1; - } - LoadGameFromFile(appData.loadGameFile, - index, - appData.loadGameFile, FALSE); - } else if (*appData.loadPositionFile != NULLCHAR) { - index = appData.loadPositionIndex; - if(index < 0) { // [HGM] autoinc - lastIndex = index = (index == -2 && first.twoMachinesColor[0] == 'b') ? lastIndex : lastIndex+1; - if(appData.rewindIndex > 0 && index > appData.rewindIndex) lastIndex = index = 1; - } - LoadPositionFromFile(appData.loadPositionFile, - index, - appData.loadPositionFile); - } + LoadGameOrPosition(matchGame); TwoMachinesEventIfReady(); } -- 1.7.0.4