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

Introduction

 
In this article, I will show how to develop a Match Making Game in C# under Visual Studio 2012.
 
In this article you will learn:
  1. How to use the foreach 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 with the "Table Layout panel" Container like:
  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 the 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 in the Label:
  1. Set it's Background Color property similar to the color of the Tool Strip Container.
  2. Change it's Auto Size property to False.
  3. Change it's Dock property to fill.
  4. Change its Text Align Property to 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:
 
game6.jpg
 
Step 4
 
Now we must add a Random Object and List of Icons. For that we must write the following code:
  1. public partial class Form1 : Form  
  2. {  
  3.         Random random = new Random();  
  4.         List<string> icons = new List<string>()  
  5.         {  
  6.              "!","!","N","N",",",",","K","K",  
  7.              "b","b","v","v","w","w","z","z"  
  8.         }; 
Ste 5
 
Now we must assign a random icon to each Label, for that we must write the following code, this code is in continuation with the code above.
  1. private void AssignIconsToSquares() {  
  2.     foreach(Control control in tableLayoutPanel1.Controls) {  
  3.         Label iconlabel = control as Label;  
  4.         if (iconlabel != null) {  
  5.             int randomNumber = random.Next(icons.Count);  
  6.             iconlabel.Text = icons[randomNumber];  
  7.             icons.RemoveAt(randomNumber);  
  8.         }  
  9.     }  
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.
  1. public Form1() {  
  2.     InitializeComponent();  
  3.     AssignIconsToSquares();  
Step 7
 
Save your Program and run it. It will show random icons assigned to each label.
 
game2.jpg
 
Now add the following code inside the foreach loop:
  1. 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.
 
game3.jpg
 
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:
  1. private void label_Click(object sender, EventArgs e) {  
  2.     Label clickedLabel = sender as Label;  
  3.     if (clickedLabel != null) {  
  4.         if (clickedLabel.ForeColor == Color.Black)  
  5.             return;  
  6.         clickedLabel.ForeColor = Color.Black;  
  7.     }  
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:
  1. public partial class Form1: Form {  
  2.         Label firstclicked = null;  
  3.         Label secondclicked = null
Step 11
 
Now you must modify the click event handler to use the firstclicked event handler.
  1. private void label_Click(object sender, EventArgs e) {  
  2.         Label clickedLabel = sender as Label;  
  3.         if (clickedLabel != null) {  
  4.             if (clickedLabel.ForeColor == Color.Black)  
  5.                 return;  
  6.             if (firstclicked == null) {  
  7.                 firstclicked = clickedLabel;  
  8.                 clickedLabel.ForeColor = Color.Black;  
  9.                 return;  
  10.             } 
Now debug your program and on the output window click on any of the Labels. After clicking the first label none of the label will appear that's because until now only the firstclicked is working.
 
Step 12
 
Now add the Timer through the ToolBox, and change it's Interval Property to 750 but leave the Enable Property to false. Double-click the Timer and add the following code to its Tick Event:
  1. private void timer1_Tick(object sender, EventArgs e) {  
  2.     timer1.Stop();  
  3.     firstclicked.ForeColor = firstclicked.BackColor;  
  4.     secondclicked.ForeColor = secondclicked.BackColor;  
  5.     firstclicked = null;  
  6.     secondclicked = null;  
Step 13
 
To Enable the timer into the label click add the following code to the label_Click Event:
  1. private void label_Click(object sender, EventArgs e) {  
  2.   if (timer1.Enabled == true)  
  3.     return;  
  4.   Label clickedLabel = sender as Label;  
  5.   if (clickedLabel != null) {  
  6.     if (clickedLabel.ForeColor == Color.Black)  
  7.       return;  
  8.     if (firstclicked == null) {  
  9.       firstclicked = clickedLabel;  
  10.       clickedLabel.ForeColor = Color.Black;  
  11.       return;  
  12.     }  
  13.     secondclicked = clickedLabel;  
  14.     secondclicked.ForeColor = Color.Black;  
  15.     timer1.Start();  
  16.   }  
Debug your program, now you can click two label's 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:
  1.         secondclicked = clickedLabel;  
  2.         secondclicked.ForeColor = Color.Black;  
  3.           if (firstclicked.Text == secondclicked.Text)  
  4.         {  
  5.             firstclicked = null;  
  6.             secondclicked = null;  
  7.             return;  
  8.         }  
  9.         timer1.Start();  
  10.     }  
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.
 
game4.jpg
 
Step 15
 
Your last task is to verify that the player has Won, for that you must make a method "winner" as shown below:
  1. private void winner() {  
  2.   foreach(Control control in tableLayoutPanel1.Controls) {  
  3.     Label iconlabel = control as Label;  
  4.     if (iconlabel != null) {  
  5.       if (iconlabel.ForeColor == iconlabel.BackColor)  
  6.         return;  
  7.     }  
  8.   }  
  9.   MessageBox.Show("You Won,Congratulations");  
  10.   Close();  
Now to again add the code in the label_Click event, so that it can call the winner method when the player has Won.
  1. secondclicked = clickedLabel;  
  2. secondclicked.ForeColor = Color.Black;  
  3. winner();  
  4. if (firstclicked.Text == secondclicked.Text) {  
  5.   firstclicked = null;  
  6.   secondclicked = null;  
  7.   return;  
Now debug the program, play the game and if you Won then you will get a confirmation message like this:
 
game5.jpg