Introduction
When I was a child, I used to play a game called "Memory". It consisted of finding couples of images hidden among a mixture of items. It was funny and it served to improve your memory.
Now, I have developed a similar game. It is easy, but if you want, you can make it more difficult, increasing the number of similar items that you have to search or decreasing the number of allowed mistakes.
Background
Well, it is simple.
There is the class Table that has a two-dimensional matrix. The bounds of this matrix will depend on the number of groups that the user had configured. When the app is thrown, this class is instantiated in order to fill this matrix with a group of random numbers from 1 to 10. Each group has one number defined.
Then, the app generates the GUI, creating one panel per number. At the same time, it links the OnClick event for every panel created.
At the same time that you are discovering items, each panel is put into a collection. When the number of tries for each turn is completed, the items into the collection are compared.
If they are equal, they stay unconcealed, by the contrary, they are removed from the collection and the panels are hidden again.
The game lasts until the number of errors allowed is exceeded.
Using the Code
Filling the matrix with random numbers:
private void Initialize()
{
Random rnd = new Random();
int x = 0;
int y = 0;
int currentNum = 0;
int totalNums = (this.panel.GetLength(0) * this.panel.GetLength(1)) / this.groups;
while (x < this.panel.GetLength(0))
{
while (y < this.panel.GetLength(1))
{
do
{
currentNum = rnd.Next(1, totalNums+1);
}
while (CheckGroup(currentNum, this.groups));
this.panel[x, y] = currentNum;
y++;
}
y = 0;
x++;
}
}
Creating the GUI:
private void ShowTable()
{
int x = 0;
int y = 0;
int xStart = 20;
int yStart = 30;
int width = this.tb.Panel.GetLength(1);
int height = this.tb.Panel.GetLength(0);
this.Width = width * 50;
this.Height = height * 62;
while (x < height)
{
while (y < width)
{
Panel p = new Panel();
p.Name = x.ToString() + "-" + y.ToString();
p.BorderStyle = BorderStyle.Fixed3D;
p.Location = new Point(xStart, yStart);
p.Size = new Size(40, 40);
p.BackColor = Color.Gray;
p.Click += new EventHandler(this.ClickPanel);
this.Controls.Add(p);
xStart += 40;
y++;
}
yStart += 40;
xStart = 20;
y = 0;
x++;
}
}
Interacting:
private void ClickPanel(object sender, EventArgs e)
{
Panel p = (Panel)sender;
if (!p.HasChildren)
{
string[] axis = p.Name.Split('-');
int x = int.Parse(axis[0]);
int y = int.Parse(axis[1]);
PictureBox pBox = this.GetNumber(this.tb.Panel[x, y]);
p.Controls.Add(pBox);
p.BackColor = Color.Beige;
this.Refresh();
this.openPanels.Add(p);
if (this.find == 0)
{
this.find = this.tb.Panel[x, y];
this.tries++;
}
else if (this.tries == this.groups)
{
if (!this.AreEqual())
{
this.CleanMistake();
this.SetMistake();
if (awdMistakes == mistakes)
{
if (MessageBox.Show("YOU LOSE!" +
Environment.NewLine + "A new game will start!", "Lose",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation) ==
DialogResult.OK)
this.Restart();
}
}
this.find = 0;
this.tries = 1;
this.openPanels.Clear();
}
else
{
this.tries++;
}
}
}
History
- 29th November, 2009: Initial post