Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="M"></param>
        /// <returns></returns>
        public bool SolveStep(SolveMethods M)
        {
            SolutionStepList L = ComputePossibleSteps(M);

            if (L.Count() == 0)
            {
                return(false);
            }

            ApplySolutionStep(L.getItem(0));

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="M"></param>
        /// <returns></returns>
        public SolutionStepList ComputePossibleSteps(SolveMethods M)
        {
            EnumeratePossibilities();

            SolutionStepList L = new SolutionStepList();

            // Looking for naked singles
            if ((M & SolveMethods.NakedSingles) > 0)
            {
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        double index = Math.Log(a[j, i], 2) + 1;

                        if (index == Math.Floor(index) && a[j, i] > 0 && s[j, i] == 0)
                        {
                            L.Add(new SolutionStep(i, j, Convert.ToInt32(index)));
                        }
                    }
                }
            }

            // Looking for hidden singles
            if ((M & SolveMethods.HiddenSingles) > 0)
            {
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        int mask = 0;

                        for (int m = 0; m < 9; m++)
                        {
                            if (m != j)
                            {
                                mask |= a[m, i];
                            }
                        }

                        int match = a[j, i] & (511 - mask);

                        if (s[j, i] == 0 && match > 0)
                        {
                            double index = Math.Log(match, 2) + 1;

                            L.Add(new SolutionStep(i, j, Convert.ToInt32(index)));
                        }
                    }
                }
            }

            // Looking for block-hidden singles
            if ((M & SolveMethods.BlockHiddenSingles) > 0)
            {
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        int blockx = i / 3;

                        int blocky = j / 3;

                        int mask = 0;

                        for (int m = 0; m < 3; m++)
                        {
                            for (int n = 0; n < 3; n++)
                            {
                                int xindex = blockx * 3 + m;

                                int yindex = blocky * 3 + n;

                                if (yindex != j || xindex != i)
                                {
                                    mask |= a[yindex, xindex];
                                }
                            }
                        }

                        int match = a[j, i] & (511 - mask);

                        if (s[j, i] == 0 && match > 0)
                        {
                            double index = Math.Log(match, 2) + 1;

                            L.Add(new SolutionStep(i, j, Convert.ToInt32(index)));
                        }
                    }
                }
            }

            return(L);
        }