C#排块游戏

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

namespace tuokuaiGame
{
    public partial class Form1 : Form
    {
        const int N = 4;//按钮的行列数
        Button[,] buttons = new Button[N, N];//按钮的数组
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //产生所有按钮
            GenerateAllButtons();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //打乱顺序
            Shuffle();
        }
        void Shuffle()
        {
            Random rnd = new Random();
            for(int i = 0; i < 100; i++)
            {
                int a = rnd.Next(N);
                int b = rnd.Next(N);
                int c = rnd.Next(N);
                int d = rnd.Next(N);
                Swap(buttons[a, b], buttons[c, d]);
            }
        }
        //交换两个按钮
        void Swap(Button btna,Button btnb)
        {
            string t = btna.Text;
            btna.Text = btnb.Text;
            btnb.Text = t;
            bool v = btna.Visible;
            btna.Visible = btnb.Visible;
            btnb.Visible = v;
        }
        void GenerateAllButtons()
        {
            int x0 = 100, y0 = 10, w = 45, d = 50;
            for(int r=0;r<N;r++)
                for(int c = 0; c < N; c++)
                {
                    int num = r * N + c;
                    Button btn = new Button();
                    btn.Text = (num + 1).ToString();
                    btn.Top = y0 + r * d;
                    btn.Left = x0 + c * d;
                    btn.Width = w;
                    btn.Height = w;
                    btn.Visible = true;
                    btn.Tag = r * N + c;
                    btn.Click += Btn_Click;
                    buttons[r, c] = btn;//放到数组中
                    this.Controls.Add(btn);//放到界面上
                }
            buttons[N - 1, N - 1].Visible = false;//最后一个按钮看不见
        }
        void Btn_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            Button btn = sender as Button;
            Button blank = FindHiddenButton();//空白按钮
            if (IsNeighbor(btn, blank))//判断是否与空白块相邻,如果是,则交换
            {
                Swap(btn, blank);
                blank.Focus();
            }
            if (ResultIsOk())
            {
                MessageBox.Show("ok");
            }
        }
        //查找要隐藏的按钮
        Button FindHiddenButton()
        {
            for(int r=0;r<N;r++)
                for(int c = 0; c < N; c++)
                {
                    if (!buttons[r, c].Visible)
                        return buttons[r, c];
                }
            return null;
        }
        //判断是否相邻
        bool IsNeighbor(Button btnA,Button btnB)
        {
            int a = (int)btnA.Tag;
            int b = (int)btnB.Tag;
            int r1 = a / N, c1 = a % N;
            int r2 = b / N, c2 = b % N;
            if(r1==r2&&(c1==c2-1||c1==c2+1)//左右相邻,行号相等,列号差一
                ||c1==c2&&(r1==r2-1||r1==r2+1))//上下相邻,列号相等,行号差一
                    return true;
            return false;
        }
        //检查是否完成
        bool ResultIsOk()
        {
            for(int r=0;r<N;r++)
                for(int c = 0; c < N; c++)
                {
                    if (buttons[r, c].Text != (r * N + c + 1).ToString())
                        return false;
                }
            return true;
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值