Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
I'm trying to make a simple sudoku creater. That's why i aimed to control columns and rows. Here is my code to make it but it doesn't work.
TextBox[,] dene = new TextBox[9,9];
            Random b = new Random();
            int cntrlclmn = 0;
            int m=0;
 
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    dene[i, j] = new TextBox();
                    dene[i, j].Text = "0" ;
                }
            }
 

            for (int i = 0; i < 9; i++)
            {
                for (int j=0;j<9;j++)
                {
                    while (m == 0)
                    {
                        int inta = b.Next(1, 10);
                        for (int k = 0; k < 9; k++)
                        {
                            int c = Convert.ToInt32(dene[i, k].Text);
                            int d = Convert.ToInt32(dene[k, j].Text);
                            if ((inta != c) && (inta != d))
                            {
                                cntrlclmn++;
                            }
                            else 
                            {
                                break;
                            }
                        }
                        if (cntrlclmn == 9)
                        {
                            dene[i, j].Text = inta.ToString();
                            cntrlclmn = 0;
                            m = 1;
                        }
                        else
                        {
                            cntrlclmn = 0;
                        }
                    }
                    m = 0;                   
                }
            }
When i step in with brekpoints there's no problem but when i debug it simulator freezes. I don't get what the problem is. I also would like to add that when i didn't add
 
int d = Convert.ToInt32(dene[k, j].Text);
and
&& (inta != d)
 
it was working for columns. When i added theese lines for rows it didn't worked. It freeze.
Posted 3-May-13 4:55am
Edited 3-May-13 5:39am
v5
Comments
CHill60 at 3-May-13 9:59am
   
Use the Improve question link to define what you mean by "it doesn't work"
Rebnerod at 3-May-13 10:02am
   
Explained it. While step in everything seems to be correct but in debug mod simulator freezes
Kenneth Haugland at 3-May-13 10:25am
   
Whats this: TextBox[,] dene = new TextBox[9,9];?
Im pretty sure you cant do that.
Rebnerod at 3-May-13 10:37am
   
you can do it. It's to create textbox array.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Well no, it won't necessarily work, and it won't necessarily exit. Ever.
 
The problem is that what you are building is not a valid suduko puzzle. Think of it this way:
If you are building a three by three suduko (instead of your 9 by nine) and the first row comes out as:
1 2 3
A perfectly valid start for line 2 is:
2 1 x
but there is no value you can put in place of the "x" - so your code will just loop round and round and round generating the random digits '1', '2' and '3' over and over again, but being unable to insert any of them, or exit your while loop until it has.
Because the problem is not in the value of 'x' - it is in the selected values before it which it can't change.
 
You cannot generate valid suduko puzzles just by generating a random number and making it fit! Try googling for "c# suduko generator" - it's quite a bit more complicated than you think!
 
I did tell you yesterday that using random number was not a good idea, didn't I? Laugh | :laugh:
  Permalink  
Comments
Rebnerod at 3-May-13 10:55am
   
OMG You're great. I couldn't even notice that. I will do so.
Sergey Alexandrovich Kryukov at 3-May-13 11:52am
   
I did not even look into it, my 5. But I answered in general: this is inappropriate application design; UI can be isolated and it's easy to do.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Not a nice approach in principle. You do all the calculation in UI, which is always bad. You mix up UI and calculations, while UI should be thoroughly isolated from the rest aspects of the application.
 
At the same time, you can easily make the separate. Declare an array int[,] and do all calculations in it. Get the whole array from UI or populate UI with the array at once, depending on your requirements. It would turn your "problem" to nothing, so I don't want to address it at all. However: use int.Parse(string).
 
Good luck,
—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 3 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100