Ejemplo n.º 1
0
 public string SolveProof(bool logical)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         StreamWriter log = new StreamWriter(ms);
         SolveResult  solns;
         if (logical)
         {
             HintSelections hard = new HintSelections(HintSelections.Level.Extreme);
             solns = solver.DoLogicalProof(this, hard, log);
         }
         else
         {
             solns = solver.DoBacktrackingProof(this, log);
         }
         log.Flush();
         ms.Position = 0;
         StringBuilder sb = new StringBuilder();
         string        line;
         using (StreamReader sr = new StreamReader(ms))
             while ((line = sr.ReadLine()) != null)
             {
                 sb.AppendLine(line);
             }
         sb.AppendLine(DescribeSolveResult(solns));
         return(sb.ToString());
     }
 }
Ejemplo n.º 2
0
 public void SolveLogically()
 {
     HintSelections.Level[] levels = new HintSelections.Level[] {
         HintSelections.Level.Easy,
         HintSelections.Level.Medium,
         HintSelections.Level.Hard,
         HintSelections.Level.Extreme,
         HintSelections.Level.Diabolical
     };
     foreach (HintSelections.Level level in levels)
     {
         HintSelections hs    = new HintSelections(level);
         SolveResult    solns = solver.DoLogicalSolve(this, hs);
         if (solns == SolveResult.SingleSolution)
         {
             SolveLevel(level);
             return;
         }
         else if (solns != SolveResult.TooDifficult)
         {
             ShowSolveResult(solns);
             return;
         }
     }
     ShowSolveResult(SolveResult.TooDifficult);
 }
Ejemplo n.º 3
0
        public SolveResult DoLogicalSolve(SudokuGrid grid, HintSelections hs)
        {
            if (hs == null)
            {
                return(DoBacktrackingSolve(grid));
            }

            while (true)
            {
                if (Solved)
                {
                    return(SolveResult.SingleSolution);
                }
                Hint hint = SingleHint(hs);
                if (hint == null)
                {
                    return(SolveResult.TooDifficult);
                }
                SolveResult result = hint.Apply(grid);
                if (result != SolveResult.Ongoing)
                {
                    return(result);
                }
            }
        }
Ejemplo n.º 4
0
 public void Generate(HintSelections hints)
 {
     // Take any solution for a clear grid
     ClearGrid();
     Candidate[] soln = solver.RandomSolution();
     if (soln == null)
     {
         return;
     }
     int[,] ans = new int[Cells, Cells];
     foreach (Candidate k in soln)
     {
         SudokuCandidate sc = (SudokuCandidate)k;
         ans[sc.x, sc.y] = sc.n;
     }
     // Add clues until soluable
     PlayMode = PlayModes.EditCell;
     while (true)
     {
         SolveResult sr = solver.DoLogicalSolve(this, hints);
         if (sr == SolveResult.SingleSolution)
         {
             break;
         }
         int x = Utils.Utils.Rnd(Cells);
         int y = Utils.Utils.Rnd(Cells);
         if (sr == SolveResult.NoSolutions)
         {
             ClearCell(x, y);
             ClearCell(8 - x, 8 - y);
         }
         else
         {
             SetCell(x, y, ans[x, y]);
             SetCell(Cells - x - 1, Cells - y - 1, ans[Cells - x - 1, Cells - y - 1]);
         }
     }
     // Remove any clues where it remains soluable
     for (int x = 0; x < Cells; ++x)
     {
         for (int y = 0; y < Cells; ++y)
         {
             ClearCell(x, y);
             if (solver.DoLogicalSolve(this, hints) != SolveResult.SingleSolution)
             {
                 SetCell(x, y, ans[x, y]);
             }
         }
     }
     Setup();
 }
Ejemplo n.º 5
0
 public void RateDifficulty(out HintSelections.Level?result, out SolveResult solns)
 {
     HintSelections.Level[] levels = new HintSelections.Level[] {
         HintSelections.Level.Easy,
         HintSelections.Level.Medium,
         HintSelections.Level.Hard,
         HintSelections.Level.Extreme,
         HintSelections.Level.Diabolical
     };
     foreach (HintSelections.Level level in levels)
     {
         HintSelections hs = new HintSelections(level);
         solns = solver.DoLogicalSolve(this, hs);
         if (solns != SolveResult.TooDifficult)
         {
             result = level;
             return;
         }
     }
     result = null;
     solns  = SolveResult.TooDifficult;
 }
Ejemplo n.º 6
0
        public SolveResult DoLogicalProof(SudokuGrid grid, HintSelections hs, TextWriter log)
        {
            while (true)
            {
                if (Solved)
                {
                    return(SolveResult.SingleSolution);
                }
                Hint hint = SingleHint(hs);
                if (hint == null)
                {
                    return(SolveResult.TooDifficult);
                }

                if (hint.IsComplex)
                {
                    int sc     = tsc;
                    var action = hint.Illustration;
                    if (action == Hint.Actions.Discard)
                    {
                        log.WriteLine("Suppose we do not " + hint.Candidate);
                        DiscardCandidate(hint.Candidate);
                    }
                    if (action == Hint.Actions.Select)
                    {
                        log.WriteLine("Suppose we " + hint.Candidate);
                        SelectCandidate(hint.Candidate);
                    }

                    int solns = 0;
                    while (true)
                    {
                        if (Solved)
                        {
                            break;
                        }
                        Requirement r = EasiestRequirement;
                        solns = r.s;
                        if (solns == 0)
                        {
                            log.WriteLine(" " + r); // No way to...
                        }
                        if (solns != 1)
                        {
                            break;
                        }
                        Candidate c = r.UnselectedCandidates[0];
                        //log.WriteLine(" then we must " + c);
                        log.WriteLine(" then " + c + " because " + r);
                        SelectCandidate(c);
                    }

                    while (tsc > sc)
                    {
                        UnselectCandidate();
                    }
                    if (action == Hint.Actions.Discard)
                    {
                        UndiscardCandidate(hint.Candidate);
                    }
                }

                log.WriteLine(hint.ToString());

                SolveResult result = hint.Apply(grid);
                if (result != SolveResult.Ongoing)
                {
                    return(result);
                }
            }
        }