private bool FinalizeCell(OpenCell cellToFinalize) { debugLog.Write(" FinalizeCell " + cellToFinalize.ToString() + "...\n"); if (!cellToFinalize.OnlyOneSolution()) { debugLog.Write("Finalize is a no-op, since there is still more than one possible solution.\n"); return(false); } data.MoveToSolvedList(cellToFinalize); data.availableDigits.Remove(cellToFinalize.GetValue()); cellToFinalize.SetCellAsSolved(); foreach (OpenCell cell in data.GetCopyOfUnsolvedCells()) { if (cell == cellToFinalize) { continue; } SolutionInfo solutionInfo = cell.UpdateValueList(true); //if (solutionInfo.MovedCloserToSolution() && cell.OnlyOneSolution()) // FinalizeCell(cell); } return(true); }
public void FinalizeBothParents(OpenCell cellToFinalize) { bool cellWasFinalized = true; foreach (CellGroup parentGroup in cellToFinalize.GetParentGroups()) { cellWasFinalized &= parentGroup.FinalizeCell(cellToFinalize); } if (cellWasFinalized) { debugLog.Write("Finalized " + cellToFinalize.ToString() + ".\n"); } }
private SolutionInfo CheckCellValuesForViability(OpenCell cell) { // debugLog.Write("CheckCellValuesForViability(" + cell.ToString() + ")"); SolutionInfo solutionInfo = new SolutionInfo(); int sumOfUnsolvedCels = SumOfAllUnsolvedCells(); if (sumOfUnsolvedCels == 0) { solutionInfo.solvable = false; return(solutionInfo); } List <int> possibleDigits = new List <int>(cell.GetPossibleValues()); int initialNumberOfDigits = cell.GetPossibleValues().Count; List <OpenCell> copyOfUnsolvedCellList = data.GetCopyOfUnsolvedCells(); copyOfUnsolvedCellList.Remove(cell); Stack <OpenCell> cellStack = new Stack <OpenCell>(copyOfUnsolvedCellList); foreach (int digit in possibleDigits) { if (digit <= sumOfUnsolvedCels) { List <int> availableDigitsFromGroup = new List <int>(data.availableDigits); availableDigitsFromGroup.Remove(digit); if (!SumWorksForGroup(sumOfUnsolvedCels - digit, availableDigitsFromGroup, cellStack)) { cell.DigitIsNotAPossibleValue(digit); } } else { cell.DigitIsNotAPossibleValue(digit); } } int finalNumberOfDigits = cell.GetPossibleValues().Count; solutionInfo.increaseInNumber = finalNumberOfDigits - initialNumberOfDigits; debugLog.Write(" Validate cell " + cell.ToString() + "(start:" + initialNumberOfDigits + " end:" + finalNumberOfDigits + ")\n"); if (finalNumberOfDigits == 0) { solutionInfo.solvable = false; } return(solutionInfo); }