思路:
涉及知识点:
效果图图下(一)【开始,初始化后,倒计时功能,停止功能】:
效果图(二)【时间结束】
核心代码如下:
class="code_img_closed" src="/Upload/Images/2017070305/0015B68B3C38AA5B.gif" alt="">1 /// <summary> 2 /// 连连看帮助类 3 /// </summary> 4 public class LinkHelper 5 { 6 /// <summary> 7 /// 连连看,看板 8 /// </summary> 9 public int[,] LinkBoard { get; set; } 10 11 /// <summary> 12 /// 连线成功事件 13 /// </summary> 14 public event EventHandler SucClick; 15 16 /// <summary> 17 /// 连接失败事件 18 /// </summary> 19 public event EventHandler FailClick; 20 21 private int col = 10; 22 23 public int Col 24 { 25 get 26 { 27 return col; 28 } 29 30 set 31 { 32 col = value; 33 } 34 } 35 36 private int row = 10; 37 38 public int Row 39 { 40 get 41 { 42 return row; 43 } 44 45 set 46 { 47 row = value; 48 } 49 } 50 51 /// <summary> 52 /// 尝试连线 53 /// </summary> 54 public void LinkLine(Point first, Point second) 55 { 56 EventArgs e = new EventArgs(); 57 if (checkLink(first, second)) 58 { 59 //连线成功 60 this.LinkBoard[first.X, first.Y] = 0; 61 this.LinkBoard[second.X, second.Y] = 0; 62 if (this.SucClick != null) 63 { 64 SucClick(this, e); 65 } 66 } 67 else { 68 //连线失败 69 if (this.FailClick != null) 70 { 71 FailClick(this, e); 72 } 73 } 74 } 75 76 /// <summary> 77 /// 是否赋值 78 /// </summary> 79 /// <param name="p"></param> 80 /// <returns></returns> 81 public bool IsChecked(Point p) 82 { 83 bool flag = false; 84 if (p.X != -1 && p.Y != -1) 85 { 86 flag = true; 87 } 88 return flag; 89 } 90 91 #region 核心算法 92 93 /// <summary> 94 /// 判断是否连线成功 95 /// </summary> 96 /// <param name="a">第一个点击对象</param> 97 /// <param name="b">第二个点击对象</param> 98 /// <returns></returns> 99 private bool checkLink(Point a, Point b) 100 { 101 if (!Point.Equals(a, b)) 102 { 103 if (this.LinkBoard[a.X, a.Y] == this.LinkBoard[b.X, b.Y]) 104 { 105 if (a.X == b.X && horizon(a, b)) 106 { 107 return true; 108 } 109 if (a.Y == b.Y && vertical(a, b)) 110 { 111 return true; 112 } 113 if (oneCorner(a, b)) 114 { 115 return true; 116 } 117 else 118 { 119 return twoCorner(a, b); 120 } 121 } 122 else { 123 //如果点击的不是同一个图案,直接返回false 124 return false; 125 } 126 } 127 else { 128 //如果点击的是同一个位置的图案,直接返回false; 129 return false; 130 } 131 } 132 133 /// <summary> 134 /// 水平连线 135 /// </summary> 136 /// <param name="a"></param> 137 /// <param name="b"></param> 138 /// <returns></returns> 139 private bool horizon(Point a, Point b) 140 { 141 int col_start = a.Y < b.Y ? a.Y : b.Y; //获取a,b中较小的y值 142 int col_end = a.Y < b.Y ? b.Y : a.Y; //获取a,b中较大的值 143 144 //遍历a,b之间是否通路,如果一个不是就返回false; 145 for (int i = col_start + 1; i < col_end; i++) 146 { 147 if (this.LinkBoard[a.X, i] != 0) 148 { 149 return false; 150 } 151 } 152 return true; 153 } 154 155 /// <summary> 156 /// 垂直连线 157 /// </summary> 158 /// <param name="a"></param> 159 /// <param name="b"></param> 160 /// <returns></returns> 161 private bool vertical(Point a, Point b) 162 { 163 int row_start = a.X < b.X ? a.X : b.X; 164 int row_end = a.X < b.X ? b.X : a.X; 165 for (int i = row_start + 1; i < row_end; i++) 166 { 167 if (this.LinkBoard[i, a.Y] != 0) 168 { 169 return false; 170 } 171 } 172 return true; 173 } 174 175 /// <summary> 176 /// 一个拐角 177 /// </summary> 178 /// <param name="a"></param> 179 /// <param name="b"></param> 180 /// <returns></returns> 181 private bool oneCorner(Point a, Point b) 182 { 183 Point c = new Point(b.X, a.Y); 184 Point d = new Point(a.X, b.Y); 185 //判断C点是否有元素 186 if (this.LinkBoard[c.X, c.Y] == 0) 187 { 188 bool path1 = horizon(b, c) && vertical(a, c); 189 return path1; 190 } 191 //判断D点是否有元素 192 if (this.LinkBoard[d.X, d.Y] == 0) 193 { 194 bool path2 = horizon(a, d) && vertical(b, d); 195 return path2; 196 } 197 else 198 { 199 return false; 200 } 201 } 202 203 /// <summary> 204 /// 两个拐角 205 /// </summary> 206 /// <param name="a"></param> 207 /// <param name="b"></param> 208 /// <returns></returns> 209 private bool twoCorner(Point a, Point b) 210 { 211 List<Line> ll = scan(a, b); 212 if (ll.Count == 0) 213 { 214 return false; 215 } 216 for (int i = 0; i < ll.Count; i++) 217 { 218 Line tmpLine = ll[i]; 219 if (tmpLine.direct == 1) 220 { 221 222 if (vertical(a, tmpLine.a) && vertical(b, tmpLine.b)) 223 { 224 return true; 225 } 226 } 227 else if (tmpLine.direct == 0) 228 { 229 if (horizon(a, tmpLine.a) && horizon(b, tmpLine.b)) 230 { 231 return true; 232 } 233 } 234 } 235 return false; 236 } 237 238 /// <summary> 239 /// 扫描A与B之间的连接点组成的线 240 /// </summary> 241 /// <param name="a"></param> 242 /// <param name="b"></param> 243 /// <returns></returns> 244 private List<Line> scan(Point a, Point b) 245 { 246 List<Line> linkList = new List<Line>(); 247 //检测a点,b点的左侧是否能够垂直直连 248 for (int i = a.Y; i >= 0; i--) 249 { 250 if (this.LinkBoard[a.X, i] == 0 && this.LinkBoard[b.X, i] == 0 && vertical(new Point(a.X, i), new Point(b.X, i))) 251 { 252 linkList.Add(new Line(new Point(a.X, i), new Point(b.X, i), 0)); 253 } 254 } 255 //检测a点,b点的右侧是否能够垂直直连 256 for (int i = a.Y; i < Col; i++) 257 { 258 if (this.LinkBoard[a.X, i] == 0 && this.LinkBoard[b.X, i] == 0 && vertical(new Point(a.X, i), new Point(b.X, i))) 259 { 260 linkList.Add(new Line(new Point(a.X, i), new Point(b.X, i), 0)); 261 } 262 } 263 //检测a点,b点的上侧是否能够水平直连 264 for (int j = a.X; j >= 0; j--) 265 { 266 if (this.LinkBoard[j, a.Y] == 0 && this.LinkBoard[j, b.Y] == 0 && horizon(new Point(j, a.Y), new Point(j, b.Y))) 267 { 268 linkList.Add(new Line(new Point(j, a.Y), new Point(j, b.Y), 1)); 269 } 270 } 271 //检测a点,b点的下侧是否能够水平直连 272 for (int j = a.X; j < Row; j++) 273 { 274 if (this.LinkBoard[j, a.Y] == 0 && this.LinkBoard[j, b.Y] == 0 && horizon(new Point(j, a.Y), new Point(j, b.Y))) 275 { 276 linkList.Add(new Line(new Point(j, a.Y), new Point(j, b.Y), 1)); 277 278 } 279 } 280 281 return linkList; 282 } 283 284 #endregion 285 }logs_code_collapse">View Code