好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用C#编写15子游戏

本文实例为大家分享了c#15子游戏的实现代码,供大家参考,具体内容如下

所需控件:一个button,拖入form1中即可。
源码:

?

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 shiwuzhigame

{

   public partial class form1 : form

   {

     public form1()

     {

       initializecomponent();

     }

 

     const int n = 4; //按钮的行、列数

     button[,] buttons = new button[n, n]; //按钮的数组

 

     private void form1_load( object sender, eventargs e)

     {

       //产生所有按钮

       generateallbuttons();

     }

     //打乱顺序

     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 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 += new eventhandler(btn_click);

 

           buttons[r, c] = btn; //放到数组中

           this .controls.add(btn); //加到界面上

         }

       buttons[n - 1, n - 1].visible = false ; //最后一个不可见

     }

 

     //交换两个按钮

     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 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( "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; //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 button1_click_1( object sender, eventargs e)

     {

       //打乱顺序

       shuffle();

     }

   }

}

运行效果如下:

程序启动时:

点击开始后:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

dy("nrwz");

查看更多关于使用C#编写15子游戏的详细内容...

  阅读:42次