匹配游戏

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  
    public partial class Form1 : Form
    {
        //Use this Random object to choose random icons for the squares
        Random random = new Random();
        //Each of these letters is an interesting icon
        //in the Webdings font,and each appears twice twice in this list
        private List<string> icons = new List<string>
        {
            "!","!","N","N",",",",","k","k","b","b","v","v","w","w","z","z",
        };
        //添加标签应用
        private Label firstClicked = null;
        private Label secondClicked = null;
        //向每个标签分配随机标签
        private void AssignIconsToSquares()
        {
            //if (tableLayoutPanel1 == null)
            //{
            //    return;
            //}
            foreach (Control control in tableLayoutPanel1.Controls)
            {
                //The TableLayoutPanel has 16 labels,
                //and the icon list has 16icons,
                //so an icon os pulled at random from the list,
                //and added to each label
                Label iconLabel = control as Label;
                if (iconLabel != null)
                {
                    int randomNumber =random.Next(icons.Count);
                    iconLabel.Text = icons[randomNumber];
                    // iconLabel.ForeColor = iconLabel.BackColor;
                    iconLabel.ForeColor = iconLabel.BackColor;
                    icons.RemoveAt(randomNumber);
                }
             
            }
        }


        //调用方法
        public Form1()
        {
            InitializeComponent();

            AssignIconsToSquares();
        }

    

        //添加 Click 事件处理程序
        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 CheckForWinner()
        {
            //go through all of the labels in the tableLayoutPanel,
            //checking each one to see if its icon is matched
            foreach (var control in tableLayoutPanel1.Controls)
            {
                Label iconLabel = control as Label;
                if (iconLabel != null)
                {
                    if (iconLabel.ForeColor == iconLabel.BackColor)
                    {
                        return;
                    }
                }
            }
            //if the loop didn't return, it didn't find
            //any unmatched icons
            //That means the user won, show a messafe and close the form
            MessageBox.Show("匹配所有图标!", "胜利");
            Close();
        }

      


        private void timer1_Tick(object sender, EventArgs e)
        {
            //停止计时器
            timer1.Stop();
           
           
            firstClicked.ForeColor = firstClicked.BackColor;
            secondClicked.ForeColor = secondClicked.BackColor;

            firstClicked = null;
            secondClicked = null;

 

        }
    }
}

posted @ 2012-04-17 22:04  |▍花舞花落泪 ╮  阅读(159)  评论(0编辑  收藏  举报