C# 九宫格游戏

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;
        }
    }
}

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值