@Test public void testSolve() { Piece[] pieces = new Piece[5]; Piece first = new Piece(0); Piece second = new Piece(1); Piece third = new Piece(1); Piece fourth = new Piece(2); Piece fifth = new Piece(3); pieces[0] = first; pieces[1] = second; pieces[2] = third; pieces[3] = fourth; pieces[4] = fifth; JigsawPuzzle puzzle = new JigsawPuzzle(pieces); Assert.assertTrue(puzzle.solve()); }
public boolean solve() { Piece aPiece = null; Piece anotherPiece = null; Stack<Piece> stackOfPieces = stackOfPieces(); while (stackOfPieces.size() > 1) { aPiece = stackOfPieces.pop(); while (!stackOfPieces.isEmpty()) { anotherPiece = stackOfPieces.pop(); if (aPiece.fitsWith(anotherPiece)) { Piece bigPiece = aPiece.joinWith(anotherPiece); stackOfPieces.push(bigPiece); break; } } } return stackOfPieces.size() == 1; }
JigsawPuzzle { public boolean solve() { Piece aPiece = null; Piece anotherPiece = null; Stack<Piece> stackOfPieces = stackOfPieces(); while (stackOfPieces.size() > 1) { aPiece = stackOfPieces.pop(); while (!stackOfPieces.isEmpty()) { anotherPiece = stackOfPieces.pop(); if (aPiece.fitsWith(anotherPiece)) { Piece bigPiece = aPiece.joinWith(anotherPiece); stackOfPieces.push(bigPiece); break; } } } return stackOfPieces.size() == 1; } }
JigsawPuzzle { public boolean solve() { Piece aPiece = null; Piece anotherPiece = null; Stack<Piece> stackOfPieces = stackOfPieces(); while (stackOfPieces.size() > 1) { aPiece = stackOfPieces.pop(); while (!stackOfPieces.isEmpty()) { anotherPiece = stackOfPieces.pop(); if (aPiece.fitsWith(anotherPiece)) { Piece bigPiece = aPiece.joinWith(anotherPiece); stackOfPieces.push(bigPiece); break; } } } return stackOfPieces.size() == 1; } JigsawPuzzle(Piece[] pieces); }
JigsawPuzzle { public boolean solve() { Piece aPiece = null; Piece anotherPiece = null; Stack<Piece> stackOfPieces = stackOfPieces(); while (stackOfPieces.size() > 1) { aPiece = stackOfPieces.pop(); while (!stackOfPieces.isEmpty()) { anotherPiece = stackOfPieces.pop(); if (aPiece.fitsWith(anotherPiece)) { Piece bigPiece = aPiece.joinWith(anotherPiece); stackOfPieces.push(bigPiece); break; } } } return stackOfPieces.size() == 1; } JigsawPuzzle(Piece[] pieces); Piece[] piecesOfPuzzle(); boolean solve(); }
JigsawPuzzle { public boolean solve() { Piece aPiece = null; Piece anotherPiece = null; Stack<Piece> stackOfPieces = stackOfPieces(); while (stackOfPieces.size() > 1) { aPiece = stackOfPieces.pop(); while (!stackOfPieces.isEmpty()) { anotherPiece = stackOfPieces.pop(); if (aPiece.fitsWith(anotherPiece)) { Piece bigPiece = aPiece.joinWith(anotherPiece); stackOfPieces.push(bigPiece); break; } } } return stackOfPieces.size() == 1; } JigsawPuzzle(Piece[] pieces); Piece[] piecesOfPuzzle(); boolean solve(); public Piece[] piecesOfPuzzle; }