mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-22 08:25:45 -07:00
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:
52
sixteen.c
52
sixteen.c
@ -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);
|
||||
|
Reference in New Issue
Block a user