-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arena.cs
112 lines (87 loc) · 2.82 KB
/
Arena.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace MatchingGame
{
public partial class Arena : Form
{
Label firstClicked = null;
Label secondClicked = null;
Random random = new Random();
List<string> icons = new List<string>()
{
"!", "!", "U", "U", "C", "C", "A", "A",
"b", "b", "k", "k", "M", "M", "1", "1"
};
public Arena()
{
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;
//clickedLabel.ForeColor = Color.Black;
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 every icon! Such a winner, you.");
Close();
}
}
}