好得很程序员自学网

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

C# 实现连连看功能(推荐)

本文是利用C#实现连连看的小例子,以供学习分享使用。

思路:

初始化布局(横竖十行十列,共100个单元格,每一个格一个按钮,背景图为水果图片,随机生成) 。

初始化对应棋盘(用二维数组表示【0表示空白,非0表示界面对象】)和页面相对应,同步操作。

判断点击的图片是否可以消掉(转化为二维数组【以水平方向,垂直方向,一个拐角,两个拐角的步骤进行判断】)。

如可以消掉,隐藏图片,增加分数。

时间限制,采用倒计时方式。

涉及知识点:

线程:Thread,后台运行时间控制【倒计时方式】。

界面闪烁:当界面中的控件较多,且有背景图时,界面就会出现闪烁【解决方式:1,双缓冲方式 2. 设置控件创建样式,统一刷新】。

TableLayoutPanel:表示一个面板,它可以在一个由行和列组成的网格中对其内容进行动态布局【新增元素,设置行列,以及样式】。

资源文件:Resources 用于存放图片及其他资源。

Button:FlatAppearance获取用于指示选中状态和鼠标状态的边框外观和颜色。

效果图图下(一)【开始,初始化后,倒计时功能,停止功能】:

效果图(二)【时间结束】

核心代码如下:

?

