Create a Matching Game using visual studio
In this tutorial, you build a matching game, where the player must match pairs of
hidden icons. You learn how to:
Hold objects using a List object.
Use a foreach loop in Visual C# or a For Each loop in Visual Basic.
Keep track of a form's state using reference variables.
Build an event handler that you can use with multiple objects.
Make a timer fire exactly once when started.
Copy this code and create project on visual studio and run it.
//
Build Number
//
Revision
//
// You can specify all the values or you can default
the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Form1.Designer.cs
namespace MatchingGame
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise,
false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.tableLayoutPanel1 = new
System.Windows.Forms.TableLayoutPanel();
this.label16 = new System.Windows.Forms.Label();
this.label15 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.BackColor =
System.Drawing.Color.CornflowerBlue;
this.tableLayoutPanel1.ColumnCount = 4;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.
ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.
ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.
ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.
ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.Controls.Add(this.label16, 3, 3);
this.tableLayoutPanel1.Controls.Add(this.label15, 2, 3);
this.tableLayoutPanel1.Controls.Add(this.label14, 1, 3);
this.tableLayoutPanel1.Controls.Add(this.label13, 0, 3);
this.tableLayoutPanel1.Controls.Add(this.label12, 3, 2);
this.tableLayoutPanel1.Controls.Add(this.label11, 2, 2);
this.tableLayoutPanel1.Controls.Add(this.label10, 1, 2);
this.tableLayoutPanel1.Controls.Add(this.label9, 0, 2);
this.tableLayoutPanel1.Controls.Add(this.label8, 3, 1);
this.tableLayoutPanel1.Controls.Add(this.label7, 2, 1);
this.tableLayoutPanel1.Controls.Add(this.label6, 1, 1);
this.tableLayoutPanel1.Controls.Add(this.label5, 0, 1);
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace MatchingGame
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Form1.cs
namespace MatchingGame
{
public partial class Form1 : Form
{
int count = 0;
// Use this Random object to choose random icons for the squares
Random random = new Random();
// firstClicked points to the first Label control
// that the player clicks, but it will be null
// if the player hasn't clicked a label yet
Label firstClicked = null;
// secondClicked points to the second Label control
// that the player clicks
Label secondClicked = null;
// Each of these letters is an interesting icon
// in the Webdings font,
// and each icon appears twice in this list
List<string> icons = new List<string>()
{
"!", "!", "N", "N", ",", ",", "k", "k",
"b", "b", "v", "v", "w", "w", "z", "z"
};
/// <summary>
/// Assign each icon from the list of icons to a random square
/// </summary>
private void AssignIconsToSquares()
{
// The TableLayoutPanel has 16 labels,
// and the icon list has 16 icons,
// so an icon is pulled at random from the list
// and added to each label
foreach (Control control in tableLayoutPanel1.Controls)
{
// The timer is only on after two non-matching
// icons have been shown to the player,
// so ignore any clicks if the timer is running
if (timer1.Enabled == true)
return;
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);
}
}
}
public Form1()
{
//AssignIconsToSquares();
InitializeComponent();
AssignIconsToSquares();
}
private void label_Click(object sender, EventArgs e)
{
// The timer is only on after two non-matching
// icons have been shown to the player,
// so ignore any clicks if the timer is running
if (timer1.Enabled == true)
return;
Label clickedLabel = sender as Label;
if (clickedLabel != null)
{
// If the clicked label is black, the player clicked
// an icon that's already been revealed -// ignore the click
if (clickedLabel.ForeColor == Color.Black)
return;
// If firstClicked is null, this is the first icon
// in the pair that the player clicked,
// so set firstClicked to the label that the player
// clicked, change its color to black, and return
if (firstClicked == null)
{
firstClicked = clickedLabel;
firstClicked.ForeColor = Color.Black;
return;
}
// If the player gets this far, the timer isn't
// running and firstClicked isn't null,
// so this must be the second icon the player clicked
// Set its color to black
secondClicked = clickedLabel;
secondClicked.ForeColor = Color.Black;
// Check to see if the player won
//CheckForWinner();
//
//
//
//
//
//
//
if
{
If the player gets this far, the player
clicked two different icons, so start the
timer (which will wait three quarters of
a second, and then hide the icons)
If the player clicked two matching icons, keep them
black and reset firstClicked and secondClicked
so the player can click another icon
(firstClicked.Text != secondClicked.Text)
firstClicked = null;
secondClicked = null;
//timer1.Start();
MessageBox.Show("first and second not match");
return;
}
timer1.Start();
CheckForWinner();
}
}
/// <summary>
/// This timer is started when the player clicks
/// two icons that don't match,
/// so it counts three quarters of a second
/// and then turns itself off and hides both icons
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
// Stop the timer
timer1.Stop();
// Hide both icons
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked = null;
secondClicked = null;
}
/// <summary>
/// Check every icon to see if it is matched, by
/// comparing its foreground color to its background color.
/// If all of the icons are matched, the player wins
/// </summary>
private void CheckForWinner()
{
//int count=0;
// 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)
{
// MessageBox.Show("iconlabel isnot null");
if (iconLabel.ForeColor != iconLabel.BackColor)
{
//MessageBox.Show("return statement");
count++;
// MessageBox.Show("count",count.ToString());
//return;
break;
}
}
}
// If the loop didn’t return, it didn't find
// any unmatched icons
// That means the user won. Show a message and close the form
if (count == 8)
{
MessageBox.Show("You matched all the icons!", "Congratulations");
Close();
}
}
}
}