public SolutionInfo Solve() { SolutionInfo result = new SolutionInfo(); if (!SumIsKnown()) { return(result); } List <OpenCell> copyOfUnsolvedCellList = data.GetCopyOfUnsolvedCells(); foreach (OpenCell unsolvedCell in copyOfUnsolvedCellList) { SolutionInfo cellsSolutionInfo = unsolvedCell.UpdateValueList(false); if (!cellsSolutionInfo.solvable) { return(cellsSolutionInfo); } cellsSolutionInfo.CombineWith(CheckCellValuesForViability(unsolvedCell)); // debugLog.Write(cell.AllPossibleValues()); if (unsolvedCell.OnlyOneSolution()) { FinalizeBothParents(unsolvedCell); } result.CombineWith(cellsSolutionInfo); } SolutionInfo newResult = result; while (newResult.MovedCloserToSolution()) { newResult = Solve(); result.CombineWith(newResult); } return(result); }
public SolutionInfo CheckAllCellsForSolvability() { List <OpenCell> copyOfUnsolvedCellList = data.GetCopyOfUnsolvedCells(); SolutionInfo overallSolutionInfo = new SolutionInfo(); foreach (OpenCell cell in copyOfUnsolvedCellList) { SolutionInfo cellSolutionInfo = cell.UpdateValueList(false); overallSolutionInfo.CombineWith(cellSolutionInfo); if (!overallSolutionInfo.solvable) { return(overallSolutionInfo); } overallSolutionInfo.CombineWith(CheckCellValuesForViability(cell)); //if (cell.OnlyOneSolution()) // SetCellToSolved(cell); } return(overallSolutionInfo); }
/*************************************************************************** * UpdateValueList() * tests the list of solutions, and removes the ones that do not work. * returns the increase in solutions (i.e. a negative number is good.) **************************************************************************/ public SolutionInfo UpdateValueList(bool debugStatementsOn) { if (debugStatementsOn) { debugLog.Write("Starting to UpdateValueList cellToTry (" + ToString() + "->"); } SolutionInfo returnInfo = new SolutionInfo(); int initialNumOfSolutions = cellSolutionInfo.NumberOfPossibleDigits(); returnInfo.CombineWith(MergeValueListWithParentGroups()); int updatedNumberOfSolutions = cellSolutionInfo.NumberOfPossibleDigits(); returnInfo.increaseInNumber = updatedNumberOfSolutions - initialNumOfSolutions; if (debugStatementsOn) { debugLog.Write(ToString() + ")\n"); } return(returnInfo); }