Beispiel #1
0
        /// <summary>
        ///   Attempts to generate a new, solved, randomized SudokuGrid to represent the Sudoku solution,
        ///    and saves it as this GridBuilder's Solution.
        ///   Takes in an integer representing the number of attempts to generate a solution before a placeholder grid will be used.
        /// </summary>
        /// <param name="attemptMax"> Number of failed attempts to generate a board allowed before a placeholder will be used. </param>
        private void GenerateSolution(int maxAttempts)
        {
            HashSet <Tuple <int, int> > EmptyCoords = new HashSet <Tuple <int, int> >();

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    EmptyCoords.Add(new Tuple <int, int>(i, j));
                }
            }

            if (EmptyCoords.Count != 81)
            {
                throw new Exception("Problem generating possible coordinates in GenerateSolution.");
            }

            Solution = new SudokuGrid();
            int placed = 0;


            while (EmptyCoords.Count > 0)
            {
                Tuple <int, int> randCoord = EmptyCoords.ElementAt(Rand.Next(EmptyCoords.Count));
                int rx = randCoord.Item1;
                int ry = randCoord.Item2;

                int[] possibleValues = Solution.LegalValues(rx, ry);

                if (possibleValues.Length > 0)
                {
                    int rval = possibleValues[Rand.Next(0, possibleValues.Length - 1)];
                    Solution.Set(rx, ry, rval);
                    placed++;

                    EmptyCoords.Remove(randCoord);
                }
                else // No possible moves at this coordinate.
                {
                    if (maxAttempts > 0)
                    {
                        GenerateSolution(maxAttempts - 1);
                    }
                    else
                    {
                        GenerateSolution();
                    }
                }
            }
            Console.Write("ay");
        }
Beispiel #2
0
        /// <summary>
        ///     Recursively generates a new Solution sudokuboard, modifying this GridBuilder's "Solution" grid.
        /// </summary>
        public void GenerateSolutionRecursively()
        {
            Solution = new SudokuGrid();

            HashSet <Tuple <int, int> > AllCoords = new HashSet <Tuple <int, int> >();

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    AllCoords.Add(new Tuple <int, int>(i, j));
                }
            }

            // Build randomized initial row
            for (int i = 0; i < 9; i++)
            {
                Tuple <int, int> initCoord = AllCoords.First();
                AllCoords.Remove(initCoord);
                int x = initCoord.Item1;
                int y = initCoord.Item2;

                int[] possibleValues = Solution.LegalValues(x, y);
                Solution.Set(x, y, possibleValues[Rand.Next(possibleValues.Length)]);
            }

            Stack <Tuple <int, int> > emptySpaces = new Stack <Tuple <int, int> >();

            while (AllCoords.Count > 0)
            {
                Tuple <int, int> randCoord = AllCoords.First();
                AllCoords.Remove(randCoord);
                emptySpaces.Push(randCoord);
            }


            if (!GenSolutionRec(emptySpaces))
            {
                throw new Exception("It asplode");
            }
        }