1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| public void solve() {
if (!pieceList.isEmpty()) {
// Take the first available piece
Piece currentPiece = (Piece)pieceList.remove(0);
for (int i = 0; i < Piece.NUMBEROFPERMUTATIONS; i++) {
Piece permutation = currentPiece.nextPermutation();
for (int j = 0; j < Board.NUMBEROFCELLS; j++) {
if (board.placePiece(permutation, j)) {
/* We have now put a piece on the board, so we have to
continue this process with the next piece by
recursively calling the solve() method */
solve();
/* We're back from the recursion and we have to continue
searching at this level, so we remove the piece we
just added from the board */
board.removePiece(permutation);
}
// Else the permutation doesn't fit on the board
}
}
// We're done with this piece
pieceList.add(0, currentPiece);
}
else {
/* All pieces have been placed on the board so we
have found a solution! */
puzzleSolved();
}
}
|