예제 #1
0
        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);
        }
예제 #2
0
        private UpdateResult SolvePuzzle()
        {
            UpdateResult updateResult = UpdateResult.unchanged;

            debugLog.Write("SolvePuzzle()\n");
            bool movedCloserToSolution = true;

            try
            {
                while (movedCloserToSolution)
                {
                    movedCloserToSolution = false;
                    //               SyncAllOpenCells();
                    foreach (CellGroup cellGroup in cellGroupsWithSums)
                    {
                        SolutionInfo result = cellGroup.Solve();
                        if (!result.solvable)
                        {
                            if (cellGroupsWithSums.Count == 0)
                            {
                                debugLog.Write("No more cell groups with sums left!\n");
                            }
                            throw (new IllegalSumException2("Solve attempt failed.  Group: " + cellGroup.ToString()));
                        }
                        if (result.MovedCloserToSolution())
                        {
                            movedCloserToSolution = true;
                            updateResult          = UpdateResult.changed;
                        }
                    }
                }
            }
            catch (types.IllegalSumException2 exception)
            {
                debugLog.Write(">>>>>>>>>>>> Caught an unsolvable exception, after the group set a sum.\n");
                debugLog.Write("  The error is:  " + exception.Message);
                updateResult = RewindLastSummedGroup();
            }
            MoveSolvedGroups();
            return(updateResult);
        }