if (pieces.size() == 0)
return pieces;
int k = 0;
while (!solved && k < pieces.size() ) {
Piece p = pieces.get(k);
int r = 0;
// try the 4 rotations
while (!solved && r < 4) {
numTries_++;
if ( solution_.fits(p) ) {
solution_ = solution_.add( p );
pieces = pieces.remove( p );
puzzlePanel.refresh(solution_, numTries_);
puzzlePanel.makeSound();
// call solvePuzzle with a simpler case (one less piece to solve)
pieces = solvePuzzle( puzzlePanel, pieces, k);
solved = pieces.size() == 0;
}
if (!solved) {
p = p.rotate();
}
r++;
}
k++;
}
if (!solved && solution_.size() > 0) {
// backtrack.
Piece p = solution_.getLast();
solution_ = solution_.removeLast();
// put it back where we took it from,
// so our list of unplaced pieces does not get out of order.
pieces = pieces.add(i, p);
}