divvy-test.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stddef.h>
  5. #include "puzzles.h"
  6. int main(int argc, char **argv)
  7. {
  8. DSF *dsf;
  9. int i;
  10. int w = 9, h = 4, k = 6, tries = 100;
  11. random_state *rs;
  12. int fail_counter = 0;
  13. rs = random_new("123456", 6);
  14. if (argc > 1)
  15. w = atoi(argv[1]);
  16. if (argc > 2)
  17. h = atoi(argv[2]);
  18. if (argc > 3)
  19. k = atoi(argv[3]);
  20. if (argc > 4)
  21. tries = atoi(argv[4]);
  22. for (i = 0; i < tries; i++) {
  23. int x, y;
  24. while ((dsf = divvy_rectangle_attempt(w, h, k, rs)) == NULL)
  25. fail_counter++;
  26. for (y = 0; y <= 2*h; y++) {
  27. for (x = 0; x <= 2*w; x++) {
  28. int miny = y/2 - 1 /*, maxy = y/2 */;
  29. int minx = x/2 - 1 /*, maxx = x/2 */;
  30. int classes[4], tx, ty;
  31. for (ty = 0; ty < 2; ty++)
  32. for (tx = 0; tx < 2; tx++) {
  33. int cx = minx+tx, cy = miny+ty;
  34. if (cx < 0 || cx >= w || cy < 0 || cy >= h)
  35. classes[ty*2+tx] = -1;
  36. else
  37. classes[ty*2+tx] = dsf_canonify(dsf, cy*w+cx);
  38. }
  39. switch (y%2 * 2 + x%2) {
  40. case 0: /* corner */
  41. /*
  42. * Cases for the corner:
  43. *
  44. * - if all four surrounding squares belong
  45. * to the same omino, we print a space.
  46. *
  47. * - if the top two are the same and the
  48. * bottom two are the same, we print a
  49. * horizontal line.
  50. *
  51. * - if the left two are the same and the
  52. * right two are the same, we print a
  53. * vertical line.
  54. *
  55. * - otherwise, we print a cross.
  56. */
  57. if (classes[0] == classes[1] &&
  58. classes[1] == classes[2] &&
  59. classes[2] == classes[3])
  60. printf(" ");
  61. else if (classes[0] == classes[1] &&
  62. classes[2] == classes[3])
  63. printf("-");
  64. else if (classes[0] == classes[2] &&
  65. classes[1] == classes[3])
  66. printf("|");
  67. else
  68. printf("+");
  69. break;
  70. case 1: /* horiz edge */
  71. if (classes[1] == classes[3])
  72. printf(" ");
  73. else
  74. printf("--");
  75. break;
  76. case 2: /* vert edge */
  77. if (classes[2] == classes[3])
  78. printf(" ");
  79. else
  80. printf("|");
  81. break;
  82. case 3: /* square centre */
  83. printf(" ");
  84. break;
  85. }
  86. }
  87. printf("\n");
  88. }
  89. printf("\n");
  90. dsf_free(dsf);
  91. }
  92. printf("%d retries needed for %d successes\n", fail_counter, tries);
  93. return 0;
  94. }