Exemple #1
0
        public List <LargeSquare> GetAllCombinations(List <LargeSquare> squareList, int smallSquareCounter, HashSet <string> invalidFields)
        {
            if (smallSquareCounter == -1)
            {
                return(squareList);
            }

            if (smallSquareCounter == 0)
            {
                squareList.Add(this);
            }

            SmallSquare        smallSquare     = GetSquare(smallSquareCounter);
            List <SmallSquare> smallSquareList = new List <SmallSquare>();

            smallSquareList = smallSquare.GetAllPossibleSquares(smallSquareList, 0, 0, invalidFields, smallSquareCounter);
            List <LargeSquare> newLargeSquares = new List <LargeSquare>();

            foreach (LargeSquare existingSquare in squareList)
            {
                foreach (SmallSquare s in smallSquareList)
                {
                    LargeSquare newSquare = Program.DeepClone(existingSquare);

                    newSquare.SetValuesForSquare(s, smallSquareCounter);


                    if (newSquare.IsPartlyValid())
                    {
                        if (smallSquareCounter < 8)
                        {
                            newLargeSquares.Add(newSquare);
                        }
                        else
                        {
                            if (newSquare.IsValid())
                            {
                                newLargeSquares.Add(newSquare);
                                System.Diagnostics.Debug.WriteLine($"Finished {newSquare.Name}");
                                return(newLargeSquares); //since we can only have on result, we can exist as soon as we have found one
                            }
                        }
                    }
                }
            }

            smallSquareCounter++;

            if (smallSquareCounter == 9)
            {
                smallSquareCounter = -1;
            }
            return(GetAllCombinations(newLargeSquares, smallSquareCounter, invalidFields));
        }
        public LargeSquare GetNextSudokuField()
        {
            if (_counter >= _largeSquares.Count())
            {
                return(null);
            }

            LargeSquare largeSquare = _largeSquares.ElementAt(_counter);

            _counter++;

            return(largeSquare);
        }
Exemple #3
0
        public void GetInvalidFields(LargeSquare largeSquare)
        {
            _invalidFields = new HashSet <string>();

            for (int y = 0; y < 9; y++)
            {
                for (int x = 0; x < 9; x++)
                {
                    Field field = largeSquare.Fields[y, x];
                    if (field.IsFixed)
                    {
                        //Does not work completely :-(
                        //AddVerticalBlocker(GetSquareNumber(x,y), field.FieldValue, x, y);
                        //AddHorizontalBlocker(GetSquareNumber(x, y), field.FieldValue, x, y);
                    }
                }
            }
        }
Exemple #4
0
        public void GetSolutions()
        {
            LargeSquare largeSquare = null;

            do
            {
                lock (_sudokuFieldProvider)
                {
                    largeSquare = _sudokuFieldProvider.GetNextSudokuField();
                }

                if (largeSquare != null)
                {
                    System.Diagnostics.Debug.WriteLine($"Working on {largeSquare.Name}");

                    List <LargeSquare> allPossibilities = new List <LargeSquare>();
                    GetInvalidFields(largeSquare);

                    allPossibilities = largeSquare.GetAllCombinations(allPossibilities, 0, _invalidFields);

                    foreach (LargeSquare lsSol in allPossibilities)
                    {
                        if (lsSol.IsValid())
                        {
                            lock (_sudokuFieldProvider)
                            {
                                _sudokuFieldProvider.AddResultToList(lsSol);
                            }

                            Console.WriteLine(lsSol.ToString());
                            Console.WriteLine(lsSol.GetResult());
                            Console.WriteLine(" ");
                        }
                    }
                }
            } while (largeSquare != null);
        }
Exemple #5
0
        private static List <LargeSquare> InitializeSquares(string[] fileInput)
        {
            List <LargeSquare> list = new List <LargeSquare>();

            LargeSquare   largeSquare = new LargeSquare();
            List <string> lines       = new List <string>();
            int           gridCounter = 0;

            foreach (string line in fileInput)
            {
                if (line.StartsWith("Grid", StringComparison.OrdinalIgnoreCase))
                {
                    largeSquare      = new LargeSquare();
                    largeSquare.Name = $"Grid {gridCounter.ToString().PadLeft(2, '0')}";
                    gridCounter++;
                    if (lines.Count > 0)
                    {
                        //initialise
                        largeSquare.Initialize(lines.ToArray());
                        list.Add(largeSquare);
                        lines = new List <string>();
                    }
                }
                else
                {
                    lines.Add(line);
                }
            }

            //initialize last
            largeSquare = new LargeSquare();
            largeSquare.Initialize(lines.ToArray());
            largeSquare.Name = $"Grid {gridCounter.ToString().PadLeft(2, '0')}";
            list.Add(largeSquare);

            return(list);
        }
 public void AddResultToList(LargeSquare largeSquare)
 {
     _solutionList.Add(largeSquare);
 }