Add a `full' parameter to validate_params(), analogous to the one in

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]
This commit is contained in:
Jacob Nevins
2005-07-05 21:27:19 +00:00
parent 968828283b
commit f3c95109c7
18 changed files with 39 additions and 26 deletions

2
cube.c
View File

@ -525,7 +525,7 @@ static void count_grid_square_callback(void *ctx, struct grid_square *sq)
classes[thisclass]++; classes[thisclass]++;
} }
static char *validate_params(game_params *params) static char *validate_params(game_params *params, int full)
{ {
int classes[5]; int classes[5];
int i; int i;

View File

@ -129,7 +129,7 @@ static game_params *custom_params(config_item *cfg)
return ret; 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) if (params->w < 2 || params->h < 2)
return "Width and height must both be at least two"; return "Width and height must both be at least two";

2
flip.c
View File

@ -182,7 +182,7 @@ static game_params *custom_params(config_item *cfg)
return ret; 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) if (params->w <= 0 || params->h <= 0)
return "Width and height must both be greater than zero"; return "Width and height must both be greater than zero";

2
gtk.c
View File

@ -1676,7 +1676,7 @@ int main(int argc, char **argv)
*seed++ = '\0'; *seed++ = '\0';
thegame.decode_params(par, params); 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); fprintf(stderr, "%s: %s\n", pname, error);
return 1; return 1;
} }

View File

@ -212,7 +212,7 @@ static game_params *custom_params(config_item *cfg)
return ret; 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) if (params->ncolours < 2 || params->npegs < 2)
return "Trivial solutions are uninteresting"; return "Trivial solutions are uninteresting";

View File

@ -789,7 +789,7 @@ int midend_num_presets(midend_data *me)
preset = me->ourgame->default_params(); preset = me->ourgame->default_params();
me->ourgame->decode_params(preset, val); 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. */ /* Drop this one from the list. */
me->ourgame->free_params(preset); me->ourgame->free_params(preset);
continue; continue;
@ -955,7 +955,7 @@ static char *midend_game_id_int(midend_data *me, char *id, int defmode)
if (par) { if (par) {
newcurparams = me->ourgame->dup_params(me->params); newcurparams = me->ourgame->dup_params(me->params);
me->ourgame->decode_params(newcurparams, par); me->ourgame->decode_params(newcurparams, par);
error = me->ourgame->validate_params(newcurparams); error = me->ourgame->validate_params(newcurparams, desc == NULL);
if (error) { if (error) {
me->ourgame->free_params(newcurparams); me->ourgame->free_params(newcurparams);
return error; return error;
@ -1046,7 +1046,7 @@ char *midend_set_config(midend_data *me, int which, config_item *cfg)
switch (which) { switch (which) {
case CFG_SETTINGS: case CFG_SETTINGS:
params = me->ourgame->custom_params(cfg); params = me->ourgame->custom_params(cfg);
error = me->ourgame->validate_params(params); error = me->ourgame->validate_params(params, TRUE);
if (error) { if (error) {
me->ourgame->free_params(params); me->ourgame->free_params(params);
@ -1480,16 +1480,24 @@ char *midend_deserialise(midend_data *me,
params = me->ourgame->default_params(); params = me->ourgame->default_params();
me->ourgame->decode_params(params, parstr); 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"; ret = "Long-term parameters in save file are invalid";
goto cleanup; goto cleanup;
} }
cparams = me->ourgame->default_params(); cparams = me->ourgame->default_params();
me->ourgame->decode_params(cparams, cparstr); 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"; ret = "Short-term parameters in save file are invalid";
goto cleanup; 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) { if (!desc) {
ret = "Game description in save file is missing"; ret = "Game description in save file is missing";
goto cleanup; goto cleanup;

View File

@ -237,7 +237,7 @@ static game_params *custom_params(config_item *cfg)
return ret; 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. * 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 * _have_ to have a gap somewhere which you can't determine the
* position of. * 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"; return "Width and height must both be greater than two";
if (params->n > params->w * params->h - 9) if (params->n > params->w * params->h - 9)
return "Too many mines for grid size"; return "Too many mines for grid size";

4
net.c
View File

@ -293,7 +293,7 @@ static game_params *custom_params(config_item *cfg)
return ret; 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) if (params->width <= 0 || params->height <= 0)
return "Width and height must both be greater than zero"; 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 * is at least 2^(number of such rows), and in particular is at
* least 2 since there must be at least one such row. [] * 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)) (params->width == 2 || params->height == 2))
return "No wrapping puzzle with a width or height of 2 can have" return "No wrapping puzzle with a width or height of 2 can have"
" a unique solution"; " a unique solution";

View File

@ -309,7 +309,7 @@ static game_params *custom_params(config_item *cfg)
return ret; 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) if (params->width <= 1 || params->height <= 1)
return "Width and height must both be greater than one"; return "Width and height must both be greater than one";

View File

@ -78,7 +78,7 @@ static game_params *custom_params(config_item *cfg)
return NULL; return NULL;
} }
static char *validate_params(game_params *params) static char *validate_params(game_params *params, int full)
{ {
return NULL; return NULL;
} }

View File

@ -165,7 +165,7 @@ static game_params *custom_params(config_item *cfg)
return ret; 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) if (params->w <= 0 || params->h <= 0)
return "Width and height must both be greater than zero"; return "Width and height must both be greater than zero";

11
pegs.c
View File

@ -120,6 +120,11 @@ static void decode_params(game_params *params, char const *string)
params->h = params->w; 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++) for (i = 0; i < lenof(pegs_lowertypes); i++)
if (!strcmp(p, pegs_lowertypes[i])) if (!strcmp(p, pegs_lowertypes[i]))
params->type = i; params->type = i;
@ -178,9 +183,9 @@ static game_params *custom_params(config_item *cfg)
return ret; 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"; 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 * soluble. For the moment, therefore, I'm going to disallow
* them at any size other than the standard one. * 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) if (params->w != 7 || params->h != 7)
return "This board type is only supported at 7x7"; return "This board type is only supported at 7x7";
} }

View File

@ -265,7 +265,7 @@ struct game {
int can_configure; int can_configure;
config_item *(*configure)(game_params *params); config_item *(*configure)(game_params *params);
game_params *(*custom_params)(config_item *cfg); 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 *(*new_desc)(game_params *params, random_state *rs,
char **aux, int interactive); char **aux, int interactive);
char *(*validate_desc)(game_params *params, char *desc); char *(*validate_desc)(game_params *params, char *desc);

2
rect.c
View File

@ -212,7 +212,7 @@ static game_params *custom_params(config_item *cfg)
return ret; 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) if (params->w <= 0 || params->h <= 0)
return "Width and height must both be greater than zero"; return "Width and height must both be greater than zero";

View File

@ -209,7 +209,7 @@ static game_params *custom_params(config_item *cfg)
return ret; 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) if (params->w < 1 || params->h < 1)
return "Width and height must both be positive"; return "Width and height must both be positive";

View File

@ -172,7 +172,7 @@ static game_params *custom_params(config_item *cfg)
return ret; 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) if (params->w < 2 || params->h < 2)
return "Width and height must both be at least two"; return "Width and height must both be at least two";

2
solo.c
View File

@ -321,7 +321,7 @@ static game_params *custom_params(config_item *cfg)
return ret; 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) if (params->c < 2 || params->r < 2)
return "Both dimensions must be at least 2"; return "Both dimensions must be at least 2";

View File

@ -209,7 +209,7 @@ static game_params *custom_params(config_item *cfg)
return ret; return ret;
} }
static char *validate_params(game_params *params) static char *validate_params(game_params *params, int full)
{ {
if (params->n < 2) if (params->n < 2)
return "Rotation radius must be at least two"; return "Rotation radius must be at least two";