shithub: puzzles

Download patch

ref: 843d4ca17def11671809786f2a5aebd75f230dd9
parent: 15f4fa851a5781cf77984a6046405ffa758e7b33
author: Ben Harris <bjh21@bjh21.me.uk>
date: Fri Feb 3 15:52:05 EST 2023

Tolerate incorrect solutions in Inertia

The "solve" operation in Inertia generates a proposed solution as a
move string.  But if such a move string is loaded from a save file it
might not actually describe a solution.  If that happens then it's
possible to reach the end of the "solution" without winning, and doing
so should probably cause a recalculation of the solution rather than
an assertion failure ("execute_move: Assertion `ret->solnpos <
ret->soln->len' failed.").

I am a little concerned by the way that normal solve operations end up
encoded in the save file, but the re-solvings caused by going off
course don't, but I haven't got a good answer to that.

Here's a save file that demonstrates the assertion failure:

SAVEFILE:41:Simon Tatham's Portable Puzzle Collection
GAME    :7:Inertia
PARAMS  :3:8x8
CPARAMS :3:8x8
DESC    :64:sbgwsmswwgggwggmmbwgwbssbwbsbwbbwsSmwbbsbbmggbmssgmgwbmmwmbmmwsw
NSTATES :2:3
STATEPOS:1:1
MOVE 000:2:S0
MOVE 000:2:00

--- a/inertia.c
+++ b/inertia.c
@@ -1741,11 +1741,10 @@
     if (ret->soln) {
 	if (ret->dead || ret->gems == 0)
 	    discard_solution(ret);
-	else if (ret->soln->list[ret->solnpos] == dir) {
+	else if (ret->soln->list[ret->solnpos] == dir &&
+            ret->solnpos+1 < ret->soln->len)
 	    ++ret->solnpos;
-	    assert(ret->solnpos < ret->soln->len); /* or gems == 0 */
-	    assert(!ret->dead); /* or not a solution */
-	} else {
+	else {
 	    const char *error = NULL;
             char *soln = solve_game(NULL, ret, NULL, &error);
 	    if (!error) {