コード例 #1
0
ファイル: Program.cs プロジェクト: jongyllen/sudoku-solver
        static void Main(string[] args)
        {
            string line;
            List<long> times = new List<long>();
            StreamReader file = new StreamReader(args[0]);

            while ((line = file.ReadLine()) != null)
            {
                Solver solver = new Solver(line);
                Console.WriteLine("Solving: \n" + solver.ToString());
                Stopwatch sw = new Stopwatch();
                sw.Restart();
                solver.Solve();
                sw.Stop();
                times.Add(sw.ElapsedMilliseconds);
                Console.WriteLine("Solved: \n" + solver.ToString() + "\nin " + sw.ElapsedMilliseconds + " miliseconds\n");
            }

            file.Close();

            Console.WriteLine("");
            Console.WriteLine("Times:");

            foreach (long time in times.OrderBy(x => x))
            {
                Console.WriteLine(time);
            }
            Console.WriteLine("Medium: " + times.Sum(x => x) / times.Count);

            Console.ReadKey();
        }
コード例 #2
0
        private void testButton_Click(object sender, EventArgs e)
        {
            int[,] sudoku = { { 0, 0, 0, 2, 6, 0, 7, 0, 1 },
                              { 6, 8, 0, 0, 7, 0, 0, 9, 0 },
                              { 1, 9, 0, 0, 0, 4, 5, 0, 0 },
                              { 8, 2, 0, 1, 0, 0, 0, 4, 0 },
                              { 0, 0, 4, 6, 0, 2, 9, 0, 0 },
                              { 0, 5, 0, 0, 0, 3, 0, 2, 8 },
                              { 0, 0, 9, 3, 0, 0, 0, 7, 4 },
                              { 0, 4, 0, 0, 5, 0, 0, 3, 6 },
                              { 7, 0, 3, 0, 1, 8, 0, 0, 0 } };

            Solver s = new Solver(sudoku);

            s.Solve();
            Console.WriteLine(s.ToString());
        }
コード例 #3
0
        private void solveSudokuButton_Click(object sender, EventArgs e)
        {
            try {
                Control table = null;
                int[,] sudoku = new int[9, 9];

                foreach (Control c in Controls)
                {
                    // Find the table layout which contains the textboxes
                    if (c.Name == "tableLayoutPanel1")
                    {
                        table = c;
                    }
                }

                string currBox = "textBox";
                int    boxNum  = 0;

                // Extracts value from each text box and transform into sudoku
                for (int i = 0; i < 9; i++)
                {
                    boxNum++;
                    for (int j = 0; j < 9; j++)
                    {
                        string  currSearch = currBox + Convert.ToString(boxNum);
                        Control tb         = Helper.GetTextBox(table, currSearch);
                        int     tbVal      = 0;

                        if (!(tb.Text == "" || tb.Text == "0"))
                        {
                            tbVal = Convert.ToInt32(tb.Text);
                        }

                        sudoku[i, j] = tbVal;
                        if (j != 8)
                        {
                            boxNum++;
                        }
                    }
                }

                // Solving the sudoku
                Solver s = new Solver(sudoku);
                Console.WriteLine(s.ToString());


                Task     t  = Task.Run(() => { s.Solve(); });
                TimeSpan ts = TimeSpan.FromMilliseconds(500);
                if (!t.Wait(ts))
                {
                    throw new Exception("Timeout, something is wrong with the sudoku!");
                }

                // Displaying the sudoku
                boxNum = 0;

                for (int i = 0; i < 9; i++)
                {
                    boxNum++;
                    for (int j = 0; j < 9; j++)
                    {
                        string  currSearch = currBox + Convert.ToString(boxNum);
                        Control tb         = Helper.GetTextBox(table, currSearch);
                        tb.Text = s.Grid[i, j].ToString();

                        if (j != 8)
                        {
                            boxNum++;
                        }
                    }
                }
            } catch (Exception error) {
                MessageBox.Show(error.Message);
            }
        }