shithub: puzzles

Download patch

ref: ad1c6ade2af0e681fb964a17cc3a031951047068
parent: 49d28f2204dfbb21156d98b0313399c8126b59e0
author: Simon Tatham <anakin@pobox.com>
date: Thu Oct 21 16:40:36 EDT 2021

Galaxies: store game solution in the aux string.

Most games store a string in 'aux' during new_game_desc() that saves
solve_game() during gameplay from having to reconstruct the solution
from scratch. Galaxies had all the facilities available to do that,
but apparently just forgot to use them.

(Reindents existing code: diff best viewed with whitespace ignored.)

--- a/galaxies.c
+++ b/galaxies.c
@@ -155,6 +155,7 @@
 static space *space_opposite_dot(const game_state *state, const space *sp,
                                  const space *dot);
 static space *tile_opposite(const game_state *state, const space *sp);
+static game_state *execute_move(const game_state *state, const char *move);
 
 /* ----------------------------------------------------------
  * Game parameters and presets
@@ -1568,6 +1569,10 @@
     dbg_state(state);
 #endif
 
+    game_state *blank = blank_game(params->w, params->h);
+    *aux = diff_game(blank, state, true, -1);
+    free_game(blank);
+
     free_game(state);
     sfree(scratch);
 
@@ -2325,21 +2330,26 @@
     int i;
     int diff;
 
-    tosolve = dup_game(currstate);
-    diff = solver_state(tosolve, DIFF_UNREASONABLE);
-    if (diff != DIFF_UNFINISHED && diff != DIFF_IMPOSSIBLE) {
-        debug(("solve_game solved with current state.\n"));
+    if (aux) {
+        tosolve = execute_move(state, aux);
         goto solved;
-    }
-    free_game(tosolve);
+    } else {
+        tosolve = dup_game(currstate);
+        diff = solver_state(tosolve, DIFF_UNREASONABLE);
+        if (diff != DIFF_UNFINISHED && diff != DIFF_IMPOSSIBLE) {
+            debug(("solve_game solved with current state.\n"));
+            goto solved;
+        }
+        free_game(tosolve);
 
-    tosolve = dup_game(state);
-    diff = solver_state(tosolve, DIFF_UNREASONABLE);
-    if (diff != DIFF_UNFINISHED && diff != DIFF_IMPOSSIBLE) {
-        debug(("solve_game solved with original state.\n"));
-        goto solved;
+        tosolve = dup_game(state);
+        diff = solver_state(tosolve, DIFF_UNREASONABLE);
+        if (diff != DIFF_UNFINISHED && diff != DIFF_IMPOSSIBLE) {
+            debug(("solve_game solved with original state.\n"));
+            goto solved;
+        }
+        free_game(tosolve);
     }
-    free_game(tosolve);
 
     return NULL;