package com.task_360t.cubes;

import java.util.List;

import com.task_360t.cubes.exceptions.InvalidPieceException;
import com.task_360t.cubes.exceptions.NoPossibleSolutionException;
import com.task_360t.cubes.models.Cube;
import com.task_360t.cubes.models.Piece;

public class CubeSolver {

	/**
	 * Arrange pieces to form a cube
	 * 
	 * @param piecesInput
	 * @return
	 * @throws NoPossibleSolutionException
	 * @throws InvalidPieceException
	 */
	public Cube createCube(List<Piece> piecesInput) throws NoPossibleSolutionException, InvalidPieceException {
		Cube cube = new Cube();
		if (piecesInput.size() != 6)
			throw new InvalidPieceException("Invalid input pieces");
		//
		while(piecesInput.size()>0) {
			int idx = cube.createNextFace(piecesInput);
			if(idx != -1)
				piecesInput.remove(idx);
		}
		//
		//if (!cube.isCubeFormed())
		//	throw new NoPossibleSolutionException("Could not form a cube using the input pieces");
		return cube;
	}
}

@@ -23,16 +23,44 @@
 cubes;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -311,17 +311,16 @@
 olver %7B%0A
-%0A
 %09/**%0A%09 *
@@ -582,34 +582,8 @@
 n %7B%0A
-%09%09Cube cube = new Cube();%0A
 %09%09if
@@ -675,87 +675,519 @@
 ;%0A%09%09
-//%0A%09%09while(piecesInput.size()%3E0) %7B%0A%09%09%09int idx = cube.createNextFace(piecesInput
+Cube solution = new Cube();%0A%09%09solution = fillCubeWithPieces(solution, piecesInput);%0A%09%09if (solution == null)%0A%09%09%09throw new NoPossibleSolutionException(%22Could not form a cube using the input pieces%22);%0A%09%09return solution;%0A%09%7D%0A%09//%0A%09private Cube fillCubeWithPieces(Cube cube, List%3CPiece%3E pieces) %7B%0A%09%09if(cube.isCubeFormed())%0A%09%09%09return cube;%0A%09%09//%0A%09%09if(pieces.size()%3C1)%0A%09%09%09return null;%0A%09%09//%0A%09%09for(Piece piece:pieces)%0A%09%09%7B%0A%09%09%09// try to fill the current cube the pieces list%0A%09%09%09Cube sol = fillNextCubeFace(cube, piece, pieces
 );%0A%09
@@ -1195,54 +1195,199 @@
 %09if(
-idx
+sol
  != 
--1
+null
 )%0A%09%09%09%09
-piecesInput.remove(idx)
+return sol;%0A%09%09%09// if this failed, flip the piece and try again%0A%09%09%09piece.flipPiece();%0A%09%09%09sol = fillNextCubeFace(cube, piece, pieces);%0A%09%09%09if(sol != null)%0A%09%09%09%09return sol
 ;%0A%09%09%7D%0A%09%09
 //%0A%09
@@ -1386,133 +1386,486 @@
 %7D%0A%09%09
-//
 %0A%09%09
-//if (!cube.isCubeFormed())%0A
+return null;%0A%09%7D%0A%09//%0A%09private Cube fillNextCubeFace(Cube cube, Piece piece, List%3CPiece%3E pieces) %7B%0A%09%09for(int i=0; i%3C4; ++i)%0A%09%09%7B%0A%09%09%09if(cube.isPieceMatch(piece))%0A%09%09%09%7B%0A%09%09%09%09Cube tmpCube = new Cube(cube);%0A%09%09%09%09tmpCube.setNextFace(piece);%0A%09
 %09%09
+%09
 //
-%09throw new NoPossibleSolutionException(%22Could not form a cube using the input pieces%22
+ Complete the rest of the cube%0A%09%09%09%09List%3CPiece%3E tmpList = new ArrayList%3CPiece%3E(pieces);%0A%09%09%09%09tmpList.remove(piece);%0A%09%09%09%09Cube sol = fillCubeWithPieces(tmpCube, tmpList);%0A%09%09%09%09if(sol!=null)%0A%09%09%09%09%09return sol;%0A%09%09%09%7D%0A%09%09%09piece.rotateClockWise(
 );%0A
+%09%09%7D%0A
 %09%09re
@@ -1869,19 +1869,19 @@
 %09return 
-cube
+null
 ;%0A%09%7D%0A%7D%0A

Update CubeSolver.java