How To Develop Match Making Game Using C# in .Net 4.5

Introduction

In this article, I will show how to develop a Match Game in C# under Visual Studio 2012.

In this article, you will learn.

  1. How to use the for-each loop
  2. How to use a Timer so that it will fire only once.
  3. How to use the list.
  4. Build an event handler that you can use with multiple objects.
  5. How to hold an object using a List Object.

Step 1. Create a new Window Form Application in C#.

In Form1 add the "Table Layout panel" from the "Tool Box".

You must make a few changes to the "Table Layout panel" Container.

  1. Change its height and width to 550 from the Property window.
  2. Change its Color according to your choice.
  3. Change its Dock property to Fill.
  4. Click on the triangle button on the upper right-hand corner of the "Table Layout panel" to display a Task Menu.
  5. Add two new Rows and two new Columns.
  6. Click on "Edit Rows and Columns" to open the Column and Row Styles, click on each row and change it's percentage to 25%, then again click on each column and change the percentage to 25%.

Step 2. Add a Label to one of the blocks in the Table Layout panel, now again you must also make some changes to the Label:

  1. Set its Background Color property similar to the color of the Tool Strip Container.
  2. Change its Auto Size property to False.
  3. Change its Dock property to fill.
  4. Change its Text Align Property to the middle center.
  5. Change it's the font to Webdings, size of 72 and Bold.
  6. Set the Text to c.

Now, copy this Label and paste it to all the blocks of the "Table Layout panel".

Step 3. Select the Timer from the ToolBox and set its Interval to 750 and Enabled=False.

Now your Form will look like this.

Forms

Step 4. Now we must add a Random Object and List of Icons. For that, we must write the following code.

public partial class Form1 : Form
{
    Random random = new Random();
    List<string> icons = new List<string>()
    {
        "!", "!", "N", "N", ",", ",", "K", "K",
        "b", "b", "v", "v", "w", "w", "z", "z"
    };
}

Step 5. Now we must assign a random icon to each Label, and we must write the following code, this code is in continuation with the code above.

private void AssignIconsToSquares()
{
    foreach(Control control in tableLayoutPanel1.Controls)
    {
        Label iconlabel = control as Label;
        if (iconlabel != null)
        {
            int randomNumber = random.Next(icons.Count);
            iconlabel.Text = icons[randomNumber];
            icons.RemoveAt(randomNumber);
        }
    }
}

If you want an action to be performed repeatedly then you can use a foreach loop. Here I use foreach because I want the same action to be performed for every label in the TableLayoutPanel.

Step 6. The AssignIconsToSquares() method should be called as soon as the program starts, for that you must write the given statement just below the InitializeComponent in the Form1 constructor.

public Form1()
{
    InitializeComponent();
    AssignIconsToSquares();
}

Step 7. Save your Program and run it. It will show random icons assigned to each label.

Random icons

Now add the following code inside the for each loop.

iconlabel.ForeColor = iconlabel.BackColor;

Now run your Program again, you will see that all the icons are invisible; now that's because they have the same color as the Background Color.

Step 8. Now you must add the click event for each label. For that you must first click on the first label, then hold the CTRL button and click on each label. In this way, all the Labels will be selected.

Now right-click on the page and in the properties window go to the Click event and type label_Click.

Click

Press Enter and you will see that a click event handler is added to the code.

Step 9. Write the following code in the label_Click Event Handler.

private void label_Click(object sender, EventArgs e)
{
    Label clickedLabel = sender as Label;
    if (clickedLabel != null)
    {
        if (clickedLabel.ForeColor == Color.Black)
            return;
        clickedLabel.ForeColor = Color.Black;
    }
}

Now run your program, you should see a Blank form. Now click on a label and continue clicking until none of the labels are in the output window.

Step 10. Now you must add the Label Reference; for that use the following code.

public partial class Form1 : Form {
    Label firstclicked = null;
    Label secondclicked = null;

Step 11. Now you must modify the click event handler to use the first clicked event handler.

private void label_Click(object sender, EventArgs e)
{
    Label clickedLabel = sender as Label;
    if (clickedLabel != null)
    {
        if (clickedLabel.ForeColor == Color.Black)
            return;
        if (firstclicked == null)
        {
            firstclicked = clickedLabel;
            clickedLabel.ForeColor = Color.Black;
            return;
        }
    }
}

Now debug your program and on the output window click on any of the Labels. After clicking the first label none of the labels will appear that's because until now only the first clicked is working.

Step 12. Now add the Timer through the ToolBox, and change its Interval Property to 750 but leave the Enable Property to false. Double-click the Timer and add the following code to its Tick Event:

private void timer1_Tick(object sender, EventArgs e) {
    timer1.Stop();
    firstclicked.ForeColor = firstclicked.BackColor;
    secondclicked.ForeColor = secondclicked.BackColor;
    firstclicked = null;
    secondclicked = null;
}

Step 13. To Enable the timer into the label click add the following code to the label_Click Event.

private void label_Click(object sender, EventArgs e)
{
    if (timer1.Enabled == true)
        return;

    Label clickedLabel = sender as Label;

    if (clickedLabel != null)
    {
        if (clickedLabel.ForeColor == Color.Black)
            return;

        if (firstclicked == null)
        {
            firstclicked = clickedLabel;
            clickedLabel.ForeColor = Color.Black;
            return;
        }

        secondclicked = clickedLabel;
        secondclicked.ForeColor = Color.Black;
        timer1.Start();
    }
}

Debug your program, now you can click two labels one by one but both of them will disappear after some time.

Step 14. To keep pairs visible you must add the following code to the label_Click

secondclicked = clickedLabel;
secondclicked.ForeColor = Color.Black;
if (firstclicked.Text == secondclicked.Text)
{
    firstclicked = null;
    secondclicked = null;
    return;
}
timer1.Start();

Debug the program and start clicking the Icons, if you click the similar icons then they will remain shown but if you click a dis-similar Label then they disappear because the Timer's Tick event will be triggered.

Game

Step 15. Your last step is to verify that the player has Won, for that must make a method "winner" as shown below.

private void winner()
{
    foreach (Control control in tableLayoutPanel1.Controls)
    {
        Label iconlabel = control as Label;
        if (iconlabel != null)
        {
            if (iconlabel.ForeColor == iconlabel.BackColor)
                return;
        }
    }
    MessageBox.Show("You Won, Congratulations");
    Close();
}

Now to againadd the code in the label_Click event, so that it can call the winner method when the player has Won.

secondclicked = clickedLabel;
secondclicked.ForeColor = Color.Black;
winner();
if (firstclicked.Text == secondclicked.Text) {
    firstclicked = null;
    secondclicked = null;
    return;
}

Now debug the program, play the game and if you Won then you will get a confirmation message like this.

Confirmation message