Click here to Skip to main content
15,881,381 members
Articles / Game Development

Sudoku game for beginners

Rate me:
Please Sign up or sign in to vote.
3.63/5 (8 votes)
12 Jan 2017CPOL3 min read 49.4K   4.6K   17   8
This article is on how a sudoku game can be developed easily.

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.

C#
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.

C#
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.

C#
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.

C#
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.

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIs this link works? Pin
Bhuvanesh Mohankumar12-Jan-17 3:15
Bhuvanesh Mohankumar12-Jan-17 3:15 
GeneralMy vote of 5 Pin
User 1106097916-Nov-14 6:49
User 1106097916-Nov-14 6:49 
GeneralRe: My vote of 5 Pin
hari1911321-Dec-14 22:20
hari1911321-Dec-14 22:20 
QuestionPuzzle generation problem Pin
gstolarov2-May-12 9:04
gstolarov2-May-12 9:04 
AnswerRe: Puzzle generation problem Pin
hari191134-May-12 2:46
hari191134-May-12 2:46 
GeneralRe: Puzzle generation problem Pin
PIEBALDconsult12-Jan-17 2:34
mvePIEBALDconsult12-Jan-17 2:34 
Interesting. Actually I have need of some non-unique puzzles...
GeneralRe: Puzzle generation problem Pin
gstolarov24-Jan-17 3:48
gstolarov24-Jan-17 3:48 
GeneralRe: Puzzle generation problem Pin
PIEBALDconsult24-Jan-17 2:49
mvePIEBALDconsult24-Jan-17 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.