1

I am planning to develop a jigsaw puzzle game. Now I already have images and image pieces, so we don't need algorithm to cut the image in pieces.

On the UI side there would be two sections

  1. First section contains the broken images in random order.
  2. Second section contains the outline of the full image. User need to drag and drop the the cut images onto the outline image.

I am not sure how can the pieces be matched on the the outline image? Any idea about the algorithm or the starting pointers?

Yi Jiang
  • 48,053
  • 16
  • 135
  • 134
anand
  • 10,761
  • 25
  • 96
  • 154
  • will the pieces be regular squares or random polygons? – st0le Oct 31 '10 at 07:01
  • "How do I do task X?" is too vague. If you have a specific problem you're facing, please ask that instead. – GManNickG Oct 31 '10 at 07:02
  • pieces would be random polygone – anand Oct 31 '10 at 07:05
  • Okay so specific problems would be: – anand Oct 31 '10 at 07:06
  • 1) How to break image into random polygons 2) what kind of data structure can solve the problem 3) UI implementation – anand Oct 31 '10 at 07:07
  • @Alien: 1 and 2 are one question, 3 is its own. And before you can accomplish 1, you need to decide how you'll be loading and presenting images, so perhaps you should ask 3 first. (Though there are existing answers in that regard.) After you have made a UI and can load and image *then* you should ask how to represent puzzle pieces. (Which may be a duplicate too.) – GManNickG Oct 31 '10 at 07:11
  • Once I am able to break the image into random polygons , UI would to simply display the images in random order but those random plygons need to be fitted on the skeleton.So the UI implementation that is 3 is not required to be known before 1 and 2. – anand Oct 31 '10 at 07:15
  • @Alien: How will you store your images? – GManNickG Oct 31 '10 at 07:37
  • I did not try anything till now.Its not a homework. – anand Nov 03 '10 at 08:31

5 Answers5

2

Allow the user to drag each piece into the outline area. Allow the piece to be rotated in 90 degree increments.

Option 1: If a piece is in the correct location in the overall puzzle, and at the correct angle, AND connected to another piece, then snap it into place with some user feedback. The outside edge of the puzzle can count for a connection to edge pieces.

Option 2: A neighbor is an adjacent puzzle piece when the puzzle is assembled. When the puzzle pieces are mixed up, they still have the same neighbors. Each puzzle piece (except the edge pieces) has four neighbors.

If a piece is near one of its neighbors at the correct angle relative to that neighbor, then snap it to the other piece. Then allow the two (or more) pieces to be dragged around as a unit, as is done with a single piece. This would allow the user to assemble subsections of the puzzle in any area, much like is done with a physical jigsaw puzzle, and connect the subsections with one another.

You can check the piece being moved to its four neighbors to see if they are close enough to snap together. If a piece has its proper edge close enough to the proper edge of its neighbor, at the same angle, then they match.

There are several ways to check relative locations. One way would be to temporarily rotate the coordinates of the piece you are testing so it is upright, then rotate the coordinates of all its desired neighbors, also temporarily, to the same angle. (Use the same center of rotation for all the rotations.) Then you can easily test to see if they are close enough to match. If the user is dragging a subassembly, then you will need to check each unmatched edge in the subassembly.

Option 2 is more complex and more realistic. Option 1 can be further simplified by omitting the rotation of pieces and making every piece the proper angle initally.

xpda
  • 15,322
  • 8
  • 50
  • 80
1

For a regular shapes you can go with a matrix. I recommend this as the first approach. Dividing the puzzle is as simple as defining X,Y dimensions of the matrix. For each piece you have a series of four values then, one for each side, saying whether it is flat, pointing out, or pointing in. This will give you a very classic jigsaw puzzle setup.

How the pieces actually look becomes a strict GUI thing. Now, for the first draft I recommend getting it working with perfectly square pieces. Taking rectangular bits of an image should be easy to do in any GUI framework.

To go to shaped pieces you'll need a series of templates. These will become masks that you apply to the image. Each mask clips out a tiny portion of the image to produce your piece. You'll probably need to dynamically create the masks in order to fit them to the puzzle. At first start with simply triangular connections. Once you have that working you can do the math to get nice bulbous connector shapes. Look up "clip" and "mask" in your GUI framework.

If you wish to do irregular polygon shapes that don't follow a general matrix layout, then you need to do a lot more work. This is why I recommend getting the square first working as a good example. Now you'll need to delve into graph theory and partitioning. Pick up some books on 3D programming -- focusing on algorithms, as they do partitioning all the time. Though I wouldn't doubt if there is a book with this exact topic in it.

Have fun.

edA-qa mort-ora-y
  • 28,019
  • 37
  • 130
  • 248
0

the data structure is simple I guess- each peace will point to it's neighbors and will hold the actual shape to display.

on the MMI (UI) of the app - what is your developing environment ? If it's windows - I would go with c# and winforms or even better wpf. if it's unix, you'll have to get someone else's advise, as I'm not an expert there.

Dani
  • 13,910
  • 11
  • 59
  • 101
0

1) How to break image into random polygons

It seems that you have figured out this part. (from : "Now I already have images and image pieces, so we don't need algorithm to cut the image in pieces.")

2) what kind of data structure can solve the problem

You can create a Class Piece like Scribble class in this example and your pieces would be array of objects of Piece class. So, you will have two arrays, (i) actual image pieces array (ii) image piece outline array

So, whenever you drag and drop one piece on to the full outline of image, it will check whether the image piece object is intersecting more than 80% and ID (member variable of Piece object) of actual image piece and image piece outline matches, then you got the right piece at right place...

3) UI implementation

Check this out.

Community
  • 1
  • 1
Eternal Noob
  • 2,677
  • 5
  • 24
  • 40
0

You could make an array of objects of the class "PuzzleTile"

Every such tile has an image and an integer
After every move, check if the integers are sorted correctly, means:
123
456
789

You could make a function for that which returns a bool.

Note: I'm currently developing under C#, that's why it's probably easiest to realize especially this concept under C#, although other platforms need none up to barely some modification to this.

MechMK1
  • 2,926
  • 7
  • 35
  • 54