Beispiel #1
0
        private void Fetch(uint quad, SimpleList <int> buffer, SudokuRegion region, SudokuState state, ICollection <IEvent> events)
        {
            var reduced = false;
            var mask    = ~quad;

            foreach (var index in region)
            {
                if (buffer.Contains(index))
                {
                    continue;
                }
                var result = state.And <ReduceNakedTriples>(index, mask);
                if (result is ValueFound)
                {
                    events.Add(result);
                }
                else if (result is ReducedOption)
                {
                    reduced = true;
                }
            }

            if (reduced)
            {
                events.Add(ReducedOptions.Ctor <ReduceNakedTriples>());
            }
        }
Beispiel #2
0
        private void Fetch(uint value, SimpleList <int> intersection, SudokuRegion intersected, SudokuState state, ICollection <IEvent> events)
        {
            IEvent result = NoReduction.Instance;
            var    mask   = ~value;

            foreach (var index in intersected)
            {
                // items in the shared section should be skipped.
                if (intersection.Contains(index))
                {
                    continue;
                }
                var test = state.And <ReducePointingPairs>(index, mask);

                if (test is ValueFound)
                {
                    events.Add(test);
                }
                else if (test is ReducedOption)
                {
                    result = test;
                }
            }

            if (result is ReducedOption)
            {
                events.Add(ReducedOptions.Ctor <ReduceNakedPairs>());
            }
        }
Beispiel #3
0
        private void Fetch(uint nakedPair, SudokuRegion region, SudokuState state, ICollection <IEvent> events)
        {
            IEvent result = NoReduction.Instance;
            var    mask   = ~nakedPair;

            foreach (var index in region)
            {
                var value = state[index];
                if (value != nakedPair)
                {
                    var test = state.And <ReduceNakedPairs>(index, mask);

                    if (test is ValueFound)
                    {
                        events.Add(test);
                    }
                    else if (test is ReducedOption)
                    {
                        result = test;
                    }
                }
            }

            if (result is ReducedOption)
            {
                events.Add(ReducedOptions.Ctor <ReduceNakedPairs>());
            }
        }
Beispiel #4
0
        private void CheckCells(SudokuState state, SudokuRegion region, uint singleValue, ICollection <IEvent> events)
        {
            var hidden = NoIndex;

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

                // the cell has the value.
                if ((singleValue & value) != 0)
                {
                    // Already value, try next.
                    if (singleValue == value)
                    {
                        return;
                    }

                    // not the first
                    if (hidden != NoIndex)
                    {
                        return;
                    }
                    hidden = index;
                }
            }

            if (hidden == NoIndex)
            {
                return;
            }
            events.Add(state.And <ReduceHiddenSingles>(hidden, singleValue));
        }
Beispiel #5
0
        private void Fetch(uint pair, SimpleList <int> hiddenPairs, SudokuState state, ICollection <IEvent> events)
        {
            IEvent result = NoReduction.Instance;

            foreach (var index in hiddenPairs)
            {
                var test = state.And <ReduceHiddenPairs>(index, pair);

                if (test is ReducedOption)
                {
                    result = test;
                }
            }
            if (result is ReducedOption)
            {
                events.Add(ReducedOptions.Ctor <ReduceHiddenPairs>());
            }
        }
Beispiel #6
0
        /// <inheritdoc />
        public void Solve(SudokuPuzzle puzzle, SudokuState state, ICollection <IEvent> events)
        {
            var reduced = false;

            for (var index = 0; index <= puzzle.MaximumIndex; index++)
            {
                // For known cells only.
                if (state.IsKnown(index))
                {
                    var mask = ~state[index];

                    // for all groups the cell belongs to.
                    foreach (var group in puzzle.Lookup[index])
                    {
                        foreach (var target in group)
                        {
                            if (target == index)
                            {
                                continue;
                            }

                            var result = state.And <ReduceNakedSingles>(target, mask);

                            if (result is ReducedOption)
                            {
                                reduced = true;
                            }
                            else if (result is ValueFound)
                            {
                                events.Add(result);
                            }
                        }
                    }
                }
            }
            if (reduced)
            {
                events.Add(ReducedOptions.Ctor <ReduceNakedSingles>());
            }
        }
Beispiel #7
0
        private bool Fetch(uint value, SudokuRegion region, SudokuState state, ICollection <IEvent> events)
        {
            var reducded = false;
            var mask     = ~value;

            foreach (var index in region)
            {
                if (buffer.Contains(index))
                {
                    continue;
                }
                var result = state.And <ReduceXWing>(index, mask);
                if (result is ValueFound)
                {
                    events.Add(result);
                }
                else if (result is ReducedOption)
                {
                    reducded = true;
                }
            }
            return(reducded);
        }