Пример #1
0
        private UpdateResult RewindLastSummedGroup()
        {
            debugLog.Write("Cleaning out all groups.\n");
            CellGroup groupToMove = cellGroupsWithSums.Pop();

            CleanOutAllCellValues();
            groupToMove.RestoreAllData();
            cellGroupsWithoutSums.Push(groupToMove);
            return(UpdateResult.repaired);
        }
Пример #2
0
        private void RandomlyOrderGroupsWithoutSums()
        {
            Stack <CellGroup> newCellGroups        = new Stack <CellGroup>();
            Randomizer        randomCellGroupIndex = new Randomizer(0, cellGroupsWithoutSums.Count - 1);

            for (int i = 0; i < cellGroupsWithoutSums.Count; ++i)
            {
                int       randomIndex = randomCellGroupIndex.Next();
                CellGroup cellGroup   = cellGroupsWithoutSums.ElementAt(randomIndex);
                newCellGroups.Push(cellGroup);
            }
            cellGroupsWithoutSums = newCellGroups;
        }
Пример #3
0
        private void MoveNextGroupToSummedQueue()
        {
            CellGroup groupToSolve = cellGroupsWithoutSums.Pop();

            cellGroupsWithSums.Push(groupToSolve);
        }
Пример #4
0
        /*************************************************************************************
        *                               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);
        }
Пример #5
0
        public bool CreateThePuzzle()
        {
            bool solvablePuzzleCreated = true;
            int  maxNumberOfLoops      = 50;

            CreateAllColumnGroups();
            CreateAllRowGroups();
            RandomlyOrderGroupsWithoutSums();
            //FindAllSquares();

            // 1. Select a random cellToTry, and pick a valid solution for it.  (Done here in CSM)
            // 2. Restrict possible solutions to all copyOfUnsolvedCellList in both parents.  (Done in the chosen parent group.)
            // 3. Remove completely solved groups from the puzzle.  (Done in the chosen parent group.)
            // 4. Are all the copyOfUnsolvedCellList solved?  No: go to 1,  Yes go to 5.  (Done here in CSM)
            // 5. Verify the solution?  Possibly not...

            int          loopCount      = 0;
            UpdateResult result         = UpdateResult.changed;
            TimeSpan     span           = new TimeSpan();
            double       mostrecentTime = span.TotalSeconds;

            while (result != UpdateResult.unchanged)
            {
                result = FindSolutionsForCells();
                if (result == UpdateResult.repaired) // "while", not "if"?
                {
                    try
                    {
                        SolvePuzzle(); // If changed to while, got to update result here.
                    }
                    catch (UnsolvableGroupException2 exception)
                    {
                        debugLog.Write("The current group cannot be solved.  Reverting last group.\n");
                        debugLog.Write(exception.Message);
                        solvablePuzzleCreated = false;
                        break;
                    }
                }
                ++loopCount;
                debugLog.Write(CellGroup.PrintAllColumnSums());
                debugLog.Write(CellGroup.PrintAllRowSums());
                debugLog.Write("\n--------\nNumber of cell groups without sums left:"
                               + cellGroupsWithoutSums.Count + "    " + result + "\n--------\n");
                if (loopCount > maxNumberOfLoops)
                {
                    solvablePuzzleCreated = false;
                    break;
                }
                double currentTime = span.TotalSeconds;
                double timeSpent   = currentTime - mostrecentTime;
                if (timeSpent > 2)
                {
                    debugLog.Write("\n\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
                    debugLog.Write("                                  Total Time spent: " + timeSpent + "\n");
                    debugLog.Write("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n\n");
                }
            }
            if (!solvablePuzzleCreated)
            {
                debugLog.Write("Had a problem.  I'll retry from the very beginning.\n");
                solvablePuzzleCreated = false;
            }
            return(solvablePuzzleCreated);
        }
Пример #6
0
 public void AddParentGroup(CellGroup parentGroup)
 {
     parentGroups.Enqueue(parentGroup);
 }