Introduction
This article is on how a sudoku game can be developed easily. This program is not optimised properly. So any suggestion will be welcome. The only idea was to assist beginners to create sudoku easily.
Background
Loads of thanks to the blog mentioned below. Without this I couldnot have generated a valid sudoku.
http://www.chrismeijers.com/post/A-Sudoku-generator.aspx
Using the code
I have always loved playing sudoku. The first thing I do in my newspaper is to solve sudoku, but there used to be only one game and that became less for me. This is when I planned on creating a suodku game for myself, and I learned a lot during this period.
Initially I designed my forms with 1 tablelayoutpanel, 9 groupbox and 81 textbox.
Creating and then formatting 81 textbox is not easy. So create first 1 textbox and format all properties and styling then copy and paste it.
After creating forms then comes the validation part. First I programmed to check groupbox for repeated values or blank ones.
private void groupBox_Validate()
{
foreach (GroupBox groupBox in tableLayoutPanel1.Controls)
{
foreach (TextBox txtBox in groupBox.Controls)
{
string s_txt_Value = txtBox.Text.ToString();
foreach (TextBox txtBox1 in groupBox.Controls)
{
if (txtBox.Name == txtBox1.Name)
{
continue;
}
else if (s_txt_Value == "")
{
arr_Add(txtBox);
continue;
}
if (s_txt_Value == txtBox1.Text)
{
arr_Add(txtBox);
}
}
}
}
}
After validating set of boxes for errors then I started validating textboxes horizontally. For that I created an array of all textbox horizontally.
public void txtBox_Horiz_Validate()
{
foreach (TextBox[] txtBox_Horiz_Controls in arr_txtBox_Horiz)
{
foreach (TextBox txtBox_Horiz in txtBox_Horiz_Controls)
{
string s_txt_Value = txtBox_Horiz.Text;
foreach (TextBox txtBox_Horiz_1 in txtBox_Horiz_Controls)
{
if (txtBox_Horiz.Name == txtBox_Horiz_1.Name)
{
continue;
}
else if (s_txt_Value == txtBox_Horiz_1.Text)
{
if (arr_control.Contains(txtBox_Horiz_1) == false)
{
arr_control.Add(txtBox_Horiz_1);
}
}
}
}
}
}
After verifying horizontal texbox then I moved on to textboxes placed vertically. For that too I did the same thing I placed everything in array and then validated using foreach.
public void txtBox_Vert_Validate()
{
foreach (TextBox[] txtBox_Vert_Controls in arr_txtBox_Vert)
{
foreach (TextBox txtBox_Vert in txtBox_Vert_Controls)
{
string s_txt_Value = txtBox_Vert.Text;
foreach (TextBox txtBox_Vert_1 in txtBox_Vert_Controls)
{
if (txtBox_Vert.Name == txtBox_Vert_1.Name)
{
continue;
}
else if (s_txt_Value == txtBox_Vert_1.Text)
{
if (arr_control.Contains(txtBox_Vert_1) == false)
{
arr_control.Add(txtBox_Vert_1);
}
}
}
}
}
}
If you follow the code then after validating I have added every control in an another array. This was to change the color of those textboxes which have wrong values.
I have changed color of textboxes using its backcolor property.
public DialogResult paint_TxtBox()
{
DialogResult d_Result = DialogResult.Cancel;
if (arr_control.Count != 0)
{
foreach (TextBox txtBox2 in arr_control)
{
txtBox2.BackColor = Color.Yellow;
}
arr_control.Clear();
}
else
{
d_Result = MessageBox.Show("GAME OVER.\nSUDOKU SOLVED SUCCESSFULLY" +
"...\nCLICK YES TO START A NEW GAME OR NO TO SET HIGH SCORE.",
"SUDOKU", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
}
return d_Result;
}
The main logic behind adding everything in an array is that if that array doesn't have any value that means the answer is correct. So I don't have to check again for repeated values.
After completing this I was totally stuck for two days because I couldn't think of any logic of creating a valid sudoku. I searched over internet for solutions but I couldn't get any one which was easy for beginner.
I was totally depressed as I thought I would have to stop this project midway. It was at that time I came across the blog of Chris Meijers and it gave a beautifull way to create a valid sudoku and I was again back to work. Using his method I completed my work soon.
Points of Interest
During this project I learned many lessons the most important part was to do the job first in hand. There was time I used to completely get lost during design time, thinking how I will validate the sudoku this created a back log. Due to this I was getting no where because neither I was able to concentrate properly on designing part nor on validation part. At this time I learned not to think of coming work but do that which has to be done first.
Suggestion
As mentioned earlier this code has not been optimised. This may contain bugs that can come to your notice. If you come across any such bugs then please do inform me about the same at "hari.19113@gmail.com".
Thank you.