-
Notifications
You must be signed in to change notification settings - Fork 3
/
mathrax.c
1792 lines (1515 loc) · 38.6 KB
/
mathrax.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
/*
* mathrax.c : Implementation for Mathrax puzzles.
* (C) 2019 Lennard Sprong
* Created for Simon Tatham's Portable Puzzle Collection
* See LICENCE for licence details
*
* Objective: Fill in the latin square with digits 1 to N.
* Some grid intersections contain clues:
* - An 'E' indicates that the four adjacent digits are even.
* - An 'O' indicates that the four adjacent digits are odd.
* - A number indicates the result of the given operation when
* applied to each pair of diagonally adjacent digits.
* (topleft * bottomright) = (topright * bottomleft)
* - An '=' indicates that diagonally adjacent digits are equal.
*
* The inventor of this puzzle type is unknown.
* More information:
* http://www.janko.at/Raetsel/Mathrax/index.htm
*/
#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 {
COL_BACKGROUND,
COL_HIGHLIGHT,
COL_LOWLIGHT,
COL_BORDER,
COL_GUESS,
COL_PENCIL,
COL_ERROR,
COL_ERRORBG,
NCOLOURS
};
#define DIFFLIST(A) \
A(EASY,Easy, e) \
A(NORMAL,Normal, n) \
A(TRICKY,Tricky, t) \
A(RECURSIVE,Recursive, r) \
#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
static char const *const mathrax_diffnames[] = { DIFFLIST(TITLE) };
static char const mathrax_diffchars[] = DIFFLIST(ENCODE);
struct game_params {
int o, diff, options;
};
typedef unsigned int clue_t;
enum {
CLUE_ADD = 1,
CLUE_SUB,
CLUE_MUL,
CLUE_DIV,
CLUE_EVN,
CLUE_ODD
};
#define CLUEMASK 7
#define OPTION_ADD 1
#define OPTION_SUB 2
#define OPTION_MUL 4
#define OPTION_DIV 8
#define OPTION_EQL 16
#define OPTION_ODD 32
#define OPTIONSMASK 63
#define CLUENUM(x) (int)((x)>>3)
#define SET_CLUENUM(x) (clue_t)((x)<<3)
#define F_IMMUTABLE 0x01
#define FE_COUNT 0x02
#define FE_TOPLEFT 0x04
#define FE_TOPRIGHT 0x08
#define FE_BOTLEFT 0x10
#define FE_BOTRIGHT 0x20
#define FE_ERRORMASK 0x3E
#define FD_FLASH 0x100
#define FD_CURSOR 0x200
#define FD_PENCIL 0x400
typedef unsigned int marks_t;
struct game_state {
int o;
digit *grid;
unsigned int *flags;
marks_t *marks;
clue_t *clues;
bool completed, cheated;
};
enum { DIFFLIST(ENUM) DIFFCOUNT,
DIFF_IMPOSSIBLE = diff_impossible,
DIFF_AMBIGUOUS = diff_ambiguous,
DIFF_UNFINISHED = diff_unfinished };
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->o = 5;
ret->diff = DIFF_EASY;
ret->options = OPTIONSMASK;
return ret;
}
const struct game_params mathrax_presets[] = {
{ 5, DIFF_EASY, OPTIONSMASK },
{ 5, DIFF_NORMAL, OPTIONSMASK },
{ 5, DIFF_TRICKY, OPTIONSMASK },
{ 6, DIFF_EASY, OPTIONSMASK },
{ 6, DIFF_NORMAL, OPTIONSMASK },
{ 6, DIFF_TRICKY, OPTIONSMASK },
{ 7, DIFF_NORMAL, OPTIONSMASK },
{ 8, DIFF_NORMAL, OPTIONSMASK },
{ 9, DIFF_NORMAL, OPTIONSMASK },
};
static bool game_fetch_preset(int i, char **name, game_params **params)
{
if (i < 0 || i >= lenof(mathrax_presets))
return false;
game_params *ret = snew(game_params);
*ret = mathrax_presets[i]; /* struct copy */
*params = ret;
int o = ret->o;
char buf[80];
sprintf(buf, "%dx%d %s", o, o, mathrax_diffnames[ret->diff]);
*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 *ret, char const *string)
{
char const *p = string;
ret->options = 0;
ret->o = atoi(p);
while (*p && isdigit((unsigned char)*p)) p++;
if (*p == 'd') {
int i;
p++;
ret->diff = DIFFCOUNT + 1; /* ...which is invalid */
if (*p) {
for (i = 0; i < DIFFCOUNT; i++) {
if (*p == mathrax_diffchars[i])
ret->diff = i;
}
p++;
}
}
if(*p == 'A')
{
ret->options |= OPTION_ADD;
p++;
}
if(*p == 'S')
{
ret->options |= OPTION_SUB;
p++;
}
if(*p == 'M')
{
ret->options |= OPTION_MUL;
p++;
}
if(*p == 'D')
{
ret->options |= OPTION_DIV;
p++;
}
if(*p == 'E')
{
ret->options |= OPTION_EQL;
p++;
}
if(*p == 'O')
{
ret->options |= OPTION_ODD;
p++;
}
if(!ret->options)
ret->options = OPTIONSMASK;
}
static char *encode_params(const game_params *params, bool full)
{
char ret[80];
sprintf(ret, "%d", params->o);
if (full)
{
sprintf(ret + strlen(ret), "d%c", mathrax_diffchars[params->diff]);
if(params->options != OPTIONSMASK)
{
if(params->options & OPTION_ADD)
sprintf(ret + strlen(ret), "A");
if(params->options & OPTION_SUB)
sprintf(ret + strlen(ret), "S");
if(params->options & OPTION_MUL)
sprintf(ret + strlen(ret), "M");
if(params->options & OPTION_DIV)
sprintf(ret + strlen(ret), "D");
if(params->options & OPTION_EQL)
sprintf(ret + strlen(ret), "E");
if(params->options & OPTION_ODD)
sprintf(ret + strlen(ret), "O");
}
}
return dupstr(ret);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(9, config_item);
ret[0].name = "Size";
ret[0].type = C_STRING;
sprintf(buf, "%d", params->o);
ret[0].u.string.sval = dupstr(buf);
ret[1].name = "Difficulty";
ret[1].type = C_CHOICES;
ret[1].u.choices.choicenames = DIFFLIST(CONFIG);
ret[1].u.choices.selected = params->diff;
ret[2].name = "Addition clues";
ret[2].type = C_BOOLEAN;
ret[2].u.boolean.bval = params->options & OPTION_ADD;
ret[3].name = "Subtraction clues";
ret[3].type = C_BOOLEAN;
ret[3].u.boolean.bval = params->options & OPTION_SUB;
ret[4].name = "Multiplication clues";
ret[4].type = C_BOOLEAN;
ret[4].u.boolean.bval = params->options & OPTION_MUL;
ret[5].name = "Division clues";
ret[5].type = C_BOOLEAN;
ret[5].u.boolean.bval = params->options & OPTION_DIV;
ret[6].name = "Equality clues";
ret[6].type = C_BOOLEAN;
ret[6].u.boolean.bval = params->options & OPTION_EQL;
ret[7].name = "Even/odd clues";
ret[7].type = C_BOOLEAN;
ret[7].u.boolean.bval = params->options & OPTION_ODD;
ret[8].name = NULL;
ret[8].type = C_END;
return ret;
}
static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
ret->o = atoi(cfg[0].u.string.sval);
ret->diff = cfg[1].u.choices.selected;
ret->options = 0;
if(cfg[2].u.boolean.bval)
ret->options |= OPTION_ADD;
if(cfg[3].u.boolean.bval)
ret->options |= OPTION_SUB;
if(cfg[4].u.boolean.bval)
ret->options |= OPTION_MUL;
if(cfg[5].u.boolean.bval)
ret->options |= OPTION_DIV;
if(cfg[6].u.boolean.bval)
ret->options |= OPTION_EQL;
if(cfg[7].u.boolean.bval)
ret->options |= OPTION_ODD;
return ret;
}
static const char *validate_params(const game_params *params, bool full)
{
if(params->o < 3)
return "Size must be at least 3";
if(params->o > 9)
return "Size must be no more than 9";
if (params->diff >= DIFFCOUNT)
return "Unknown difficulty rating";
if(full && !params->options)
return "At least one clue type must be enabled";
return NULL;
}
static game_state *blank_game(int o)
{
int s = o*o;
int cs = (o-1)*(o-1);
game_state *state = snew(game_state);
state->o = o;
state->completed = state->cheated = false;
state->flags = snewn(s, unsigned int);
state->marks = snewn(s, marks_t);
state->clues = snewn(cs, clue_t);
state->grid = snewn(s, digit);
memset(state->flags, 0, s*sizeof(unsigned int));
memset(state->marks, 0, s*sizeof(marks_t));
memset(state->clues, 0, cs*sizeof(clue_t));
memset(state->grid, 0, s*sizeof(digit));
return state;
}
static game_state *dup_game(const game_state *state)
{
int o = state->o, s = o*o;
int cs = (o-1)*(o-1);
game_state *ret = blank_game(o);
memcpy(ret->flags, state->flags, s*sizeof(unsigned int));
memcpy(ret->marks, state->marks, s*sizeof(marks_t));
memcpy(ret->clues, state->clues, cs*sizeof(clue_t));
memcpy(ret->grid, state->grid, s*sizeof(digit));
ret->completed = state->completed;
ret->cheated = state->cheated;
return ret;
}
static void free_game(game_state *state)
{
sfree(state->flags);
sfree(state->marks);
sfree(state->clues);
sfree(state->grid);
sfree(state);
}
enum { STATUS_COMPLETE, STATUS_UNFINISHED, STATUS_INVALID };
#define BIT(d) (marks_t)(1<<((d)-1))
/*
* Get all marks which are valid when combined with the marks in the opposite square.
*/
static marks_t mathrax_options(int o, clue_t clue, marks_t mark, bool simple)
{
clue_t cluetype = clue & CLUEMASK;
switch(cluetype)
{
case CLUE_ADD:
case CLUE_SUB:
case CLUE_MUL:
case CLUE_DIV:
{
/*
* In Easy mode, only look for options if the other space is confirmed
* (only one bit in mark is set)
*/
if(simple && mark & (mark - 1))
return ~0;
int cnum = CLUENUM(clue);
marks_t ret = 0;
digit a, b;
for(a = 1; a <= 9; a++)
{
if(!(mark & BIT(a))) continue;
for(b = 1; b <= 9; b++)
{
if(cluetype == CLUE_ADD && a + b == cnum)
ret |= BIT(b);
else if(cluetype == CLUE_SUB && abs(a - b) == cnum)
ret |= BIT(b);
else if(cluetype == CLUE_MUL && a * b == cnum)
ret |= BIT(b);
else if(cluetype == CLUE_DIV && max(a, b) / min(a, b) == cnum && max(a, b) % min(a, b) == 0)
ret |= BIT(b);
}
}
return ret;
}
case CLUE_EVN:
/* 2, 4, 6, 8 */
return 0xAA;
case CLUE_ODD:
/* 1, 3, 5, 7, 9 */
return 0x155;
default:
/* All numbers */
return ~0;
}
}
static int mathrax_validate_game(game_state *state, int *temp, bool is_solver)
{
int o = state->o, co = o-1;
int x, y;
digit d;
int ret = STATUS_COMPLETE;
marks_t maxbits = (1<<o)-1;
marks_t bits;
bool hastemp = temp != NULL;
if(!hastemp)
temp = snewn(o*o*2, int);
memset(temp, 0, o*o*2*sizeof(int));
for (x = 0; x < o; x++)
{
for (y = 0; y < o; y++)
{
state->flags[y*o+x] &= ~FE_ERRORMASK;
d = state->grid[y*o+x];
if(!d) continue;
temp[((d-1)*o)+y]++;
temp[((d-1)*o)+(o*o)+x]++;
}
}
for(y = 0; y < o; y++)
for(x = 0; x < o; x++)
{
d = state->grid[y*o+x];
bits = d ? BIT(d) : is_solver ? state->marks[y*o+x] : maxbits;
if(!d)
{
if(ret == STATUS_COMPLETE)
ret = STATUS_UNFINISHED;
}
else
{
if(temp[((d-1)*o)+y] > 1 || temp[((d-1)*o)+(o*o)+x] > 1)
state->flags[y*o+x] |= FE_COUNT;
}
if(y < o-1 && x < o-1)
{
int other = state->grid[(y+1)*o+x+1];
if(!(mathrax_options(o, state->clues[y*co+x], other ? BIT(other) : maxbits, false) & bits))
state->flags[y*o+x] |= FE_BOTRIGHT;
}
if(y > 0 && x < o-1)
{
int other = state->grid[(y-1)*o+x+1];
if(!(mathrax_options(o, state->clues[(y-1)*co+x], other ? BIT(other) : maxbits, false) & bits))
state->flags[y*o+x] |= FE_TOPRIGHT;
}
if(y < o-1 && x > 0)
{
int other = state->grid[(y+1)*o+x-1];
if(!(mathrax_options(o, state->clues[y*co+x-1], other ? BIT(other) : maxbits, false) & bits))
state->flags[y*o+x] |= FE_BOTLEFT;
}
if(y > 0 && x > 0)
{
int other = state->grid[(y-1)*o+x-1];
if(!(mathrax_options(o, state->clues[(y-1)*co+x-1], other ? BIT(other) : maxbits, false) & bits))
state->flags[y*o+x] |= FE_TOPLEFT;
}
if(state->flags[y*o+x] & FE_ERRORMASK)
ret = STATUS_INVALID;
}
if(!hastemp)
sfree(temp);
return ret;
}
struct solver_ctx {
int o;
marks_t *marks;
clue_t *clues;
};
static struct solver_ctx *blank_ctx(int o)
{
int co = o-1;
struct solver_ctx *ctx = snew(struct solver_ctx);
ctx->o = o;
ctx->marks = snewn(o*o, marks_t);
ctx->clues = snewn(co*co, clue_t);
return ctx;
}
static struct solver_ctx *new_ctx(game_state *state)
{
int o = state->o;
int co = o-1;
int i;
marks_t maxbits = (1<<o)-1;
struct solver_ctx *ctx = blank_ctx(o);
memcpy(ctx->clues, state->clues, co*co*sizeof(clue_t));
for(i = 0; i < o*o; i++)
ctx->marks[i] = state->grid[i] ? BIT(state->grid[i]) : maxbits;
return ctx;
}
static void *clone_ctx(void *vctx)
{
struct solver_ctx *octx = (struct solver_ctx *)vctx;
int o = octx->o;
int co = o-1;
struct solver_ctx *nctx = blank_ctx(o);
memcpy(nctx->marks, octx->marks, o*o*sizeof(marks_t));
memcpy(nctx->clues, octx->clues, co*co*sizeof(clue_t));
return nctx;
}
static void free_ctx(void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
sfree(ctx->marks);
sfree(ctx->clues);
sfree(ctx);
}
/* ****** *
* Solver *
* ****** */
static int mathrax_solver_apply_options(struct latin_solver *solver, struct solver_ctx *ctx, int diff)
{
int o = solver->o, co = o-1;
int x, y;
digit d;
int ret = 0;
bool simple = diff == DIFF_EASY;
for(y = 0; y < o; y++)
for(x = 0; x < o; x++)
for(d = 1; d <= o; d++)
{
/* Synchronize our own marks array with the latin solver. */
if(!cube(x,y,d))
ctx->marks[y*o+x] &= ~BIT(d);
}
for(y = 0; y < o; y++)
for(x = 0; x < o; x++)
{
/* Remove all marks which would violate one of the grid clues */
marks_t marks = ctx->marks[y*o+x];
if(y < o-1 && x < o-1)
marks &= mathrax_options(o, ctx->clues[y*co+x], ctx->marks[(y+1)*o+x+1], simple);
if(y > 0 && x < o-1)
marks &= mathrax_options(o, ctx->clues[(y-1)*co+x], ctx->marks[(y-1)*o+x+1], simple);
if(y < o-1 && x > 0)
marks &= mathrax_options(o, ctx->clues[y*co+x-1], ctx->marks[(y+1)*o+x-1], simple);
if(y > 0 && x > 0)
marks &= mathrax_options(o, ctx->clues[(y-1)*co+x-1], ctx->marks[(y-1)*o+x-1], simple);
if(!marks)
return -1;
/*
* On Normal mode and below, only apply clues if it immediately
* confirms a number in a square (only one bit is set).
*/
if (diff <= DIFF_NORMAL && (marks & (marks - 1)))
continue;
for(d = 1; d <= o; d++)
{
/* Synchronize the bitmap back to the latin solver. */
if(cube(x,y,d) && !(marks & BIT(d)))
{
cube(x,y,d) = false;
ret++;
}
}
}
return ret;
}
static bool mathrax_valid(struct latin_solver *solver, void *vctx) {
return true;
}
static int mathrax_solver_easy(struct latin_solver *solver, void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
return mathrax_solver_apply_options(solver, ctx, DIFF_EASY);
}
static int mathrax_solver_normal(struct latin_solver *solver, void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
return mathrax_solver_apply_options(solver, ctx, DIFF_NORMAL);
}
static int mathrax_solver_tricky(struct latin_solver *solver, void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
return mathrax_solver_apply_options(solver, ctx, DIFF_TRICKY);
}
static usersolver_t const mathrax_solvers[] = { mathrax_solver_easy, mathrax_solver_normal, mathrax_solver_tricky, NULL, NULL };
static int mathrax_solve(game_state *state, int maxdiff)
{
int o = state->o;
struct solver_ctx *ctx = new_ctx(state);
struct latin_solver *solver = snew(struct latin_solver);
int diff;
latin_solver_alloc(solver, state->grid, o);
diff = latin_solver_main(solver, maxdiff,
DIFF_EASY, DIFF_NORMAL, DIFF_TRICKY,
DIFF_TRICKY, DIFF_RECURSIVE,
mathrax_solvers, mathrax_valid, ctx, clone_ctx, free_ctx);
free_ctx(ctx);
latin_solver_free(solver);
sfree(solver);
if (diff == DIFF_IMPOSSIBLE)
return -1;
if (diff == DIFF_UNFINISHED)
return 0;
if (diff == DIFF_AMBIGUOUS)
return 2;
return 1;
}
/* **************** *
* Puzzle Generator *
* **************** */
static void mathrax_strip_grid_clues(game_state *state, int diff, random_state *rs)
{
int o = state->o, o2 = o*o;
int *spaces = snewn(o2, int);
digit *grid = snewn(o2, digit);
int i, j;
for (i = 0; i < o2; i++) spaces[i] = i;
shuffle(spaces, o2, sizeof(*spaces), rs);
for (i = 0; i < o2; i++)
{
j = spaces[i];
/* Space is already empty */
if (state->grid[j] == 0)
continue;
memcpy(grid, state->grid, o2 * sizeof(digit));
state->grid[j] = 0;
if (mathrax_solve(state, diff))
grid[j] = 0;
memcpy(state->grid, grid, o2 * sizeof(digit));
}
sfree(spaces);
sfree(grid);
}
static void mathrax_strip_math_clues(game_state *state, int diff, random_state *rs)
{
int o = state->o, o2 = o*o, co = o-1, cs = co*co;
int *spaces = snewn(cs, int);
digit *grid = snewn(o2, digit);
int i, j;
clue_t temp;
memcpy(grid, state->grid, o2 * sizeof(digit));
for (i = 0; i < cs; i++) spaces[i] = i;
shuffle(spaces, cs, sizeof(*spaces), rs);
for (i = 0; i < cs; i++)
{
j = spaces[i];
temp = state->clues[j];
/* Space is already empty */
if (temp == 0)
continue;
state->clues[j] = 0;
if (!mathrax_solve(state, diff))
state->clues[j] = temp;
memcpy(state->grid, grid, o2 * sizeof(digit));
}
sfree(spaces);
sfree(grid);
}
static clue_t mathrax_candidate_clue(digit a1, digit b1, digit a2, digit b2, int options)
{
assert(a1 && b1 && a2 && b2);
if (a1 < b1)
{
digit temp = a1;
a1 = b1;
b1 = temp;
}
if (a2 < b2)
{
digit temp = a2;
a2 = b2;
b2 = temp;
}
if ((options & OPTION_ADD) && (a1 + b1 == a2 + b2))
return CLUE_ADD | SET_CLUENUM(a1 + b1);
if ((options & OPTION_SUB) && (a1 - b1 == a2 - b2 && a1 - b1 > 0))
return CLUE_SUB | SET_CLUENUM(a1 - b1);
if ((options & OPTION_EQL) && (a1 == b1 && a2 == b2))
return CLUE_SUB | SET_CLUENUM(0);
if ((options & OPTION_MUL) && (a1 * b1 == a2 * b2))
return CLUE_MUL | SET_CLUENUM(a1 * b1);
if ((options & OPTION_DIV) && (a1 / b1 == a2 / b2 && a1 % b1 == 0 && a2 % b2 == 0 && a1 / b1 != 1))
return CLUE_DIV | SET_CLUENUM(a1 / b1);
if ((options & OPTION_ODD) && (a1 & b1 & a2 & b2 & 1))
return CLUE_ODD;
if ((options & OPTION_ODD) && !((a1 | b1 | a2 | b2) & 1))
return CLUE_EVN;
return 0;
}
static char *new_game_desc(const game_params *params, random_state *rs,
char **aux, bool interactive)
{
int o = params->o, s = o*o, i, x, y, co = o-1, cs = co*co;
int options = params->options;
if(!options) options = OPTIONSMASK;
game_state *state = blank_game(o);
state->grid = latin_generate(o, rs);
for (y = 0; y < co; y++)
for (x = 0; x < co; x++)
{
state->clues[y*co + x] = mathrax_candidate_clue(
state->grid[y*o + x],
state->grid[(y+1)*o + x + 1],
state->grid[(y+1)*o + x],
state->grid[y*o + x + 1],
options
);
}
mathrax_strip_grid_clues(state, params->diff, rs);
mathrax_strip_math_clues(state, params->diff, rs);
char *ret, *p;
ret = snewn((s*3) + 2, char);
p = ret;
int run = 0;
for (i = 0; i < s; i++)
{
if (state->grid[i] != 0)
{
if (run)
{
*p++ = ('a'-1) + run;
run = 0;
}
*p++ = state->grid[i] + '0';
}
else
{
if (run == 26)
{
*p++ = ('a'-1) + run;
run = 0;
}
run++;
}
}
if (run)
*p++ = ('a'-1) + run;
*p++ = ',';
run = 0;
for (i = 0; i < cs; i++)
{
clue_t clue = state->clues[i];
if (clue != 0)
{
if (run)
{
*p++ = ('a' - 1) + run;
run = 0;
}
switch (clue & CLUEMASK)
{
case CLUE_ADD:
p += sprintf(p, "A%d", CLUENUM(clue));
break;
case CLUE_SUB:
p += sprintf(p, "S%d", CLUENUM(clue));
break;
case CLUE_MUL:
p += sprintf(p, "M%d", CLUENUM(clue));
break;
case CLUE_DIV:
p += sprintf(p, "D%d", CLUENUM(clue));
break;
case CLUE_EVN:
p += sprintf(p, "E");
break;
case CLUE_ODD:
p += sprintf(p, "O");
break;
default:
/* Should never get here. Place an empty space just to be safe */
p += sprintf(p, "a");
}
}
else
{
if (run == 26)
{
*p++ = ('a' - 1) + run;
run = 0;
}
run++;
}
}
if (run)
*p++ = ('a' - 1) + run;
free_game(state);
*p++ = '\0';
return ret;
}
static game_state *load_game(const game_params *params, const char *desc, const char **fail)
{
int o = params->o, s = o*o, co = o-1, cs = co*co;
const char *p = desc;
char c;
digit d;
int pos, num;
game_state *ret = blank_game(o);
pos = 0;
while(*p && *p != ',')
{
c = *p++;
d = 0;
if(pos >= s)
{
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
{
free_game(ret);
*fail = "Grid description contains invalid characters.";
return NULL;
}
if(d > 0 && d <= o)
{
ret->flags[pos] |= F_IMMUTABLE;
ret->grid[pos] = d;
pos++;
}
else if(d > o)
{
free_game(ret);
*fail = "Grid clue is out of range.";
return NULL;
}
}
if(pos > 0 && pos < s)
{
free_game(ret);
*fail = "Description is too short.";
return NULL;
}
if(*p == ',')
{
p++;
pos = 0;
while(*p)
{
if(pos >= cs)
{
free_game(ret);
*fail = "Clue description is too long.";
return NULL;
}
c = *p++;
if(c >= 'a' && c <= 'z')
pos += (c - 'a') + 1;
if(c >= 'A' && c <= 'Z')
{
switch(c)
{
case 'A':
ret->clues[pos] = CLUE_ADD;
break;
case 'S':
ret->clues[pos] = CLUE_SUB;
break;
case 'M':
ret->clues[pos] = CLUE_MUL;
break;
case 'D':
ret->clues[pos] = CLUE_DIV;
break;
case 'E':
ret->clues[pos] = CLUE_EVN;
break;
case 'O':
ret->clues[pos] = CLUE_ODD;
break;
default:
free_game(ret);
*fail = "Invalid clue in description.";
return NULL;
}
num = atoi(p);
if(num > 99)
{
free_game(ret);