# Logic For Puzzle Game - How do I do it?

What is the game all about?

It's a puzzle game. I am using AndEngine to build it on Android platform (currently). I have divided the game into various World, with each World containing various Levels.

What is the Issue?

The purpose of the game is to drag-and-drop the broken pieces into the "drop" area (a.k.a. holder) and form the circle. I've developed the basic 2 levels of the game. This is the third level. Where there are 3 pieces as different objects. I've to drop all 3 pieces into the drop. The issue is how do I know that all the broken pieces have filled the holder? I'm using AndEngine for the first time and have very little idea how this can be done. Tapping on any broken object would rotate it by 90 degrees. So there can be 4 possible solutions for each of them).

Also, the logic should hold true for any shape, any number of broken pieces, any number of holders.

Below is a picture of level 3. There are 3 pieces named 1, 2 and 3 at different positions. All the pieces must be placed inside the circle.

Ideas that I've thought about

We've thought to solve this using:

1. Area of the holder is filled -> But how do I programmatically do that?
2. Take center coordinates of each broken piece and find the distance from the center of the circle. Verify this in all the 4 conditions (all 4 rotations by 90 degree each). -> I'm trying to figure this out.
3. See if all the broken pieces are used in the holder area -> What if there are more than one holder?

Any technical help or any references would be really great.

• Can you provide a screenshot of the game in progress? It's hard to visualize the problem from the description only. – msell Jun 7 '13 at 6:21
• Link for image..there are 3 pieces named 1,2,3 at different position. I've to place all this in a circle. Image for level3 – Vaibhav Devpura Jun 7 '13 at 6:27
• Please edit your question and add the image directly there. Links are discouraged for many reasons. – MartinTeeVarga Jun 7 '13 at 6:36
• Sorry for inconvenience. But I'm unable to post images due to my reputation. – Vaibhav Devpura Jun 7 '13 at 6:43
• It very much depends on your implementation. It doesn't really matter where the pivots of your pieces are, as long as you are consistent. Are your pieces sprites? Then you need to test distances between the actual sprite position, and the correct sprite position and if they are within a threshold, and the rotation is correct, then count it as placed. We're going to need more info about your implementation to help you thoroughly. – DaleyPaley Jun 7 '13 at 7:58

## 2 Answers

There are different approaches for this, especially considering there might be different solutions (like rotating the correct solutions).

If you're indeed looking into a rather limited amount of possible rotations and positions you'll just have to verify the given solution matches the expected result following a few rules:

• The order tiles are verified/considered must always be constant for the same puzzle.
• Tile coordinates are rounded to some extend (you'd probably want some snap-in system anyway).
• If there's no fixed offset (i.e. it shouldn't matter where the completed shape is), use the first tile's coordinates as offset.
• Each and every tile has a unique identifier (like a letter or string) unique to its combination of size and shape (same size and shape results in the same identifier).
• The whole solution given by the player is (behind the scenes) always rotated till the first tile isn't considered rotated at all.
• Now just compare all tiles and ensure they have the correct orientation and position.

For the last thing to check, the easiest way might be creating two strings, one based on your solution and one on the current state of the game:

• For each tile add the following to the string:
• Tile identifier
• Separator
• Tile position
• Separator
• Tile rotation
• Separator
• The result could be something like this: A|0,0|0|B|3,1|0|B|1,3|1|C|2,2|3...
• Now if the string generated based on the player's solution and your pregenerated solution match, the puzzle is solved.

As an alternative, you can of course compare the different values on their own using some loop and multiple comparisons or create one string with positions and one with rotations, etc.

Out of my gut feeling, I would go for your first idea: "Area of the holder is filled"

That's how I would approach it:

Get a tiled representation of your play area. Like 40x40 tiles or so. Every tile gets the "number of shapes occupying this".

You will need a routine that finds all tiles a current shape is touching, for example the easiest is probably "for each tile, test if tile center is inside shape".

Whenever you move or drop one of the shapes, run the routine to remove it from the current tiles before moving the shape. The re-run it again when the shape has been placed to increase the use counter.

You are finished, when all tiles in your target zone have exactly an count of 1.

If you want to forbid the player to overlap shapes at any time, you can use the very same routine to test for these overlaps.

If overlaps are OK for you (as long as the end-result doesn't overlap), you actually don't need to check any tile outside the holder area. This will probably considerably speedup the testing.

Keep the actual number of X/Y tiles some constant, so that you can easily change it. You might want to experiment with the size of the tiles later. They should be tiny enough to not give wrong results and big enough to be still performant and allow for some small inaccuracy.

The big advantage here is, that you can go nuts with your shapes. You can have any number of target zones and even can make overlapping target zones (where the end shapes need to overlap too etc..).