栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

思路:

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

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

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

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

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

涉及知识点:

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

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

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

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

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

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

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

核心代码如下:

/// 
  /// 连连看帮助类
  /// 
  public class linkHelper
  {
    /// 
    /// 连连看,看板
    /// 
    public int[,] linkBoard { get; set; }
    /// 
    /// 连线成功事件
    /// 
    public event EventHandler SucClick;
    /// 
    /// 连接失败事件
    /// 
    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;
      }
    }
    /// 
    /// 尝试连线
    /// 
    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);
 }
      }
    }
    /// 
    /// 是否赋值
    /// 
    /// 
    /// 
    public bool IsChecked(Point p)
    {
      bool flag = false;
      if (p.X != -1 && p.Y != -1)
      {
 flag = true;
      }
      return flag;
    }
    #region 核心算法
    /// 
    /// 判断是否连线成功
    /// 
    /// 第一个点击对象
    /// 第二个点击对象
    /// 
    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;
      }
    }
    /// 
    /// 水平连线
    /// 
    /// 
    /// 
    /// 
    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;
    }
    /// 
    /// 垂直连线
    /// 
    /// 
    /// 
    /// 
    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;
    }
    /// 
    /// 一个拐角
    /// 
    /// 
    /// 
    /// 
    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;
      }
    }
    /// 
    /// 两个拐角
    /// 
    /// 
    /// 
    /// 
    private bool twoCorner(Point a, Point b)
    {
      List 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;
    }
    /// 
    /// 扫描A与B之间的连接点组成的线
    /// 
    /// 
    /// 
    /// 
    private List scan(Point a, Point b)
    {
      List linkList = new List();
      //检测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# 实现连连看功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123341.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号