From 255ca56ed73a1532db7ed66d5bdc05e4557de701 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 19 Aug 2015 07:43:00 -0700 Subject: [PATCH] when no moves found, reset engine and try again. Works around bug where you do "hint", then "prev hint" and get told there are no moves found only to have the next "prev hint" succeed. This is a hack, but the right fix is eluding me, and will certainly be riskier. --- xwords4/common/engine.c | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/xwords4/common/engine.c b/xwords4/common/engine.c index 1b3ab0763..334c69342 100644 --- a/xwords4/common/engine.c +++ b/xwords4/common/engine.c @@ -345,9 +345,6 @@ chooseMove( EngineCtxt* engine, PossibleMove** move ) result = (NULL != chosen) && (chosen->score > 0); - if ( !result ) { - engine_reset( engine ); - } return result; } /* chooseMove */ @@ -381,7 +378,7 @@ normalizeIQ( EngineCtxt* engine, XP_U16 iq ) XP_Bool engine_findMove( EngineCtxt* engine, const ModelCtxt* model, XP_U16 turn, const Tile* tiles, - XP_U16 nTiles, XP_Bool usePrev, + const XP_U16 nTiles, XP_Bool usePrev, #ifdef XWFEATURE_BONUSALL XP_U16 allTilesBonus, #endif @@ -394,7 +391,9 @@ engine_findMove( EngineCtxt* engine, const ModelCtxt* model, XP_Bool result = XP_TRUE; XP_U16 star_row; XP_Bool canMove = XP_FALSE; + XP_Bool isRetry = XP_FALSE; + retry: engine->nTilesMax = XP_MIN( MAX_TRAY_TILES, nTiles ); #ifdef XWFEATURE_BONUSALL engine->allTilesBonus = allTilesBonus; @@ -542,6 +541,22 @@ engine_findMove( EngineCtxt* engine, const ModelCtxt* model, newMove->nTiles = 0; } + /* Gross hack alert: there's an elusive bug in move cacheing that means + when we move forward or back from the highest-scoring move to the + lowest (or vice-versa) no move is found. But the next try succeeds, + because an engine_reset clears the state that makes that happen. So as + a workaround, try doing that when no moves are found. If none is found + for some other reason, e.g. no tiles, at least the search should be + quick. */ + if ( !canMove ) { + engine_reset( engine ); + if ( !isRetry ) { + isRetry = XP_TRUE; + XP_LOGF( "%s: no moves found so retrying", __func__ ); + goto retry; + } + } + *canMoveP = canMove; return result; } /* engine_findMove */