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

  105. if (iconLabel != null)
  106. {
  107. if (iconLabel.ForeColor == iconLabel.BackColor)
  108. return;
  109. }
  110. }
  111. //On completion show message
  112. MessageBox.Show("You matched all the icons!", "Congratulations!");
  113. Close();
  114. }
  115. }
  116. }
Now, check the output of the game:
output