This is not a trivial task to solve and a
number of papers[
^] have been written on this subject. The things they tend to have in common is that they start off with the assumption that they are dealing with computer vision processing, that any piece with a flat side must be an edge piece (if you're dealing with the traditional jigsaw form), and that a piece with two flat sides will be an edge. Part of the issue you are going to face is that you cannot guarantee what orientation each piece is, so you will also have to consider rotation when solving your code.