private UpdateResult RewindLastSummedGroup() { debugLog.Write("Cleaning out all groups.\n"); CellGroup groupToMove = cellGroupsWithSums.Pop(); CleanOutAllCellValues(); groupToMove.RestoreAllData(); cellGroupsWithoutSums.Push(groupToMove); return(UpdateResult.repaired); }
/************************************************************************************* * FindSolutionsForCells *------------------------------------------------------------------------------------ * This method takes a cellToTry from the matrix, and examines the cellToTry's corresponding * parent groups (i.e. the collumn and row to which the cellToTry belongs.) One of * the parent groups is selected to be maximized, and the other is minimized. The * digit for the cellToTry is then the only possible digit at the interesection of the two * * Outstanding problems: * A sum can be set for a group that is impossible to achieve because possible values * for copyOfUnsolvedCellList have not been checked. * *************************************************************************************/ private UpdateResult FindSolutionsForCells() { debugLog.Write("\n________________________________\n\n"); UpdateResult setSumResult = UpdateResult.unchanged; UpdateResult solvePuzzleResult = UpdateResult.unchanged; if (cellGroupsWithoutSums.Count != 0) { CellGroup groupToSolve = cellGroupsWithoutSums.Peek(); try { setSumResult = groupToSolve.SetTheSum(); // write a new method that checks for groups that could swapcells. If a pair is found, // throw an IllegalSumException2. MoveNextGroupToSummedQueue(); } catch (IllegalSumException2 exception) { debugLog.Write("There was a problem finding a sum. Restoring group.\n"); debugLog.Write(exception.Message); setSumResult = groupToSolve.RestoreAllData(); CleanOutAllCellValues(); } // The following has been removed to a higher level. now we just let it go, // and recreate the puzzle from scratch. //catch (UnsolvableGroupException2 exception) //{ // debugLog.Write("The current group cannot be solved. Reverting last group.\n"); // debugLog.Write(exception.Message); // setSumResult = groupToSolve.RestoreAllData(); // No... Something more is needed. Complete reset. // setSumResult = RewindLastSummedGroup(); //} debugLog.Write("=================>SetTheSum returned success:" + setSumResult + "\n"); solvePuzzleResult = SolvePuzzle(); } else { solvePuzzleResult = SolvePuzzle(); debugLog.Write("=================>SolvePuzzle returned success:" + solvePuzzleResult + "\n"); foreach (CellGroup group in cellGroupsWithSums) { if (group.CannotBeSolved()) { debugLog.Write(" SolvePuzzle found an unsolvable group.\n"); debugLog.Write(" I'm not going to raise an error, I'll just handle it here.\n"); RewindLastSummedGroup(); return(UpdateResult.repaired); } } } if ((setSumResult == UpdateResult.repaired) || (solvePuzzleResult == UpdateResult.repaired)) { return(UpdateResult.repaired); } if ((setSumResult == UpdateResult.changed) || (solvePuzzleResult == UpdateResult.changed)) { return(UpdateResult.changed); } return(UpdateResult.unchanged); }