/// <summary>
        /// Reduce possibilities as low as possible
        /// with known numbers in row/column/square
        /// </summary>

        public void Solve(object param)
        {
            Sudoku sudoku = (Sudoku)param;

            int index = 0;

            do
            {
                if (Solver.locker.Acquire(index))
                {
                    if (sudoku.GetField(index).Value == 0)
                    {
                        List <int> toRemove   = Solver.GetNumbersInSquare(index, sudoku);
                        List <int> horizontal = Solver.GetNumbersInRow(index, sudoku);
                        foreach (int v in horizontal)
                        {
                            if (!toRemove.Contains(v))
                            {
                                toRemove.Add(v);
                            }
                        }

                        List <int> vertical = Solver.GetNumbersInColumn(index, sudoku);
                        foreach (int v in vertical)
                        {
                            if (!toRemove.Contains(v))
                            {
                                toRemove.Add(v);
                            }
                        }

                        List <int> possibilities = sudoku.GetPossibilities(index);
                        foreach (int v in toRemove)
                        {
                            if (possibilities.Contains(v))
                            {
                                sudoku.RemovePossibility(index, v);
                            }
                        }

                        if (possibilities.Count == 1)
                        {
                            sudoku.Set(index, possibilities[0]);
                            sudoku.RemovePossibility(index, possibilities[0]);
                        }
                    }

                    Solver.locker.Release(index);
                }

                index = (index + 1) % Sudoku.SIZE;
            } while (!Solver.IsSolved());
        }
Beispiel #2
0
        public static List <Field> GetFieldsInColumn(int index, Sudoku sudoku)
        {
            List <Field> tuple = new List <Field>();

            index = index % 9;

            for (int i = 0; i < (73 + index); i += 9)
            {
                tuple.Add(sudoku.GetField(index + i));
            }

            return(tuple);
        }
Beispiel #3
0
        public static List <Field> GetFieldsInRow(int index, Sudoku sudoku)
        {
            List <Field> tuple = new List <Field>();

            index = index / 9 * 9;

            for (int i = 0; i < 9; i++)
            {
                tuple.Add(sudoku.GetField(index + i));
            }

            return(tuple);
        }
Beispiel #4
0
        private void NakedGroupRemove(List <Field> candidates, List <int> possibilities, Sudoku sudoku)
        {
            foreach (Field otherField in candidates)
            {
                int index = sudoku.IndexOf(otherField);

                Solver.locker.WaitingAcquire(index);
                for (int i = 0; i < possibilities.Count; i++)
                {
                    sudoku.GetField(index).RemovePossibility(possibilities[i]);
                }
                Solver.locker.Release(index);
            }
        }
Beispiel #5
0
        public static List <Field> GetFieldsInSquare(int index, Sudoku sudoku)
        {
            List <Field> tuple = new List <Field>();

            index = (index / 27 * 27) + (index % 9) - (index % 3);

            for (int j = 0; j <= 18; j += 9)
            {
                for (int i = 0; i < 3; i++)
                {
                    tuple.Add(sudoku.GetField(index + j + i));
                }
            }

            return(tuple);
        }
Beispiel #6
0
        /// <summary>
        /// If the number is the only possibility in a row/column/square
        /// even if there are more possibilities for this field,
        /// then this number will be set.
        /// </summary>

        public void Solve(object param)
        {
            Sudoku sudoku = (Sudoku)param;

            int index = 0;

            do
            {
                if (Solver.locker.Acquire(index))
                {
                    if (sudoku.GetField(index).Value == 0)
                    {
                        int        horizontal       = index / 9 * 9;
                        int        vertical         = index % 9;
                        int        square           = (index / 27 * 27) + (index % 9) - (index % 3);
                        List <int> ownPossibilities = sudoku.GetPossibilities(index);

                        // Get all fields in a row/column/square
                        foreach (int value in ownPossibilities)
                        {
                            Field field;
                            bool  foundHorizontal = false;
                            bool  foundVertical   = false;
                            bool  foundSquare     = false;

                            for (int i = 0; i < 9; i++)
                            {
                                field = sudoku.GetField(horizontal + i);
                                if ((horizontal + i) != index && (field.Value == value || field.GetPossibilities().Contains(value)))
                                {
                                    foundHorizontal = true;
                                    break;
                                }
                            }
                            for (int i = 0; i < (73 + vertical); i += 9)
                            {
                                field = sudoku.GetField(vertical + i);
                                if ((vertical + i) != index && (field.Value == value || field.GetPossibilities().Contains(value)))
                                {
                                    foundVertical = true;
                                    break;
                                }
                            }
                            for (int j = 0; j <= 18; j += 9)
                            {
                                for (int i = 0; i < 3; i++)
                                {
                                    field = sudoku.GetField(square + i + j);
                                    if ((square + i + j) != index && (field.Value == value || field.GetPossibilities().Contains(value)))
                                    {
                                        foundSquare = true;
                                        break;
                                    }
                                }
                            }

                            if (!foundHorizontal || !foundVertical || !foundSquare)
                            {
                                sudoku.Set(index, value);
                                sudoku.RemoveAllPossibilities(index);
                            }
                        }
                    }

                    Solver.locker.Release(index);
                }

                index = (index + 1) % Sudoku.SIZE;
            } while (!Solver.IsSolved());
        }