Matching Game Of Image In Windows Application Using C# .Net

This blog will show you how you can create a very cool image matching game for the kids, using C#.NET in Windows Application. Thus, for this article, we will create a new Windows Application and design the form, as shown below.
 
design
 
Afterwards, we will add the code, given below, into the form.cs.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Drawing;  
  4. using System.Windows.Forms;  
  5.   
  6. namespace MatchingGame  
  7. {  
  8.     public partial class Form1 : Form  
  9.     {  
  10.         Label firstClicked = null;  
  11.         Label secondClicked = null;  
  12.   
  13.         // Use this Random object to choose random icons for the squares.  
  14.         Random random = new Random();  
  15.   
  16.         //Symbol show as image  
  17.         List<string> icons = new List<string>()   
  18.         {   
  19.             "!""!""N""N"","",""k""k",  
  20.             "b""b""v""v""w""w""z""z"  
  21.         };  
  22.   
  23.         /// <summary>   
  24.         /// Assign each icon from the list of icons to a random square   
  25.         /// </summary>   
  26.         private void AssignIconsToSquares()  
  27.         {  
  28.             foreach (Control control in tableLayoutPanel1.Controls)  
  29.             {  
  30.                 Label iconLabel = control as Label;  
  31.                 if (iconLabel != null)  
  32.                 {  
  33.                     int randomNumber = random.Next(icons.Count);  
  34.                     iconLabel.Text = icons[randomNumber];  
  35.                     iconLabel.ForeColor = iconLabel.BackColor;  
  36.                     icons.RemoveAt(randomNumber);  
  37.                 }  
  38.             }  
  39.         }  
  40.   
  41.   
  42.         public Form1()  
  43.         {  
  44.             InitializeComponent();  
  45.             AssignIconsToSquares();  
  46.         }  
  47.   
  48.         /// <summary>   
  49.         /// Every label's Click event is handled by this event handler.  
  50.         /// </summary>   
  51.         /// <param name="sender">The label that was clicked.</param>  
  52.         /// <param name="e"></param>  
  53.         private void label_Click(object sender, EventArgs e)  
  54.         {  
  55.             if (timer1.Enabled == true)  
  56.                 return;  
  57.   
  58.             Label clickedLabel = sender as Label;  
  59.   
  60.             if (clickedLabel != null)  
  61.             {  
  62.                 if (clickedLabel.ForeColor == Color.Black)  
  63.                     // All done - leave the if statements.  
  64.                     return;  
  65.   
  66.                 if (firstClicked == null)  
  67.                 {  
  68.                     firstClicked = clickedLabel;  
  69.                     firstClicked.ForeColor = Color.Black;  
  70.   
  71.                     // All done - leave the if statements.  
  72.                     return;  
  73.                 }  
  74.   
  75.                 secondClicked = clickedLabel;  
  76.                 secondClicked.ForeColor = Color.Black;  
  77.   
  78.                 // Check to see if the player won.  
  79.                 CheckForWinner();  
  80.   
  81.                 if (firstClicked.Text == secondClicked.Text)  
  82.                 {  
  83.                     firstClicked = null;  
  84.                     secondClicked = null;  
  85.                     return;  
  86.                 }            
  87.                 timer1.Start();  
  88.             }  
  89.         }  
  90.   
  91.         /// <summary>   
  92.         /// This timer is started when the player clicks    
  93.         /// two icons that don't match,   
  94.         /// so it counts three quarters of a second    
  95.         /// and then turns itself off and hides both icons.  
  96.         /// </summary>   
  97.         /// <param name="sender"></param>  
  98.         /// <param name="e"></param>  
  99.         private void timer1_Tick(object sender, EventArgs e)  
  100.         {  
  101.             // Stop the timer.  
  102.             timer1.Stop();  
  103.   
  104.             // Hide both icons.  
  105.             firstClicked.ForeColor = firstClicked.BackColor;  
  106.             secondClicked.ForeColor = secondClicked.BackColor;  
  107.   
  108.             // Reset firstClicked and secondClicked  .  
  109.             firstClicked = null;  
  110.             secondClicked = null;  
  111.         }  
  112.   
  113.         /// <summary>   
  114.         /// Check every icon to see if it is matched, by    
  115.         /// comparing its foreground color to its background color.    
  116.         /// If all of the icons are matched, the player wins.   
  117.         /// </summary>   
  118.         private void CheckForWinner()  
  119.         {  
  120.             foreach (Control control in tableLayoutPanel1.Controls)  
  121.             {  
  122.                 Label iconLabel = control as Label; 

  123.   
  124.                 if (iconLabel != null)  
  125.                 {  
  126.                     if (iconLabel.ForeColor == iconLabel.BackColor)  
  127.                         return;  
  128.                 }  
  129.             }  
  130.             //On completion show message  
  131.             MessageBox.Show("You matched all the icons!""Congratulations!");  
  132.             Close();  
  133.         }  
  134.   
  135.     }  

Now, check the output of the game:
 
output
Ebook Download
View all
Learn
View all