mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 08:01:30 -07:00
Files

Having stated the principle in the previous commit, I should apply it consistently. A source file linked into the Puzzles library of common support code should not also define a main() under ifdef. This commit only goes as far as the _library_ support modules. It would be a much bigger job to do the same for all the actual _puzzles_ that have test main()s or standalone-solver main()s. And it's not necessary, because modifying one of those source files only triggers a rebuild of _one_ puzzle, not absolutely everything. (Not to mention that it's quite likely the puzzle and the test main() will need to be modified in conjunction anyway.) As in the previous commit, this has required exposing a few internal API functions as global, and maybe editing them a bit. In particular, the one-shot internal function that divvy_rectangle() loops on until it succeeds is now exposed as divvy_rectangle_attempt(), which means the test program doesn't have to condition a failure counter into the real function. I've thrown away penrose-vector-test completely, because that didn't look like a test program with any ongoing use at all - it was surely vestigial, while James was getting the vector representation up and running in the first place.
104 lines
2.3 KiB
C
104 lines
2.3 KiB
C
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stddef.h>
|
|
|
|
#include "puzzles.h"
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int *dsf;
|
|
int i;
|
|
int w = 9, h = 4, k = 6, tries = 100;
|
|
random_state *rs;
|
|
int fail_counter = 0;
|
|
|
|
rs = random_new("123456", 6);
|
|
|
|
if (argc > 1)
|
|
w = atoi(argv[1]);
|
|
if (argc > 2)
|
|
h = atoi(argv[2]);
|
|
if (argc > 3)
|
|
k = atoi(argv[3]);
|
|
if (argc > 4)
|
|
tries = atoi(argv[4]);
|
|
|
|
for (i = 0; i < tries; i++) {
|
|
int x, y;
|
|
|
|
while ((dsf = divvy_rectangle_attempt(w, h, k, rs)) == NULL)
|
|
fail_counter++;
|
|
|
|
for (y = 0; y <= 2*h; y++) {
|
|
for (x = 0; x <= 2*w; x++) {
|
|
int miny = y/2 - 1 /*, maxy = y/2 */;
|
|
int minx = x/2 - 1 /*, maxx = x/2 */;
|
|
int classes[4], tx, ty;
|
|
for (ty = 0; ty < 2; ty++)
|
|
for (tx = 0; tx < 2; tx++) {
|
|
int cx = minx+tx, cy = miny+ty;
|
|
if (cx < 0 || cx >= w || cy < 0 || cy >= h)
|
|
classes[ty*2+tx] = -1;
|
|
else
|
|
classes[ty*2+tx] = dsf_canonify(dsf, cy*w+cx);
|
|
}
|
|
switch (y%2 * 2 + x%2) {
|
|
case 0: /* corner */
|
|
/*
|
|
* Cases for the corner:
|
|
*
|
|
* - if all four surrounding squares belong
|
|
* to the same omino, we print a space.
|
|
*
|
|
* - if the top two are the same and the
|
|
* bottom two are the same, we print a
|
|
* horizontal line.
|
|
*
|
|
* - if the left two are the same and the
|
|
* right two are the same, we print a
|
|
* vertical line.
|
|
*
|
|
* - otherwise, we print a cross.
|
|
*/
|
|
if (classes[0] == classes[1] &&
|
|
classes[1] == classes[2] &&
|
|
classes[2] == classes[3])
|
|
printf(" ");
|
|
else if (classes[0] == classes[1] &&
|
|
classes[2] == classes[3])
|
|
printf("-");
|
|
else if (classes[0] == classes[2] &&
|
|
classes[1] == classes[3])
|
|
printf("|");
|
|
else
|
|
printf("+");
|
|
break;
|
|
case 1: /* horiz edge */
|
|
if (classes[1] == classes[3])
|
|
printf(" ");
|
|
else
|
|
printf("--");
|
|
break;
|
|
case 2: /* vert edge */
|
|
if (classes[2] == classes[3])
|
|
printf(" ");
|
|
else
|
|
printf("|");
|
|
break;
|
|
case 3: /* square centre */
|
|
printf(" ");
|
|
break;
|
|
}
|
|
}
|
|
printf("\n");
|
|
}
|
|
printf("\n");
|
|
sfree(dsf);
|
|
}
|
|
|
|
printf("%d retries needed for %d successes\n", fail_counter, tries);
|
|
|
|
return 0;
|
|
}
|