Пример #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));
        }
Пример #2
0
        public bool IsComplete()
        {
            for (int x = 0; x < 9; x++)
            {
                SmallSquare smallSquare = GetSquare(x);
                if (!smallSquare.IsComplete())
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #3
0
        private bool CheckSubSquareValidity()
        {
            for (int x = 0; x < 9; x++)
            {
                SmallSquare smallSquare = GetSquare(x);

                if (!smallSquare.IsValid() || !smallSquare.IsComplete())
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #4
0
        private void SetValuesForSquare(SmallSquare smallSquare, int squareNumber)
        {
            int xOffset = 0;
            int yOffset = 0;

            GetOffSet(squareNumber, out xOffset, out yOffset);

            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    Fields[x + xOffset, y + yOffset] = smallSquare.Fields[x, y];
                }
            }
        }
Пример #5
0
        private SmallSquare GetSquare(int position)
        {
            SmallSquare smallSquare = new SmallSquare();
            int         xOffset     = 0;
            int         yOffset     = 0;

            GetOffSet(position, out xOffset, out yOffset);

            for (int x = 0; x < 3; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    smallSquare.Fields[x, y] = Fields[x + xOffset, y + yOffset];
                }
            }

            return(smallSquare);
        }
Пример #6
0
        public List <SmallSquare> GetAllPossibleSquares(List <SmallSquare> listOfSquares, int xPosition, int yPosition, HashSet <string> invalidFields, int smallSquareNumber)
        {
            int elementCounter = 0;

            if (listOfSquares.Count == 0)
            {
                listOfSquares.Add(this);
            }

            if (yPosition == 3)
            {
                System.Diagnostics.Debug.WriteLine("GetAllPossibleSquares return");

                Dictionary <string, SmallSquare> newList = new Dictionary <string, SmallSquare>();

                foreach (SmallSquare sq in listOfSquares)
                {
                    bool isOK = true;
                    if (smallSquareNumber < 6)
                    {
                        for (int y = 0; y < 3; y++)
                        {
                            for (int x = 0; x < 3; x++)
                            {
                                if (!sq.Fields[y, x].IsFixed)
                                {
                                    if (invalidFields.Contains(SudokuSolver.GetBlockListEntry(smallSquareNumber, sq.Fields[y, x].FieldValue, x, y)))
                                    {
                                        isOK = false;
                                        break;
                                    }
                                }

                                if (!isOK)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    if (isOK && sq.IsComplete())
                    {
                        if (!newList.ContainsKey(sq.GetKey()))
                        {
                            newList.Add(sq.GetKey(), sq);
                        }
                    }
                }

                if (!newList.Any())
                {
                    System.Diagnostics.Debug.WriteLine("no results");
                }

                return(newList.Select(val => val.Value).ToList());
            }

            Dictionary <string, SmallSquare> newSquares = new Dictionary <string, SmallSquare>();

            foreach (SmallSquare existingSquare in listOfSquares)
            {
                elementCounter = 0;

                for (int x = 0; x < 9; x++)
                {
                    SmallSquare   newSquare        = Program.DeepClone(existingSquare);
                    HashSet <int> availableNumbers = newSquare.GetNumbersStillAvailable();

                    if (newSquare.Fields[yPosition, xPosition].IsFixed == false)
                    {
                        newSquare.Fields[yPosition, xPosition].FieldValue = availableNumbers.ElementAt(elementCounter);
                        elementCounter++;
                    }

                    if (!newSquares.ContainsKey(newSquare.GetKey()))
                    {
                        newSquares.Add(newSquare.GetKey(), newSquare);
                    }


                    if (availableNumbers.Count() == elementCounter)
                    {
                        break;
                    }
                }
            }

            if (xPosition == 2)
            {
                xPosition = 0;
                yPosition++;
            }
            else
            {
                xPosition++;
            }

            return(GetAllPossibleSquares(newSquares.Select(val => val.Value).ToList(), xPosition, yPosition, invalidFields, smallSquareNumber));
        }