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.

Afterwards, we will add the code, given below, into the form.cs.
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- namespace MatchingGame
- {
- public partial class Form1 : Form
- {
- Label firstClicked = null;
- Label secondClicked = null;
- // Use this Random object to choose random icons for the squares.
- Random random = new Random();
- //Symbol show as image
- List<string> icons = new List<string>()
- {
- "!", "!", "N", "N", ",", ",", "k", "k",
- "b", "b", "v", "v", "w", "w", "z", "z"
- };
- /// <summary>
- /// Assign each icon from the list of icons to a random square
- /// </summary>
- 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];
- iconLabel.ForeColor = iconLabel.BackColor;
- icons.RemoveAt(randomNumber);
- }
- }
- }
- public Form1()
- {
- InitializeComponent();
- AssignIconsToSquares();
- }
- /// <summary>
- /// Every label's Click event is handled by this event handler.
- /// </summary>
- /// <param name="sender">The label that was clicked.</param>
- /// <param name="e"></param>
- 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)
- // All done - leave the if statements.
- return;
- if (firstClicked == null)
- {
- firstClicked = clickedLabel;
- firstClicked.ForeColor = Color.Black;
- // All done - leave the if statements.
- return;
- }
- secondClicked = clickedLabel;
- secondClicked.ForeColor = Color.Black;
- // Check to see if the player won.
- CheckForWinner();
- if (firstClicked.Text == secondClicked.Text)
- {
- firstClicked = null;
- secondClicked = null;
- return;
- }
- timer1.Start();
- }
- }
- /// <summary>
- /// This timer is started when the player clicks
- /// two icons that don't match,
- /// so it counts three quarters of a second
- /// and then turns itself off and hides both icons.
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void timer1_Tick(object sender, EventArgs e)
- {
- // Stop the timer.
- timer1.Stop();
- // Hide both icons.
- firstClicked.ForeColor = firstClicked.BackColor;
- secondClicked.ForeColor = secondClicked.BackColor;
- // Reset firstClicked and secondClicked .
- firstClicked = null;
- secondClicked = null;
- }
- /// <summary>
- /// Check every icon to see if it is matched, by
- /// comparing its foreground color to its background color.
- /// If all of the icons are matched, the player wins.
- /// </summary>
- private void CheckForWinner()
- {
- foreach (Control control in tableLayoutPanel1.Controls)
- {
- Label iconLabel = control as Label;
- if (iconLabel != null)
- {
- if (iconLabel.ForeColor == iconLabel.BackColor)
- return;
- }
- }
- //On completion show message
- MessageBox.Show("You matched all the icons!", "Congratulations!");
- Close();
- }
- }
- }


Manas SPosted May 15, 2018, 11:25 PM
Thanks for the share. Wanted to know, how to hide the first icon when the user doesn't select the 2nd icon in time (let's say 500 ms)?
kalu singh raoPosted Jul 7, 2016, 9:07 AM
Nice...
Vipul MalhotraPosted Jul 7, 2016, 2:57 AM
Nice share