namespace WindowsFormsApp3{
public partial class Form1 : Form
{
bool allowClick = false;
PictureBox firstGuess;
Random rnd = new Random();
Timer clickTimer = new Timer();
int time = 60;
Timer timer = new Timer { Interval = 1000 };
public Form1()
{ InitializeComponent(); }
private PictureBox[] pictureBoxes
{
get { return Controls.OfType<picturebox>().ToArray(); }
}
private static IEnumerable
images
{
get
{
return new Image[]
{
Properties.Resources.Circle,
Properties.Resources.Kite,
Properties.Resources.Rectangle,
Properties.Resources.Rhombus,
Properties.Resources.square,
Properties.Resources.Triangle,
};
}
}
private void startGameTimer()
{
timer.Start();
timer.Tick += delegate
{
time--;
if (time < 0)
{
timer.Stop();
MessageBox.Show("Out of time!");
ResetImages();
}
var ssTime = TimeSpan.FromSeconds(time);
label1.Text = "00: " + time.ToString();
};
}
private void ResetImages()
{
foreach (var pic in pictureBoxes)
{
pic.Tag = null;
pic.Visible = true;
}
HideImages();
setRandomImages();
time = 60;
timer.Start();
}
private void HideImages()
{
foreach (var pic in pictureBoxes)
{
pic.Image = Properties.Resources.Question_Mark;
}
}
private PictureBox getFreeSlot()
{
int num;
do
{
num = rnd.Next(0, pictureBoxes.Count());
}
while (pictureBoxes[num].Tag != null);
return pictureBoxes[num];
}
private void setRandomImages()
{
foreach (var image in images)
{
getFreeSlot().Tag = image;
getFreeSlot().Tag = image;
}
}
private void CLICKTIMER_TICK(object sender, EventArgs e)
{
HideImages();
allowClick = true;
clickTimer.Stop();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void clickImage(object sender, EventArgs e)
{
if (!allowClick) return;
var pic = (PictureBox)sender;
if (firstGuess == null)
{
firstGuess = pic;
pic.Image = (Image)pic.Tag;
return;
}
pic.Image = (Image)pic.Tag;
if (pic.Image == firstGuess.Image && pic != firstGuess)
{
pic.Visible = firstGuess.Visible = false;
{
firstGuess = pic;
}
HideImages();
}
else
{
allowClick = false;
clickTimer.Start();
}
firstGuess = null;
if (pictureBoxes.Any(p => p.Visible)) return;
MessageBox.Show("You win now try again");
ResetImages();
}
private void startGame(object sender, EventArgs e)
{
allowClick = true;
setRandomImages();
HideImages();
startGameTimer();
clickTimer.Interval = 1000;
clickTimer.Tick += CLICKTIMER_TICK;
button1.Enabled = false;
}
}
}