-
Notifications
You must be signed in to change notification settings - Fork 3
/
salad.c
2644 lines (2270 loc) · 57.7 KB
/
salad.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
/*
* salad.c: Implemention of ABC End View puzzles.
* (C) 2013 Lennard Sprong
* Created for Simon Tatham's Portable Puzzle Collection
* See LICENCE for licence details
*
* This puzzle has two different game modes: ABC End View and Number Ball.
* Objective: Enter each letter once in each row and column.
* Some squares remain empty.
* ABC End View: The letters on the edge indicate which letter
* is encountered first when 'looking' into the grid.
* Number Ball: A circle indicates that a number must be placed here,
* and a cross indicates a space that remains empty.
*
* Number Ball was invented by Inaba Naoki.
* I don't know who first designed ABC End View.
*
* http://www.janko.at/Raetsel/AbcEndView/index.htm
* http://www.janko.at/Raetsel/Nanbaboru/index.htm
*/
/*
* TODO:
* - Add difficulty levels
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include "puzzles.h"
#include "latin.h"
enum { GAMEMODE_LETTERS, GAMEMODE_NUMBERS };
enum {
COL_BACKGROUND,
COL_HIGHLIGHT,
COL_LOWLIGHT,
COL_BORDER,
COL_BORDERCLUE,
COL_PENCIL,
COL_I_NUM, /* Immutable */
COL_I_BALL,
COL_I_BALLBG,
COL_I_HOLE,
COL_G_NUM, /* Guess */
COL_G_BALL,
COL_G_BALLBG,
COL_G_HOLE,
COL_E_BORDERCLUE,
COL_E_NUM, /* Error */
COL_E_HOLE,
NCOLOURS
};
#define DIFFLIST(A) \
A(EASY,Normal,salad_solver_easy, e) \
A(HARD,Extreme,NULL,x)
#define ENUM(upper,title,func,lower) DIFF_ ## upper,
#define TITLE(upper,title,func,lower) #title,
#define ENCODE(upper,title,func,lower) #lower
#define CONFIG(upper,title,func,lower) ":" #title
static char const salad_diffchars[] = DIFFLIST(ENCODE);
#define DIFFCOUNT lenof(salad_diffchars)
enum { DIFFLIST(ENUM) DIFF_IMPOSSIBLE = diff_impossible,
DIFF_AMBIGUOUS = diff_ambiguous, DIFF_UNFINISHED = diff_unfinished };
#define DIFF_HOLESONLY (DIFF_EASY - 1)
struct game_params {
int order;
int nums;
int mode;
int diff;
};
struct game_state {
game_params *params;
digit *borderclues;
digit *gridclues;
digit *grid;
/* Extra map of confirmed holes/characters */
char *holes;
bool completed, cheated;
unsigned int *marks;
};
#define DEFAULT_PRESET 0
static const struct game_params salad_presets[] = {
{4, 3, GAMEMODE_LETTERS, DIFF_EASY},
{5, 3, GAMEMODE_LETTERS, DIFF_EASY},
{5, 3, GAMEMODE_NUMBERS, DIFF_EASY},
{5, 4, GAMEMODE_LETTERS, DIFF_EASY},
{6, 3, GAMEMODE_NUMBERS, DIFF_EASY},
{6, 4, GAMEMODE_LETTERS, DIFF_EASY},
{6, 4, GAMEMODE_NUMBERS, DIFF_EASY},
{7, 4, GAMEMODE_LETTERS, DIFF_EASY},
{7, 4, GAMEMODE_NUMBERS, DIFF_EASY},
{8, 5, GAMEMODE_LETTERS, DIFF_EASY},
{8, 5, GAMEMODE_NUMBERS, DIFF_EASY},
};
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
*ret = salad_presets[DEFAULT_PRESET]; /* struct copy */
return ret;
}
static bool game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret;
char buf[64];
if(i < 0 || i >= lenof(salad_presets))
return false;
ret = snew(game_params);
*ret = salad_presets[i]; /* struct copy */
*params = ret;
if(ret->mode == GAMEMODE_LETTERS)
sprintf(buf, "Letters: %dx%d A~%c", ret->order, ret->order, ret->nums + 'A' - 1);
else
sprintf(buf, "Numbers: %dx%d 1~%c", ret->order, ret->order, ret->nums + '0');
*name = dupstr(buf);
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)
{
char const *p = string;
params->order = atoi(string);
while (*p && isdigit((unsigned char)*p)) ++p;
if (*p == 'n')
{
++p;
params->nums = atoi(p);
while (*p && isdigit((unsigned char)*p)) ++p;
}
if (*p == 'B')
{
params->mode = GAMEMODE_NUMBERS;
}
else if(*p == 'L')
{
params->mode = GAMEMODE_LETTERS;
}
if (*p == 'd') {
int i;
p++;
params->diff = DIFFCOUNT + 1; /* ...which is invalid */
if (*p) {
for (i = 0; i < DIFFCOUNT; i++) {
if (*p == salad_diffchars[i])
params->diff = i;
}
p++;
}
}
}
static char *encode_params(const game_params *params, bool full)
{
char ret[80];
sprintf(ret, "%dn%d%c", params->order, params->nums,
params->mode == GAMEMODE_LETTERS ? 'L' : 'B');
if (full)
sprintf(ret + strlen(ret), "d%c", salad_diffchars[params->diff]);
return dupstr(ret);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(5, config_item);
ret[0].name = "Game Mode";
ret[0].type = C_CHOICES;
ret[0].u.choices.choicenames = ":ABC End View:Number Ball";
ret[0].u.choices.selected = params->mode;
ret[1].name = "Size (s*s)";
ret[1].type = C_STRING;
sprintf(buf, "%d", params->order);
ret[1].u.string.sval = dupstr(buf);
ret[2].name = "Symbols";
ret[2].type = C_STRING;
sprintf(buf, "%d", params->nums);
ret[2].u.string.sval = dupstr(buf);
ret[3].name = "Difficulty";
ret[3].type = C_CHOICES;
ret[3].u.choices.choicenames = DIFFLIST(CONFIG);
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 = default_params();
ret->mode = cfg[0].u.choices.selected;
ret->order = atoi(cfg[1].u.string.sval);
ret->nums = atoi(cfg[2].u.string.sval);
ret->diff = cfg[3].u.choices.selected;
return ret;
}
static const char *validate_params(const game_params *params, bool full)
{
if(params->nums < 2)
return "Symbols must be at least 2.";
if(params->nums >= params->order)
return "Symbols must be lower than the size.";
if(params->order < 3)
return "Size must be at least 3.";
if(params->nums > 9)
return "Symbols must be no more than 9.";
if (params->diff >= DIFFCOUNT)
return "Unknown difficulty rating";
return NULL;
}
static game_state *blank_game(const game_params *params)
{
int o = params->order;
int o2 = o*o;
game_state *state = snew(game_state);
state->params = snew(game_params);
*(state->params) = *params; /* structure copy */
state->grid = snewn(o2, digit);
state->holes = snewn(o2, char);
state->borderclues = snewn(o*4, digit);
state->gridclues = snewn(o2, digit);
state->marks = snewn(o2, unsigned int);
state->completed = state->cheated = false;
memset(state->marks, 0, o2 * sizeof(unsigned int));
return state;
}
static game_state *dup_game(const game_state *state)
{
int o = state->params->order;
int o2 = o*o;
game_state *ret = blank_game(state->params);
memcpy(ret->grid, state->grid, o2 * sizeof(digit));
memcpy(ret->holes, state->holes, o2 * sizeof(char));
memcpy(ret->borderclues, state->borderclues, o*4 * sizeof(digit));
memcpy(ret->gridclues, state->gridclues, o2 * sizeof(digit));
memcpy(ret->marks, state->marks, o2 * sizeof(unsigned int));
ret->completed = state->completed;
ret->cheated = state->cheated;
return ret;
}
static void free_game(game_state *state)
{
free_params(state->params);
sfree(state->grid);
sfree(state->holes);
sfree(state->borderclues);
sfree(state->gridclues);
sfree(state->marks);
sfree(state);
}
/* *********************** *
* Latin square with holes *
* *********************** */
/* This square definitely doesn't contain a character */
#define LATINH_CROSS 'X'
/* This square must contain a character */
#define LATINH_CIRCLE 'O'
struct solver_ctx {
game_state *state;
int order;
int nums;
};
static struct solver_ctx *new_ctx(game_state *state, int order, int nums)
{
struct solver_ctx *ctx = snew(struct solver_ctx);
ctx->state = state;
ctx->order = order;
ctx->nums = nums;
return ctx;
}
static void *clone_ctx(void *vctx)
{
struct solver_ctx *octx = (struct solver_ctx *)vctx;
struct solver_ctx *nctx = new_ctx(octx->state, octx->order, octx->nums);
return nctx;
}
static void free_ctx(void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
sfree(ctx);
}
static int latinholes_solver_sync(struct latin_solver *solver, struct solver_ctx *sctx)
{
/* Check the marks for each square, and see if it confirms a square
* being empty or not empty */
int i, n;
int o = solver->o;
int o2 = o*o;
bool match;
int nums = sctx->nums;
int nchanged = 0;
if(nums == o)
return 0;
for(i = 0; i < o2; i++)
{
if(sctx->state->holes[i])
continue;
/* Check for possibilities for numbers */
match = false;
for(n = 0; n < nums; n++)
{
if(cube(i%o, i/o, n+1))
match = true;
}
if(!match)
{
#ifdef STANDALONE_SOLVER
if(solver_show_working)
printf("Synchronize hole at %d\n", i);
#endif
/* This square must be a hole */
nchanged++;
sctx->state->holes[i] = LATINH_CROSS;
continue;
}
/* Check for possibilities for hole */
match = false;
for(n = nums; n < o; n++)
{
if(cube(i%o, i/o, n+1))
match = true;
}
if(!match)
{
#ifdef STANDALONE_SOLVER
if(solver_show_working)
printf("Synchronize number at %d\n", i);
#endif
/* This square must be a number */
nchanged++;
sctx->state->holes[i] = LATINH_CIRCLE;
}
}
return nchanged;
}
static int latinholes_solver_place_cross(struct latin_solver *solver, struct solver_ctx *sctx, int x, int y)
{
int n;
int nchanged = 0;
int nums = sctx->nums;
#ifdef STANDALONE_SOLVER
if(solver_show_working)
printf("Place cross at %d,%d\n", x, y);
#endif
for(n = 0; n < nums; n++)
{
if(!cube(x, y, n+1))
continue;
cube(x, y, n+1) = false;
nchanged++;
}
return nchanged;
}
static int latinholes_solver_place_circle(struct latin_solver *solver, struct solver_ctx *sctx, int x, int y)
{
int n;
int nchanged = 0;
int o = solver->o;
int nums = sctx->nums;
#ifdef STANDALONE_SOLVER
if(solver_show_working)
printf("Place circle at %d,%d\n", x, y);
#endif
for(n = nums; n < o; n++)
{
if(!cube(x, y, n+1))
continue;
cube(x, y, n+1) = false;
nchanged++;
}
return nchanged;
}
static int latinholes_solver_count(struct latin_solver *solver, struct solver_ctx *sctx)
{
int o = solver->o;
int nums = sctx->nums;
int dir, holecount, circlecount;
int x, y, i, j;
int nchanged = 0;
x = 0; y = 0;
for(dir = 0; dir < 2; dir++)
{
for(i = 0; i < o; i++)
{
if(dir) x = i; else y = i;
holecount = 0;
circlecount = 0;
for(j = 0; j < o; j++)
{
if(dir) y = j; else x = j;
if(sctx->state->holes[y*o+x] == LATINH_CROSS)
holecount++;
if(sctx->state->holes[y*o+x] == LATINH_CIRCLE)
circlecount++;
}
if(holecount == (o-nums))
{
for(j = 0; j < o; j++)
{
if(dir) y = j; else x = j;
if(!sctx->state->holes[y*o+x])
nchanged += latinholes_solver_place_circle(solver, sctx, x, y);
}
}
else if(circlecount == nums)
{
for(j = 0; j < o; j++)
{
if(dir) y = j; else x = j;
if(!sctx->state->holes[y*o+x])
nchanged += latinholes_solver_place_cross(solver, sctx, x, y);
}
}
}
}
return nchanged;
}
static int latinholes_check(game_state *state)
{
int o = state->params->order;
int nums = state->params->nums;
int od = o*nums;
int x, y, i;
bool fail;
digit d;
fail = false;
int *rows, *cols, *hrows, *hcols;
rows = snewn(od, int);
cols = snewn(od, int);
hrows = snewn(o, int);
hcols = snewn(o, int);
memset(rows, 0, od * sizeof(int));
memset(cols, 0, od * sizeof(int));
memset(hrows, 0, o * sizeof(int));
memset(hcols, 0, o * sizeof(int));
for(x = 0; x < o; x++)
for(y = 0; y < o; y++)
{
d = state->grid[y*o+x];
if(d == 0 || d > nums)
{
hrows[y]++;
hcols[x]++;
}
else
{
rows[y*nums+d-1]++;
cols[x*nums+d-1]++;
}
if(d == 0 && state->holes[y*o+x] == LATINH_CIRCLE)
fail = true;
}
for(i = 0; i < o; i++)
{
if(hrows[i] != (o-nums) || hcols[i] != (o-nums))
{
#ifdef STANDALONE_SOLVER
if(solver_show_working)
printf("Hole miscount in %d\n", i);
#endif
fail = true;
}
}
for(i = 0; i < od; i++)
{
if(rows[i] != 1 || cols[i] != 1)
{
#ifdef STANDALONE_SOLVER
if(solver_show_working)
printf("Number miscount in %d\n", i);
#endif
fail = true;
}
}
sfree(rows);
sfree(cols);
sfree(hrows);
sfree(hcols);
return !fail;
}
/* ******************** *
* Salad Letters solver *
* ******************** */
static int salad_letters_solver_dir(struct latin_solver *solver, struct solver_ctx *sctx, int si, int di, int ei, int cd)
{
char clue;
clue = sctx->state->borderclues[cd];
if(!clue)
return 0;
int i, j;
int o = solver->o;
int nums = sctx->nums;
int nchanged = 0;
int dist = 0;
int maxdist;
bool found = false;
bool outofrange = false;
/*
* Determine max. distance by counting the holes
* which can never be used for the clue
*/
maxdist = o-nums;
for(i = si + (di*(o-nums)); i != ei; i+=di)
{
if(sctx->state->holes[i] == LATINH_CROSS)
maxdist--;
}
for(i = si; i != ei; i+=di)
{
/* Rule out other possibilities near clue */
if(!found)
{
for(j = 1; j <= nums; j++)
{
if(j == clue)
continue;
if(cube(i%o, i/o, j))
{
#ifdef STANDALONE_SOLVER
if(solver_show_working)
printf("Border %c (%d) rules out %c at %d,%d\n", clue+'A'-1, cd, j+'A'-1, i%o, i/o);
#endif
cube(i%o, i/o, j) = false;
nchanged++;
}
}
}
if(sctx->state->holes[i] != LATINH_CROSS)
found = true;
/* Rule out this possibility too far away from clue */
if(outofrange)
{
if(cube(i%o, i/o, clue))
{
#ifdef STANDALONE_SOLVER
if(solver_show_working)
printf("Border %c is too far away from %d,%d\n", clue+'A'-1, i%o, i/o);
#endif
cube(i%o, i/o, clue) = false;
nchanged++;
}
}
dist++;
if(sctx->state->holes[i] == LATINH_CIRCLE || dist > maxdist)
outofrange = true;
}
return nchanged;
}
static int salad_letters_solver(struct latin_solver *solver, struct solver_ctx *sctx)
{
int nchanged = 0;
int o = solver->o;
int o2 = o*o;
int i;
for(i = 0; i < o; i++)
{
/* Top */
nchanged += salad_letters_solver_dir(solver, sctx, i, o, o2+i, i+0);
/* Left */
nchanged += salad_letters_solver_dir(solver, sctx, i*o, 1, ((i+1)*o), i+o);
/* Bottom */
nchanged += salad_letters_solver_dir(solver, sctx, (o2-o)+i, -o, i-o, i+(o*2));
/* Right */
nchanged += salad_letters_solver_dir(solver, sctx, ((i+1)*o)-1, -1, i*o - 1, i+(o*3));
}
return nchanged;
}
static bool game_can_format_as_text_now(const game_params *params)
{
return true;
}
static char *game_text_format(const game_state *state)
{
int o = state->params->order;
int mode = state->params->mode;
int i, j;
int lr = 8 + (o*2);
int s = (lr * (o+4));
char c;
digit d;
char hole;
char *ret = snewn(s + 1, char);
memset(ret, ' ', s * sizeof(char));
ret[s] = '\0';
/* Place newlines */
for(i = 1; i <= o+4; i++)
{
ret[lr*i - 1] = '\n';
}
/* Draw corners */
ret[lr + 2] = '+';
ret[lr*2 - 4] = '+';
ret[lr*(o+2) + 2] = '+';
ret[lr*(o+3) - 4] = '+';
/* Draw horizontal border */
for(i = 3; i < lr-4; i++)
{
ret[i + lr] = '-';
ret[i + (lr*(o+2))] = '-';
}
/* Draw vertical border */
for(i = 2; i < 2+o; i++)
{
ret[i*lr + 2] = '|';
ret[i*lr + (o*2) + 4] = '|';
}
/* Draw grid */
for(i = 0; i < o; i++)
for(j = 0; j < o; j++)
{
d = state->grid[i*o+j];
hole = state->holes[i*o+j];
if(hole == LATINH_CROSS)
c = 'x';
else if(!d)
c = hole == LATINH_CIRCLE ? 'O' : '.';
else
c = mode == GAMEMODE_LETTERS ? 'A' + d - 1 : '0' + d;
ret[(i+2)*lr + 2*j + 4] = c;
}
/* Draw border clues */
for(i = 0; i < o; i++)
{
/* Top */
if(state->borderclues[i])
ret[(i*2)+4] = state->borderclues[i] + 'A' - 1;
/* Left */
if(state->borderclues[i + o])
ret[((i+2)*lr)] = state->borderclues[i + o] + 'A' - 1;
/* Bottom */
if(state->borderclues[i + (o*2)])
ret[(i*2)+4+(lr*(o+3))] = state->borderclues[i + (o*2)] + 'A' - 1;
/* Right */
if(state->borderclues[i + (o*3)])
ret[((i+3)*lr)-2] = state->borderclues[i + (o*3)] + 'A' - 1;
}
return ret;
}
static game_state *load_game(const game_params *params, const char *desc, char **fail)
{
int o = params->order;
int nums = params->nums;
int o2 = o*o;
int ox4 = o*4;
int c, pos;
digit d;
game_state *ret = blank_game(params);
memset(ret->grid, 0, o2 * sizeof(digit));
memset(ret->holes, 0, o2 * sizeof(char));
memset(ret->borderclues, 0, ox4 * sizeof(char));
memset(ret->gridclues, 0, o2 * sizeof(digit));
const char *p = desc;
/* Read border clues */
if(params->mode == GAMEMODE_LETTERS)
{
pos = 0;
while(*p && *p != ',')
{
c = *p++;
d = 0;
if(pos >= ox4)
{
free_game(ret);
*fail = "Border description is too long.";
return NULL;
}
if(c >= 'a' && c <= 'z')
pos += (c - 'a') + 1;
else if(c >= '1' && c <= '9')
d = c - '0';
else if(c >= 'A' && c <= 'I')
d = (c - 'A') + 1;
else
{
free_game(ret);
*fail = "Border description contains invalid characters.";
return NULL;
}
if(d > 0 && d <= nums)
{
ret->borderclues[pos] = d;
pos++;
}
else if(d > nums)
{
free_game(ret);
*fail = "Border clue is out of range.";
return NULL;
}
}
if(pos < ox4)
{
free_game(ret);
*fail = "Description is too short.";
return NULL;
}
if(*p == ',')
p++;
}
/* Read grid clues */
pos = 0;
while(*p)
{
c = *p++;
d = 0;
if(pos >= o2)
{
free_game(ret);
*fail = "Grid description is too long.";
return NULL;
}
if(c >= 'a' && c <= 'z')
pos += (c - 'a') + 1;
else if(c >= '1' && c <= '9')
d = c - '0';
else if(c >= 'A' && c <= 'I')
d = (c - 'A') + 1;
else if(c == 'O')
{
ret->gridclues[pos] = LATINH_CIRCLE;
ret->holes[pos] = LATINH_CIRCLE;
pos++;
}
else if(c == 'X')
{
ret->gridclues[pos] = LATINH_CROSS;
ret->holes[pos] = LATINH_CROSS;
pos++;
}
else
{
free_game(ret);
*fail = "Grid description contains invalid characters.";
return NULL;
}
if(d > 0 && d <= nums)
{
ret->gridclues[pos] = d;
ret->grid[pos] = d;
ret->holes[pos] = LATINH_CIRCLE;
pos++;
}
else if(d > nums)
{
free_game(ret);
*fail = "Grid clue is out of range.";
return NULL;
}
}
if(pos > 0 && pos < o2)
{
free_game(ret);
*fail = "Description is too short.";
return NULL;
}
return ret;
}
static const char *validate_desc(const game_params *params, const char *desc)
{
char *fail = NULL;
game_state *state = load_game(params, desc, &fail);
if(state)
free_game(state);
if(fail)
return fail;
return NULL;
}
static int salad_solver_easy(struct latin_solver *solver, void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
int nchanged = 0;
nchanged += latinholes_solver_sync(solver, ctx);
if(ctx->state->params->mode == GAMEMODE_LETTERS)
{
nchanged += salad_letters_solver(solver, ctx);
}
nchanged += latinholes_solver_count(solver, ctx);
return nchanged;
}
static digit salad_scan_dir(digit *grid, char *holes, int si, int di, int ei, bool direct)
{
int i;
for(i = si; i != ei; i+=di)
{
if(direct && grid[i] == 0 && holes[i] != LATINH_CROSS)
return 0;
if(grid[i] != 0 && grid[i] != LATINH_CROSS)
return grid[i];
}
return 0;
}
static bool salad_checkborders(game_state *state)
{
int o = state->params->order;
int o2 = o*o;
int i; char c;
for(i = 0; i < o; i++)
{
/* Top */
if(state->borderclues[i])
{
c = salad_scan_dir(state->grid, NULL, i, o, o2+i, false);
if(c != state->borderclues[i])
return false;
}
/* Left */
if(state->borderclues[i+o])
{
c = salad_scan_dir(state->grid, NULL, i*o, 1, ((i+1)*o), false);
if(c != state->borderclues[i+o])
return false;
}
/* Bottom */
if(state->borderclues[i+(o*2)])
{
c = salad_scan_dir(state->grid, NULL, (o2-o)+i, -o, i-o, false);
if(c != state->borderclues[i+(o*2)])
return false;
}
/* Right */
if(state->borderclues[i+(o*3)])
{
c = salad_scan_dir(state->grid, NULL, ((i+1)*o)-1, -1, i*o - 1, false);
if(c != state->borderclues[i+(o*3)])
return false;
}
}
return true;
}
static bool salad_valid(struct latin_solver *solver, void *vctx) {
return true;
}
#define SOLVER(upper,title,func,lower) func,
static usersolver_t const salad_solvers[] = { DIFFLIST(SOLVER) };
static int salad_solve(game_state *state, int maxdiff)
{
int o = state->params->order;
int nums = state->params->nums;
int o2 = o*o;
struct solver_ctx *ctx = new_ctx(state, o, nums);
struct latin_solver *solver = snew(struct latin_solver);