namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MatchGraphic();
}
Random random = new Random();
Label firstLabel = null;
Label lastLabel = null;
List<string> icons = new List<string>()
{
"!", "!", "N", "N", ",", ",", "k", "k",
"b", "b", "v", "v", "w", "w", "z", "z"
};
private void MatchGraphic()
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Label iconLable=control as Label;
if (iconLable!=null)
{
int randomNumber = random.Next(icons.Count);
iconLable.Text = icons[randomNumber];
iconLable.ForeColor = iconLable.BackColor;
icons.RemoveAt(randomNumber);
}
}
}
private void label_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
return;
Label sendLabel = sender as Label;
if (sendLabel != null)
{
if(sendLabel.ForeColor == Color.Black)
return;
if (firstLabel == null)
{
firstLabel = sendLabel;
sendLabel.ForeColor = Color.Black;
return;
}
lastLabel = sendLabel;
lastLabel.ForeColor = Color.Black;
CheckForWinner();
if (firstLabel.Text == lastLabel.Text)
{
firstLabel = null;
lastLabel = null;
return;
}
timer1.Start();
}
}
private void CheckForWinner()
{
// Go through all of the labels in the TableLayoutPanel,
// checking each one to see if its icon is matched
foreach (Control control in tableLayoutPanel1.Controls)
{
Label iconLabel = control as Label;
if (iconLabel != null)
{
if (iconLabel.ForeColor == iconLabel.BackColor)
return;
}
}
MessageBox.Show("游戏胜利!", "恭喜!");
Close();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
firstLabel.ForeColor = firstLabel.BackColor;
lastLabel.ForeColor = lastLabel.BackColor;
firstLabel = null;
lastLabel = null;
}
}
}
附上链接