aboutsummaryrefslogtreecommitdiff
path: root/galaxies.c
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2021-05-23 10:04:47 +0100
committerSimon Tatham <anakin@pobox.com>2021-05-25 10:52:25 +0100
commit12b64a1db1a2cb94b938295875e5583237dbe168 (patch)
tree92ee4482e2d76c4eede42a8066605df58a652093 /galaxies.c
parentd5b53853aad6c3bbfe255c063c045e7986de33ad (diff)
downloadpuzzles-12b64a1db1a2cb94b938295875e5583237dbe168.zip
puzzles-12b64a1db1a2cb94b938295875e5583237dbe168.tar.gz
puzzles-12b64a1db1a2cb94b938295875e5583237dbe168.tar.bz2
puzzles-12b64a1db1a2cb94b938295875e5583237dbe168.tar.xz
Build a lot of conditioned-out test and helper programs.
Most of these aren't especially useful, but if we're going to have them in the code base at all, we should at least ensure they compile: bit-rotted conditioned-out code is of no value. One of the new programs is 'galaxieseditor', which borrows most of the Galaxies code but changes the UI so that you can create and remove _dots_ instead of edges, and then run the solver to see whether it can solve the puzzle you've designed. Unlike the rest, this is a GUI helper tool, using the 'guiprogram' cmake function introduced in the previous commit. The programs are: - 'combi', a test program for the utility module that generates all combinations of n things - 'divvy', a test program for the module that divides a rectangle at random into equally-sized polyominoes - 'penrose-test', a test program for the Penrose-tiling generator used in Loopy, which outputs an SVG of a piece of tiling - 'penrose-vector', a much smaller test program for the vector arithmetic subroutines in that code - 'sort-test', a test of this code base's local array sorting routine - 'tree234-test', the exhaustive test code that's been in tree234.c all along. Not all of them compiled first time. Most of the fixes were the usual kind of thing: fixing compiler warnings by removing unused variables/functions, bringing uses of internal APIs up to date. A notable one was that galaxieseditor's interpret_move() modified the input game state, which was an error all along and is now detected by me having made it a const pointer; I had to replace that with an extra wrinkle in the move-string format, so that now execute_move() makes the modification. The one I'm _least_ proud of is squelching a huge number of format-string warnings in tree234-test by interposing a variadic function without __attribute__((printf)).
Diffstat (limited to 'galaxies.c')
-rw-r--r--galaxies.c71
1 files changed, 60 insertions, 11 deletions
diff --git a/galaxies.c b/galaxies.c
index b2158b7..52dcaff 100644
--- a/galaxies.c
+++ b/galaxies.c
@@ -372,20 +372,21 @@ static bool ok_to_add_assoc_with_opposite_internal(
return toret;
}
+#ifndef EDITOR
static bool ok_to_add_assoc_with_opposite(
const game_state *state, space *tile, space *dot)
{
space *opposite = space_opposite_dot(state, tile, dot);
return ok_to_add_assoc_with_opposite_internal(state, tile, opposite);
}
+#endif
static void add_assoc_with_opposite(game_state *state, space *tile, space *dot) {
space *opposite = space_opposite_dot(state, tile, dot);
- if(opposite)
+ if(opposite && ok_to_add_assoc_with_opposite_internal(
+ state, tile, opposite))
{
- assert(ok_to_add_assoc_with_opposite_internal(state, tile, opposite));
-
remove_assoc_with_opposite(state, tile);
add_assoc(state, tile, dot);
remove_assoc_with_opposite(state, opposite);
@@ -684,7 +685,7 @@ static void tiles_from_edge(game_state *state, space *sp, space **ts)
/* Returns a move string for use by 'solve', including the initial
* 'S' if issolve is true. */
static char *diff_game(const game_state *src, const game_state *dest,
- bool issolve)
+ bool issolve, int set_cdiff)
{
int movelen = 0, movesize = 256, x, y, len;
char *move = snewn(movesize, char), buf[80];
@@ -698,6 +699,26 @@ static char *diff_game(const game_state *src, const game_state *dest,
move[movelen++] = 'S';
sep = ";";
}
+#ifdef EDITOR
+ if (set_cdiff >= 0) {
+ switch (set_cdiff) {
+ case DIFF_IMPOSSIBLE:
+ movelen += sprintf(move+movelen, "%sII", sep);
+ break;
+ case DIFF_AMBIGUOUS:
+ movelen += sprintf(move+movelen, "%sIA", sep);
+ break;
+ case DIFF_UNFINISHED:
+ movelen += sprintf(move+movelen, "%sIU", sep);
+ break;
+ default:
+ movelen += sprintf(move+movelen, "%si%c",
+ sep, galaxies_diffchars[set_cdiff]);
+ break;
+ }
+ sep = ";";
+ }
+#endif
move[movelen] = '\0';
for (x = 0; x < src->sx; x++) {
for (y = 0; y < src->sy; y++) {
@@ -747,7 +768,8 @@ static char *diff_game(const game_state *src, const game_state *dest,
/* Returns true if a dot here would not be too close to any other dots
* (and would avoid other game furniture). */
-static bool dot_is_possible(game_state *state, space *sp, bool allow_assoc)
+static bool dot_is_possible(const game_state *state, space *sp,
+ bool allow_assoc)
{
int bx = 0, by = 0, dx, dy;
space *adj;
@@ -2309,7 +2331,7 @@ solved:
*/
for (i = 0; i < tosolve->sx*tosolve->sy; i++)
tosolve->grid[i].flags &= ~F_TILE_ASSOC;
- ret = diff_game(currstate, tosolve, true);
+ ret = diff_game(currstate, tosolve, true, -1);
free_game(tosolve);
return ret;
}
@@ -2448,15 +2470,13 @@ static char *interpret_move(const game_state *state, game_ui *ui,
px = 2*FROMCOORD((float)x) + 0.5;
py = 2*FROMCOORD((float)y) + 0.5;
- state->cdiff = -1;
-
if (button == 'C' || button == 'c') return dupstr("C");
if (button == 'S' || button == 's') {
char *ret;
game_state *tmp = dup_game(state);
- state->cdiff = solver_state(tmp, DIFF_UNREASONABLE-1);
- ret = diff_game(state, tmp, 0);
+ int cdiff = solver_state(tmp, DIFF_UNREASONABLE-1);
+ ret = diff_game(state, tmp, 0, cdiff);
free_game(tmp);
return ret;
}
@@ -2514,7 +2534,7 @@ static char *interpret_move(const game_state *state, game_ui *ui,
char *ret;
game_state *tmp = dup_game(state);
solver_obvious(tmp);
- ret = diff_game(state, tmp, false);
+ ret = diff_game(state, tmp, false, -1);
free_game(tmp);
return ret;
}
@@ -2973,6 +2993,35 @@ static game_state *execute_move(const game_state *state, const char *move)
} else if (c == 'C') {
move++;
clear_game(ret, true);
+ } else if (c == 'i') {
+ int diff;
+ move++;
+ for (diff = 0; diff <= DIFF_UNREASONABLE; diff++)
+ if (*move == galaxies_diffchars[diff])
+ break;
+ if (diff > DIFF_UNREASONABLE)
+ goto badmove;
+
+ ret->cdiff = diff;
+ move++;
+ } else if (c == 'I') {
+ int diff;
+ move++;
+ switch (*move) {
+ case 'A':
+ diff = DIFF_AMBIGUOUS;
+ break;
+ case 'I':
+ diff = DIFF_IMPOSSIBLE;
+ break;
+ case 'U':
+ diff = DIFF_UNFINISHED;
+ break;
+ default:
+ goto badmove;
+ }
+ ret->cdiff = diff;
+ move++;
#endif
} else if (c == 'S') {
move++;