//find all possible combinations public void GenerateAllCombinations() { Debug.Log("Generating Combinations..."); int combinationCount = 5 * 4 * 3 * 2 * orientationsPerPiece * orientationsPerPiece * orientationsPerPiece * orientationsPerPiece * orientationsPerPiece; int[] pieceAtSlot = new int[6]; pieceAtSlot[0] = 0; for (pieceAtSlot[1] = 0; pieceAtSlot[1] < 6; pieceAtSlot[1]++) { for (pieceAtSlot[2] = 0; pieceAtSlot[2] < 6; pieceAtSlot[2]++) { for (pieceAtSlot[3] = 0; pieceAtSlot[3] < 6; pieceAtSlot[3]++) { for (pieceAtSlot[4] = 0; pieceAtSlot[4] < 6; pieceAtSlot[4]++) { for (pieceAtSlot[5] = 0; pieceAtSlot[5] < 6; pieceAtSlot[5]++) { if (isPermutation(pieceAtSlot)) //OK!!! :) { int[] orientationAtSlot = new int[6]; orientationAtSlot[0] = 0; for (orientationAtSlot[1] = 0; orientationAtSlot[1] < orientationsPerPiece; orientationAtSlot[1]++) { for (orientationAtSlot[2] = 0; orientationAtSlot[2] < orientationsPerPiece; orientationAtSlot[2]++) { for (orientationAtSlot[3] = 0; orientationAtSlot[3] < orientationsPerPiece; orientationAtSlot[3]++) { for (orientationAtSlot[4] = 0; orientationAtSlot[4] < orientationsPerPiece; orientationAtSlot[4]++) { for (orientationAtSlot[5] = 0; orientationAtSlot[5] < orientationsPerPiece; orientationAtSlot[5]++) { PieceInPosition[] solution = new PieceInPosition[6]; for (int slotIndex = 0; slotIndex < 6; slotIndex++) { solution[slotIndex] = new PieceInPosition(pieceAtSlot[slotIndex], GetOrientation(orientationAtSlot[slotIndex])[0], GetOrientation(orientationAtSlot[slotIndex])[1]); } solutionList.Add(solution); } } } } } Debug.Log("Generating Combination: " + solutionList.Count + " of " + combinationCount + " (" + 100f * ((float)solutionList.Count / (float)combinationCount) + "%)"); } } } } } } Debug.Log("All " + solutionList.Count + " combinations generated..."); }
private void FixedUpdate() { if (solutionList.Count == 0 || done) { return; } bool skip = false; do { skip = false; if (sulotionIndex < solutionList.Count) { PieceInPosition[] solution = solutionList[sulotionIndex]; if (jammerPiece != null && enableSkip) { PieceInPosition possibleJammerPiece = solution[jammerPiece.slotIndex]; if (possibleJammerPiece.pieceIndex == jammerPiece.pieceIndex && possibleJammerPiece.rotationY == jammerPiece.rotationY && possibleJammerPiece.rotationX == jammerPiece.rotationX) { skip = true; } else { jammerPiece = null; } } if (!skip) { world.Clear(); bool wasted = false; for (int slotIndex = 0; slotIndex < 6; slotIndex++) { bool couldPlace = slots[slotIndex].CanPlaceInWorld(PieceSet.GetPiece(solution[slotIndex].pieceIndex), solution[slotIndex].rotationY, solution[slotIndex].rotationX, world); slots[slotIndex].ForcePlaceInWorld(PieceSet.GetPiece(solution[slotIndex].pieceIndex), solution[slotIndex].rotationY, solution[slotIndex].rotationX, world, wasted || !couldPlace); if (!couldPlace || wasted) { if (!wasted) { jammerPiece = solution[slotIndex]; jammerPiece.slotIndex = slotIndex; } wasted = true; if (enableSkip) { break; } } else { if (slotIndex + 1 > piecePlacedRecord) { piecePlacedRecord = slotIndex + 1; recordWorld = new World(world); if (piecePlacedRecord == solutionLimit) { break; } } } } } sulotionIndex++; if (sulotionIndex % 100 == 0) { float timeElapsed = Time.realtimeSinceStartup - startTime; float timeLeft = (((float)solutionList.Count / (float)sulotionIndex) * timeElapsed - timeElapsed) / 60f; Debug.Log("Checking combination: " + sulotionIndex + " of " + solutionList.Count + ", Checked: " + 100f * ((float)sulotionIndex / (float)solutionList.Count) + " %, Record: " + piecePlacedRecord + " pieces, Estimated time left: < " + (timeLeft < 60 ? timeLeft + " minutes" : timeLeft / 60f + " hours")); } if (piecePlacedRecord == solutionLimit) { Debug.Log("Yey!! :) Solution found!!"); done = true; } visualWorld.Refresh(world); } else { Debug.Log("This puzzle is Shit! Every possible combination tried (" + solutionList.Count + "). Could only place " + piecePlacedRecord + " pieces. One of the best solutions is shown."); visualWorld.Refresh(recordWorld); done = true; } } while (skip); }