首页 > 作文

C#实现数字华容道游戏

更新时间:2023-04-05 01:39:14 阅读: 评论:0

本文实例为大家分享了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 windowsformsapp6{undefined  public partial class form1 : form  {undefined    public form1()    {undefined      initializecomponent();    }    const int n = 4;    button[,] buttons = new button[n, n];    private void form1_load(object nder, event一对一高中补课args e)    {undefined      //产生所有按钮      generateallbuttons();    }    private void button1_click(object nder, eventargs e)    {undefined      shuffle();    }    //打乱顺序    void shuffle()    {undefined      //多次随机交换两个按钮      random rnd = new random();      for (int i=0;i<100;i++)      {undefined        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 generateallbuttons()    {undefined      int x0 = 100, y0 = 10, w = 45,刘萌萌 d = 50;      for(int r=0;r<n;r++)        for(int c = 0; c < n; c++)        {undefined    继父母      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 += new eventhandler(btn_click);          buttons[r, c] = btn;          this.controls.add(btn);        }      buttons[n - 1, n - 1].visible = fal;//最后一个不可见    }    //交换两个按钮    void swap(button btna,button btnb)    {undefined      string t 幼儿园中班主题活动= btna.text;      btna.text = btnb.text;      btnb.text = t;      bool v = btna.visible;      btna.visible = btnb.visible;      btnb.visible = v;    }    //按钮点击事件处理    void btn_click(object nder, eventargs e)    {undefined      button btn = nder as button;//当前点中按钮      button blank= findhiddenbutton();//空白按钮      //判断与空白按钮是否相邻,如果是,交换      if (isneighbor(btn, blank))      {undefined        swap(btn, blank);        blank.focus();      }      //判断是否完成了      if (resultisok())      {undefined        messagebox.show("ok");      }    }    //查找要隐藏的按钮    button findhiddenbutton()    {undefined      for (int r = 0; r < n; r++)        for (int c = 0; c < n; c++)        {undefined          if (!buttons[r, c].visible)          {undefined            return buttons[r工作难找怎么办, c];          }        }      return null;    }    //判断是否相邻    bool isneighbor(button btna, button btnb)    {undefined      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 fal;    }    //检查是否完成    bool resultisok()    {undefined      for (int r = 0; r < n; r++)        for (int c = 0; c < n; c++)        {undefined          if (buttons[r, c].text != (r * n + c + 1).tostring())          {undefined            return fal;          }        }      return true;    }  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

本文发布于:2023-04-05 01:39:13,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/062002e4a08d9fa3842d6a1a358d50ca.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:C#实现数字华容道游戏.doc

本文 PDF 下载地址:C#实现数字华容道游戏.pdf

标签:按钮   华容道   行列   判断是否
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图