public MainWindow()
        {
            InitializeComponent();

            // My code below
            SudokuGenerater sudo = new SudokuGenerater();

            int[,] puzzle;

            try
            {
                sudo.FillNextGrid(0, 0);
            }
            catch (SudokuLibrary.SuccessGeneratingException ex)
            {
                puzzle = ex.puzzle;

                // 挖空
                var rnd        = new Random();
                var digNumbers = new int[9];

                // 生成9个随机数 range: 2~9;
                do
                {
                    for (int i = 0; i < 9; i++)
                    {
                        digNumbers[i] = rnd.Next(2, 10);
                    }
                }while (digNumbers.Sum() > 60 || digNumbers.Sum() < 30);

                // 挖空,即标志该位为0
                var grids = new int[, ] {
                    { 0, 0 }, { 0, 1 }, { 0, 2 },
                    { 1, 0 }, { 1, 1 }, { 1, 2 },
                    { 2, 0 }, { 2, 1 }, { 2, 2 }
                };
                int[] numbers = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };

                for (int i = 0; i < 9; i++)
                {
                    int   basex           = (i / 3) * 3;
                    int   basey           = (i % 3) * 3;
                    int[] MyRandomNumbers = numbers.OrderBy(x => rnd.Next()).ToArray();

                    for (int j = 0; j < digNumbers[i]; j++)
                    {
                        int digx = grids[MyRandomNumbers[j], 0];
                        int digy = grids[MyRandomNumbers[j], 1];

                        puzzle[basex + digx, basey + digy] = 0;
                    }
                }
                // 显示初始数独
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        puzzleArray[i, j] = new TextBox();
                        MyGrid.Children.Add(puzzleArray[i, j]);
                        Grid.SetRow(puzzleArray[i, j], i);
                        Grid.SetColumn(puzzleArray[i, j], j);
                        puzzleArray[i, j].FontSize = 30;
                        puzzleArray[i, j].HorizontalContentAlignment = HorizontalAlignment.Center;
                        puzzleArray[i, j].VerticalContentAlignment   = VerticalAlignment.Center;
                        puzzleArray[i, j].BorderBrush = Brushes.Black;

                        if (puzzle[i, j] != 0)
                        {
                            // 未挖空的数字显示为数字,只读,并加粗
                            puzzleArray[i, j].IsReadOnly = true;
                            puzzleArray[i, j].Text       = puzzle[i, j].ToString();
                            puzzleArray[i, j].FontWeight = FontWeights.Bold;
                        }
                        else
                        {
                            puzzleArray[i, j].Text = "";
                        }

                        // 加粗小宫格的边框
                        if (i % 3 == 0)
                        {
                            if (j % 3 == 0)
                            {
                                puzzleArray[i, j].BorderThickness = new Thickness(2.5, 2.5, 1.5, 1.5);
                            }
                            else if (j % 3 == 2)
                            {
                                puzzleArray[i, j].BorderThickness = new Thickness(1.5, 2.5, 2.5, 1.5);
                            }
                            else
                            {
                                puzzleArray[i, j].BorderThickness = new Thickness(1.5, 2.5, 1.5, 1.5);
                            }
                        }
                        else if (i % 3 == 2)
                        {
                            if (j % 3 == 0)
                            {
                                puzzleArray[i, j].BorderThickness = new Thickness(2.5, 1.5, 1.5, 2.5);
                            }
                            else if (j % 3 == 2)
                            {
                                puzzleArray[i, j].BorderThickness = new Thickness(1.5, 1.5, 2.5, 2.5);
                            }
                            else
                            {
                                puzzleArray[i, j].BorderThickness = new Thickness(1.5, 2.5, 1.5, 2.5);
                            }
                        }
                        else
                        {
                            if (j % 3 == 0)
                            {
                                puzzleArray[i, j].BorderThickness = new Thickness(2.5, 1.5, 1.5, 1.5);
                            }
                            else if (j % 3 == 2)
                            {
                                puzzleArray[i, j].BorderThickness = new Thickness(1.5, 1.5, 2.5, 1.5);
                            }
                            else
                            {
                                puzzleArray[i, j].BorderThickness = new Thickness(1.5, 2.5, 1.5, 1.5);
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            // 输入处理
            if (args.Length != 2)
            {
                System.Console.WriteLine("{0} arguments detected.", args.Length);
                Functions.HelpOutput();
                return;
            }
            if (args[0] == "-c")
            {
                try
                {
                    int num = Int32.Parse(args[1]);
                    if (num > 1000000 || num < 1)
                    {
                        System.Console.WriteLine("Your input <N> is not " +
                                                 "between 0 and 1000,000");
                        return;
                    }

                    // clear the content of sudoku.txt
                    using (System.IO.StreamWriter outputfile =
                               new System.IO.StreamWriter(@"sudoku.txt"))
                    {
                        outputfile.Write("");
                    }

                    // begin generate
                    Stopwatch stopWatch = new Stopwatch();
                    stopWatch.Start();
                    var sudo = new SudokuGenerater
                    {
                        bound = num
                    };
                    sudo.grid[0, 0] = 5;
                    try
                    {
                        sudo.FillNextGrid(0, 1);
                    }
                    catch (SudokuLibrary.EnoughResultsException ex)
                    {
                        System.Console.WriteLine(ex);
                    }
                    stopWatch.Stop();
                    // Get the elapsed time as a TimeSpan value.
                    TimeSpan ts = stopWatch.Elapsed;

                    // Format and display the TimeSpan value.
                    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                                       ts.Hours, ts.Minutes, ts.Seconds,
                                                       ts.Milliseconds / 10);
                    Console.WriteLine("RunTime " + elapsedTime);
                }
                catch (System.FormatException)
                {
                    System.Console.WriteLine("Input <N> was not in a correct format(integer).");
                    return;
                }
            }
            else if (args[0] == "-s")
            {
                // sudoku solve
                try
                {
                    // Read each line of the file into a string array. Each element
                    // of the array is one line of the file.
                    string []    lines        = System.IO.File.ReadAllLines(args[1]);
                    SudokuSolver sudokuSolver = new SudokuSolver();

                    // clear the content of sudoku.txt
                    using (System.IO.StreamWriter outputfile =
                               new System.IO.StreamWriter(@"sudoku.txt"))
                    {
                        outputfile.Write("");
                    }

                    string[] sublines1 = new string[9];
                    Array.Copy(lines, 0, sublines1, 0, 9);
                    sudokuSolver.ReadIntoPuzzle(sublines1);
                    sudokuSolver.Solve();
                    sudokuSolver.AppendResultToFile(@"sudoku.txt", false);

                    for (int i = 10; i < lines.Length; i += 10)
                    {
                        string[] sublines = new string[9];
                        Array.Copy(lines, i, sublines, 0, 9);
                        sudokuSolver.ReadIntoPuzzle(sublines);
                        sudokuSolver.Solve();
                        sudokuSolver.AppendResultToFile(@"sudoku.txt", true);
                    }
                }
                catch (System.IO.FileNotFoundException ex)
                {
                    System.Console.WriteLine(ex.Message);
                }
            }
            else
            {
                Functions.HelpOutput();
                return;
            }
        }