C# 九宫格游戏

来源:互联网 发布:淘宝3c数码是哪个类目 编辑:程序博客网 时间:2024/04/29 03:26

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 qq
{
    /// <summary>
    /// 2 4 6 8 1 3 7 9 正确步骤
    /// </summary>
    public partial class Nine : Form
    {
        //点击次数计数器
        int count = 0;
        public Nine()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool result=false;
            count++;
            this.label2.Text = count.ToString();//在label2显示
            String btnText = ((Button)sender).Text;
            if (btnText.Equals("1"))
            {
                button1.BackColor = button1.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button2.BackColor = button2.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button4.BackColor = button4.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button5.BackColor = button5.BackColor == Color.Red ? Color.Yellow : Color.Red;
            }
            else if (btnText.Equals("2"))
            {
                button1.BackColor = button1.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button2.BackColor = button2.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button3.BackColor = button3.BackColor == Color.Red ? Color.Yellow : Color.Red;
            }
            else if (btnText.Equals("3"))
            {               
                button2.BackColor = button2.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button3.BackColor = button3.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button5.BackColor = button5.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button6.BackColor = button6.BackColor == Color.Red ? Color.Yellow : Color.Red;
            }
            else if (btnText.Equals("4"))
            {
                button1.BackColor = button1.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button4.BackColor = button4.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button7.BackColor = button7.BackColor == Color.Red ? Color.Yellow : Color.Red;              
            }
            else if (btnText.Equals("5"))
            {
                button2.BackColor = button2.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button4.BackColor = button4.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button5.BackColor = button5.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button6.BackColor = button6.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button8.BackColor = button8.BackColor == Color.Red ? Color.Yellow : Color.Red;
            }
            else if (btnText.Equals("6"))
            {
                button3.BackColor = button3.BackColor == Color.Red ? Color.Yellow : Color.Red;                               
                button6.BackColor = button6.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button9.BackColor = button9.BackColor == Color.Red ? Color.Yellow : Color.Red;
            }
            else if (btnText.Equals("7"))
            {
                button4.BackColor = button4.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button5.BackColor = button5.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button7.BackColor = button7.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button8.BackColor = button8.BackColor == Color.Red ? Color.Yellow : Color.Red;
            }
            else if (btnText.Equals("8"))
            {                               
                button7.BackColor = button7.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button8.BackColor = button8.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button9.BackColor = button9.BackColor == Color.Red ? Color.Yellow : Color.Red;
            }
            else if (btnText.Equals("9"))
            {
                button5.BackColor = button5.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button6.BackColor = button6.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button8.BackColor = button8.BackColor == Color.Red ? Color.Yellow : Color.Red;
                button9.BackColor = button9.BackColor == Color.Red ? Color.Yellow : Color.Red;
            }
            result = button5.BackColor == Color.Yellow
                && button1.BackColor==Color.Red
                && button2.BackColor==Color.Red
                && button3.BackColor==Color.Red
                && button4.BackColor==Color.Red
                && button6.BackColor==Color.Red
                && button7.BackColor==Color.Red
                && button8.BackColor==Color.Red
                && button9.BackColor==Color.Red;
            if (result)
            {
                MessageBox.Show("你赢了!!!!!!!!");
                this.button10.Enabled = true;
                setButton(false);
            }
        }

        private void Nine_Load(object sender, EventArgs e)
        {
            setButton(false);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            //自身不可用
            this.button10.Enabled = false;
            setButton(true);
            //恢复按钮的背景颜色为初始颜色
            this.button1.BackColor = Control.DefaultBackColor;
            this.button2.BackColor = Control.DefaultBackColor;
            this.button3.BackColor = Control.DefaultBackColor;
            this.button4.BackColor = Control.DefaultBackColor;
            this.button5.BackColor = Control.DefaultBackColor;
            this.button6.BackColor = Control.DefaultBackColor;
            this.button7.BackColor = Control.DefaultBackColor;
            this.button8.BackColor = Control.DefaultBackColor;
            this.button9.BackColor = Control.DefaultBackColor;           
        }
        //设置按钮的交互状态,是否可用
        public void setButton(bool en)
        {
            this.button1.Enabled = en;
            this.button2.Enabled = en;
            this.button3.Enabled = en;
            this.button4.Enabled = en;
            this.button5.Enabled = en;
            this.button6.Enabled = en;
            this.button7.Enabled = en;
            this.button8.Enabled = en;
            this.button9.Enabled = en;
        }
    }
}