-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosaic.c
1627 lines (1506 loc) · 48.4 KB
/
mosaic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* mosaic.c: A puzzle based on a square grid, with some of the tiles
* having clues as to how many black squares are around them.
* the purpose of the game is to find what should be on all tiles (black or
* unmarked)
*
* The game is also known as: ArtMosaico, Count and Darken, Cuenta Y Sombrea,
* Fill-a-Pix, Fill-In, Komsu Karala, Magipic, Majipiku, Mosaico, Mosaik,
* Mozaiek, Nampre Puzzle, Nurie-Puzzle, Oekaki-Pix, Voisimage.
*
* Implementation is loosely based on https://github.com/mordechaim/Mosaic, UI
* interaction is based on the range puzzle in the collection.
*/
#include <assert.h>
#include <ctype.h>
#ifdef NO_TGMATH_H
# include <math.h>
#else
# include <tgmath.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "puzzles.h"
#define DEFAULT_SIZE 10
#define DEFAULT_AGGRESSIVENESS true
#define MAX_TILES 10000
#define MAX_TILES_ERROR "Maximum size is 10000 tiles"
#define DEFAULT_TILE_SIZE 32
#define DEBUG_IMAGE 1
#undef DEBUG_IMAGE
#define FLASH_TIME 0.5F
/* To enable debug prints define DEBUG_PRINTS */
/* Getting the coordinates and returning NULL when out of scope
* The parentheses are needed to avoid order of operations issues
*/
#define get_coords(params, array, x, y) \
(((x) >= 0 && (y) >= 0) && ((x) < params->width && (y) < params->height)) \
? array + ((y)*params->width) + x \
: NULL
#define COORD_FROM_CELL(d) ((d * ds->tilesize) + ds->tilesize / 2) - 1
enum {
COL_BACKGROUND = 0,
COL_UNMARKED,
COL_GRID,
COL_MARKED,
COL_BLANK,
COL_TEXT_SOLVED,
COL_ERROR,
COL_CURSOR,
NCOLOURS,
COL_TEXT_DARK = COL_MARKED,
COL_TEXT_LIGHT = COL_BLANK
};
enum cell_state {
STATE_UNMARKED = 0,
STATE_MARKED = 1,
STATE_BLANK = 2,
STATE_SOLVED = 4,
STATE_ERROR = 8,
STATE_UNMARKED_ERROR = STATE_ERROR | STATE_UNMARKED,
STATE_MARKED_ERROR = STATE_ERROR | STATE_MARKED,
STATE_BLANK_ERROR = STATE_ERROR | STATE_BLANK,
STATE_BLANK_SOLVED = STATE_SOLVED | STATE_BLANK,
STATE_MARKED_SOLVED = STATE_MARKED | STATE_SOLVED,
STATE_OK_NUM = STATE_BLANK | STATE_MARKED
};
struct game_params {
int width;
int height;
bool aggressive;
};
typedef struct board_state board_state;
typedef struct needed_list_item needed_list_item;
struct needed_list_item {
int x, y;
needed_list_item *next;
};
struct game_state {
bool cheating;
int not_completed_clues;
int width;
int height;
char *cells_contents;
board_state *board;
};
struct board_state {
unsigned int references;
struct board_cell *actual_board;
};
struct board_cell {
signed char clue;
bool shown;
};
struct solution_cell {
signed char cell;
bool solved;
bool needed;
};
struct desc_cell {
char clue;
bool shown;
bool value;
bool full;
bool empty;
};
struct game_ui {
bool solved;
bool in_progress;
int last_x, last_y, last_state;
int cur_x, cur_y;
bool cur_visible;
};
struct game_drawstate {
int tilesize;
int *state;
};
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->width = DEFAULT_SIZE;
ret->height = DEFAULT_SIZE;
ret->aggressive = DEFAULT_AGGRESSIVENESS;
return ret;
}
static bool game_fetch_preset(int i, char **name, game_params **params)
{
const int sizes[6] = { 3, 5, 10, 15, 25, 50 };
const bool aggressiveness[6] = { true, true, true, true, true, false };
if (i < 0 || i > 5) {
return false;
}
game_params *res = snew(game_params);
res->height = sizes[i];
res->width = sizes[i];
res->aggressive = aggressiveness[i];
*params = res;
char value[80];
sprintf(value, "Size: %dx%d", sizes[i], sizes[i]);
*name = dupstr(value);
return true;
}
static void free_params(game_params *params)
{
sfree(params);
}
static game_params *dup_params(const game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
return ret;
}
static void decode_params(game_params *params, char const *string)
{
params->width = params->height = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
if (*string == 'x') {
string++;
params->height = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
}
if (*string == 'h') {
string++;
params->aggressive = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
}
}
static char *encode_params(const game_params *params, bool full)
{
char encoded[128];
int pos = 0;
pos += sprintf(encoded + pos, "%dx%d", params->width, params->height);
if (full) {
if (params->aggressive != DEFAULT_AGGRESSIVENESS)
pos += sprintf(encoded + pos, "h%d", params->aggressive);
}
return dupstr(encoded);
}
static config_item *game_configure(const game_params *params)
{
config_item *config = snewn(4, config_item);
char value[80];
config[0].type = C_STRING;
config[0].name = "Height";
sprintf(value, "%d", params->height);
config[0].u.string.sval = dupstr(value);
config[1].type = C_STRING;
config[1].name = "Width";
sprintf(value, "%d", params->width);
config[1].u.string.sval = dupstr(value);
config[2].name = "Aggressive generation (longer)";
config[2].type = C_BOOLEAN;
config[2].u.boolean.bval = params->aggressive;
config[3].type = C_END;
return config;
}
static game_params *custom_params(const config_item *cfg)
{
game_params *res = snew(game_params);
res->height = atol(cfg[0].u.string.sval);
res->width = atol(cfg[1].u.string.sval);
res->aggressive = cfg[2].u.boolean.bval;
return res;
}
static const char *validate_params(const game_params *params, bool full)
{
if (params->height < 3 || params->width < 3) {
return "Minimal size is 3x3";
}
if (params->height > MAX_TILES / params->width) {
return MAX_TILES_ERROR;
}
return NULL;
}
static bool get_pixel(const game_params *params, const bool *image,
const int x, const int y)
{
const bool *pixel;
pixel = get_coords(params, image, x, y);
if (pixel) {
return *pixel;
}
return 0;
}
static void populate_cell(const game_params *params, const bool *image,
const int x, const int y, bool edge,
struct desc_cell *desc)
{
int clue = 0;
bool xEdge = false;
bool yEdge = false;
if (edge) {
if (x > 0) {
clue += get_pixel(params, image, x - 1, y);
if (y > 0) {
clue += get_pixel(params, image, x - 1, y - 1);
}
if (y < params->height - 1) {
clue += get_pixel(params, image, x - 1, y + 1);
}
} else {
xEdge = true;
}
if (y > 0) {
clue += get_pixel(params, image, x, y - 1);
} else {
yEdge = true;
}
if (x < params->width - 1) {
clue += get_pixel(params, image, x + 1, y);
if (y > 0) {
clue += get_pixel(params, image, x + 1, y - 1);
}
if (y < params->height - 1) {
clue += get_pixel(params, image, x + 1, y + 1);
}
} else {
xEdge = true;
}
if (y < params->height - 1) {
clue += get_pixel(params, image, x, y + 1);
} else {
yEdge = true;
}
} else {
clue += get_pixel(params, image, x - 1, y - 1);
clue += get_pixel(params, image, x - 1, y);
clue += get_pixel(params, image, x - 1, y + 1);
clue += get_pixel(params, image, x, y - 1);
clue += get_pixel(params, image, x, y + 1);
clue += get_pixel(params, image, x + 1, y - 1);
clue += get_pixel(params, image, x + 1, y);
clue += get_pixel(params, image, x + 1, y + 1);
}
desc->value = get_pixel(params, image, x, y);
clue += desc->value;
if (clue == 0) {
desc->empty = true;
desc->full = false;
} else {
desc->empty = false;
/* setting the default */
desc->full = false;
if (clue == 9) {
desc->full = true;
} else if (edge && ((xEdge && yEdge && clue == 4) ||
((xEdge || yEdge) && clue == 6))) {
desc->full = true;
}
}
desc->shown = true;
desc->clue = clue;
}
static void count_around(const game_params *params,
struct solution_cell *sol, int x, int y,
int *marked, int *blank, int *total)
{
int i, j;
struct solution_cell *curr = NULL;
(*total) = 0;
(*blank) = 0;
(*marked) = 0;
for (i = -1; i < 2; i++) {
for (j = -1; j < 2; j++) {
curr = get_coords(params, sol, x + i, y + j);
if (curr) {
(*total)++;
if ((curr->cell & STATE_BLANK) != 0) {
(*blank)++;
} else if ((curr->cell & STATE_MARKED) != 0) {
(*marked)++;
}
}
}
}
}
static void count_around_state(const game_state *state, int x, int y,
int *marked, int *blank, int *total)
{
int i, j;
char *curr = NULL;
(*total) = 0;
(*blank) = 0;
(*marked) = 0;
for (i = -1; i < 2; i++) {
for (j = -1; j < 2; j++) {
curr = get_coords(state, state->cells_contents, x + i, y + j);
if (curr) {
(*total)++;
if ((*curr & STATE_BLANK) != 0) {
(*blank)++;
} else if ((*curr & STATE_MARKED) != 0) {
(*marked)++;
}
}
}
}
}
static void count_clues_around(const game_params *params,
struct desc_cell *desc, int x, int y,
int *clues, int *total)
{
int i, j;
struct desc_cell *curr = NULL;
(*total) = 0;
(*clues) = 0;
for (i = -1; i < 2; i++) {
for (j = -1; j < 2; j++) {
curr = get_coords(params, desc, x + i, y + j);
if (curr) {
(*total)++;
if (curr->shown) {
(*clues)++;
}
}
}
}
}
static void mark_around(const game_params *params,
struct solution_cell *sol, int x, int y, int mark)
{
int i, j;
struct solution_cell *curr;
for (i = -1; i < 2; i++) {
for (j = -1; j < 2; j++) {
curr = get_coords(params, sol, x + i, y + j);
if (curr) {
if (curr->cell == STATE_UNMARKED) {
curr->cell = mark;
}
}
}
}
}
static char solve_cell(const game_params *params, struct desc_cell *desc,
struct board_cell *board, struct solution_cell *sol,
int x, int y)
{
struct desc_cell curr;
if (desc) {
curr.shown = desc[(y * params->width) + x].shown;
curr.clue = desc[(y * params->width) + x].clue;
curr.full = desc[(y * params->width) + x].full;
curr.empty = desc[(y * params->width) + x].empty;
} else {
curr.shown = board[(y * params->width) + x].shown;
curr.clue = board[(y * params->width) + x].clue;
curr.full = false;
curr.empty = false;
}
int marked = 0, total = 0, blank = 0;
if (sol[(y * params->width) + x].solved) {
return 0;
}
count_around(params, sol, x, y, &marked, &blank, &total);
if (curr.full && curr.shown) {
sol[(y * params->width) + x].solved = true;
if (marked + blank < total) {
sol[(y * params->width) + x].needed = true;
}
mark_around(params, sol, x, y, STATE_MARKED);
return 1;
}
if (curr.empty && curr.shown) {
sol[(y * params->width) + x].solved = true;
if (marked + blank < total) {
sol[(y * params->width) + x].needed = true;
}
mark_around(params, sol, x, y, STATE_BLANK);
return 1;
}
if (curr.shown) {
if (!sol[(y * params->width) + x].solved) {
if (marked == curr.clue) {
sol[(y * params->width) + x].solved = true;
if (total != marked + blank) {
sol[(y * params->width) + x].needed = true;
}
mark_around(params, sol, x, y, STATE_BLANK);
} else if (curr.clue == (total - blank)) {
sol[(y * params->width) + x].solved = true;
if (total != marked + blank) {
sol[(y * params->width) + x].needed = true;
}
mark_around(params, sol, x, y, STATE_MARKED);
} else if (total == marked + blank) {
return -1;
} else {
return 0;
}
return 1;
}
return 0;
} else if (total == marked + blank) {
sol[(y * params->width) + x].solved = true;
return 1;
} else {
return 0;
}
}
static bool solve_check(const game_params *params, struct desc_cell *desc,
random_state *rs, struct solution_cell **sol_return)
{
int x, y, i;
int board_size = params->height * params->width;
struct solution_cell *sol = snewn(board_size, struct solution_cell),
*curr_sol;
bool made_progress = true, error = false;
int solved = 0, curr = 0, shown = 0;
needed_list_item *head = NULL, *curr_needed, **needed_array;
struct desc_cell *curr_desc;
memset(sol, 0, board_size * sizeof(*sol));
for (y = 0; y < params->height; y++) {
for (x = 0; x < params->width; x++) {
curr_desc = get_coords(params, desc, x, y);
if (curr_desc->shown) {
curr_needed = snew(needed_list_item);
curr_needed->next = head;
head = curr_needed;
curr_needed->x = x;
curr_needed->y = y;
shown++;
}
}
}
needed_array = snewn(shown, needed_list_item *);
curr_needed = head;
i = 0;
while (curr_needed) {
needed_array[i] = curr_needed;
curr_needed = curr_needed->next;
i++;
}
if (rs) {
shuffle(needed_array, shown, sizeof(*needed_array), rs);
}
solved = 0;
while (solved < shown && made_progress && !error) {
made_progress = false;
for (i = 0; i < shown; i++) {
curr = solve_cell(params, desc, NULL, sol, needed_array[i]->x,
needed_array[i]->y);
if (curr < 0) {
error = true;
#ifdef DEBUG_PRINTS
printf("error in cell x=%d, y=%d\n", needed_array[i]->x,
needed_array[i]->y);
#endif
break;
}
if (curr > 0) {
solved++;
made_progress = true;
}
}
}
while (head) {
curr_needed = head;
head = curr_needed->next;
sfree(curr_needed);
}
sfree(needed_array);
solved = 0;
/* verifying all the board is solved */
if (made_progress) {
for (y = 0; y < params->height; y++) {
for (x = 0; x < params->width; x++) {
curr_sol = get_coords(params, sol, x, y);
if ((curr_sol->cell & (STATE_MARKED | STATE_BLANK)) > 0) {
solved++;
}
}
}
}
if (sol_return) {
*sol_return = sol;
} else {
sfree(sol);
}
return solved == board_size;
}
static bool solve_game_actual(const game_params *params,
struct board_cell *desc,
struct solution_cell **sol_return)
{
int x, y;
int board_size = params->height * params->width;
struct solution_cell *sol = snewn(board_size, struct solution_cell);
bool made_progress = true, error = false;
int solved = 0, curr = 0;
memset(sol, 0, params->height * params->width * sizeof(*sol));
solved = 0;
while (solved < params->height * params->width && made_progress
&& !error) {
for (y = 0; y < params->height; y++) {
for (x = 0; x < params->width; x++) {
curr = solve_cell(params, NULL, desc, sol, x, y);
if (curr < 0) {
error = true;
#ifdef DEBUG_PRINTS
printf("error in cell x=%d, y=%d\n", x, y);
#endif
break;
}
if (curr > 0) {
made_progress = true;
}
solved += curr;
}
}
}
if (sol_return) {
*sol_return = sol;
} else {
sfree(sol);
}
return solved == params->height * params->width;
}
static void hide_clues(const game_params *params, struct desc_cell *desc,
random_state *rs)
{
int shown, total, x, y, i;
int needed = 0;
struct desc_cell *curr;
struct solution_cell *sol = NULL, *curr_sol = NULL;
needed_list_item *head = NULL, *curr_needed, **needed_array;
#ifdef DEBUG_PRINTS
printf("Hiding clues\n");
#endif
solve_check(params, desc, rs, &sol);
for (y = 0; y < params->height; y++) {
for (x = 0; x < params->width; x++) {
count_clues_around(params, desc, x, y, &shown, &total);
curr = get_coords(params, desc, x, y);
curr_sol = get_coords(params, sol, x, y);
if (curr_sol->needed && params->aggressive) {
curr_needed = snew(needed_list_item);
curr_needed->x = x;
curr_needed->y = y;
curr_needed->next = head;
head = curr_needed;
needed++;
} else if (!curr_sol->needed) {
curr->shown = false;
}
}
}
if (params->aggressive) {
curr_needed = head;
needed_array = snewn(needed, needed_list_item *);
memset(needed_array, 0, needed * sizeof(*needed_array));
i = 0;
while (curr_needed) {
needed_array[i] = curr_needed;
curr_needed = curr_needed->next;
i++;
}
shuffle(needed_array, needed, sizeof(*needed_array), rs);
for (i = 0; i < needed; i++) {
curr_needed = needed_array[i];
curr =
get_coords(params, desc, curr_needed->x, curr_needed->y);
if (curr) {
curr->shown = false;
if (!solve_check(params, desc, NULL, NULL)) {
#ifdef DEBUG_PRINTS
printf("Hiding cell %d, %d not possible.\n",
curr_needed->x, curr_needed->y);
#endif
curr->shown = true;
}
sfree(curr_needed);
needed_array[i] = NULL;
}
curr_needed = NULL;
}
sfree(needed_array);
}
#ifdef DEBUG_PRINTS
printf("needed %d\n", needed);
#endif
sfree(sol);
}
static bool start_point_check(size_t size, struct desc_cell *desc)
{
int i;
for (i = 0; i < size; i++) {
if (desc[i].empty || desc[i].full) {
return true;
}
}
return false;
}
static void game_get_cursor_location(const game_ui *ui,
const game_drawstate *ds,
const game_state *state,
const game_params *params, int *x,
int *y, int *w, int *h)
{
if (ui->cur_visible) {
*x = COORD_FROM_CELL(ui->cur_x);
*y = COORD_FROM_CELL(ui->cur_y);
*w = *h = ds->tilesize;
}
}
static void generate_image(const game_params *params, random_state *rs,
bool *image)
{
int x, y;
for (y = 0; y < params->height; y++) {
for (x = 0; x < params->width; x++) {
image[(y * params->width) + x] = random_bits(rs, 1);
}
}
}
static char *new_game_desc(const game_params *params, random_state *rs,
char **aux, bool interactive)
{
bool *image = snewn(params->height * params->width, bool);
bool valid = false;
char *desc_string = snewn((params->height * params->width) + 1, char);
char *compressed_desc =
snewn((params->height * params->width) + 1, char);
char space_count;
struct desc_cell *desc =
snewn(params->height * params->width, struct desc_cell);
int x, y, location_in_str;
while (!valid) {
generate_image(params, rs, image);
#ifdef DEBUG_IMAGE
image[0] = 1;
image[1] = 1;
image[2] = 0;
image[3] = 1;
image[4] = 1;
image[5] = 0;
image[6] = 0;
image[7] = 0;
image[8] = 0;
#endif
for (y = 0; y < params->height; y++) {
for (x = 0; x < params->width; x++) {
populate_cell(params, image, x, y,
x * y == 0 || y == params->height - 1 ||
x == params->width - 1,
&desc[(y * params->width) + x]);
}
}
valid =
start_point_check((params->height - 1) * (params->width - 1),
desc);
if (!valid) {
#ifdef DEBUG_PRINTS
printf("Not valid, regenerating.\n");
#endif
} else {
valid = solve_check(params, desc, rs, NULL);
if (!valid) {
#ifdef DEBUG_PRINTS
printf("Couldn't solve, regenerating.");
#endif
} else {
hide_clues(params, desc, rs);
}
}
}
location_in_str = 0;
for (y = 0; y < params->height; y++) {
for (x = 0; x < params->width; x++) {
if (desc[(y * params->width) + x].shown) {
#ifdef DEBUG_PRINTS
printf("%d(%d)", desc[(y * params->width) + x].value,
desc[(y * params->width) + x].clue);
#endif
sprintf(desc_string + location_in_str, "%d",
desc[(y * params->width) + x].clue);
} else {
#ifdef DEBUG_PRINTS
printf("%d( )", desc[(y * params->width) + x].value);
#endif
sprintf(desc_string + location_in_str, " ");
}
location_in_str += 1;
}
#ifdef DEBUG_PRINTS
printf("\n");
#endif
}
location_in_str = 0;
space_count = 'a' - 1;
for (y = 0; y < params->height; y++) {
for (x = 0; x < params->width; x++) {
if (desc[(y * params->width) + x].shown) {
if (space_count >= 'a') {
sprintf(compressed_desc + location_in_str, "%c",
space_count);
location_in_str++;
space_count = 'a' - 1;
}
sprintf(compressed_desc + location_in_str, "%d",
desc[(y * params->width) + x].clue);
location_in_str++;
} else {
if (space_count <= 'z') {
space_count++;
} else {
sprintf(compressed_desc + location_in_str, "%c",
space_count);
location_in_str++;
space_count = 'a' - 1;
}
}
}
}
if (space_count >= 'a') {
sprintf(compressed_desc + location_in_str, "%c", space_count);
location_in_str++;
}
compressed_desc[location_in_str] = '\0';
#ifdef DEBUG_PRINTS
printf("compressed_desc: %s\n", compressed_desc);
#endif
return compressed_desc;
}
static const char *validate_desc(const game_params *params,
const char *desc)
{
int size_dest = params->height * params->width;
int length;
length = 0;
while (*desc != '\0') {
if (*desc >= 'a' && *desc <= 'z') {
length += *desc - 'a';
} else if (*desc < '0' || *desc > '9')
return "Invalid character in game description";
length++;
desc++;
}
if (length != size_dest) {
return "Desc size mismatch";
}
return NULL;
}
static game_state *new_game(midend *me, const game_params *params,
const char *desc)
{
game_state *state = snew(game_state);
char *curr_desc = dupstr(desc);
char *desc_base = curr_desc;
int dest_loc;
int spaces, total_spaces;
state->cheating = false;
state->not_completed_clues = 0;
dest_loc = 0;
state->height = params->height;
state->width = params->width;
state->cells_contents = snewn(params->height * params->width, char);
memset(state->cells_contents, 0, params->height * params->width);
state->board = snew(board_state);
state->board->references = 1;
state->board->actual_board =
snewn(params->height * params->width, struct board_cell);
while (*curr_desc != '\0') {
if (*curr_desc >= '0' && *curr_desc <= '9') {
state->board->actual_board[dest_loc].shown = true;
state->not_completed_clues++;
state->board->actual_board[dest_loc].clue = *curr_desc - '0';
} else {
if (*curr_desc != ' ') {
total_spaces = *curr_desc - 'a' + 1;
} else {
total_spaces = 1;
}
spaces = 0;
while (spaces < total_spaces) {
state->board->actual_board[dest_loc].shown = false;
state->board->actual_board[dest_loc].clue = -1;
spaces++;
if (spaces < total_spaces) {
dest_loc++;
}
}
}
curr_desc++;
dest_loc++;
}
sfree(desc_base);
return state;
}
static game_state *dup_game(const game_state *state)
{
game_state *ret = snew(game_state);
ret->cheating = state->cheating;
ret->not_completed_clues = state->not_completed_clues;
ret->width = state->width;
ret->height = state->height;
ret->cells_contents = snewn(state->height * state->width, char);
memcpy(ret->cells_contents, state->cells_contents,
state->height * state->width);
ret->board = state->board;
ret->board->references++;
return ret;
}
static void free_game(game_state *state)
{
sfree(state->cells_contents);
state->cells_contents = NULL;
if (state->board->references <= 1) {
sfree(state->board->actual_board);
sfree(state->board);
state->board = NULL;
} else {
state->board->references--;
}
sfree(state);
}
static char *solve_game(const game_state *state,
const game_state *currstate, const char *aux,
const char **error)
{
struct solution_cell *sol = NULL;
game_params param;
bool solved;
char *ret = NULL;
unsigned int curr_ret;
int i, bits, ret_loc = 1;
int size = state->width * state->height;
param.width = state->width;
param.height = state->height;
solved = solve_game_actual(¶m, state->board->actual_board, &sol);
if (!solved) {
*error = dupstr("Could not solve this board");
sfree(sol);
return NULL;
}
ret = snewn((size / 4) + 3, char);
ret[0] = 's';
i = 0;
while (i < size) {
curr_ret = 0;
bits = 0;
while (bits < 8 && i < size) {
curr_ret <<= 1;
curr_ret |= sol[i].cell == STATE_MARKED;
i++;
bits++;
}
curr_ret <<= 8 - bits;
sprintf(ret + ret_loc, "%02x", curr_ret);
ret_loc += 2;
}
sfree(sol);
return ret;
}
static bool game_can_format_as_text_now(const game_params *params)
{
return true;
}
static char *game_text_format(const game_state *state)
{
size_t desc_len = state->height * (state->width * 3 + 1);
char *desc_string = snewn(desc_len + 1, char);
int location_in_str = 0, x, y;
for (y = 0; y < state->height; y++) {
for (x = 0; x < state->width; x++) {
if (state->board->actual_board[(y * state->width) + x].shown) {
sprintf(desc_string + location_in_str, "|%d|",
state->board->actual_board[(y * state->width) +
x].clue);
} else {
sprintf(desc_string + location_in_str, "| |");
}
location_in_str += 3;
}
sprintf(desc_string + location_in_str, "\n");
location_in_str += 1;
}
assert(location_in_str == desc_len);