• 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 数码游戏
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            const int N = 3;  //按钮的行,列数
            Button[,] buttons = new Button[N, N]; //按钮的数组
    
            /*
             * 产生按钮
             */ 
            private void Form1_Load(object sender, EventArgs e)
            {
                //产生所有按钮
                GenerateAllButtons();
            }
            private void btnStart_Click(object sender, EventArgs e)
            {
                //MessageBox.Show("Hello");
                //打乱顺序
                Shuffle();
            }
    
            /*
             * 生产界面
             */
            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;
                        //记录在tag上,这里记录行列
                        btn.Tag = r * N + c;
    
                        //注册事件--给btnStart_Click这个按钮注册事件
                        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)
            {
                //当前点中的按钮, 事件源
                Button btn = sender as Button;
                Button blank = FindHiddenButton(); //空白按钮
    
                //判断是否与空白块相邻,如果是,则交换
                if (IsNeighbor(btn, blank))
                {
                    Swap(btn, blank);
                    blank.Focus();
                }
    
                //判断是否完成了
                if (ResultIsOk())
                {
                    MessageBox.Show("YOU WIN !");
                }
            }
    
            //打乱顺序
            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;
            }
    
            //查找要隐藏的按钮
           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;  //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;
            }
            
            /*
             * 下面实现自动寻路算法
             */ 
            private void btnSolve_Click(object sender, EventArgs e)
            {
    
            }
    
        }
    }

  • 相关阅读:
    计算机网络(1)----概述
    博客园自定义样式
    linux进程
    接口回调解析
    优先级队列
    双栈实现队列
    递归解决反转链表的一部分
    Multisim 之逻辑转换仪
    Multisim 如何添加文本 如何编辑文本字体
    Multisim 中的一些快捷键
  • 原文地址:https://www.cnblogs.com/douzujun/p/6554326.html
Copyright © 2020-2023  润新知