Esempio n. 1
0
        private void ReduceRegion(int skip, SudokuRegion region, SudokuState state, ICollection <IEvent> events)
        {
            var quad = 0u;

            buffer.Clear();

            foreach (var index in region.Skip(skip))
            {
                var value = state[index];

                if (SudokuCell.Count(value) < 4)
                {
                    quad |= value;

                    // Joined the represent more then 3 values.
                    if (SudokuCell.Count(quad) > 4 || buffer.Count > 3)
                    {
                        return;
                    }
                    buffer.Add(index);
                }
            }

            if (buffer.Count == 4)
            {
                Fetch(quad, buffer, region, state, events);
            }
        }
Esempio n. 2
0
        /// <summary>Reduced a cell by excluding the options of the other.</summary>
        public ReduceResult AndMask(int index, uint mask)
        {
            unchecked
            {
                var val = m_Values[index];
                var nw  = val & mask;
                m_Values[index] = nw;
                if (nw == SudokuPuzzle.Invalid)
                {
                    return(ReduceResult.Inconsistent);
                }

                if (SudokuCell.Count(nw) == 1 && SudokuCell.Count(val) != 1)
                {
                    Unknowns--;
                    if (IsSolved)
                    {
                        return(ReduceResult.Solved | ReduceResult.Reduced | ReduceResult.Found);
                    }
                    return(ReduceResult.Reduced | ReduceResult.Found);
                }

                return(val != nw ? ReduceResult.Reduced : ReduceResult.None);
            }
        }
Esempio n. 3
0
        private void ReduceRegion(SudokuRegion region, SudokuState state, ICollection <IEvent> events)
        {
            var nakedPair = 0u;
            var count     = 0;

            foreach (var index in region)
            {
                var value = state[index];

                // nothing found yet.
                if (nakedPair == default)
                {
                    if (SudokuCell.Count(value) == 2)
                    {
                        nakedPair = value;
                        count++;
                    }
                }

                // Equal to the first (potential) naked pair.
                else if (value == nakedPair && count++ > 2)
                {
                    throw new InvalidPuzzleException();
                }
            }

            if (count > 1)
            {
                Fetch(nakedPair, region, state, events);
            }
        }
Esempio n. 4
0
 public ReduceHiddenPairs()
 {
     for (var f = 1; f <= 8; f++)
     {
         for (var s = f + 1; s <= 9; s++)
         {
             Pairs.Add(SudokuCell.Single(f) | SudokuCell.Single(s));
         }
     }
 }
Esempio n. 5
0
        /// <summary>Reduced a cell by excluding the options of the other.</summary>
        public IEvent And <TSolver>(int index, uint mask)
        {
            unchecked
            {
                var val = m_Values[index];
                var nw  = val & mask;
                m_Values[index] = nw;

                if (nw == SudokuPuzzle.Invalid)
                {
                    throw new InvalidPuzzleException();
                }

                if (SudokuCell.Count(nw) == 1 && SudokuCell.Count(val) != 1)
                {
                    return(ValueFound.Ctor <TSolver>(index, nw));
                }

                return(val != nw ? ReducedOption.Instance : (IEvent)NoReduction.Instance);
            }
        }
Esempio n. 6
0
 /// <summary>Gets the number of optional values for a given index of the Sudoku state.</summary>
 public int Count(int index) => SudokuCell.Count(m_Values[index]);