Add James H's new puzzle, `Unequal' (otherwise known as the
[sgt/puzzles] / latin.h
CommitLineData
149255d7 1#ifndef LATIN_H
2#define LATIN_H
3
4#include "puzzles.h"
5
6typedef unsigned char digit;
7
8/* --- Solver structures, definitions --- */
9
10#ifdef STANDALONE_SOLVER
11int solver_show_working, solver_recurse_depth;
12#endif
13
14struct latin_solver {
15 int o; /* order of latin square */
16 unsigned char *cube; /* o^3, indexed by x, y, and digit:
17 TRUE in that position indicates a possibility */
18 digit *grid; /* o^2, indexed by x and y: for final deductions */
19
20 unsigned char *row; /* o^2: row[y*cr+n-1] TRUE if n is in row y */
21 unsigned char *col; /* o^2: col[x*cr+n-1] TRUE if n is in col x */
22};
23#define cubepos(x,y,n) (((x)*solver->o+(y))*solver->o+(n)-1)
24#define cube(x,y,n) (solver->cube[cubepos(x,y,n)])
25
26#define gridpos(x,y) ((y)*solver->o+(x))
27#define grid(x,y) (solver->grid[gridpos(x,y)])
28
29/* A solo solver using this code would need these defined. See solo.c. */
30#ifndef YTRANS
31#define YTRANS(y) (y)
32#endif
33#ifndef YUNTRANS
34#define YUNTRANS(y) (y)
35#endif
36
37
38/* --- Solver individual strategies --- */
39
40/* Place a value at a specific location. */
41void latin_solver_place(struct latin_solver *solver, int x, int y, int n);
42
43/* Positional elimination. */
44int latin_solver_elim(struct latin_solver *solver, int start, int step
45#ifdef STANDALONE_SOLVER
46 , char *fmt, ...
47#endif
48 );
49
50struct latin_solver_scratch; /* private to latin.c */
51/* Set elimination */
52int latin_solver_set(struct latin_solver *solver,
53 struct latin_solver_scratch *scratch,
54 int start, int step1, int step2
55#ifdef STANDALONE_SOLVER
56 , char *fmt, ...
57#endif
58 );
59
60/* Forcing chains */
61int latin_solver_forcing(struct latin_solver *solver,
62 struct latin_solver_scratch *scratch);
63
64
65/* --- Solver allocation --- */
66
67/* Fills in (and allocates members for) a latin_solver struct.
68 * Will allocate members of snew, but not snew itself
69 * (allowing 'struct latin_solver' to be the first element in a larger
70 * struct, for example). */
71void latin_solver_alloc(struct latin_solver *solver, digit *grid, int o);
72void latin_solver_free(struct latin_solver *solver);
73
74/* Allocates scratch space (for _set and _forcing) */
75struct latin_solver_scratch *
76 latin_solver_new_scratch(struct latin_solver *solver);
77void latin_solver_free_scratch(struct latin_solver_scratch *scratch);
78
79
80/* --- Solver guts --- */
81
82/* Looped positional elimination */
83int latin_solver_diff_simple(struct latin_solver *solver);
84
85/* Looped set elimination; *extreme is set if it used
86 * the more difficult single-number elimination. */
87int latin_solver_diff_set(struct latin_solver *solver,
88 struct latin_solver_scratch *scratch,
89 int *extreme);
90
91typedef int (latin_solver_callback)(digit *, int, int, void*);
92/* Use to provide a standard way of dealing with solvers which can recurse;
93 * pass in your enumeration for 'recursive diff' and your solver
94 * callback. Returns #solutions (0 == already solved). */
95int latin_solver_recurse(struct latin_solver *solver, int recdiff,
96 latin_solver_callback cb, void *ctx);
97
98/* Individual puzzles should use their enumerations for their
99 * own difficulty levels, ensuring they don't clash with these. */
100enum { diff_impossible = 10, diff_ambiguous, diff_unfinished };
101int latin_solver(digit *grid, int order, int maxdiff, void *unused);
102
103void latin_solver_debug(unsigned char *cube, int o);
104
105/* --- Generation and checking --- */
106
107digit *latin_generate_quick(int o, random_state *rs);
108digit *latin_generate(int o, random_state *rs);
109
110int latin_check(digit *sq, int order); /* !0 => not a latin square */
111
112void latin_debug(digit *sq, int order);
113
114#endif