コード例 #1
0
        // Submit button - run the Solver and write the results to the text boxes
        private void button1_Click(object sender, EventArgs e)
        {
            Solver solver = new Solver(grabUserInput());
            int[,] results = solver.solve();

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    textBoxes[i, j].Text = results[i, j].ToString();
                }
            }
        }
コード例 #2
0
        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            List<object> param = (List<object>)e.Argument;

            bool demo = (bool)param[1];

            int[,] seed = new int[9, 9];

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    seed[i, j] = -1;
                }
            }

            Random rand = new Random();
            for (int i = 1; i <= 9; i++)
            {
                int r = rand.Next(0, 8);
                int c = rand.Next(0, 8);

                //textBoxes[r, c].Text = i + "";  //bleh
                seed[r, c] = i;

            }

            Solver solver = new Solver(seed);
            int[,] solution = solver.solve();

            int[,] puzzle = new int[9, 9];
            Array.Copy(solution, puzzle, solution.Length);

            if (demo)
            {
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 9; j++)
                    {
                        //textBoxes[i, j].Text = puzzle[i, j] + "";
                        //updateBoard(i, j, puzzle[i, j]);
                        bgWorker.ReportProgress(puzzle[i, j], new Point(i, j));
                    }
                }
            }

            //this.Refresh();

            // remove random values...
            //puzzle[0, 0] = -1;

            Solver uniqueCheck = new Solver();
            // uniqueCheck.

            List<TreeNode> children = new List<TreeNode>();
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    children.Add(new TreeNode(i, j, puzzle[i, j]));
                }
            }

            TreeNode node = children[rand.Next(0, children.Count - 1)];
            children.Remove(node);
            node.setChildren(children);

            int n = 0;
            //int limit = slider.Value;
            //int limit = 50;
            int limit = 81 - (int)param[0];
            Console.WriteLine("Starting algorithm with limit: " + limit);
            while (n < limit)
            {

                // Clear out current square
                puzzle[node.r, node.c] = -1;

                // Increment n every time we set something to -1
                n++;

                uniqueCheck.setInput(puzzle);

                // If it not uniquely solvable, back up
                if (uniqueCheck.isUnique(solution))
                {
                    //textBoxes[node.r, node.c].Text = "";
                    if (demo)
                    {
                        bgWorker.ReportProgress(-1, new Point(node.r, node.c));
                    }
                    //this.Refresh();
                }
                else
                {
                    // Decrement n every time we undo an operation
                    puzzle[node.r, node.c] = node.lastValue;
                    //textBoxes[node.r, node.c].Text = node.lastValue + "";
                    if (demo)
                    {
                        bgWorker.ReportProgress(node.lastValue, new Point(node.r, node.c));
                    }
                    //this.Refresh();
                    n--;
                    node = node.getParent();
                }

                // Make sure we haven't reached the top
                if (node == null)
                    break;

                // Randomly pick the next node
                // If the current node has no more children, back up

                TreeNode nextNode = node.getNextNode();
                while (nextNode == null && node != null)
                {
                    puzzle[node.r, node.c] = node.lastValue;
                    //textBoxes[node.r, node.c].Text = node.lastValue + "";
                    if (demo)
                    {
                        bgWorker.ReportProgress(node.lastValue, new Point(node.r, node.c));
                    }
                    //this.Refresh();
                    n--;
                    node = node.getParent();
                    nextNode = node.getNextNode();
                }

                // Make sure we haven't reached the top
                if (node == null)
                    break;

                node = nextNode;

                // return the current count;
                bgWorker.ReportProgress(100, 81 - n);
            }

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    if (puzzle[i, j] != -1)
                        //textBoxes[i, j].Text = puzzle[i, j] + "";
                        bgWorker.ReportProgress(puzzle[i, j], new Point(i, j));
                    else
                    {
                        //textBoxes[i, j].Text = "";
                        bgWorker.ReportProgress(-1, new Point(i, j));
                    }
                }
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            // Initialise board
            Board b = new Board();
            b.set(new int[,] {
                { 0,1,4, 2,0,0, 0,9,0},
                { 0,2,0, 0,0,0, 0,0,3},
                { 0,6,5, 0,8,0, 0,4,0},

                { 0,0,8, 0,3,0, 0,0,0},
                { 2,0,0, 5,0,7, 0,0,8},
                { 0,0,0, 0,4,0, 6,0,0},

                { 0,8,0, 0,2,0, 1,6,0},
                { 4,0,0, 0,0,0, 0,5,0},
                { 0,7,0, 0,0,5, 9,8,0} });
            //b.set(new int[,] {
            //    { 0,0,0, 0,0,0, 0,0,0},
            //    { 0,0,0, 0,0,0, 0,0,0},
            //    { 0,0,0, 0,0,0, 0,0,0},

            //    { 0,0,0, 0,0,0, 0,0,0},
            //    { 0,0,0, 0,0,0, 0,0,0},
            //    { 0,0,0, 0,0,0, 0,0,0},

            //    { 0,0,0, 0,0,0, 0,0,0},
            //    { 0,0,0, 0,0,0, 0,0,0},
            //    { 0,0,0, 0,0,0, 0,0,0} });
            //b.set(new int[,]
            //{
            //    { 0,6,1, 2,0,8, 3,0,9},
            //    { 0,0,0, 0,0,1, 4,0,2},
            //    { 0,0,2, 0,0,0, 6,0,0},

            //    { 6,2,0, 0,5,3, 0,0,4},
            //    { 5,0,0, 0,8,0, 0,0,6},
            //    { 1,0,0, 6,2,0, 0,7,3},

            //    { 0,0,4, 0,0,0, 7,0,0},
            //    { 8,0,6, 3,0,0, 0,0,0},
            //    { 3,0,5, 7,0,2, 9,6,0}
            //});

            // Ask user for input
            string input = "";
            int x = 0, y = 0, value = 0;
            string[] split;

            while (true)
            {
                Console.Clear();
                b.render();
                Console.WriteLine("Enter data in the format [x y value], or enter 0 to solve: ");
                input = Console.ReadLine();

                split = input.Split(' ');
                if (split.Length == 3)
                {
                    x = Int32.Parse(split[0]);
                    y = Int32.Parse(split[1]);
                    value = Int32.Parse(split[2]);

                    b.set(x, y, value);
                } else if (input == "0")
                {
                    break;
                }
            }

            // Attempt to solve sudoku
            Solver s = new Solver(b.copy());

            Stopwatch watch = new Stopwatch();

            watch.Start();
            Board result = s.solve();
            watch.Stop();

            // Check complete
            if (result == null || !result.checkComplete())
            {
                Console.WriteLine(":( Unable to solve.");
            }
            else
            {
                Console.Clear();
                b.render();
                Console.WriteLine("Solved! Time used " + watch.Elapsed.TotalMilliseconds + "ms. Here it is: ");
                result.render();
            }
        }