mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 08:01:30 -07:00
Prevent starting in a solved state in Fifteen & Flood
(cherry picked from Android port, commit cb38abdc71780bd9b393b90514396c338306fa69)
This commit is contained in:
15
fifteen.c
15
fifteen.c
@ -156,6 +156,14 @@ static int perm_parity(int *perm, int n)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int is_completed(int *tiles, int n) {
|
||||||
|
int p;
|
||||||
|
for (p = 0; p < n; p++)
|
||||||
|
if (tiles[p] != (p < n-1 ? p+1 : 0))
|
||||||
|
return 0;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static char *new_game_desc(const game_params *params, random_state *rs,
|
static char *new_game_desc(const game_params *params, random_state *rs,
|
||||||
char **aux, bool interactive)
|
char **aux, bool interactive)
|
||||||
{
|
{
|
||||||
@ -171,6 +179,7 @@ static char *new_game_desc(const game_params *params, random_state *rs,
|
|||||||
tiles = snewn(n, int);
|
tiles = snewn(n, int);
|
||||||
used = snewn(n, bool);
|
used = snewn(n, bool);
|
||||||
|
|
||||||
|
do {
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
tiles[i] = -1;
|
tiles[i] = -1;
|
||||||
used[i] = false;
|
used[i] = false;
|
||||||
@ -246,6 +255,7 @@ static char *new_game_desc(const game_params *params, random_state *rs,
|
|||||||
tiles[x2] = p1;
|
tiles[x2] = p1;
|
||||||
assert(perm_parity(tiles, n) == parity);
|
assert(perm_parity(tiles, n) == parity);
|
||||||
}
|
}
|
||||||
|
} while (is_completed(tiles, n));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Now construct the game description, by describing the tile
|
* Now construct the game description, by describing the tile
|
||||||
@ -786,11 +796,8 @@ static game_state *execute_move(const game_state *from, const char *move)
|
|||||||
/*
|
/*
|
||||||
* See if the game has been completed.
|
* See if the game has been completed.
|
||||||
*/
|
*/
|
||||||
if (!ret->completed) {
|
if (!ret->completed && is_completed(ret->tiles, ret->n)) {
|
||||||
ret->completed = ret->movecount;
|
ret->completed = ret->movecount;
|
||||||
for (p = 0; p < ret->n; p++)
|
|
||||||
if (ret->tiles[p] != (p < ret->n-1 ? p+1 : 0))
|
|
||||||
ret->completed = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
2
flood.c
2
flood.c
@ -552,8 +552,10 @@ static char *new_game_desc(const game_params *params, random_state *rs,
|
|||||||
/*
|
/*
|
||||||
* Invent a random grid.
|
* Invent a random grid.
|
||||||
*/
|
*/
|
||||||
|
do {
|
||||||
for (i = 0; i < wh; i++)
|
for (i = 0; i < wh; i++)
|
||||||
scratch->grid[i] = random_upto(rs, params->colours);
|
scratch->grid[i] = random_upto(rs, params->colours);
|
||||||
|
} while (completed(w, h, scratch->grid));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Run the solver, and count how many moves it uses.
|
* Run the solver, and count how many moves it uses.
|
||||||
|
Reference in New Issue
Block a user