mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-20 23:51:29 -07:00
Validate the number of pegs and holes in a Pegs game ID
Without this, "1:O" causes an assertion violation, '!"new_ui found nowhere for cursor"'. We may as well require two pegs and one hole, since that's the minimum for a game in which there are any moves to make.
This commit is contained in:
11
pegs.c
11
pegs.c
@ -663,7 +663,7 @@ static char *new_game_desc(const game_params *params, random_state *rs,
|
|||||||
|
|
||||||
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 len;
|
int len, i, npeg = 0, nhole = 0;
|
||||||
|
|
||||||
len = params->w * params->h;
|
len = params->w * params->h;
|
||||||
|
|
||||||
@ -671,6 +671,15 @@ static const char *validate_desc(const game_params *params, const char *desc)
|
|||||||
return "Game description is wrong length";
|
return "Game description is wrong length";
|
||||||
if (len != strspn(desc, "PHO"))
|
if (len != strspn(desc, "PHO"))
|
||||||
return "Invalid character in game description";
|
return "Invalid character in game description";
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
npeg += desc[i] == 'P';
|
||||||
|
nhole += desc[i] == 'H';
|
||||||
|
}
|
||||||
|
/* The minimal soluble game has two pegs and a hole: "3x1:PPH". */
|
||||||
|
if (npeg < 2)
|
||||||
|
return "Too few pegs in game description";
|
||||||
|
if (nhole < 1)
|
||||||
|
return "Too few holes in game description";
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user