[BUG & ENHANCEMENT] Dealing with Contradictions -> adding references to rules
Describe the current behavior of what you're trying to improve. If your enhancement request related to a problem, please also describe the problem.
This is a big one! Probably will need to spend a whole semester on this!
OK, so first of all: I have been pushing for rules to be implemented on the basis of/by reference to other rules. For example: take Nurikabe's 'Fill in Black' rule, that says that we can make the unknown cell near the bottom right black, because if it was white it could never become part of a room:
Now, note the reasoning there: "If it was white, we would have a contradiction in that it couldn't become part of a room, and so it has to be black". Well, this is something we can explicitly do in LEGUP as well: using Black or White case rule we branch on whether that square is black or white, and then we indicate a 'No Number' contradiction in the case that it is white:
Better yet, we can implement the code for the 'Fill in Black' rule by reference to the 'No Number' rule: once we have implemented the latter, we should be able to implement the former by reference to the latter! And I know this has been done for various rules for various games, and I think this is the right thing to do.
However, we should be careful here. Consider:
Note that I made the very top left square of the board black, and it is being justified by the Fill in Black rule, either though it should of course have nothing to do with the 'Fill in Black' rule!!! Is this a bug? Well, yes and no. Note that we are in the branch where that square near the bottom right was made white. So, when calling 'Fill in Black', the code says: well, that square at the top left is either black or white, but if it is white, we have a 'No Number' contradiction, and so it has to be black. Well .... yes, there is a 'No Number' contradiction on the board .. but that contradiction was there already ... and it has nothing to do with the square that we made black as part of the current transition.
Please note that every time we implemented a direct rule by reference to an earlier implemented contradiction rule you get this kind of behavior, so this is certain;ly not restricted to this specific 'No Number' rule of Nurikabe: you can probably create these weird kinds of behaviors relative to any of the contradiction rules for any puzzle!
Describe the improvement you'd like
In the scenario above, I really think the rule should not check out. There are a few ways we can try and do this:
-
Like I said, the contradiction was already there: making the top left square didn't create that contradiction. So, we can do the following: Check if without the added square we do not have a 'No Number' contradiction, but with the added square we do have a contradiction. This is probably the easiest way to deal with this issue.
-
We make our contradiction rules (and other rules) sensitive to certain reference squares. That is: instead of checking whether there is a contradiction of a certain kind anywhere on the board (which is how LEGUP implements contradiction rules currently), it checks whether there is a contradiction relative to that specific given square. For example, the 'No number' contradiction rule would check out for the white square surrounded by all blacks near the bottom right, but it would not check out for any other square.
With such a square-specific function we can solve the issue above: the 'Fill in Black' code will call the square-specific 'No Number' function with the square being the one that the use just made black: that is, the 'Fill in Black' function will ask: "if the square as indicate by the user were white, do we then have a 'No number' contradiction specific to that very white square?"
Note that if we had such a square-specific function, we can start asking the user to indicate where they believe some kind of contradiction is. That is, the user would not just select the 'No Number' contradiction rule, but would also have indicate (e.g by right-clicking?) the square relative to which they believe a contradiction exists, and now only the square-specific 'No Number' contradiction code is called relative to the indicated square.
Indeed, maybe we can have some kind if 'mode' that determines to what extent users need to do stuff and to what extent things can be left to the LEGUP engine. Indeed, in 'permissive' mode we use the good old LEGUP rule for any contradiction: is there a contradiction anywhere on the board? But note that we can always implement the good old 'board-wide' ('global') 'No Number' function as simply calling the square-specific function on all squares of the board.
Finally, once we start asking the user to indicate certain reference points in relation to the application of some rule, we can do this for non-contradiction rules as well. E.g. for 'Black between region' the user would need to indicate the numbers of the involved regions as reference points: such a square-specific 'Black between region' function would consider what were to happen if the square that was made black was in fact made white, and if that would lead to a 'Multiple Numbers' contradiction given the referenced number squares.
OK, so yeah, this could quickly get big: this would change the UI-flow, and we would need to have square/reference-speficic versions for most (all?) of the rules in LEGUP!
Describe alternatives you've considered
No response
Additional Context
No response