using System; using System.Windows.Forms; namespace MatchingGame { public partial class Form1 : Form { Label firstClicked = null; Label secondClicked = null; Random random = new Random(); List icons = new List() { "!", "!", "N", "N", "@", "@", "k", "k", "b", "b", "v", "v", "%", "%", "~", "~" }; public Form1() { InitializeComponent(); AssignIconsToSquares(); } 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); iconLabel.ForeColor = iconLabel.BackColor; } } } private void label1_Click(object sender, EventArgs e) { if (timer1.Enabled == true) return; Label clickedLabel = sender as Label; if (clickedLabel != null) { if (clickedLabel.ForeColor == Color.White) return; if (firstClicked == null) { firstClicked = clickedLabel; firstClicked.ForeColor = Color.White; return; } secondClicked = clickedLabel; secondClicked.ForeColor = Color.White; CheckForWinner(); if (firstClicked.Text == secondClicked.Text) { firstClicked = null; secondClicked = null; return; } timer1.Start(); } } private void timer1_Tick(object sender, EventArgs e) { timer1.Stop(); firstClicked.ForeColor = firstClicked.BackColor; secondClicked.ForeColor = secondClicked.BackColor; firstClicked = null; secondClicked = null; } private void CheckForWinner() { foreach (Control control in tableLayoutPanel1.Controls) { Label iconLabel = control as Label; if (iconLabel != null) { if (iconLabel.ForeColor == iconLabel.BackColor) return; } } MessageBox.Show("You matched all the icons!", "Congratulations"); Close(); } } }