chiark / gitweb /
Add a `full' parameter to validate_params(), analogous to the one in
authorJacob Nevins <jacobn@chiark.greenend.org.uk>
Tue, 5 Jul 2005 21:27:19 +0000 (21:27 +0000)
committerJacob Nevins <jacobn@chiark.greenend.org.uk>
Tue, 5 Jul 2005 21:27:19 +0000 (21:27 +0000)
encode_params(). This is necessary for cases where generation-time parameters
that are normally omitted from descriptive IDs can place restrictions on other
parameters; in particular, when the default value of a relevant generation-time
parameter is not the one used to generate the descriptive ID, validation could
reject self-generated IDs (e.g., Net `5x2w:56182ae7c2', and some cases in
`Pegs').

[originally from svn r6068]

18 files changed:
cube.c
fifteen.c
flip.c
gtk.c
guess.c
midend.c
mines.c
net.c
netslide.c
nullgame.c
pattern.c
pegs.c
puzzles.h
rect.c
samegame.c
sixteen.c
solo.c
twiddle.c

diff --git a/cube.c b/cube.c
index 768f53f69c28d5fe86a3e075f48e61eab3847973..200316338e15b3d437230707dc0b86d0c317754e 100644 (file)
--- a/cube.c
+++ b/cube.c
@@ -525,7 +525,7 @@ static void count_grid_square_callback(void *ctx, struct grid_square *sq)
     classes[thisclass]++;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     int classes[5];
     int i;
index 319a585ebe21326641e80c5aa29692a9542603bf..3a387bbdc2f5fce11a2b368cad4b90bf10670c35 100644 (file)
--- a/fifteen.c
+++ b/fifteen.c
@@ -129,7 +129,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     if (params->w < 2 || params->h < 2)
        return "Width and height must both be at least two";
diff --git a/flip.c b/flip.c
index b38aff1a62ff865f9c27beaca0fb6bed9a2076f5..4125d6ddc04b49e5e9d45414ab20fb9369c5cfe1 100644 (file)
--- a/flip.c
+++ b/flip.c
@@ -182,7 +182,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     if (params->w <= 0 || params->h <= 0)
         return "Width and height must both be greater than zero";
diff --git a/gtk.c b/gtk.c
index 1a79f0ef3571c8dab9d6bb66240a5cbc9a18cf96..8ebddd6395bb5512f800c774c6582f9507a18061 100644 (file)
--- a/gtk.c
+++ b/gtk.c
@@ -1676,7 +1676,7 @@ int main(int argc, char **argv)
                 *seed++ = '\0';
            thegame.decode_params(par, params);
         }
-        if ((error = thegame.validate_params(par)) != NULL) {
+        if ((error = thegame.validate_params(par, TRUE)) != NULL) {
            fprintf(stderr, "%s: %s\n", pname, error);
             return 1;
         }
diff --git a/guess.c b/guess.c
index 39241b04d2bafc854e78ed3d8ffbf10e3245d851..dddd0529725f60f5cb060841f34b439ba12965dc 100644 (file)
--- a/guess.c
+++ b/guess.c
@@ -212,7 +212,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     if (params->ncolours < 2 || params->npegs < 2)
        return "Trivial solutions are uninteresting";
index 8223d8758ed57907913dae9468b4943a7f89eaae..7ef05cb85e6c353dce6b46e232f22b94d1c424ca 100644 (file)
--- a/midend.c
+++ b/midend.c
@@ -789,7 +789,7 @@ int midend_num_presets(midend_data *me)
                 preset = me->ourgame->default_params();
                 me->ourgame->decode_params(preset, val);
 
-               if (me->ourgame->validate_params(preset)) {
+               if (me->ourgame->validate_params(preset, TRUE)) {
                    /* Drop this one from the list. */
                    me->ourgame->free_params(preset);
                    continue;
@@ -955,7 +955,7 @@ static char *midend_game_id_int(midend_data *me, char *id, int defmode)
     if (par) {
         newcurparams = me->ourgame->dup_params(me->params);
         me->ourgame->decode_params(newcurparams, par);
-        error = me->ourgame->validate_params(newcurparams);
+        error = me->ourgame->validate_params(newcurparams, desc == NULL);
         if (error) {
             me->ourgame->free_params(newcurparams);
             return error;
@@ -1046,7 +1046,7 @@ char *midend_set_config(midend_data *me, int which, config_item *cfg)
     switch (which) {
       case CFG_SETTINGS:
        params = me->ourgame->custom_params(cfg);
-       error = me->ourgame->validate_params(params);
+       error = me->ourgame->validate_params(params, TRUE);
 
        if (error) {
            me->ourgame->free_params(params);
@@ -1480,16 +1480,24 @@ char *midend_deserialise(midend_data *me,
 
     params = me->ourgame->default_params();
     me->ourgame->decode_params(params, parstr);
-    if (me->ourgame->validate_params(params)) {
+    if (me->ourgame->validate_params(params, TRUE)) {
         ret = "Long-term parameters in save file are invalid";
         goto cleanup;
     }
     cparams = me->ourgame->default_params();
     me->ourgame->decode_params(cparams, cparstr);
-    if (me->ourgame->validate_params(cparams)) {
+    if (me->ourgame->validate_params(cparams, FALSE)) {
         ret = "Short-term parameters in save file are invalid";
         goto cleanup;
     }
+    if (seed && me->ourgame->validate_params(cparams, TRUE)) {
+        /*
+         * The seed's no use with this version, but we can perfectly
+         * well use the rest of the data.
+         */
+        sfree(seed);
+        seed = NULL;
+    }
     if (!desc) {
         ret = "Game description in save file is missing";
         goto cleanup;
diff --git a/mines.c b/mines.c
index b70167eae7891596b7be52515244829fc2d4b4e3..f3e216c63377f894f85c0cbf2b272e47719a7050 100644 (file)
--- a/mines.c
+++ b/mines.c
@@ -237,7 +237,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     /*
      * Lower limit on grid size: each dimension must be at least 3.
@@ -253,7 +253,7 @@ static char *validate_params(game_params *params)
      * _have_ to have a gap somewhere which you can't determine the
      * position of.
      */
-    if (params->w <= 2 || params->h <= 2)
+    if (full && params->unique && (params->w <= 2 || params->h <= 2))
        return "Width and height must both be greater than two";
     if (params->n > params->w * params->h - 9)
        return "Too many mines for grid size";
diff --git a/net.c b/net.c
index cb8e613b056bbc09dc2cc7e4441c9effe8550b08..5d0806c083e0902cfece0a8a29a26b94e74ff9b0 100644 (file)
--- a/net.c
+++ b/net.c
@@ -293,7 +293,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     if (params->width <= 0 || params->height <= 0)
        return "Width and height must both be greater than zero";
@@ -347,7 +347,7 @@ static char *validate_params(game_params *params)
      * is at least 2^(number of such rows), and in particular is at
      * least 2 since there must be at least one such row. []
      */
-    if (params->unique && params->wrapping &&
+    if (full && params->unique && params->wrapping &&
         (params->width == 2 || params->height == 2))
         return "No wrapping puzzle with a width or height of 2 can have"
         " a unique solution";
index b16cb0bf222aaa1484251c6a5ef5648e65357c5a..12fabd7af1f27f82dc369c77cc07276062db7aea 100644 (file)
@@ -309,7 +309,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     if (params->width <= 1 || params->height <= 1)
        return "Width and height must both be greater than one";
index bdf0e07153726b4bebaff36ab9572cec3929c7e6..8af76ce00671b6bbe52a99d38532953b31f04398 100644 (file)
@@ -78,7 +78,7 @@ static game_params *custom_params(config_item *cfg)
     return NULL;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     return NULL;
 }
index e9b6f98704b3b38a91318a4a284b638af6351167..f83746ae2981fc4c6bdc9b6bf05c8612d99a7075 100644 (file)
--- a/pattern.c
+++ b/pattern.c
@@ -165,7 +165,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     if (params->w <= 0 || params->h <= 0)
        return "Width and height must both be greater than zero";
diff --git a/pegs.c b/pegs.c
index 0874d1df26b635b2897bcd8354605ecf9505fffb..5ed30d097650f23e9609f8d9249e67474e1b78ac 100644 (file)
--- a/pegs.c
+++ b/pegs.c
@@ -120,6 +120,11 @@ static void decode_params(game_params *params, char const *string)
         params->h = params->w;
     }
 
+    /*
+     * Assume a random generation scheme unless told otherwise, for the
+     * sake of internal consistency.
+     */
+    params->type = TYPE_RANDOM;
     for (i = 0; i < lenof(pegs_lowertypes); i++)
        if (!strcmp(p, pegs_lowertypes[i]))
            params->type = i;
@@ -178,9 +183,9 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
-    if (params->w <= 3 || params->h <= 3)
+    if (full && (params->w <= 3 || params->h <= 3))
        return "Width and height must both be greater than three";
 
     /*
@@ -189,7 +194,7 @@ static char *validate_params(game_params *params)
      * soluble. For the moment, therefore, I'm going to disallow
      * them at any size other than the standard one.
      */
-    if (params->type == TYPE_CROSS || params->type == TYPE_OCTAGON) {
+    if (full && (params->type == TYPE_CROSS || params->type == TYPE_OCTAGON)) {
        if (params->w != 7 || params->h != 7)
            return "This board type is only supported at 7x7";
     }
index d41d9646b6ba3aadff838fcf64e00a7fc876c5ab..57b5c3399bfe19259c8498086526128283c74098 100644 (file)
--- a/puzzles.h
+++ b/puzzles.h
@@ -265,7 +265,7 @@ struct game {
     int can_configure;
     config_item *(*configure)(game_params *params);
     game_params *(*custom_params)(config_item *cfg);
-    char *(*validate_params)(game_params *params);
+    char *(*validate_params)(game_params *params, int full);
     char *(*new_desc)(game_params *params, random_state *rs,
                      char **aux, int interactive);
     char *(*validate_desc)(game_params *params, char *desc);
diff --git a/rect.c b/rect.c
index 952f133246f0603bba30d5d7ae5fd090d3af895d..fbe5b7958b98beb47950e9b4c3102d78dbc2140c 100644 (file)
--- a/rect.c
+++ b/rect.c
@@ -212,7 +212,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     if (params->w <= 0 || params->h <= 0)
        return "Width and height must both be greater than zero";
index 35f2ae2105259f200501fb56c90243ae1c524f36..5a5230322240552d7f803a9ec422c861eaf83887 100644 (file)
@@ -209,7 +209,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     if (params->w < 1 || params->h < 1)
        return "Width and height must both be positive";
index f293fab67c126ff3209741c227e96b81a7a21440..5eb0715f39936af3820b9cb8c5946e7d419db921 100644 (file)
--- a/sixteen.c
+++ b/sixteen.c
@@ -172,7 +172,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     if (params->w < 2 || params->h < 2)
        return "Width and height must both be at least two";
diff --git a/solo.c b/solo.c
index 47b4c394c17f87b31fa238320627fc7aae55d783..881589e71b83cac6d1fb301ec97e05965110cddb 100644 (file)
--- a/solo.c
+++ b/solo.c
@@ -321,7 +321,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     if (params->c < 2 || params->r < 2)
        return "Both dimensions must be at least 2";
index ce3d0ece979abbccd35dad00de6ff479eb11e3f2..ac6f385dfd47f98e40527de91a865e5e604bc1fa 100644 (file)
--- a/twiddle.c
+++ b/twiddle.c
@@ -209,7 +209,7 @@ static game_params *custom_params(config_item *cfg)
     return ret;
 }
 
-static char *validate_params(game_params *params)
+static char *validate_params(game_params *params, int full)
 {
     if (params->n < 2)
        return "Rotation radius must be at least two";