How to check if a puzzle is solved?
I made a jigsaw puzzle with the UI using drag and drop. You can drag puzzle pieces and drop them on panels, but I don't know how to check if the puzzle is solved or not. How should that be checked? Checking which piece is a child of every panel? Thank you.
Answer by Major · Jun 25, 2017 at 09:06 PM
You would first need to develop a system for determining if the puzzle pieces were in the correct spot. Perhaps one way of doing this would be a Vector2 ID position system in which the ID of the piece and the ID of its slot would have to match.
I would then communicate with a master script to communicate whether or not the piece is in the right place. Instead of checking each slot, or node, every frame, I would instead use a counter system. What I mean by this is that your master script will have a variable corresponding to the total number of pieces in the puzzle, say this variable is called pieces and it equals 100. You would then have a counter variable, say piecesLeft and have that set to pieces. For clarity sake what I mean here would look something like this:
public int pieces = 100;
public int piecesLeft;
void Start () {
piecesLeft = pieces;
}
Each of your piece slots would then subtract 1 from piecesLeft if the correct piece was placed. If, for whatever reason, that same piece was removed it would add 1 to pieces left, and of course if it wasn't the correct piece you would do nothing.
From here you can just check if piecesLeft == 0. If it does, you have completed the puzzle. This would save you from having to use any loops, so you could, in theory of course, make a puzzle with 10,000 pieces and not see a performance hit.
Hope this was helpful!
Your answer
Follow this Question
Related Questions
How to move the player only 1 tile per buttonPress? 2 Answers
Why is collision Detection only registering in the corners? 0 Answers
Stopping camera at room boundaries in 2.5D sidescrolling puzzle game 0 Answers
How to create a book that can be called anywhere and interacted with. Like a journal. 0 Answers
How do you make a player pass through a wall depending on the color 1 Answer