14 Commits

Author SHA1 Message Date
5030d87903 latin_solver_alloc: handle clashing numbers in input grid.
In the setup phase of the centralised latin.c solver, we start by
going over the input grid containing already-placed clue numbers, and
calling latin_solver_place to enter each on into the solver's data
structure. This has the side effect of ruling out each number from the
rest of the row and column, and _also_ checking by assertion that the
number being placed is not ruled out.

Those are a bad combination, because it means that if you give an
obviously inconsistent input grid to latin_solver_alloc (e.g. with two
identical numbers in a row already), it will fail an assertion. In
that situation, you want the solver run as a whole to return
diff_impossible so that the error is reported cleanly.

This assertion failure could be provoked by giving either Towers or
Group a manually-constructed game description inconsistent in that
way, and hitting Solve. Worse, it could be provoked during live play
in Unequal, by filling in a number clashing with a clue and then
pressing 'h' to get hints.
2023-02-05 10:41:17 +00:00
f21d3e4c74 latin.c: call a user-provided validator function. [NFC]
I've only just realised that there's a false-positive bug in the
latin.c solver framework.

It's designed to solve puzzles in which the solution is a latin square
but with some additional constraints provided by the individual
puzzle, and so during solving, it runs a mixture of its own standard
deduction functions that apply to any latin-square puzzle and extra
functions provided by the client puzzle to do deductions based on the
extra clues or constraints.

But what happens if the _last_ move in the solving process is
performed by one of the latin.c built-in methods, and it causes a
violation of the client puzzle's extra constraints? Nothing will ever
notice, and so the solver will report that the puzzle has a solution
when it actually has none.

An example is the Group game id 12i:m12b9a1zd9i6d10c3y2l11q4r . This
was reported by 'groupsolver -g' as being ambiguous. But if you look
at the two 'solutions' reported in the verbose diagnostics, one of
them is arrant nonsense: it has no identity element at all, and
therefore, it fails associativity all over the place. Actually that
puzzle _does_ have a unique solution.

This bug has been around for ages, and nobody has reported a problem.
For recursive solving, that's not much of a surprise, because it would
cause a spurious accusation of ambiguity, so that at generation time
some valid puzzles would be wrongly discarded, and you'd never see
them. But at non-recursive levels, I can't see a reason why this bug
_couldn't_ have led one of the games to present an actually impossible
puzzle believing it to be soluble.

Possibly this never came up because the other clients of latin.c are
more forgiving of this error in some way. For example, they might all
be very likely to use their extra clues early in the solving process,
so that the requirements are already baked in by the time the final
grid square is filled. I don't know!

Anyway. The fix is to introduce last-minute client-side validation:
whenever the centralised latin_solver thinks it's come up with a
filled grid, it should present it to a puzzle-specific validator
function and check that it's _really_ a legal solution.

This commit does the plumbing for all of that: it introduces the new
validator function as one of the many parameters to latin_solver, and
arranges to call it in an appropriate way during the solving process.
But all the per-puzzle validation functions are empty, for the moment.
2020-05-23 09:08:08 +01:00
a550ea0a47 Replace TRUE/FALSE with C99 true/false throughout.
This commit removes the old #defines of TRUE and FALSE from puzzles.h,
and does a mechanical search-and-replace throughout the code to
replace them with the C99 standard lowercase spellings.
2018-11-13 21:48:24 +00:00
08915945e6 Adopt C99 bool in the shared Latin-square API.
latin_check now returns bool, and latin_solver_diff_set takes a bool
'extreme' flag. Should be non-disruptive.
2018-11-13 21:48:24 +00:00
3189fb484d Fix an inaccurate comment.
latin_solver_diff_set takes a boolean input parameter to indicate
whether the 'Extreme'-level variant of set elimination is permitted.
But it's commented in the header file as having a boolean _output_
parameter which it sets if it _ended up_ using that variant. Probably
the latter was how it worked in an early draft, and I changed my mind
later without changing the comment.
2018-11-06 18:37:23 +00:00
a58c1b216b Make the code base clean under -Wwrite-strings.
I've also added that warning option and -Werror to the build script,
so that I'll find out if I break this property in future.
2017-10-01 16:35:40 +01:00
58e0d0bc2d New puzzle from James Harvey: 'Singles', an implementation of
Hitori. One infrastructure change in the process: latin.c has
acquired a utility function to generate a latin rectangle rather
than a full square.

[originally from svn r8828]
2010-01-11 21:21:07 +00:00
771f5446a8 Retire the YTRANS and YUNTRANS macros in latin.[ch]. They were
introduced to mimic similar macros in solo.c, in case Solo ever
moved over to being based on the latin.c solver framework; but even
Solo has long since lost those macros, so latin.c has no need to
keep them.

[originally from svn r8827]
2010-01-11 20:32:55 +00:00
13d7ff6ae5 Add a facility in the latin.c solver diagnostics to allow a puzzle
to call the digit values by custom names.

[originally from svn r8811]
2010-01-05 23:40:40 +00:00
3c3368d07f I've never trusted common variables. Take those bare ints out of
latin.h and put them in latin.c with 'extern' declarations in the
header.

[originally from svn r8794]
2009-12-27 10:01:18 +00:00
189f833980 Refactor latin.c to make it easier to reuse. Instead of client
programs having to clone the latin_solver() function and insert
their own extra deduction routines, they can now just _call_
latin_solver with enough parameters to let it fit its own deductions
into their difficulty framework and call a set of provided function
pointers to do user deductions. Modified Unequal to work in the new
world, of course.

[originally from svn r8791]
2009-12-27 10:01:10 +00:00
d54b4c4651 latin_generate_quick is long gone, so remove it from the header file.
[originally from svn r8789]
2009-12-23 11:34:25 +00:00
472deca37c Patch from James H to fix the occasional generation of puzzles
harder than requested.

[originally from svn r7113]
2007-01-15 20:07:18 +00:00
2f8dba9257 Add James H's new puzzle, `Unequal' (otherwise known as the
Guardian's `Futoshiki').

[originally from svn r7100]
2007-01-13 14:44:50 +00:00