コード例 #1
0
ファイル: SudokuTest.cs プロジェクト: sriram-dev/sudokusolver
        public void TestSolve()
        {
            // int[][] input = new int[4][]{new int[4]{1,0,0,0}, new int[4]{0,0,0,3}, new int[4]{4,0,0,0}, new int[4]{0,2,0,0}};
            int[][] input = new int[9][] {new int[9]{ 9,0,0,1,3,0,4,0,0}, new int[9]{ 0,5,0,8,9,6,3,7,0}, new int[9]{ 0,2,6,0,0,4,0,0,0},
            new int[9]{ 0,0,1,6,4,7,0,0,0}, new int[9]{ 8,6,7,5,0,9,0,0,0}, new int[9]{0,0,0,2,0,3,0,0,0} ,
            new int[9]{6,0,0,3,0,0,9,4,0}, new int[9]{0,1,3,9,2,5,0,6,0}, new int[9]{0,0,5,4,0,0,1,0,3}};

            int[][] input2 = new int[9][]{ new int[9]{5,2,0,0,0,4,0,0,0}, new int[9]{8,0,7,0,0,1,0,0,0} , new int[9]{0,1,3,5,0,0,2,0,8},
                new int[9]{0,8,0,1,6,0,0,0,4},new int[9]{6,9,0,0,0,0,0,8,2} , new int[9]{3,0,0,0,5,8,0,1,0},
                new int[9]{2,0,6,7,0,5,8,4,0}, new int[9]{0,0,0,4,0,0,3,0,0}, new int[9]{1,0,0,8,0,0,0,0,5}};

            //2 star puzzle in hindu -- SOLVED IN 0.09 Secs !!
            int[][] input3 = new int[9][]{ new int[9]{6,0,9,0,0,0,7,3,0} , new int[9]{0,3,7,0,1,9,0,0,0}, new int[9]{0,1,0,4,0,0,0,9,0} , new int[9]{0,2,0,0,0,0,0,0,0}, new int[9]{0,0,0,3,5,2,0,0,0}, new int[9]{0,0,0,0,0,0,0,6,0},
                new int[9]{0,8,0,0,0,1,0,7,0}, new int[9]{0,0,0,9,7,0,2,5,0} , new int[9]{0,7,5,0,0,0,3,0,4} };

            //5 star from hindu - SOLVED IN 0.08 secs !
            int[][] input4 = new int[9][]{ new int[9]{0,4,0,5,0,0,0,0,0} , new int[9]{0,0,3,0,0,2,0,1,4}, new int[9]{0,0,8,0,0,7,0,5,0},
                new int[9]{2,0,0,0,0,0,4,6,0}, new int[9]{3,0,0,0,0,0,0,0,1}, new int[9]{0,1,4,0,0,0,0,0,7}, new int[9]{0,2,0,9,0,0,3,0,0},
                new int[9]{7,8,0,2,0,0,6,0,0} , new int[9]{0,0,0,0,0,4,0,9,0}};

            Solver s = new Solver();
              //  Display2x2(input);
            int[][] output = s.Solve(input4);
               // Console.WriteLine(output[0][2]);
            Display2x2(output);
        }
コード例 #2
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();
        }
コード例 #3
0
 public static void Main()
 {
     Console.WriteLine("Enter Suduko here:");
     var input = Console.ReadLine();
     var sudoku = new Sudoku(input);
     var displayer = new Displayer();
     displayer.Show(sudoku);
     Console.WriteLine();
     Console.WriteLine("Show solve? (y/n)");
     var showSolve = Console.ReadLine() == "y";
     var solver = new Solver(sudoku, showSolve);
     Console.WriteLine();
     Console.WriteLine("***SOLVING***");
     Console.WriteLine();
     solver.Solve();
     Console.WriteLine("SOLUTION:");
     Console.WriteLine();
     displayer.Show(sudoku);
     Console.ReadLine();
 }
コード例 #4
0
        static void Main(string[] args)
        {
            bool shortOutput = false;
            var  arguments   = args.ToList();

            if (arguments.Count == 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Please provide a valid argument:");
                Console.WriteLine("\n-solve [Grid]");
                Console.WriteLine("-solve [Path to file with multiple grids]");
                Console.WriteLine("\n-validate [Grid]");
                Console.WriteLine("-validate [Path to file with multiple grids]");
                Console.ReadLine();
                return;
            }
            if (arguments.Contains("-short"))
            {
                shortOutput = true;
            }
            if (arguments.Contains("-solve"))
            {
                int index = arguments.IndexOf("-solve");
                try
                {
                    var grids = Helper.GetGridsFromFile(arguments[index + 1]);
                    if (grids.Length == 1 && grids[0] == "")
                    {
                        grids[0] = arguments[index + 1];
                    }

                    foreach (string grid in grids)
                    {
                        Solver.Solve(grid, shortOutput);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("An Exception occured: " + e.Message);
                    Console.ReadLine();
                }
            }

            else if (arguments.Contains("-validate"))
            {
                int index = arguments.IndexOf("-validate");
                try
                {
                    var grids = Helper.GetGridsFromFile(arguments[index + 1]);
                    if (grids.Length == 1 && grids[0] == "")
                    {
                        grids[0] = arguments[index + 1];
                    }

                    foreach (string grid in grids)
                    {
                        Solver.Validate(grid, shortOutput);
                    }
                }
                catch
                {
                    Console.WriteLine("Please input your grid!");
                    Console.ReadLine();
                }
            }
        }
コード例 #5
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);
            }
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: EttienneS/SudokuSolver
 private static void Main(string[] args)
 {
     Solver.Solve(Grid.Load(SudokuMedium));
     Console.ReadKey();
 }