Exemplo n.º 1
0
        /// <inheritdoc />
        public void Solve(SudokuPuzzle puzzle, SudokuState state, ICollection <IEvent> events)
        {
            foreach (var region in puzzle.Regions)
            {
                foreach (var intersected in region.Intersected)
                {
                    intersection.Clear();
                    intersection.AddRange(region.Intersect(intersected));

                    // We found candidate intersections
                    if (intersection.Count > 1)
                    {
                        foreach (var value in puzzle.SingleValues)
                        {
                            var pre = events.Count;
                            Solve(state, region, intersected, value, events);

                            if (pre != events.Count)
                            {
                                return;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Solve(SudokuPuzzle puzzle, SudokuState state, ICollection <IEvent> events)
        {
            var count = -1;

            while (count != events.Count)
            {
                count = events.Count;

                for (var i = 0; i < solvers.Length; i++)
                {
                    var solver = solvers[i];
                    solver.Solve(puzzle, state, events);

                    if (events.Count != count)
                    {
                        if (state.IsSolved)
                        {
                            events.Add(SolvedPuzzle.Instance);
                            return;
                        }
                        i = solvers.Length;
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        public void Solve(SudokuPuzzle puzzle, SudokuState state, ICollection <IEvent> events)
        {
            foreach (var region in puzzle.Regions)
            {
                var pre = events.Count;
                ReduceRegion(region, state, events);

                if (pre != events.Count)
                {
                    return;
                }
            }
        }
Exemplo n.º 4
0
        /// <inheritdoc />
        public void Solve(SudokuPuzzle puzzle, SudokuState state, ICollection <IEvent> events)
        {
            // check all groups.
            foreach (var region in puzzle.Regions)
            {
                foreach (var singleValue in puzzle.SingleValues)
                {
                    var pre = events.Count;
                    CheckCells(state, region, singleValue, events);

                    if (pre != events.Count)
                    {
                        return;
                    }
                }
            }
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        public void Solve(SudokuPuzzle puzzle, SudokuState state, ICollection <IEvent> events)
        {
            foreach (var region in puzzle.Regions)
            {
                foreach (var pair in Pairs)
                {
                    var pre = events.Count;

                    ReduceRegion(pair, region, state, events);

                    // We found some, let simpler strategies try again.
                    if (pre != events.Count)
                    {
                        return;
                    }
                }
            }
        }
Exemplo n.º 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>());
            }
        }
Exemplo n.º 7
0
        private void Solve(SudokuPuzzle puzzle, SudokuState state, ICollection <IEvent> events, SudokuRegionType type, SudokuRegionType otherType)
        {
            var pre = events.Count;

            foreach (var first in puzzle.Regions.Where(r => r.RegionType == type))
            {
                foreach (var second in puzzle.Regions.Where(r => r.RegionType == otherType))
                {
                    if (first != second)
                    {
                        foreach (var value in SudokuCell.Singles)
                        {
                            Solve(value, first, second, otherType, state, events);
                            if (events.Count != pre)
                            {
                                return;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
 public void Solve(SudokuPuzzle puzzle, SudokuState state, ICollection <IEvent> events)
 {
     Solve(puzzle, state, events, SudokuRegionType.Row, SudokuRegionType.Column);
     Solve(puzzle, state, events, SudokuRegionType.Column, SudokuRegionType.Row);
 }
Exemplo n.º 9
0
 /// <summary>Initializes a new solver for a given puzzle.</summary>
 public SudokuSolver(SudokuPuzzle puzzle, SudokuSolverMethods methods)
 {
     if (puzzle == null) { throw new ArgumentNullException(); }
     Puzzle = puzzle;
     Methods = methods;
 }
Exemplo n.º 10
0
 /// <summary>Initializes a new solver for a given puzzle.</summary>
 public SudokuSolver(SudokuPuzzle puzzle)
     : this(puzzle, SudokuSolverMethods.All)
 {
 }
Exemplo n.º 11
0
 /// <summary>Initializes a new solver for a given puzzle.</summary>
 public SudokuSolver(SudokuPuzzle puzzle)
 {
     Puzzle = puzzle ?? throw new ArgumentNullException(nameof(puzzle));
     Solver = new MixedSolver();
 }