Implement selection of game seeds, by reusing the config box

mechanism I've just invented (the midend handles the standard game
selection configuration). Each game is now required to validate its
own seed data before attempting to base a game on it and potentially
confusing itself.

[originally from svn r4186]
This commit is contained in:
Simon Tatham
2004-05-03 08:51:31 +00:00
parent eb88ee0973
commit 6e42ddd31b
9 changed files with 257 additions and 39 deletions

View File

@ -257,6 +257,58 @@ char *new_game_seed(game_params *params)
return ret;
}
char *validate_seed(game_params *params, char *seed)
{
char *p, *err;
int i, area;
int *used;
area = params->w * params->h;
p = seed;
err = NULL;
used = snewn(area, int);
for (i = 0; i < area; i++)
used[i] = FALSE;
for (i = 0; i < area; i++) {
char *q = p;
int n;
if (*p < '0' || *p > '9') {
err = "Not enough numbers in string";
goto leave;
}
while (*p >= '0' && *p <= '9')
p++;
if (i < area-1 && *p != ',') {
err = "Expected comma after number";
goto leave;
}
else if (i == area-1 && *p) {
err = "Excess junk at end of string";
goto leave;
}
n = atoi(q);
if (n < 1 || n > area) {
err = "Number out of range";
goto leave;
}
if (used[n-1]) {
err = "Number used twice";
goto leave;
}
used[n-1] = TRUE;
if (*p) p++; /* eat comma */
}
leave:
sfree(used);
return err;
}
game_state *new_game(game_params *params, char *seed)
{
game_state *state = snew(game_state);