-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopy.c
3904 lines (3422 loc) · 127 KB
/
loopy.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
/*
* loopy.c:
*
* An implementation of the Nikoli game 'Loop the loop'.
* (c) Mike Pinna, 2005, 2006
* Substantially rewritten to allowing for more general types of grid.
* (c) Lambros Lambrou 2008
*
* vim: set shiftwidth=4 :set textwidth=80:
*/
/*
* Possible future solver enhancements:
*
* - There's an interesting deductive technique which makes use
* of topology rather than just graph theory. Each _face_ in
* the grid is either inside or outside the loop; you can tell
* that two faces are on the same side of the loop if they're
* separated by a LINE_NO (or, more generally, by a path
* crossing no LINE_UNKNOWNs and an even number of LINE_YESes),
* and on the opposite side of the loop if they're separated by
* a LINE_YES (or an odd number of LINE_YESes and no
* LINE_UNKNOWNs). Oh, and any face separated from the outside
* of the grid by a LINE_YES or a LINE_NO is on the inside or
* outside respectively. So if you can track this for all
* faces, you figure out the state of the line between a pair
* once their relative insideness is known.
* + The way I envisage this working is simply to keep a flip dsf
* of all _faces_, which indicates whether they're on
* opposite sides of the loop from one another. We also
* include a special entry in the dsf for the infinite
* exterior "face".
* + So, the simple way to do this is to just go through the
* edges: every time we see an edge in a state other than
* LINE_UNKNOWN which separates two faces that aren't in the
* same dsf class, we can rectify that by merging the
* classes. Then, conversely, an edge in LINE_UNKNOWN state
* which separates two faces that _are_ in the same dsf
* class can immediately have its state determined.
* + But you can go one better, if you're prepared to loop
* over all _pairs_ of edges. Suppose we have edges A and B,
* which respectively separate faces A1,A2 and B1,B2.
* Suppose that A,B are in the same edge-dsf class and that
* A1,B1 (wlog) are in the same face-dsf class; then we can
* immediately place A2,B2 into the same face-dsf class (as
* each other, not as A1 and A2) one way round or the other.
* And conversely again, if A1,B1 are in the same face-dsf
* class and so are A2,B2, then we can put A,B into the same
* face-dsf class.
* * Of course, this deduction requires a quadratic-time
* loop over all pairs of edges in the grid, so it should
* be reserved until there's nothing easier left to be
* done.
*
* - The generalised grid support has made me (SGT) notice a
* possible extension to the loop-avoidance code. When you have
* a path of connected edges such that no other edges at all
* are incident on any vertex in the middle of the path - or,
* alternatively, such that any such edges are already known to
* be LINE_NO - then you know those edges are either all
* LINE_YES or all LINE_NO. Hence you can mentally merge the
* entire path into a single long curly edge for the purposes
* of loop avoidance, and look directly at whether or not the
* extreme endpoints of the path are connected by some other
* route. I find this coming up fairly often when I play on the
* octagonal grid setting, so it might be worth implementing in
* the solver.
*
* - (Just a speed optimisation.) Consider some todo list queue where every
* time we modify something we mark it for consideration by other bits of
* the solver, to save iteration over things that have already been done.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#ifdef NO_TGMATH_H
# include <math.h>
#else
# include <tgmath.h>
#endif
#include "puzzles.h"
#include "tree234.h"
#include "grid.h"
#include "loopgen.h"
/* Debugging options */
/*
#define DEBUG_CACHES
#define SHOW_WORKING
#define DEBUG_DLINES
*/
/* ----------------------------------------------------------------------
* Struct, enum and function declarations
*/
enum {
COL_BACKGROUND,
COL_FOREGROUND,
COL_LINEUNKNOWN,
COL_HIGHLIGHT,
COL_MISTAKE,
COL_SATISFIED,
COL_FAINT,
NCOLOURS
};
struct game_state {
grid *game_grid; /* ref-counted (internally) */
/* Put -1 in a face that doesn't get a clue */
signed char *clues;
/* Array of line states, to store whether each line is
* YES, NO or UNKNOWN */
char *lines;
bool *line_errors;
bool exactly_one_loop;
bool solved;
bool cheated;
/* Used in game_text_format(), so that it knows what type of
* grid it's trying to render as ASCII text. */
int grid_type;
};
enum solver_status {
SOLVER_SOLVED, /* This is the only solution the solver could find */
SOLVER_MISTAKE, /* This is definitely not a solution */
SOLVER_AMBIGUOUS, /* This _might_ be an ambiguous solution */
SOLVER_INCOMPLETE /* This may be a partial solution */
};
/* ------ Solver state ------ */
typedef struct solver_state {
game_state *state;
enum solver_status solver_status;
/* NB looplen is the number of dots that are joined together at a point, ie a
* looplen of 1 means there are no lines to a particular dot */
int *looplen;
/* Difficulty level of solver. Used by solver functions that want to
* vary their behaviour depending on the requested difficulty level. */
int diff;
/* caches */
char *dot_yes_count;
char *dot_no_count;
char *face_yes_count;
char *face_no_count;
bool *dot_solved, *face_solved;
DSF *dotdsf;
/* Information for Normal level deductions:
* For each dline, store a bitmask for whether we know:
* (bit 0) at least one is YES
* (bit 1) at most one is YES */
char *dlines;
/* Hard level information */
DSF *linedsf;
} solver_state;
/*
* Difficulty levels. I do some macro ickery here to ensure that my
* enum and the various forms of my name list always match up.
*/
#define DIFFLIST(A) \
A(EASY,Easy,e) \
A(NORMAL,Normal,n) \
A(TRICKY,Tricky,t) \
A(HARD,Hard,h)
#define ENUM(upper,title,lower) DIFF_ ## upper,
#define TITLE(upper,title,lower) #title,
#define ENCODE(upper,title,lower) #lower
#define CONFIG(upper,title,lower) ":" #title
enum { DIFFLIST(ENUM) DIFF_MAX };
static char const *const diffnames[] = { DIFFLIST(TITLE) };
static char const diffchars[] = DIFFLIST(ENCODE);
#define DIFFCONFIG DIFFLIST(CONFIG)
/*
* Solver routines, sorted roughly in order of computational cost.
* The solver will run the faster deductions first, and slower deductions are
* only invoked when the faster deductions are unable to make progress.
* Each function is associated with a difficulty level, so that the generated
* puzzles are solvable by applying only the functions with the chosen
* difficulty level or lower.
*/
#define SOLVERLIST(A) \
A(trivial_deductions, DIFF_EASY) \
A(dline_deductions, DIFF_NORMAL) \
A(linedsf_deductions, DIFF_HARD) \
A(loop_deductions, DIFF_EASY)
#define SOLVER_FN_DECL(fn,diff) static int fn(solver_state *);
#define SOLVER_FN(fn,diff) &fn,
#define SOLVER_DIFF(fn,diff) diff,
SOLVERLIST(SOLVER_FN_DECL)
static int (*(solver_fns[]))(solver_state *) = { SOLVERLIST(SOLVER_FN) };
static int const solver_diffs[] = { SOLVERLIST(SOLVER_DIFF) };
static const int NUM_SOLVERS = sizeof(solver_diffs)/sizeof(*solver_diffs);
struct game_params {
int w, h;
int diff;
int type;
};
/* line_drawstate is the same as line_state, but with the extra ERROR
* possibility. The drawing code copies line_state to line_drawstate,
* except in the case that the line is an error. */
enum line_state { LINE_YES, LINE_UNKNOWN, LINE_NO };
enum line_drawstate { DS_LINE_YES, DS_LINE_UNKNOWN,
DS_LINE_NO, DS_LINE_ERROR };
#define OPP(line_state) \
(2 - line_state)
struct game_drawstate {
bool started;
int tilesize;
bool flashing;
int *textx, *texty;
char *lines;
bool *clue_error;
bool *clue_satisfied;
};
static const char *validate_desc(const game_params *params, const char *desc);
static int dot_order(const game_state* state, int i, char line_type);
static int face_order(const game_state* state, int i, char line_type);
static solver_state *solve_game_rec(const solver_state *sstate);
#ifdef DEBUG_CACHES
static void check_caches(const solver_state* sstate);
#else
#define check_caches(s)
#endif
/*
* Grid type config options available in Loopy.
*
* Annoyingly, we have to use an enum here which doesn't match up
* exactly to the grid-type enum in grid.h. Values in params->types
* are given by names such as LOOPY_GRID_SQUARE, which shouldn't be
* confused with GRID_SQUARE which is the value you pass to grid_new()
* and friends. So beware!
*
* (This is partly for historical reasons - Loopy's version of the
* enum is encoded in game parameter strings, so we keep it for
* backwards compatibility. But also, we need to store additional data
* here alongside each enum value, such as names for the presets menu,
* which isn't stored in grid.h; so we have to have our own list macro
* here anyway, and C doesn't make it easy to enforce that that lines
* up exactly with grid.h.)
*
* Do not add values to this list _except_ at the end, or old game ids
* will stop working!
*/
#define GRIDLIST(A) \
A("Squares",SQUARE,3,3) \
A("Triangular",TRIANGULAR,3,3) \
A("Honeycomb",HONEYCOMB,3,3) \
A("Snub-Square",SNUBSQUARE,3,3) \
A("Cairo",CAIRO,3,4) \
A("Great-Hexagonal",GREATHEXAGONAL,3,3) \
A("Octagonal",OCTAGONAL,3,3) \
A("Kites",KITE,3,3) \
A("Floret",FLORET,1,2) \
A("Dodecagonal",DODECAGONAL,2,2) \
A("Great-Dodecagonal",GREATDODECAGONAL,2,2) \
A("Penrose (kite/dart)",PENROSE_P2,3,3) \
A("Penrose (rhombs)",PENROSE_P3,3,3) \
A("Great-Great-Dodecagonal",GREATGREATDODECAGONAL,2,2) \
A("Kagome",KAGOME,3,3) \
A("Compass-Dodecagonal",COMPASSDODECAGONAL,2,2) \
A("Hats",HATS,6,6) \
A("Spectres",SPECTRES,6,6) \
/* end of list */
#define GRID_NAME(title,type,amin,omin) title,
#define GRID_CONFIG(title,type,amin,omin) ":" title
#define GRID_LOOPYTYPE(title,type,amin,omin) LOOPY_GRID_ ## type,
#define GRID_GRIDTYPE(title,type,amin,omin) GRID_ ## type,
#define GRID_SIZES(title,type,amin,omin) \
{amin, omin, \
"Width and height for this grid type must both be at least " #amin, \
"At least one of width and height for this grid type must be at least " #omin,},
enum { GRIDLIST(GRID_LOOPYTYPE) LOOPY_GRID_DUMMY_TERMINATOR };
static char const *const gridnames[] = { GRIDLIST(GRID_NAME) };
#define GRID_CONFIGS GRIDLIST(GRID_CONFIG)
static grid_type grid_types[] = { GRIDLIST(GRID_GRIDTYPE) };
#define NUM_GRID_TYPES (sizeof(grid_types) / sizeof(grid_types[0]))
static const struct {
int amin, omin;
const char *aerr, *oerr;
} grid_size_limits[] = { GRIDLIST(GRID_SIZES) };
/* Generates a (dynamically allocated) new grid, according to the
* type and size requested in params. Does nothing if the grid is already
* generated. */
static grid *loopy_generate_grid(const game_params *params,
const char *grid_desc)
{
return grid_new(grid_types[params->type], params->w, params->h, grid_desc);
}
/* ----------------------------------------------------------------------
* Preprocessor magic
*/
/* General constants */
#define PREFERRED_TILE_SIZE 32
#define BORDER(tilesize) ((tilesize) / 2)
#define FLASH_TIME 0.5F
#define BIT_SET(field, bit) ((field) & (1<<(bit)))
#define SET_BIT(field, bit) (BIT_SET(field, bit) ? false : \
((field) |= (1<<(bit)), true))
#define CLEAR_BIT(field, bit) (BIT_SET(field, bit) ? \
((field) &= ~(1<<(bit)), true) : false)
#define CLUE2CHAR(c) \
((c < 0) ? ' ' : c < 10 ? c + '0' : c - 10 + 'A')
/* ----------------------------------------------------------------------
* General struct manipulation and other straightforward code
*/
static game_state *dup_game(const game_state *state)
{
game_state *ret = snew(game_state);
ret->game_grid = state->game_grid;
ret->game_grid->refcount++;
ret->solved = state->solved;
ret->cheated = state->cheated;
ret->clues = snewn(state->game_grid->num_faces, signed char);
memcpy(ret->clues, state->clues, state->game_grid->num_faces);
ret->lines = snewn(state->game_grid->num_edges, char);
memcpy(ret->lines, state->lines, state->game_grid->num_edges);
ret->line_errors = snewn(state->game_grid->num_edges, bool);
memcpy(ret->line_errors, state->line_errors,
state->game_grid->num_edges * sizeof(bool));
ret->exactly_one_loop = state->exactly_one_loop;
ret->grid_type = state->grid_type;
return ret;
}
static void free_game(game_state *state)
{
if (state) {
grid_free(state->game_grid);
sfree(state->clues);
sfree(state->lines);
sfree(state->line_errors);
sfree(state);
}
}
static solver_state *new_solver_state(const game_state *state, int diff) {
int i;
int num_dots = state->game_grid->num_dots;
int num_faces = state->game_grid->num_faces;
int num_edges = state->game_grid->num_edges;
solver_state *ret = snew(solver_state);
ret->state = dup_game(state);
ret->solver_status = SOLVER_INCOMPLETE;
ret->diff = diff;
ret->dotdsf = dsf_new(num_dots);
ret->looplen = snewn(num_dots, int);
for (i = 0; i < num_dots; i++) {
ret->looplen[i] = 1;
}
ret->dot_solved = snewn(num_dots, bool);
ret->face_solved = snewn(num_faces, bool);
memset(ret->dot_solved, 0, num_dots * sizeof(bool));
memset(ret->face_solved, 0, num_faces * sizeof(bool));
ret->dot_yes_count = snewn(num_dots, char);
memset(ret->dot_yes_count, 0, num_dots);
ret->dot_no_count = snewn(num_dots, char);
memset(ret->dot_no_count, 0, num_dots);
ret->face_yes_count = snewn(num_faces, char);
memset(ret->face_yes_count, 0, num_faces);
ret->face_no_count = snewn(num_faces, char);
memset(ret->face_no_count, 0, num_faces);
if (diff < DIFF_NORMAL) {
ret->dlines = NULL;
} else {
ret->dlines = snewn(2*num_edges, char);
memset(ret->dlines, 0, 2*num_edges);
}
if (diff < DIFF_HARD) {
ret->linedsf = NULL;
} else {
ret->linedsf = dsf_new_flip(state->game_grid->num_edges);
}
return ret;
}
static void free_solver_state(solver_state *sstate) {
if (sstate) {
free_game(sstate->state);
dsf_free(sstate->dotdsf);
sfree(sstate->looplen);
sfree(sstate->dot_solved);
sfree(sstate->face_solved);
sfree(sstate->dot_yes_count);
sfree(sstate->dot_no_count);
sfree(sstate->face_yes_count);
sfree(sstate->face_no_count);
/* OK, because sfree(NULL) is a no-op */
sfree(sstate->dlines);
dsf_free(sstate->linedsf);
sfree(sstate);
}
}
static solver_state *dup_solver_state(const solver_state *sstate) {
game_state *state = sstate->state;
int num_dots = state->game_grid->num_dots;
int num_faces = state->game_grid->num_faces;
int num_edges = state->game_grid->num_edges;
solver_state *ret = snew(solver_state);
ret->state = state = dup_game(sstate->state);
ret->solver_status = sstate->solver_status;
ret->diff = sstate->diff;
ret->dotdsf = dsf_new(num_dots);
ret->looplen = snewn(num_dots, int);
dsf_copy(ret->dotdsf, sstate->dotdsf);
memcpy(ret->looplen, sstate->looplen,
num_dots * sizeof(int));
ret->dot_solved = snewn(num_dots, bool);
ret->face_solved = snewn(num_faces, bool);
memcpy(ret->dot_solved, sstate->dot_solved, num_dots * sizeof(bool));
memcpy(ret->face_solved, sstate->face_solved, num_faces * sizeof(bool));
ret->dot_yes_count = snewn(num_dots, char);
memcpy(ret->dot_yes_count, sstate->dot_yes_count, num_dots);
ret->dot_no_count = snewn(num_dots, char);
memcpy(ret->dot_no_count, sstate->dot_no_count, num_dots);
ret->face_yes_count = snewn(num_faces, char);
memcpy(ret->face_yes_count, sstate->face_yes_count, num_faces);
ret->face_no_count = snewn(num_faces, char);
memcpy(ret->face_no_count, sstate->face_no_count, num_faces);
if (sstate->dlines) {
ret->dlines = snewn(2*num_edges, char);
memcpy(ret->dlines, sstate->dlines,
2*num_edges);
} else {
ret->dlines = NULL;
}
if (sstate->linedsf) {
ret->linedsf = dsf_new_flip(num_edges);
dsf_copy(ret->linedsf, sstate->linedsf);
} else {
ret->linedsf = NULL;
}
return ret;
}
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
#ifdef SLOW_SYSTEM
ret->h = 7;
ret->w = 7;
#else
ret->h = 10;
ret->w = 10;
#endif
ret->diff = DIFF_EASY;
ret->type = 0;
return ret;
}
static game_params *dup_params(const game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
return ret;
}
static const game_params loopy_presets_top[] = {
#ifdef SMALL_SCREEN
{ 7, 7, DIFF_EASY, LOOPY_GRID_SQUARE },
{ 7, 7, DIFF_NORMAL, LOOPY_GRID_SQUARE },
{ 7, 7, DIFF_HARD, LOOPY_GRID_SQUARE },
{ 7, 7, DIFF_HARD, LOOPY_GRID_TRIANGULAR },
{ 5, 5, DIFF_HARD, LOOPY_GRID_SNUBSQUARE },
{ 7, 7, DIFF_HARD, LOOPY_GRID_CAIRO },
{ 5, 5, DIFF_HARD, LOOPY_GRID_KITE },
{ 6, 6, DIFF_HARD, LOOPY_GRID_PENROSE_P2 },
{ 6, 6, DIFF_HARD, LOOPY_GRID_PENROSE_P3 },
#else
{ 7, 7, DIFF_EASY, LOOPY_GRID_SQUARE },
{ 10, 10, DIFF_EASY, LOOPY_GRID_SQUARE },
{ 7, 7, DIFF_NORMAL, LOOPY_GRID_SQUARE },
{ 10, 10, DIFF_NORMAL, LOOPY_GRID_SQUARE },
{ 7, 7, DIFF_HARD, LOOPY_GRID_SQUARE },
{ 10, 10, DIFF_HARD, LOOPY_GRID_SQUARE },
{ 12, 10, DIFF_HARD, LOOPY_GRID_TRIANGULAR },
{ 7, 7, DIFF_HARD, LOOPY_GRID_SNUBSQUARE },
{ 9, 9, DIFF_HARD, LOOPY_GRID_CAIRO },
{ 5, 5, DIFF_HARD, LOOPY_GRID_KITE },
{ 10, 10, DIFF_HARD, LOOPY_GRID_PENROSE_P2 },
{ 10, 10, DIFF_HARD, LOOPY_GRID_PENROSE_P3 },
#endif
};
static const game_params loopy_presets_more[] = {
#ifdef SMALL_SCREEN
{ 7, 7, DIFF_HARD, LOOPY_GRID_HONEYCOMB },
{ 5, 4, DIFF_HARD, LOOPY_GRID_GREATHEXAGONAL },
{ 5, 4, DIFF_HARD, LOOPY_GRID_KAGOME },
{ 5, 5, DIFF_HARD, LOOPY_GRID_OCTAGONAL },
{ 3, 3, DIFF_HARD, LOOPY_GRID_FLORET },
{ 3, 3, DIFF_HARD, LOOPY_GRID_DODECAGONAL },
{ 3, 3, DIFF_HARD, LOOPY_GRID_GREATDODECAGONAL },
{ 3, 2, DIFF_HARD, LOOPY_GRID_GREATGREATDODECAGONAL },
{ 3, 3, DIFF_HARD, LOOPY_GRID_COMPASSDODECAGONAL },
{ 6, 6, DIFF_HARD, LOOPY_GRID_HATS },
{ 6, 6, DIFF_HARD, LOOPY_GRID_SPECTRES },
#else
{ 10, 10, DIFF_HARD, LOOPY_GRID_HONEYCOMB },
{ 5, 4, DIFF_HARD, LOOPY_GRID_GREATHEXAGONAL },
{ 5, 4, DIFF_HARD, LOOPY_GRID_KAGOME },
{ 7, 7, DIFF_HARD, LOOPY_GRID_OCTAGONAL },
{ 5, 5, DIFF_HARD, LOOPY_GRID_FLORET },
{ 5, 4, DIFF_HARD, LOOPY_GRID_DODECAGONAL },
{ 5, 4, DIFF_HARD, LOOPY_GRID_GREATDODECAGONAL },
{ 5, 3, DIFF_HARD, LOOPY_GRID_GREATGREATDODECAGONAL },
{ 5, 4, DIFF_HARD, LOOPY_GRID_COMPASSDODECAGONAL },
{ 10, 10, DIFF_HARD, LOOPY_GRID_HATS },
{ 10, 10, DIFF_HARD, LOOPY_GRID_SPECTRES },
#endif
};
static void preset_menu_add_preset_with_title(struct preset_menu *menu,
const game_params *params)
{
char buf[80];
game_params *dup_params;
sprintf(buf, "%dx%d %s - %s", params->h, params->w,
gridnames[params->type], diffnames[params->diff]);
dup_params = snew(game_params);
*dup_params = *params;
preset_menu_add_preset(menu, dupstr(buf), dup_params);
}
static struct preset_menu *game_preset_menu(void)
{
struct preset_menu *top, *more;
int i;
top = preset_menu_new();
for (i = 0; i < lenof(loopy_presets_top); i++)
preset_menu_add_preset_with_title(top, &loopy_presets_top[i]);
more = preset_menu_add_submenu(top, dupstr("More..."));
for (i = 0; i < lenof(loopy_presets_more); i++)
preset_menu_add_preset_with_title(more, &loopy_presets_more[i]);
return top;
}
static void free_params(game_params *params)
{
sfree(params);
}
static void decode_params(game_params *params, char const *string)
{
params->h = params->w = atoi(string);
params->diff = DIFF_EASY;
while (*string && isdigit((unsigned char)*string)) string++;
if (*string == 'x') {
string++;
params->h = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
}
if (*string == 't') {
string++;
params->type = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
}
if (*string == 'd') {
int i;
string++;
for (i = 0; i < DIFF_MAX; i++)
if (*string == diffchars[i])
params->diff = i;
if (*string) string++;
}
}
static char *encode_params(const game_params *params, bool full)
{
char str[80];
sprintf(str, "%dx%dt%d", params->w, params->h, params->type);
if (full)
sprintf(str + strlen(str), "d%c", diffchars[params->diff]);
return dupstr(str);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(5, config_item);
ret[0].name = "Width";
ret[0].type = C_STRING;
sprintf(buf, "%d", params->w);
ret[0].u.string.sval = dupstr(buf);
ret[1].name = "Height";
ret[1].type = C_STRING;
sprintf(buf, "%d", params->h);
ret[1].u.string.sval = dupstr(buf);
ret[2].name = "Grid type";
ret[2].type = C_CHOICES;
ret[2].u.choices.choicenames = GRID_CONFIGS;
ret[2].u.choices.selected = params->type;
ret[3].name = "Difficulty";
ret[3].type = C_CHOICES;
ret[3].u.choices.choicenames = DIFFCONFIG;
ret[3].u.choices.selected = params->diff;
ret[4].name = NULL;
ret[4].type = C_END;
return ret;
}
static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
ret->w = atoi(cfg[0].u.string.sval);
ret->h = atoi(cfg[1].u.string.sval);
ret->type = cfg[2].u.choices.selected;
ret->diff = cfg[3].u.choices.selected;
return ret;
}
static const char *validate_params(const game_params *params, bool full)
{
const char *err;
if (params->type < 0 || params->type >= NUM_GRID_TYPES)
return "Illegal grid type";
if (params->w < grid_size_limits[params->type].amin ||
params->h < grid_size_limits[params->type].amin)
return grid_size_limits[params->type].aerr;
if (params->w < grid_size_limits[params->type].omin &&
params->h < grid_size_limits[params->type].omin)
return grid_size_limits[params->type].oerr;
err = grid_validate_params(grid_types[params->type], params->w, params->h);
if (err != NULL) return err;
/*
* This shouldn't be able to happen at all, since decode_params
* and custom_params will never generate anything that isn't
* within range.
*/
assert(params->diff < DIFF_MAX);
return NULL;
}
/* Returns a newly allocated string describing the current puzzle */
static char *state_to_text(const game_state *state)
{
grid *g = state->game_grid;
char *retval;
int num_faces = g->num_faces;
char *description = snewn(num_faces + 1, char);
char *dp = description;
int empty_count = 0;
int i;
for (i = 0; i < num_faces; i++) {
if (state->clues[i] < 0) {
if (empty_count > 25) {
dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1));
empty_count = 0;
}
empty_count++;
} else {
if (empty_count) {
dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1));
empty_count = 0;
}
dp += sprintf(dp, "%c", (int)CLUE2CHAR(state->clues[i]));
}
}
if (empty_count)
dp += sprintf(dp, "%c", (int)(empty_count + 'a' - 1));
retval = dupstr(description);
sfree(description);
return retval;
}
#define GRID_DESC_SEP '_'
/* Splits up a (optional) grid_desc from the game desc. Returns the
* grid_desc (which needs freeing) and updates the desc pointer to
* start of real desc, or returns NULL if no desc. */
static char *extract_grid_desc(const char **desc)
{
char *sep = strchr(*desc, GRID_DESC_SEP), *gd;
int gd_len;
if (!sep) return NULL;
gd_len = sep - (*desc);
gd = snewn(gd_len+1, char);
memcpy(gd, *desc, gd_len);
gd[gd_len] = '\0';
*desc = sep+1;
return gd;
}
/* We require that the params pass the test in validate_params and that the
* description fills the entire game area */
static const char *validate_desc(const game_params *params, const char *desc)
{
int count = 0;
grid *g;
char *grid_desc;
const char *ret;
/* It's pretty inefficient to do this just for validation. All we need to
* know is the precise number of faces. */
grid_desc = extract_grid_desc(&desc);
ret = grid_validate_desc(grid_types[params->type], params->w, params->h, grid_desc);
if (ret) {
sfree(grid_desc);
return ret;
}
g = loopy_generate_grid(params, grid_desc);
sfree(grid_desc);
for (; *desc; ++desc) {
if ((*desc >= '0' && *desc <= '9') || (*desc >= 'A' && *desc <= 'Z')) {
count++;
continue;
}
if (*desc >= 'a') {
count += *desc - 'a' + 1;
continue;
}
grid_free(g);
return "Unknown character in description";
}
if (count < g->num_faces) {
grid_free(g);
return "Description too short for board size";
}
if (count > g->num_faces) {
grid_free(g);
return "Description too long for board size";
}
grid_free(g);
return NULL;
}
/* Sums the lengths of the numbers in range [0,n) */
/* See equivalent function in solo.c for justification of this. */
static int len_0_to_n(int n)
{
int len = 1; /* Counting 0 as a bit of a special case */
int i;
for (i = 1; i < n; i *= 10) {
len += max(n - i, 0);
}
return len;
}
static char *encode_solve_move(const game_state *state)
{
int len;
char *ret, *p;
int i;
int num_edges = state->game_grid->num_edges;
/* This is going to return a string representing the moves needed to set
* every line in a grid to be the same as the ones in 'state'. The exact
* length of this string is predictable. */
len = 1; /* Count the 'S' prefix */
/* Numbers in all lines */
len += len_0_to_n(num_edges);
/* For each line we also have a letter */
len += num_edges;
ret = snewn(len + 1, char);
p = ret;
p += sprintf(p, "S");
for (i = 0; i < num_edges; i++) {
switch (state->lines[i]) {
case LINE_YES:
p += sprintf(p, "%dy", i);
break;
case LINE_NO:
p += sprintf(p, "%dn", i);
break;
}
}
/* No point in doing sums like that if they're going to be wrong */
assert(strlen(ret) <= (size_t)len);
return ret;
}
struct game_ui {
/*
* User preference: should grid lines in LINE_NO state be drawn
* very faintly so users can still see where they are, or should
* they be completely invisible?
*/
bool draw_faint_lines;
/*
* User preference: when clicking an edge that has only one
* possible edge connecting to one (or both) of its ends, should
* that edge also change to the same state as the edge we just
* clicked?
*/
enum {
AF_OFF, /* no, all grid edges are independent in the UI */
AF_FIXED, /* yes, but only based on the grid itself */
AF_ADAPTIVE /* yes, and consider edges user has already set to NO */
} autofollow;
};
static void legacy_prefs_override(struct game_ui *ui_out)
{
static bool initialised = false;
static int draw_faint_lines = -1;
static int autofollow = -1;
if (!initialised) {
char *env;
initialised = true;
draw_faint_lines = getenv_bool("LOOPY_FAINT_LINES", -1);
if ((env = getenv("LOOPY_AUTOFOLLOW")) != NULL) {
if (!strcmp(env, "off"))
autofollow = AF_OFF;
else if (!strcmp(env, "fixed"))
autofollow = AF_FIXED;
else if (!strcmp(env, "adaptive"))
autofollow = AF_ADAPTIVE;
}
}
if (draw_faint_lines != -1)
ui_out->draw_faint_lines = draw_faint_lines;
if (autofollow != -1)
ui_out->autofollow = autofollow;
}
static game_ui *new_ui(const game_state *state)
{
game_ui *ui = snew(game_ui);
ui->draw_faint_lines = true;
ui->autofollow = AF_OFF;
legacy_prefs_override(ui);
return ui;
}
static void free_ui(game_ui *ui)
{
sfree(ui);
}
static config_item *get_prefs(game_ui *ui)
{
config_item *ret;
ret = snewn(3, config_item);
ret[0].name = "Draw excluded grid lines faintly";
ret[0].kw = "draw-faint-lines";
ret[0].type = C_BOOLEAN;
ret[0].u.boolean.bval = ui->draw_faint_lines;
ret[1].name = "Auto-follow unique paths of edges";
ret[1].kw = "auto-follow";
ret[1].type = C_CHOICES;
ret[1].u.choices.choicenames =
":No:Based on grid only:Based on grid and game state";
ret[1].u.choices.choicekws = ":off:fixed:adaptive";
ret[1].u.choices.selected = ui->autofollow;
ret[2].name = NULL;
ret[2].type = C_END;
return ret;
}
static void set_prefs(game_ui *ui, const config_item *cfg)
{
ui->draw_faint_lines = cfg[0].u.boolean.bval;
ui->autofollow = cfg[1].u.choices.selected;
}
static void game_changed_state(game_ui *ui, const game_state *oldstate,
const game_state *newstate)
{
}
static void game_compute_size(const game_params *params, int tilesize,
const game_ui *ui, int *x, int *y)
{
int grid_width, grid_height, rendered_width, rendered_height;
int g_tilesize;
grid_compute_size(grid_types[params->type], params->w, params->h,
&g_tilesize, &grid_width, &grid_height);
/* multiply first to minimise rounding error on integer division */
rendered_width = grid_width * tilesize / g_tilesize;
rendered_height = grid_height * tilesize / g_tilesize;
*x = rendered_width + 2 * BORDER(tilesize) + 1;
*y = rendered_height + 2 * BORDER(tilesize) + 1;
}
static void game_set_size(drawing *dr, game_drawstate *ds,
const game_params *params, int tilesize)
{
ds->tilesize = tilesize;
}
static float *game_colours(frontend *fe, int *ncolours)
{
float *ret = snewn(3 * NCOLOURS, float);
frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);