Files
puzzles/combi.c
Simon Tatham 12b64a1db1 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)).
2021-05-25 10:52:25 +01:00

106 lines
1.9 KiB
C

#include <assert.h>
#include <string.h>
#include "puzzles.h"
/* horrific and doesn't check overflow. */
static long factx(long x, long y)
{
long acc = 1, i;
for (i = y; i <= x; i++)
acc *= i;
return acc;
}
void reset_combi(combi_ctx *combi)
{
int i;
combi->nleft = combi->total;
for (i = 0; i < combi->r; i++)
combi->a[i] = i;
}
combi_ctx *new_combi(int r, int n)
{
long nfr, nrf;
combi_ctx *combi;
assert(r <= n);
assert(n >= 1);
combi = snew(combi_ctx);
memset(combi, 0, sizeof(combi_ctx));
combi->r = r;
combi->n = n;
combi->a = snewn(r, int);
memset(combi->a, 0, r * sizeof(int));
nfr = factx(n, r+1);
nrf = factx(n-r, 1);
combi->total = (int)(nfr / nrf);
reset_combi(combi);
return combi;
}
/* returns NULL when we're done otherwise returns input. */
combi_ctx *next_combi(combi_ctx *combi)
{
int i = combi->r - 1, j;
if (combi->nleft == combi->total)
goto done;
else if (combi->nleft <= 0)
return NULL;
while (combi->a[i] == combi->n - combi->r + i)
i--;
combi->a[i] += 1;
for (j = i+1; j < combi->r; j++)
combi->a[j] = combi->a[i] + j - i;
done:
combi->nleft--;
return combi;
}
void free_combi(combi_ctx *combi)
{
sfree(combi->a);
sfree(combi);
}
/* compile this with:
* gcc -o combi.exe -DSTANDALONE_COMBI_TEST combi.c malloc.c
*/
#ifdef STANDALONE_COMBI_TEST
#include <stdio.h>
int main(int argc, char *argv[])
{
combi_ctx *c;
int i, r, n;
if (argc < 3) {
fprintf(stderr, "Usage: combi R N\n");
exit(1);
}
r = atoi(argv[1]); n = atoi(argv[2]);
c = new_combi(r, n);
printf("combi %d of %d, %d elements.\n", c->r, c->n, c->total);
while (next_combi(c)) {
for (i = 0; i < c->r; i++) {
printf("%d ", c->a[i]);
}
printf("\n");
}
free_combi(c);
}
#endif