|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- namespace Paixu
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- const int N = 4;//按钮的行数与列数
- Button[,] buttons_creat = new Button[N, N];
- /*规定按钮控件的具体情况,为二维数组
- * new Button[N,N]是表示的个数,不要搞错了!为4*4的按钮阵列
- */
- private void Form1_Load(object sender, EventArgs e)
- {
- GenerateAllButtons();
- }
-
- void GenerateAllButtons()
- {
- int x = 100, y = 10, w = 45, d = 50;
- for (int i = 0; i < N; i++)
- //先出的循环,i必然是大组级的个数
- for (int j = 0; j < N; j++)
-
- {//后出的循环,j小组级的个数
- int num = i * N + j;
- Button btn = new Button();
- btn.Text = (num + 1).ToString();
- btn.Top = y + d * i;
- btn.Left = x + d * j;//j+1,left增加50
- btn.Width = w;
- btn.Height = w;//方形按钮
- btn.Visible = true;//按钮设为可见
- btn.Tag = i * N + j;//这里把所有的按钮打上行列位置的标签
- btn.Click += new EventHandler(Btn_Click);
- //注册事件,点击btn就会触发btn_click事件
- buttons_creat[i, j] = btn;
- //先大组后小组,确定位置,放入按钮
- //这里的[i,j]不是数量,是确定位置,所以从0起
- this.Controls.Add(btn);
- //把按钮加入界面
-
- }
- buttons_creat[N - 1, N - 1].Visible = false;
- }
- //创建点击数字按钮,其与临近的空白交换的函数
- private void Btn_Click(object sender, EventArgs e)
- {
- Button btn = sender as Button;//事件发出者,被点击的按钮?
- Button blank = hidden_button();//调用空白按钮
- //调用临近函数
- if(neighbor(btn,blank))
- {
- Swap(btn, blank);//交换
- blank.Focus();//锁定blank
- }
- //判断是否完成?调用函数
- if(ok_no())
- {
- MessageBox.Show("OK");
- }
- }
- //创建Game win函数
- bool ok_no()
- {
- for (int i = 0; i < N; i++)
- //先出的循环,i必然是大组级的个数
- for (int j = 0; j < N; j++)
- {//后出的循环,j小组级的个数
- if (buttons_creat[i, j].Text != (i * N + j + 1).ToString()) ;
- {
- return false;
- }
- }
- return true;
- }
- //创建相邻函数
- bool neighbor(Button btn_a,Button btn_b)
- {
- int a = (int)btn_a.Tag;//Tag=i * N + j,i是行,j为列
- int b = (int)btn_b.Tag;
- int i1 = a / N, j1 = a % N;
- int i2 = b / N, j2 = b % N;
- //如何判断相邻呢,行号相等,列号差1
- //或列号相等,行号差1
- if ((i1 == i2 && (j1 == j2 - 1 || j1 == j2 + 1))
- || (j1 == j2 && (i1 == i2 - 1 || i1 == i2 + 1)));
- return true;
- return false;
- }
- //创建调用空白的函数,遍历16个按钮,
- //返回不可见的按钮或null
- Button hidden_button()
- {
- for(int i=0;i<N;i++)
-
- for(int j=0;j<N;j++)
-
- if(buttons_creat[i, j].Visible == false)
- {
- return buttons_creat[i, j];
- }
-
-
- return null;
- }
- //创建打乱顺序这个函数
- void Daluan_shunxun()
- {
- //多次随机交换
- Random rnd = new Random();
- for (int i =0; i <100;i++)
- {
- int a = rnd.Next(N);
- /* random.Next()返回非负的一个随机数
- random.Next(MaxValue)返回小于最大值的随机数
- (minValue , maxValue )*/
- int b = rnd.Next(N);
- int c = rnd.Next(N);
- int d = rnd.Next(N);
- Swap(buttons_creat[a, b], buttons_creat[c, d]);
- //运行交换函数,这个函数需要自己写
- }
- }
- //创建交换函数
- void Swap(Button btn_a,Button btn_b)
- {
- string t = btn_a.Text;//存储a值
- btn_a.Text = btn_b.Text;//把b值附给a
- btn_b.Text = t;//把a值赋给b
- //考虑16号的隐藏情况,可见性也要进行调换
- bool v = btn_a.Visible;
- btn_a.Visible = btn_b.Visible;
- btn_b.Visible = v;
- }
-
-
- private void button1_Click(object sender, EventArgs e)
- {
- //运行打乱顺序
- Daluan_shunxun();
- }
-
- }
- }
复制代码
其他都没有啥问题,但就是,现在任何按钮都可以和隐藏按钮交换,自己查了几遍没有看出neighbor函数有啥问题... |
|