Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        /***************************************************************************
         * 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);
        }