/// <summary>

   /// 连连看帮助类

   /// </summary>

   public class LinkHelper

   {

     /// <summary>

     /// 连连看,看板

     /// </summary>

     public int [,] LinkBoard { get ; set ; }

     /// <summary>

     /// 连线成功事件

     /// </summary>

     public event EventHandler SucClick;

     /// <summary>

     /// 连接失败事件

     /// </summary>

     public event EventHandler FailClick;

     private int col = 10;

     public int Col

     {

       get

       {

         return col;

       }

       set

       {

         col = value;

       }

     }

     private int row = 10;

     public int Row

     {

       get

       {

         return row;

       }

       set

       {

         row = value;

       }

     }

     /// <summary>

     /// 尝试连线

     /// </summary>

     public void LinkLine(Point first, Point second)

     {

       EventArgs e = new EventArgs();

       if (checkLink(first, second))

       {

         //连线成功

         this .LinkBoard[first.X, first.Y] = 0;

         this .LinkBoard[second.X, second.Y] = 0;

         if ( this .SucClick != null )

         {

           SucClick( this , e);

         }

       }

       else {

         //连线失败

         if ( this .FailClick != null )

         {

           FailClick( this , e);

         }

       }

     }

     /// <summary>

     /// 是否赋值

     /// </summary>

     /// <param name="p"></param>

     /// <returns></returns>

     public bool IsChecked(Point p)

     {

       bool flag = false ;

       if (p.X != -1 && p.Y != -1)

       {

         flag = true ;

       }

       return flag;

     }

     #region 核心算法

     /// <summary>

     /// 判断是否连线成功

     /// </summary>

     /// <param name="a">第一个点击对象</param>

     /// <param name="b">第二个点击对象</param>

     /// <returns></returns>

     private bool checkLink(Point a, Point b)

     {

       if (!Point.Equals(a, b))

       {

         if ( this .LinkBoard[a.X, a.Y] == this .LinkBoard[b.X, b.Y])

         {

           if (a.X == b.X && horizon(a, b))

           {

             return true ;

           }

           if (a.Y == b.Y && vertical(a, b))

           {

             return true ;

           }

           if (oneCorner(a, b))

           {

             return true ;

           }

           else

           {

             return twoCorner(a, b);

           }

         }

         else {

           //如果点击的不是同一个图案,直接返回false

           return false ;

         }

       }

       else {

         //如果点击的是同一个位置的图案,直接返回false;

         return false ;

       }

     }

     /// <summary>

     /// 水平连线

     /// </summary>

     /// <param name="a"></param>

     /// <param name="b"></param>

     /// <returns></returns>

     private bool horizon(Point a, Point b)

     {

       int col_start = a.Y < b.Y ? a.Y : b.Y;    //获取a,b中较小的y值

       int col_end = a.Y < b.Y ? b.Y : a.Y;     //获取a,b中较大的值

       //遍历a,b之间是否通路,如果一个不是就返回false;

       for ( int i = col_start + 1; i < col_end; i++)

       {

         if ( this .LinkBoard[a.X, i] != 0)

         {

           return false ;

         }

       }

       return true ;

     }

     /// <summary>

     /// 垂直连线

     /// </summary>

     /// <param name="a"></param>

     /// <param name="b"></param>

     /// <returns></returns>

     private bool vertical(Point a, Point b)

     {

       int row_start = a.X < b.X ? a.X : b.X;

       int row_end = a.X < b.X ? b.X : a.X;

       for ( int i = row_start + 1; i < row_end; i++)

       {

         if ( this .LinkBoard[i, a.Y] != 0)

         {

           return false ;

         }

       }

       return true ;

     }

     /// <summary>

     /// 一个拐角

     /// </summary>

     /// <param name="a"></param>

     /// <param name="b"></param>

     /// <returns></returns>

     private bool oneCorner(Point a, Point b)

     {

       Point c = new Point(b.X, a.Y);

       Point d = new Point(a.X, b.Y);

       //判断C点是否有元素       

       if ( this .LinkBoard[c.X, c.Y] == 0)

       {

         bool path1 = horizon(b, c) && vertical(a, c);

         return path1;

       }

       //判断D点是否有元素

       if ( this .LinkBoard[d.X, d.Y] == 0)

       {

         bool path2 = horizon(a, d) && vertical(b, d);

         return path2;

       }

       else

       {

         return false ;

       }

     }

     /// <summary>

     /// 两个拐角

     /// </summary>

     /// <param name="a"></param>

     /// <param name="b"></param>

     /// <returns></returns>

     private bool twoCorner(Point a, Point b)

     {

       List<Line> ll = scan(a, b);

       if (ll.Count == 0)

       {

         return false ;

       }

       for ( int i = 0; i < ll.Count; i++)

       {

         Line tmpLine = ll[i];

         if (tmpLine.direct == 1)

         {

           if (vertical(a, tmpLine.a) && vertical(b, tmpLine.b))

           {

             return true ;

           }

         }

         else if (tmpLine.direct == 0)

         {

           if (horizon(a, tmpLine.a) && horizon(b, tmpLine.b))

           {

             return true ;

           }

         }

       }

       return false ;

     }

     /// <summary>

     /// 扫描A与B之间的连接点组成的线

     /// </summary>

     /// <param name="a"></param>

     /// <param name="b"></param>

     /// <returns></returns>

     private List<Line> scan(Point a, Point b)

     {

       List<Line> linkList = new List<Line>();

       //检测a点,b点的左侧是否能够垂直直连

       for ( int i = a.Y; i >= 0; i--)

       {

         if ( this .LinkBoard[a.X, i] == 0 && this .LinkBoard[b.X, i] == 0 && vertical( new Point(a.X, i), new Point(b.X, i)))

         {

           linkList.Add( new Line( new Point(a.X, i), new Point(b.X, i), 0));

         }

       }

       //检测a点,b点的右侧是否能够垂直直连

       for ( int i = a.Y; i < Col; i++)

       {

         if ( this .LinkBoard[a.X, i] == 0 && this .LinkBoard[b.X, i] == 0 && vertical( new Point(a.X, i), new Point(b.X, i)))

         {

           linkList.Add( new Line( new Point(a.X, i), new Point(b.X, i), 0));

         }

       }

       //检测a点,b点的上侧是否能够水平直连

       for ( int j = a.X; j >= 0; j--)

       {

         if ( this .LinkBoard[j, a.Y] == 0 && this .LinkBoard[j, b.Y] == 0 && horizon( new Point(j, a.Y), new Point(j, b.Y)))

         {

           linkList.Add( new Line( new Point(j, a.Y), new Point(j, b.Y), 1));

         }

       }

       //检测a点,b点的下侧是否能够水平直连

       for ( int j = a.X; j < Row; j++)

       {

         if ( this .LinkBoard[j, a.Y] == 0 && this .LinkBoard[j, b.Y] == 0 && horizon( new Point(j, a.Y), new Point(j, b.Y)))

         {

           linkList.Add( new Line( new Point(j, a.Y), new Point(j, b.Y), 1));

         }

       }

       return linkList;

     }

     #endregion

   }

以上所述是小编给大家介绍的C# 实现连连看功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://HdhCmsTestcnblogs测试数据/hsiang/archive/2017/07/03/7108274.html

dy("nrwz");

查看更多关于C# 实现连连看功能(推荐)的详细内容...

  阅读:37次