Form1の全コード
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace MatchingGame
{
public partial class Form1 : Form
{
Random random = new Random();
List icons = new List()
{ "!", "!", "N", "N", ",", ",", "k", "k", "b", "b", "v", "v", "w", "w", "z", "z" };
Label firstClicked = null;
Label secondClicked = null;
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];
iconLabel.ForeColor = iconLabel.BackColor;
icons.RemoveAt(randomNumber);
}
}
}
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) return;
if (firstClicked == null)
{
firstClicked = clickedLabel;
firstClicked.ForeColor = Color.Black;
return;
}
secondClicked = clickedLabel;
secondClicked.ForeColor = Color.Black;
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();
}
}
}