OOD - Jigsaw puzzle


Given N numbers of jigsaw puzzle pieces, design a solution to solve this puzzle.
We don’t know the length or width of the puzzle.
Each piece may not put in the correct angle, may need rotation to solve it.
Each edge of a piece only has 1 other edge can match it.
isMatched(Edge e) will return true if two edges match.

jigsaw puzzle

class Piece {
    Edge top;
    Edge bot;
    Edge left;
    Edge right;

    int finalDegree;

class Edge{
    enum Type{

    Type type;

    boolean isMatched(Edge edge) {}

class Puzzle {
    Piece[][] result;
    Edge[] inners;
    Edge[] outers;
    Edge[] flats;

    Edge exposed_edge;

    void rotatePiece(String curDirection, String tarDirection, Piece p) {
        // rotate piece to target direction, set finalDegree

    void solve() {
        // find one corner piece
        // try to solve one row by iteration started from 1 edge of the corner piece
        //   use isMatched() to find next edge & piece
        //   use rotatePiece() rotate piece to correct degree/angle
        // store the other exposed edge of corner piece to exposed_edge
        // repeat above iteration to solve row by row 



Author: Zijun Zhou
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Zijun Zhou !