liouh / picross

Playable picross puzzle generator written in JavaScript
66 stars 25 forks source link

When multiple solutions are present, only one arbitrary solution is valid #1

Closed aimperiali closed 5 years ago

aimperiali commented 6 years ago

When a cell is selected, the validity of that solution is checked against a single predetermined solution. This does not take into account scenarios where multiple solutions exist [See image below]

guess: function(x, y, guess) {
    var solution = this.get('solution')[x][y];
    ...

    ...
    if(solution === guess) {
        state[x][y] = guess;
    } else {
        state[x][y] = solution * -1;
        mistakes++;
    }
    ...

    ...
}

One solution would be to add an else if statement between the if and else statements shown above. This else if statement would check if the selected cell contradicts the rest of the board constraints. This solution is not enough on its own because the stored solution would have to be updated to reflect the new solution required.

Because of this, I would focus deriving an algorithm that can efficiently check if the current state of the board can reach a solved state or if there is bound to be a contradiction to the board constraints. This algorithm could then be used in place of the stored-solution-check noted above. I'll be giving this some more thought in my spare time.

picross15x15_2

aimperiali commented 6 years ago

To expand on the existing method, the following pseudocode would fall between the if and else statements noted above. This is intended to check the minimum number of cells required to know if the player is following a solution different valid solution for the given board constraints. This method makes the assumption that alternate solutions arise in the form of the corners of a rectangle that can be swapped around without violating the board constraints displayed.

User selects a cell (lets name it cell c0)
Cell is checked against the solution array

If cell is different than the corresponding cell in the original solution (called solution1):
    for every empty cell (called cA) in the same column (providing cA != c0):
        for every empty cell (called cB) in the same row as cA:
            remaining corner of this rectangle can be called cell cC
            create a temporary solution where the contents of cell cA are switched with cell c0 and the contents of cell cB are switched with cell cC (called solution2)
            check if solution2 has the same board constraints as solution1
            if solution2 is as valid as solution1:
                replace solution1 with solution 2
                mark cell c0 as correct
                break (stop checking)
            else:
                mark cell c0 as incorrect
                (keep checking)
josephcsible commented 6 years ago

FWIW, here's another board that exhibits this bug: picross2

m-sterling commented 5 years ago

I'd like to bump this thread to ask if either of you have found a solution for this?

I encountered a puzzle with this same bug (seed 1560713029296) with four different solutions.

Unsolved puzzle

Possible solutions include the following: 1, 2, 3, and 4, but note that only solution 3 is recognized as the solution.

If I was more well-versed in JavaScript I might consider tackling this issue myself, but unfortunately that is not the case. 😄 However, I do propose a solution:

Of course, this then requires the "unmark" functionality, which hopefully doesn't seem like much. If I ever have time, I'll try to look at this with my limited JS knowledge.

liouh commented 5 years ago

I'm still surprised at the amount of traffic this 7-year-old project is getting. Apparently this version of the game is now the first hit for "picross" on Google 😮 Given the game's popularity, I was able to set aside a few hours today to look into a the multiple solutions problem.

I really liked the live-validating aspect of the original, and I tried to preserve it by regenerating the solutions board to match the constraints whenever a new "guess" was deemed incorrect. However, this is likely too slow for actual gameplay (see http://a.teall.info/nonogram/ to get a sense of the delay).

I ended up going with a variant of the suggestion by @kkirigaya -- removing the live-validation, and adding a button for the player to manually check à la https://www.puzzle-nonograms.com/. The seed generation hasn't changed, so you can test with your existing seeds.

The new version is available online at http://liouh.com/picross2/

Thanks for everyone that reported the issue, and enjoy!

bertek41 commented 2 years ago

I think algorithm should check if its solvable because I got this: image and I can't even start. Seed is 1640425856979 Tried http://a.teall.info/nonogram/ with {"ver":[[4,1],[2],[1,2],[1,2,1],[1,1,2],[1,1,1,1,1],[3],[1,3],[1,1,1,1],[3,1,1]],"hor":[[2,1,1],[2,1,1,1],[1,1,1],[1,1,2],[1],[1,3,1],[1,1,1],[5],[1,1,2,1],[2,1,1]]} and it couldn't solve either.

liouh commented 2 years ago

That seed is challenging but still solveable. It does require some trial and error, but the column with "5" and the row with "3" help narrow it down quite a bit. Make sure to enable show crossouts in the menu so you can see when the conditions are met.

liouh commented 2 years ago

Solution (spoilers): https://user-images.githubusercontent.com/1654183/147387037-b5f5ea25-04d2-4361-b560-cfb247f44d2c.png

bertek41 commented 2 years ago

Yeah, I thought every step had to have a logical explanation. Column with "5" and row with "3" is a good start. However I couldn't finish I think I need to do some trial and error like you said. Thank you.