mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 16:05:44 -07:00
Tighten validation of Tents game descriptions
Specifically, TENT and NONTENT markers ('!' and '-') cannot appear as the first or last character of a description, because that would attempt to place them on squares outside the grid. This was caught by assertions in new_game(), as can be demonstrated by feeding these descriptions to older versions of Tents: "4:-p,0,0,0,0,0,0,0,0" ("new_game: Assertion `i >= 0 && i <= w*h' failed.") and 4:p-,0,0,0,0,0,0,0,0 ("new_game: Assertion `*desc == ','' failed.").
This commit is contained in:
22
tents.c
22
tents.c
@ -1191,6 +1191,21 @@ static char *new_game_desc(const game_params *params_in, random_state *rs,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Grid description format:
|
||||||
|
*
|
||||||
|
* _ = tree
|
||||||
|
* a = 1 BLANK then TREE
|
||||||
|
* ...
|
||||||
|
* y = 25 BLANKs then TREE
|
||||||
|
* z = 25 BLANKs
|
||||||
|
* ! = set previous square to TENT
|
||||||
|
* - = set previous square to NONTENT
|
||||||
|
*
|
||||||
|
* Last character must be one that would insert a tree as the first
|
||||||
|
* square after the grid.
|
||||||
|
*/
|
||||||
|
|
||||||
static const char *validate_desc(const game_params *params, const char *desc)
|
static const char *validate_desc(const game_params *params, const char *desc)
|
||||||
{
|
{
|
||||||
int w = params->w, h = params->h;
|
int w = params->w, h = params->h;
|
||||||
@ -1204,9 +1219,10 @@ static const char *validate_desc(const game_params *params, const char *desc)
|
|||||||
area += *desc - 'a' + 2;
|
area += *desc - 'a' + 2;
|
||||||
else if (*desc == 'z')
|
else if (*desc == 'z')
|
||||||
area += 25;
|
area += 25;
|
||||||
else if (*desc == '!' || *desc == '-')
|
else if (*desc == '!' || *desc == '-') {
|
||||||
/* do nothing */;
|
if (area == 0 || area > w * h)
|
||||||
else
|
return "Tent or non-tent placed off the grid";
|
||||||
|
} else
|
||||||
return "Invalid character in grid specification";
|
return "Invalid character in grid specification";
|
||||||
|
|
||||||
desc++;
|
desc++;
|
||||||
|
Reference in New Issue
Block a user