C#俄罗斯方块小游戏程序设计与简单实现

C#俄罗斯方块小游戏程序设计与简单实现

相信90后或者80后都玩过这款小游戏,一直想干一票,琢磨一下,但又不太懂,于是网上搜集修改就有了以下效果!bug较多,多多包涵!

1.效果展示

2.实现方法

参考https://blog.csdn.net/qian_f/article/details/19758671 感谢博主分享,我在这里也没修改啥,有时间修复几个bug

2.1对象分析

把每个砖块当成一个对象。每个砖块都有共同的行为,就是可以左移、 右移、下移和变形。既然这是他们共同的行为(方法),那么可以定义一个虚基类Brick,然后在该基类中声明这些行为。当然,砖块在做这些行为前需要知道能不能进行这些行为,比如说到了左边界就不能左移;到了下边界就不能下移;周围空间不够大,就不能变形等等。因此该基类还需要声明一些虚函数:CanTransform() CanLeftMove() CanRightMove() CanDropMove()等。

2.2继承实现  

继承定义的基类,每种砖块根据自身的形状具体实现相应函数。据说在标准的俄罗斯方块中,一共有七种形状。本练习项目中定义的方块和变形方式(绕着中心点顺时针旋转,途中颜色较深的点就是中心点)如下:

根据上图就可以知道,表示砖块最好的方法就是用二维数组了。对于砖块而言,这个二维数组就是它的变形范围,数组中的数字为0,代表砖块在该区域中无显示,为1代表有显示。在实现CanTransform() CanLeftMove() CanRightMove() CanDropMove()这四个函数时,要尤其小心,这边是最容易出错的地方。

2.3画布处理

完成砖块下面就要进行画布的处理了。可以想象一下,把画布分成多个方格,也就相当于二维数组了,然后把砖块所对应的二维数组按指定的位置放到代表画布的二维数组中。在显示的时候就可以根据值为1的方格来获取位置并进行绘图了。所以,该项目中定义了一个名为Canvas的类,核心功能是用于获取这个二维数组的值,其中包含根据砖块设置数组的值、行满(一行里所有的值都为1)之后消除、超出高度后返回失败等。

2.4绘图

真正的绘图操作。根据二维数组的值绘制显示,并响应方向键操作。

3.代码实现

Block基类

  1     public abstract class Block
  2     {
  3         protected int _curChangeTimes;  //变化次数
  4         public int _needRows;           //行数
  5         public int _needColumns;        //列数
  6         public int[,] _range;           //变化范围
  7         public Point _center;           //中心点 相对于必要区域
  8         public Point _Pos;              //中心点位置,相对于画布
  9 
 10 
 11         /// <summary>
 12         /// 能否变形 ,能变形的条件为在方块的变形范围内不能有其它的方块
 13         /// </summary>
 14         /// <param name="arr"></param>
 15         /// <param name="rows"></param>
 16         /// <param name="cloumns"></param>
 17         /// <returns></returns>
 18         public abstract bool CanChange(int[,] arr ,int rows,int cloumns);
 19 
 20         /// <summary>
 21         /// 变形
 22         /// </summary>
 23         public abstract void Change();
 24 
 25         /// <summary>
 26         /// 能否左移动
 27         /// </summary>
 28         /// <param name="arr"></param>
 29         /// <param name="rows"></param>
 30         /// <param name="columns"></param>
 31         /// <returns></returns>
 32         public abstract bool CanLeftMove(int[,] arr,int rows,int columns);
 33 
 34         /// <summary>
 35         /// 左移
 36         /// </summary>
 37         public void LeftMove()
 38         {
 39             _Pos.Y -= 1;
 40         }
 41 
 42         /// <summary>
 43         /// 能否右移
 44         /// </summary>
 45         /// <param name="arr"></param>
 46         /// <param name="rows"></param>
 47         /// <param name="columns"></param>
 48         /// <returns></returns>
 49         public abstract bool CanRightMove(int[,] arr, int rows, int columns);
 50 
 51 
 52         /// <summary>
 53         /// 右侧移动
 54         /// </summary>
 55         public void RightMove()
 56         {
 57             _Pos.Y += 1;
 58         }
 59 
 60         /// <summary>
 61         /// 能否下移
 62         /// </summary>
 63         /// <param name="arr"></param>
 64         /// <param name="rows"></param>
 65         /// <param name="columns"></param>
 66         /// <returns></returns>
 67         public abstract bool CanDownMove(int[,] arr, int rows, int columns);
 68 
 69 
 70         /// <summary>
 71         /// 下侧移动
 72         /// </summary>
 73         public void DownMove()
 74         {
 75             _Pos.X += 1;
 76         }
 77 
 78 
 79         /// <summary>
 80         /// 随机生成一个可以通过变形得到的形状
 81         /// </summary>
 82         public void RandomShape()
 83         {
 84             Random random = new Random();
 85             this._curChangeTimes = random.Next(4);
 86             this.Change();
 87         }
 88 
 89         /// <summary>
 90         /// 设置中心点相对于画布的位置
 91         /// </summary>
 92         /// <param name="x">横向位置</param>
 93         /// <param name="y">纵向位置</param>
 94         public void SetCenterPos(int x, int y)
 95         {
 96             this._Pos = new Point(x, y);
 97         }
 98 
 99         /// <summary>
100         /// 获取方块出现时中心点的Y轴坐标
101         /// </summary>
102         /// <returns></returns>
103         public abstract int Appear();
104     }
View Code

Block1类

 1     class Block1:Block
 2     {
 3         public Block1()
 4         {
 5             this._curChangeTimes = 0;
 6             this._needRows = 2;
 7             this._needColumns = 2;
 8             _range = new int[2, 2]{
 9                                     {1,1},
10                                     {1,1} 
11                                   };
12 
13             this._center = new Point(0,0);
14             this._Pos = new Point();
15         }
16 
17         public override bool CanChange(int[,] arr, int rows, int cloumns)
18         {
19             return false;
20         }
21 
22         public override void Change()
23         {
24             return;
25         }
26 
27         public override bool CanLeftMove(int[,] arr, int rows, int columns)
28         {
29             if (_Pos.X < 0)
30             {
31                 if (_Pos.Y == 0 || arr[_Pos.X + 1, _Pos.Y - 1] == 1)
32                 {
33                     return false;
34                 }
35                 else
36                 {
37                     return true;
38                 }
39             }
40             if (_Pos.Y == 0 || arr[_Pos.X, _Pos.Y - 1] == 1 || arr[_Pos.X + 1, _Pos.Y - 1] == 1)
41                 return false;
42             else
43                 return true;
44         }
45 
46         public override bool CanRightMove(int[,] arr, int rows, int columns)
47         {
48             if (_Pos.X < 0)
49             {
50                 if (_Pos.Y == columns - 2 || arr[_Pos.X + 1, _Pos.Y + 2] == 1)
51                 {
52                     return false;
53                 }
54                 else
55                 {
56                     return true;
57                 }
58             }
59             if (_Pos.Y == columns - 2 || arr[_Pos.X, _Pos.Y + 2] == 1 || arr[_Pos.X + 1, _Pos.Y + 2] == 1)
60                 return false;
61             else
62                 return true;
63         }
64 
65         public override bool CanDownMove(int[,] arr, int rows, int columns)
66         {
67             if (_Pos.X < rows - 2 && arr[_Pos.X + 2, _Pos.Y] == 0 && arr[_Pos.X + 2, _Pos.Y + 1] == 0)
68                 return true;
69             return false;
70         }
71 
72         public override int Appear()
73         {
74             return -1;
75         }
76     }
View Code

Block2类

  1     class Block2:Block
  2     {
  3         public Block2()
  4         {
  5             this._curChangeTimes = 0;
  6             this._needRows = 5;
  7             this._needColumns = 5;
  8             this._range = new int[5, 5]{{0,0,0,0,0},
  9                                          {0,0,0,0,0},
 10                                          {0,1,1,1,1},
 11                                          {0,0,0,0,0},
 12                                          {0,0,0,0,0}};
 13             this._center = new Point(2, 2);
 14         }
 15         public override bool CanChange(int[,] arr, int rows, int cloumns)
 16         {
 17             bool result = false;
 18             if (_Pos.X - 2 >= 0 && _Pos.X + 2 <= rows - 1 && _Pos.Y - 2 >= 0 && _Pos.Y + 2 <= cloumns - 1)
 19             {
 20                 switch (_curChangeTimes)
 21                 {
 22                     case 0:
 23                         arr[_Pos.X, _Pos.Y - 1] = 0;
 24                         arr[_Pos.X, _Pos.Y] = 0;
 25                         arr[_Pos.X, _Pos.Y + 1] = 0;
 26                         arr[_Pos.X, _Pos.Y + 2] = 0;
 27                         break;
 28                     case 1:
 29                         arr[_Pos.X - 1, _Pos.Y] = 0;
 30                         arr[_Pos.X, _Pos.Y] = 0;
 31                         arr[_Pos.X + 1, _Pos.Y] = 0;
 32                         arr[_Pos.X + 2, _Pos.Y] = 0;
 33                         break;
 34                     case 2:
 35                         arr[_Pos.X, _Pos.Y - 2] = 0;
 36                         arr[_Pos.X, _Pos.Y - 1] = 0;
 37                         arr[_Pos.X, _Pos.Y] = 0;
 38                         arr[_Pos.X, _Pos.Y + 1] = 0;
 39                         break;
 40                     case 3:
 41                         arr[_Pos.X - 2, _Pos.Y] = 0;
 42                         arr[_Pos.X - 1, _Pos.Y] = 0;
 43                         arr[_Pos.X, _Pos.Y] = 0;
 44                         arr[_Pos.X + 1, _Pos.Y] = 0;
 45                         break;
 46                     default:
 47                         break;
 48                 }
 49                 bool temp = true;
 50                 for (int i = -2; i < 3; i++)
 51                 {
 52                     for (int j = -2; j < 3; j++)
 53                     {
 54                         if (arr[_Pos.X + i, _Pos.Y + j] == 1)
 55                         {
 56                             temp = false;
 57                             goto break2;
 58                         }
 59                     }
 60                 }
 61             break2:
 62                 result = temp;
 63                 switch (_curChangeTimes)
 64                 {
 65                     case 0:
 66                         arr[_Pos.X, _Pos.Y - 1] = 1;
 67                         arr[_Pos.X, _Pos.Y] = 1;
 68                         arr[_Pos.X, _Pos.Y + 1] = 1;
 69                         arr[_Pos.X, _Pos.Y + 2] = 1;
 70                         break;
 71                     case 1:
 72                         arr[_Pos.X - 1, _Pos.Y] = 1;
 73                         arr[_Pos.X, _Pos.Y] = 1;
 74                         arr[_Pos.X + 1, _Pos.Y] = 1;
 75                         arr[_Pos.X + 2, _Pos.Y] = 1;
 76                         break;
 77                     case 2:
 78                         arr[_Pos.X, _Pos.Y - 2] = 1;
 79                         arr[_Pos.X, _Pos.Y - 1] = 1;
 80                         arr[_Pos.X, _Pos.Y] = 1;
 81                         arr[_Pos.X, _Pos.Y + 1] = 1;
 82                         break;
 83                     case 3:
 84                         arr[_Pos.X - 2, _Pos.Y] = 1;
 85                         arr[_Pos.X - 1, _Pos.Y] = 1;
 86                         arr[_Pos.X, _Pos.Y] = 1;
 87                         arr[_Pos.X + 1, _Pos.Y] = 1;
 88                         break;
 89                     default:
 90                         break;
 91                 }
 92             }
 93             return result;
 94         }
 95 
 96         public override void Change()
 97         {
 98             switch (_curChangeTimes)
 99             {
100                 case 0:
101                     _range = new int[5, 5]{{0,0,0,0,0},
102                                             {0,0,1,0,0},
103                                             {0,0,1,0,0},
104                                             {0,0,1,0,0},
105                                             {0,0,1,0,0}};
106                     _curChangeTimes = 1;
107                     break;
108                 case 1:
109                     _range = new int[5, 5]{{0,0,0,0,0},
110                                             {0,0,0,0,0},
111                                             {1,1,1,1,0},
112                                             {0,0,0,0,0},
113                                             {0,0,0,0,0}};
114                     _curChangeTimes = 2;
115                     break;
116                 case 2:
117                     _range = new int[5, 5]{{0,0,1,0,0},
118                                             {0,0,1,0,0},
119                                             {0,0,1,0,0},
120                                             {0,0,1,0,0},
121                                             {0,0,0,0,0}};
122                     _curChangeTimes = 3;
123                     break;
124                 case 3:
125                     _range = new int[5, 5]{{0,0,0,0,0},
126                                             {0,0,0,0,0},
127                                             {0,1,1,1,1},
128                                             {0,0,0,0,0},
129                                             {0,0,0,0,0}};
130                     _curChangeTimes = 0;
131                     break;
132                 default:
133                     break;
134             }
135         }
136 
137         public override bool CanLeftMove(int[,] arr, int rows, int columns)
138         {
139             bool result = false;
140             switch (_curChangeTimes)
141             {
142                 case 0:
143                     if (_Pos.Y > 1)
144                     {
145                         if (arr[_Pos.X, _Pos.Y - 2] == 0)
146                         {
147                             result = true;
148                         }
149                         else
150                         {
151                             result = false;
152                         }
153                     }
154                     break;
155                 case 1:
156                     if (_Pos.Y > 0)
157                     {
158                         bool temp = true;
159                         for (int i = -1; i < 3; i++)
160                         {
161                             if (_Pos.X + i < 0)
162                             {
163                                 continue;
164                             }
165                             else
166                             {
167                                 if (arr[_Pos.X + i, _Pos.Y - 1] == 1)
168                                 {
169                                     temp = false;
170                                     break;
171                                 }
172                             }
173                         }
174                         result = temp;
175                     }
176                     break;
177                 case 2:
178                     if (_Pos.Y > 2)
179                     {
180                         if (arr[_Pos.X, _Pos.Y - 3] == 0)
181                         {
182                             result = true;
183                         }
184                         else
185                         {
186                             result = false;
187                         }
188                     }
189                     break;
190                 case 3:
191                     if (_Pos.Y > 0)
192                     {
193                         bool temp = true;
194                         for (int i = -2; i < 2; i++)
195                         {
196                             if (_Pos.X + i < 0)
197                             {
198                                 continue;
199                             }
200                             else
201                             {
202                                 if (arr[_Pos.X + i, _Pos.Y - 1] == 1)
203                                 {
204                                     temp = false;
205                                     break;
206                                 }
207                             }
208                         }
209                         result = temp;
210                     }
211                     break;
212                 default:
213                     break;
214             }
215             return result;
216         }
217 
218         public override bool CanRightMove(int[,] arr, int rows, int columns)
219         {
220             bool result = false;
221             switch (_curChangeTimes)
222             {
223                 case 0:
224                     if (_Pos.Y < columns - 3)
225                     {
226                         if (arr[_Pos.X, _Pos.Y + 3] == 0)
227                         {
228                             result = true;
229                         }
230                         else
231                         {
232                             result = false;
233                         }
234                     }
235                     break;
236                 case 1:
237                     if (_Pos.Y < columns - 1)
238                     {
239                         bool temp = true;
240                         for (int i = -1; i < 3; i++)
241                         {
242                             if (_Pos.X + i < 0)
243                             {
244                                 continue;
245                             }
246                             else
247                             {
248                                 if (arr[_Pos.X + i, _Pos.Y + 1] == 1)
249                                 {
250                                     temp = false;
251                                     break;
252                                 }
253                             }
254                         }
255                         result = temp;
256                     }
257                     break;
258                 case 2:
259                     if (_Pos.Y < columns - 2)
260                     {
261                         if (arr[_Pos.X, _Pos.Y + 2] == 0)
262                         {
263                             result = true;
264                         }
265                         else
266                         {
267                             result = false;
268                         }
269                     }
270                     break;
271                 case 3:
272                     if (_Pos.Y < columns - 1)
273                     {
274                         bool temp = true;
275                         for (int i = -2; i < 2; i++)
276                         {
277                             if (_Pos.X + i < 0)
278                             {
279                                 continue;
280                             }
281                             else
282                             {
283                                 if (arr[_Pos.X + i, _Pos.Y + 1] == 1)
284                                 {
285                                     temp = false;
286                                     break;
287                                 }
288                             }
289                         }
290                         result = temp;
291                     }
292                     break;
293                 default:
294                     break;
295             }
296             return result;
297         }
298 
299         public override bool CanDownMove(int[,] arr, int rows, int columns)
300         {
301             bool result = false;
302             switch (_curChangeTimes)
303             {
304                 case 0:
305                     if (_Pos.X != rows - 1)
306                     {
307                         if (arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0 && arr[_Pos.X + 1, _Pos.Y + 2] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
308                         {
309                             result = true;
310                         }
311                         else
312                         {
313                             result = false;
314                         }
315                     }
316                     break;
317                 case 1:
318                     if (_Pos.X != rows - 3)
319                     {
320                         if (arr[_Pos.X + 3, _Pos.Y] == 0)
321                         {
322                             result = true;
323                         }
324                         else
325                         {
326                             result = false;
327                         }
328                     }
329                     break;
330                 case 2:
331                     if (_Pos.X != rows - 1)
332                     {
333                         if (arr[_Pos.X + 1, _Pos.Y - 2] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
334                         {
335                             result = true;
336                         }
337                         else
338                         {
339                             result = false;
340                         }
341                     }
342                     break;
343                 case 3:
344                     if (_Pos.X != rows - 2)
345                     {
346                         if (arr[_Pos.X + 2, _Pos.Y] == 0)
347                         {
348                             result = true;
349                         }
350                         else
351                         {
352                             result = false;
353                         }
354                     }
355                     break;
356                 default:
357                     break;
358             }
359             return result;
360         }
361 
362         public override int Appear()
363         {
364             int result = 0;
365             switch (_curChangeTimes)
366             {
367                 case 0:
368                     result = 0;
369                     break;
370                 case 1:
371                     result = -2;
372                     break;
373                 case 2:
374                     result = 0;
375                     break;
376                 case 3:
377                     result = -1;
378                     break;
379                 default:
380                     break;
381             }
382             return result;
383         }
384     }
View Code

Block3

  1 ///////////////////////////////////////////////////////////
  2 //  Class            :    Block3.cs
  3 //  CLRVersion        :    4.0.30319.42000
  4 //  NameSpace        :    BenNHTetris
  5 //  Created on        :    2018/5/31 11:41:56
  6 //  Original author    :    JIYONGFEI 
  7 //  JiYF笨男孩博客  :   https://www.cnblogs.com/JiYF/
  8 ///////////////////////////////////////////////////////////
  9 using System;
 10 using System.Collections.Generic;
 11 using System.Drawing;
 12 using System.Linq;
 13 using System.Text;
 14 
 15 namespace BenNHTetris
 16 {
 17     class Block3:Block
 18     {
 19         public Block3()
 20         {
 21             _curChangeTimes = 0;
 22             _needRows = 3;
 23             _needColumns = 3;
 24             _range = new int[3, 3]{{0,1,0},
 25                                     {1,1,1},
 26                                     {0,0,0}};
 27             _center = new Point(1, 1);
 28         }
 29         public override bool CanChange(int[,] arr, int rows, int cloumns)
 30         {
 31             bool result = true;
 32             if (_Pos.X - 1 >= 0 && _Pos.X + 1 <= rows - 1 && _Pos.Y - 1 >= 0 && _Pos.Y + 1 <= cloumns - 1)
 33             {
 34                 for (int i = -1; i < 2; i++)
 35                 {
 36                     for (int j = -1; j < 2; j++)
 37                     {
 38                         switch (_curChangeTimes)
 39                         {
 40                             case 0:
 41                                 if (i == -1 && j == 0 || i == 0 && j == -1 || i == 0 && j == 0 || i == 0 && j == 1)
 42                                 {
 43                                     continue;
 44                                 }
 45                                 break;
 46                             case 1:
 47                                 if (i == -1 && j == 0 || i == 0 && j == 0 || i == 0 && j == 1 || i == 1 && j == 0)
 48                                 {
 49                                     continue;
 50                                 }
 51                                 break;
 52                             case 2:
 53                                 if (i == 0 && j == -1 || i == 0 && j == 0 || i == 0 && j == 1 || i == 1 && j == 0)
 54                                 {
 55                                     continue;
 56                                 }
 57                                 break;
 58                             case 3:
 59                                 if (i == -1 && j == 0 || i == 0 && j == -1 || i == 0 && j == 0 || i == 1 && j == 0)
 60                                 {
 61                                     continue;
 62                                 }
 63                                 break;
 64                             default:
 65                                 break;
 66                         }
 67                         if (arr[_Pos.X + i, _Pos.Y + j] == 1)
 68                         {
 69                             result = false;
 70                         }
 71                     }
 72                 }
 73             }
 74             return result;
 75         }
 76 
 77         public override void Change()
 78         {
 79             switch (_curChangeTimes)
 80             {
 81                 case 0:
 82                     _range = new int[3, 3]{{0,1,0},
 83                                             {0,1,1},
 84                                             {0,1,0}};
 85                     _curChangeTimes = 1;
 86                     break;
 87                 case 1:
 88                     _range = new int[3, 3]{{0,0,0},
 89                                             {1,1,1},
 90                                             {0,1,0}};
 91                     _curChangeTimes = 2;
 92                     break;
 93                 case 2:
 94                     _range = new int[3, 3]{{0,1,0},
 95                                            {1,1,0},
 96                                            {0,1,0}};
 97                     _curChangeTimes = 3;
 98                     break;
 99                 case 3:
100                     _range = new int[3, 3]{{0,1,0},
101                                             {1,1,1},
102                                             {0,0,0}};
103                     _curChangeTimes = 0;
104                     break;
105                 default:
106                     break;
107             }
108         }
109 
110         public override bool CanLeftMove(int[,] arr, int rows, int columns)
111         {
112             bool result = false;
113             switch (_curChangeTimes)
114             {
115                 case 0:
116                     if (_Pos.Y - 1 != 0)
117                     {
118                         if (_Pos.X == 0)
119                         {
120                             if (arr[_Pos.X, _Pos.Y - 2] == 0)
121                                 result = true;
122                         }
123                         else
124                         {
125                             if (arr[_Pos.X - 1, _Pos.Y - 1] == 0 && arr[_Pos.X, _Pos.Y - 2] == 0)
126                                 result = true;
127                         }
128                     }
129                     break;
130                 case 1:
131                     if (_Pos.Y != 0)
132                     {
133                         if (_Pos.X == -1)
134                         {
135                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0)
136                                 result = true;
137                         }
138                         else if (_Pos.X == 0)
139                         {
140                             if (arr[_Pos.X, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
141                                 result = true;
142                         }
143                         else
144                         {
145                             if (arr[_Pos.X - 1, _Pos.Y - 1] == 0 && arr[_Pos.X, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
146                                 result = true;
147                         }
148                     }
149                     break;
150                 case 2:
151                     if (_Pos.Y - 1 != 0)
152                     {
153                         if (_Pos.X == -1)
154                         {
155                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0)
156                                 result = true;
157                         }
158                         else
159                         {
160                             if (arr[_Pos.X, _Pos.Y - 2] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
161                                 result = true;
162                         }
163                     }
164                     break;
165                 case 3:
166                     if (_Pos.Y - 1 != 0)
167                     {
168                         if (_Pos.X == -1)
169                         {
170                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0)
171                                 result = true;
172                         }
173                         else if (_Pos.X == 0)
174                         {
175                             if (arr[_Pos.X, _Pos.Y - 2] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
176                                 result = true;
177                         }
178                         else
179                         {
180                             if (arr[_Pos.X - 1, _Pos.Y - 1] == 0 && arr[_Pos.X, _Pos.Y - 2] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
181                                 result = true;
182                         }
183                     }
184                     break;
185                 default:
186                     break;
187             }
188             return result;
189         }
190 
191         public override bool CanRightMove(int[,] arr, int rows, int columns)
192         {
193             bool result = false;
194             switch (_curChangeTimes)
195             {
196                 case 0:
197                     if (_Pos.Y + 1 < columns - 1)
198                     {
199                         if (_Pos.X == 0)
200                         {
201                             if (arr[_Pos.X, _Pos.Y + 2] == 0)
202                                 result = true;
203                         }
204                         else
205                         {
206                             if (arr[_Pos.X - 1, _Pos.Y + 1] == 0 && arr[_Pos.X, _Pos.Y + 2] == 0)
207                                 result = true;
208                         }
209                     }
210                     break;
211                 case 1:
212                     if (_Pos.Y + 1 < columns - 1)
213                     {
214                         if (_Pos.X == -1)
215                         {
216                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0)
217                                 result = true;
218                         }
219                         else if (_Pos.X == 0)
220                         {
221                             if (arr[_Pos.X, _Pos.Y + 2] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
222                                 result = true;
223                         }
224                         else
225                         {
226                             if (arr[_Pos.X - 1, _Pos.Y + 1] == 0 && arr[_Pos.X, _Pos.Y + 2] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
227                                 result = true;
228                         }
229                     }
230                     break;
231                 case 2:
232                     if (_Pos.Y + 1 < columns - 1)
233                     {
234                         if (_Pos.X == -1)
235                         {
236                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0)
237                                 result = true;
238                         }
239                         else
240                         {
241                             if (arr[_Pos.X, _Pos.Y + 2] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
242                                 result = true;
243                         }
244                     }
245                     break;
246                 case 3:
247                     if (_Pos.Y < columns - 1)
248                     {
249                         if (_Pos.X == -1)
250                         {
251                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0)
252                                 result = true;
253                         }
254                         else if (_Pos.X == 0)
255                         {
256                             if (arr[_Pos.X, _Pos.Y + 1] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
257                                 result = true;
258                         }
259                         else
260                         {
261                             if (arr[_Pos.X - 1, _Pos.Y + 1] == 0 && arr[_Pos.X, _Pos.Y + 1] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
262                                 result = true;
263                         }
264                     }
265                     break;
266                 default:
267                     break;
268             }
269             return result;
270         }
271 
272         public override bool CanDownMove(int[,] arr, int rows, int columns)
273         {
274             bool result = false;
275             switch (_curChangeTimes)
276             {
277                 case 0:
278                     if (_Pos.X != rows - 1)
279                     {
280                         if (arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
281                             result = true;
282                     }
283                     break;
284                 case 1:
285                     if (_Pos.X + 1 != rows - 1)
286                     {
287                         if (arr[_Pos.X + 2, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
288                             result = true;
289                     }
290                     break;
291                 case 2:
292                     if (_Pos.X + 1 != rows - 1)
293                     {
294                         if (arr[_Pos.X + 2, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
295                             result = true;
296                     }
297                     break;
298                 case 3:
299                     if (_Pos.X + 1 != rows - 1)
300                     {
301                         if (arr[_Pos.X + 2, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
302                             result = true;
303                     }
304                     break;
305                 default:
306                     break;
307             }
308             return result;
309         }
310 
311         public override int Appear()
312         {
313             int result = 0;
314             switch (_curChangeTimes)
315             {
316                 case 0:
317                     result = 0;
318                     break;
319                 case 1:
320                     result = -1;
321                     break;
322                 case 2:
323                     result = -1;
324                     break;
325                 case 3:
326                     result = -1;
327                     break;
328                 default:
329                     break;
330             }
331             return result;
332         }
333     }
334 }
View Code

Block4

  1 ///////////////////////////////////////////////////////////
  2 //  Class            :    Class1.cs
  3 //  CLRVersion        :    4.0.30319.42000
  4 //  NameSpace        :    BenNHTetris
  5 //  Created on        :    2018/5/31 11:42:01
  6 //  Original author    :    JIYONGFEI 
  7 //  JiYF笨男孩博客  :   https://www.cnblogs.com/JiYF/
  8 ///////////////////////////////////////////////////////////
  9 using System;
 10 using System.Collections.Generic;
 11 using System.Drawing;
 12 using System.Linq;
 13 using System.Text;
 14 
 15 namespace BenNHTetris
 16 {
 17     class Block4:Block
 18     {
 19         public Block4()
 20         {
 21             _curChangeTimes= 0;
 22             _needRows = 3;
 23             _needColumns = 3;
 24             _range = new int[3, 3]{{0,0,0},
 25                                     {1,1,0},
 26                                     {0,1,1}};
 27             _center = new Point(1, 1);
 28         }
 29 
 30         public override bool CanChange(int[,] arr, int rows, int cloumns)
 31         {
 32             bool result = true;
 33             if (_Pos.X - 1 >= 0 && _Pos.X + 1 <= rows - 1 && _Pos.Y - 1 >= 0 && _Pos.Y + 1 <= cloumns - 1)
 34             {
 35                 for (int i = -1; i < 2; i++)
 36                 {
 37                     for (int j = -1; j < 2; j++)
 38                     {
 39                         switch (_curChangeTimes)
 40                         {
 41                             case 0:
 42                                 if (i == 0 && j == -1 || i == 0 && j == 0 || i == 1 && j == 0 || i == 1 && j == 1)
 43                                     continue;
 44                                 break;
 45                             case 1:
 46                                 if (i == -1 && j == 0 || i == 0 && j == 0 || i == 0 && j == -1 || i == 1 && j == -1)
 47                                     continue;
 48                                 break;
 49                             case 2:
 50                                 if (i == -1 && j == -1 || i == -1 && j == 0 || i == 0 && j == 0 || i == 0 && j == 1)
 51                                     continue;
 52                                 break;
 53                             case 3:
 54                                 if (i == -1 && j == 1 || i == 0 && j == 0 || i == 0 && j == 1 || i == 1 && j == 0)
 55                                     continue;
 56                                 break;
 57                             default:
 58                                 break;
 59                         }
 60                         if (arr[_Pos.X + i, _Pos.Y + j] == 1)
 61                         {
 62                             result = false;
 63                             goto break1;
 64                         }
 65                     }
 66                 }
 67             }
 68         break1: return result;
 69         }
 70 
 71         public override void Change()
 72         {
 73             switch (_curChangeTimes)
 74             {
 75                 case 0:
 76                     _range = new int[3, 3]{{0,1,0},
 77                                             {1,1,0},
 78                                             {1,0,0}};
 79                     _curChangeTimes = 1;
 80                     break;
 81                 case 1:
 82                     _range = new int[3, 3]{{1,1,0},
 83                                             {0,1,1},
 84                                             {0,0,0}};
 85                     _curChangeTimes = 2;
 86                     break;
 87                 case 2:
 88                     _range = new int[3, 3]{{0,0,1},
 89                                             {0,1,1},
 90                                             {0,1,0}};
 91                     _curChangeTimes = 3;
 92                     break;
 93                 case 3:
 94                     _range = new int[3, 3]{{0,0,0},
 95                                            {1,1,0},
 96                                            {0,1,1}};
 97                     _curChangeTimes = 0;
 98                     break;
 99                 default:
100                     break;
101             }
102         }
103 
104         public override bool CanLeftMove(int[,] arr, int rows, int columns)
105         {
106             bool result = false;
107             switch (_curChangeTimes)
108             {
109                 case 0:
110                     if (_Pos.Y - 1 > 0)
111                     {
112                         if (_Pos.X == -1)
113                         {
114                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0)
115                                 result = true;
116                         }
117                         else
118                         {
119                             if (arr[_Pos.X, _Pos.Y - 2] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
120                                 result = true;
121                         }
122                     }
123                     break;
124                 case 1:
125                     if (_Pos.Y - 1 > 0)
126                     {
127                         if (_Pos.X == -1)
128                         {
129                             if (arr[_Pos.X + 1, _Pos.Y - 2] == 0)
130                                 result = true;
131                         }
132                         else if (_Pos.X == 0)
133                         {
134                             if (arr[_Pos.X, _Pos.Y - 2] == 0 && arr[_Pos.X + 1, _Pos.Y - 2] == 0)
135                                 result = true;
136                         }
137                         else
138                         {
139                             if (arr[_Pos.X, _Pos.Y - 2] == 0 && arr[_Pos.X + 1, _Pos.Y - 2] == 0 && arr[_Pos.X - 1, _Pos.Y - 1] == 0)
140                                 result = true;
141                         }
142                     }
143                     break;
144                 case 2:
145                     if (_Pos.Y - 1 > 0)
146                     {
147                         if (_Pos.X == 0)
148                         {
149                             if (arr[_Pos.X, _Pos.Y - 1] == 0)
150                                 result = true;
151                         }
152                         else
153                         {
154                             if (arr[_Pos.X, _Pos.Y - 1] == 0 && arr[_Pos.X - 1, _Pos.Y - 2] == 0)
155                                 result = true;
156                         }
157                     }
158                     break;
159                 case 3:
160                     if (_Pos.Y > 0)
161                     {
162                         if (_Pos.X == -1)
163                         {
164                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0)
165                                 result = true;
166                         }
167                         else if (_Pos.X == 0)
168                         {
169                             if (arr[_Pos.X, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
170                                 result = true;
171                         }
172                         else
173                         {
174                             if (arr[_Pos.X, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X - 1, _Pos.Y] == 0)
175                                 result = true;
176                         }
177                     }
178                     break;
179                 default:
180                     break;
181             }
182             return result;
183         }
184 
185         public override bool CanRightMove(int[,] arr, int rows, int columns)
186         {
187             bool result = false;
188             switch (_curChangeTimes)
189             {
190                 case 0:
191                     if (_Pos.Y + 1 < columns - 1)
192                     {
193                         if (_Pos.X == -1)
194                         {
195                             if (arr[_Pos.X + 1, _Pos.Y + 2] == 0)
196                                 result = true;
197                         }
198                         else
199                         {
200                             if (arr[_Pos.X, _Pos.Y + 1] == 0 && arr[_Pos.X + 1, _Pos.Y + 2] == 0)
201                                 result = true;
202                         }
203                     }
204                     break;
205                 case 1:
206                     if (_Pos.Y < columns - 1)
207                     {
208                         if (_Pos.X == -1)
209                         {
210                             if (arr[_Pos.X + 1, _Pos.Y] == 0)
211                                 result = true;
212                         }
213                         else if (_Pos.X == 0)
214                         {
215                             if (arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X, _Pos.Y + 1] == 0)
216                                 result = true;
217                         }
218                         else
219                         {
220                             if (arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X, _Pos.Y + 1] == 0 && arr[_Pos.X - 1, _Pos.Y + 1] == 0)
221                                 result = true;
222                         }
223                     }
224                     break;
225                 case 2:
226                     if (_Pos.Y + 1 < columns - 1)
227                     {
228                         if (_Pos.X == 0)
229                         {
230                             if (arr[_Pos.X, _Pos.Y + 2] == 0)
231                                 result = true;
232                         }
233                         else
234                         {
235                             if (arr[_Pos.X, _Pos.Y + 2] == 0 && arr[_Pos.X - 1, _Pos.Y + 1] == 0)
236                                 result = true;
237                         }
238                     }
239                     break;
240                 case 3:
241                     if (_Pos.Y + 1 < columns - 1)
242                     {
243                         if (_Pos.X == -1)
244                         {
245                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0)
246                                 result = true;
247                         }
248                         else if (_Pos.X == 0)
249                         {
250                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0 && arr[_Pos.X, _Pos.Y + 2] == 0)
251                                 result = true;
252                         }
253                         else
254                         {
255                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0 && arr[_Pos.X, _Pos.Y + 2] == 0 && arr[_Pos.X - 1, _Pos.Y + 2] == 0)
256                                 result = true;
257                         }
258                     }
259                     break;
260                 default:
261                     break;
262             }
263             return result;
264         }
265 
266         public override bool CanDownMove(int[,] arr, int rows, int columns)
267         {
268             bool result = false;
269 
270             switch (_curChangeTimes)
271             {
272                 case 0:
273                     if (_Pos.X + 1 < rows - 1)
274                     {
275                         if (arr[_Pos.X + 2, _Pos.Y] == 0 && arr[_Pos.X + 2, _Pos.Y + 1] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
276                             result = true;
277                     }
278                     break;
279                 case 1:
280                     if (_Pos.X + 1 < rows - 1)
281                     {
282                         if (arr[_Pos.X + 2, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y] == 0)
283                             result = true;
284                     }
285                     break;
286                 case 2:
287                     if (_Pos.X < rows - 1)
288                     {
289                         if (arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0 && arr[_Pos.X, _Pos.Y - 1] == 0)
290                             result = true;
291                     }
292                     break;
293                 case 3:
294                     if (_Pos.X + 1 < rows - 1)
295                     {
296                         if (arr[_Pos.X + 2, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
297                             result = true;
298                     }
299                     break;
300                 default:
301                     break;
302             }
303             return result;
304         }
305 
306         public override int Appear()
307         {
308             int result = 0;
309             switch (_curChangeTimes)
310             {
311                 case 0:
312                     result = -1;
313                     break;
314                 case 1:
315                     result = -1;
316                     break;
317                 case 2:
318                     result = 0;
319                     break;
320                 case 3:
321                     result = -1;
322                     break;
323                 default:
324                     break;
325             }
326             return result;
327         }
328     }
329 }
View Code

Block5

  1 ///////////////////////////////////////////////////////////
  2 //  Class            :    Block5.cs
  3 //  CLRVersion        :    4.0.30319.42000
  4 //  NameSpace        :    BenNHTetris
  5 //  Created on        :    2018/5/31 11:42:29
  6 //  Original author    :    JIYONGFEI 
  7 //  JiYF笨男孩博客  :   https://www.cnblogs.com/JiYF/
  8 ///////////////////////////////////////////////////////////
  9 using System;
 10 using System.Collections.Generic;
 11 using System.Drawing;
 12 using System.Linq;
 13 using System.Text;
 14 
 15 namespace BenNHTetris
 16 {
 17     class Block5:Block
 18     {
 19         public Block5()
 20         {
 21             _curChangeTimes = 0;
 22             _needRows = 3;
 23             _needColumns = 3;
 24             _range = new int[3, 3]{{0,0,0},
 25                                     {0,1,1},
 26                                     {1,1,0}};
 27             _center = new Point(1, 1);
 28         }
 29         public override bool CanChange(int[,] arr, int rows, int cloumns)
 30         {
 31             bool result = true;
 32             if (_Pos.X - 1 >= 0 && _Pos.X + 1 <= rows - 1 && _Pos.Y - 1 >= 0 && _Pos.Y + 1 <= cloumns - 1)
 33             {
 34                 for (int i = -1; i < 2; i++)
 35                 {
 36                     for (int j = -1; j < 2; j++)
 37                     {
 38                         switch (_curChangeTimes)
 39                         {
 40                             case 0:
 41                                 if (i == 0 && j == 0 || i == 0 && j == 1 || i == 1 && j == -1 || i == 1 && j == 0)
 42                                     continue;
 43                                 break;
 44                             case 1:
 45                                 if (i == -1 && j == -1 || i == 0 && j == -1 || i == 0 && j == 0 || i == 1 && j == 0)
 46                                     continue;
 47                                 break;
 48                             case 2:
 49                                 if (i == -1 && j == 0 || i == -1 && j == 1 || i == 0 && j == -1 || i == 0 && j == 0)
 50                                     continue;
 51                                 break;
 52                             case 3:
 53                                 if (i == -1 && j == 0 || i == 0 && j == 0 || i == 0 && j == 1 || i == 1 && j == 1)
 54                                     continue;
 55                                 break;
 56                             default:
 57                                 break;
 58                         }
 59                         if (arr[_Pos.X + i, _Pos.Y + j] == 1)
 60                         {
 61                             result = false;
 62                             goto break1;
 63                         }
 64                     }
 65                 }
 66             }
 67         break1: return result;
 68         }
 69 
 70         public override void Change()
 71         {
 72             switch (_curChangeTimes)
 73             {
 74                 case 0:
 75                     _range = new int[3, 3]{{1,0,0},
 76                                             {1,1,0},
 77                                             {0,1,0}};
 78                     _curChangeTimes = 1;
 79                     break;
 80                 case 1:
 81                     _range = new int[3, 3]{{0,1,1},
 82                                             {1,1,0},
 83                                             {0,0,0}};
 84                     _curChangeTimes = 2;
 85                     break;
 86                 case 2:
 87                     _range = new int[3, 3]{{0,1,0},
 88                                             {0,1,1},
 89                                             {0,0,1}};
 90                     _curChangeTimes = 3;
 91                     break;
 92                 case 3:
 93                     _range = new int[3, 3]{{0,0,0},
 94                                            {0,1,1},
 95                                            {1,1,0}};
 96                     _curChangeTimes = 0;
 97                     break;
 98                 default:
 99                     break;
100             }
101         }
102 
103         public override bool CanLeftMove(int[,] arr, int rows, int columns)
104         {
105             bool result = false;
106             switch (_curChangeTimes)
107             {
108                 case 0:
109                     if (_Pos.Y - 1 > 0)
110                     {
111                         if (_Pos.X == -1)
112                         {
113                             if (arr[_Pos.X + 1, _Pos.Y - 2] == 0)
114                                 result = true;
115                         }
116                         else
117                         {
118                             if (arr[_Pos.X, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y - 2] == 0)
119                                 result = true;
120                         }
121                     }
122                     break;
123                 case 1:
124                     if (_Pos.Y - 1 > 0)
125                     {
126                         if (_Pos.X == -1)
127                         {
128                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0)
129                                 result = true;
130                         }
131                         else if (_Pos.X == 0)
132                         {
133                             if (arr[_Pos.X, _Pos.Y - 2] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
134                                 result = true;
135                         }
136                         else
137                         {
138                             if (arr[_Pos.X, _Pos.Y - 2] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X - 1, _Pos.Y - 2] == 0)
139                                 result = true;
140                         }
141                     }
142                     break;
143                 case 2:
144                     if (_Pos.Y - 1 > 0)
145                     {
146                         if (_Pos.X == 0)
147                         {
148                             if (arr[_Pos.X, _Pos.Y - 2] == 0)
149                                 result = true;
150                         }
151                         else
152                         {
153                             if (arr[_Pos.X, _Pos.Y - 2] == 0 && arr[_Pos.X - 1, _Pos.Y - 1] == 0)
154                                 result = true;
155                         }
156                     }
157                     break;
158                 case 3:
159                     if (_Pos.Y > 0)
160                     {
161                         if (_Pos.X == -1)
162                         {
163                             if (arr[_Pos.X + 1, _Pos.Y] == 0)
164                                 result = true;
165                         }
166                         else if (_Pos.X == 0)
167                         {
168                             if (arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X, _Pos.Y - 1] == 0)
169                                 result = true;
170                         }
171                         else
172                         {
173                             if (arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X, _Pos.Y - 1] == 0 && arr[_Pos.X - 1, _Pos.Y - 1] == 0)
174                                 result = true;
175                         }
176                     }
177                     break;
178                 default:
179                     break;
180             }
181             return result;
182         }
183 
184         public override bool CanRightMove(int[,] arr, int rows, int columns)
185         {
186             bool result = false;
187             switch (_curChangeTimes)
188             {
189                 case 0:
190                     if (_Pos.Y + 1 < columns - 1)
191                     {
192                         if (_Pos.X == -1)
193                         {
194                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0)
195                                 result = true;
196                         }
197                         else
198                         {
199                             if (arr[_Pos.X, _Pos.Y + 2] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
200                                 result = true;
201                         }
202                     }
203                     break;
204                 case 1:
205                     if (_Pos.Y < columns - 1)
206                     {
207                         if (_Pos.X == -1)
208                         {
209                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0)
210                                 result = true;
211                         }
212                         else if (_Pos.X == 0)
213                         {
214                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0 && arr[_Pos.X, _Pos.Y + 1] == 0)
215                                 result = true;
216                         }
217                         else
218                         {
219                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0 && arr[_Pos.X, _Pos.Y + 1] == 0 && arr[_Pos.X - 1, _Pos.Y] == 0)
220                                 result = true;
221                         }
222                     }
223                     break;
224                 case 2:
225                     if (_Pos.Y + 1 < columns - 1)
226                     {
227                         if (_Pos.X == 0)
228                         {
229                             if (arr[_Pos.X, _Pos.Y + 1] == 0)
230                                 result = true;
231                         }
232                         else
233                         {
234                             if (arr[_Pos.X, _Pos.Y + 1] == 0 && arr[_Pos.X - 1, _Pos.Y + 2] == 0)
235                                 result = true;
236                         }
237                     }
238                     break;
239                 case 3:
240                     if (_Pos.Y + 1 < columns - 1)
241                     {
242                         if (_Pos.X == -1)
243                         {
244                             if (arr[_Pos.X + 1, _Pos.Y + 2] == 0)
245                                 result = true;
246                         }
247                         else if (_Pos.X == 0)
248                         {
249                             if (arr[_Pos.X + 1, _Pos.Y + 2] == 0 && arr[_Pos.X, _Pos.Y + 2] == 0)
250                                 result = true;
251                         }
252                         else
253                         {
254                             if (arr[_Pos.X + 1, _Pos.Y + 2] == 0 && arr[_Pos.X, _Pos.Y + 2] == 0 && arr[_Pos.X - 1, _Pos.Y + 1] == 0)
255                                 result = true;
256                         }
257                     }
258                     break;
259                 default:
260                     break;
261             }
262             return result;
263         }
264 
265         public override bool CanDownMove(int[,] arr, int rows, int columns)
266         {
267             bool result = false;
268             switch (_curChangeTimes)
269             {
270                 case 0:
271                     if (_Pos.X + 1 < rows - 1)
272                     {
273                         if (arr[_Pos.X + 2, _Pos.Y - 1] == 0 && arr[_Pos.X + 2, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
274                             result = true;
275                     }
276                     break;
277                 case 1:
278                     if (_Pos.X + 1 < rows - 1)
279                     {
280                         if (arr[_Pos.X + 2, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
281                             result = true;
282                     }
283                     break;
284                 case 2:
285                     if (_Pos.X < rows - 1)
286                     {
287                         if (arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X, _Pos.Y + 1] == 0)
288                             result = true;
289                     }
290                     break;
291                 case 3:
292                     if (_Pos.X + 1 < rows - 1)
293                     {
294                         if (arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X + 2, _Pos.Y + 1] == 0)
295                             result = true;
296                     }
297                     break;
298                 default:
299                     break;
300             }
301             return result;
302         }
303 
304         public override int Appear()
305         {
306             int result = 0;
307             switch (_curChangeTimes)
308             {
309                 case 0:
310                     result = -1;
311                     break;
312                 case 1:
313                     result = -1;
314                     break;
315                 case 2:
316                     result = 0;
317                     break;
318                 case 3:
319                     result = -1;
320                     break;
321                 default:
322                     break;
323             }
324             return result;
325         }
326     }
327 }
View Code

Block6

  1 ///////////////////////////////////////////////////////////
  2 //  Class            :    Block6.cs
  3 //  CLRVersion        :    4.0.30319.42000
  4 //  NameSpace        :    BenNHTetris
  5 //  Created on        :    2018/5/31 11:42:37
  6 //  Original author    :    JIYONGFEI 
  7 //  JiYF笨男孩博客  :   https://www.cnblogs.com/JiYF/
  8 ///////////////////////////////////////////////////////////
  9 using System;
 10 using System.Collections.Generic;
 11 using System.Drawing;
 12 using System.Linq;
 13 using System.Text;
 14 
 15 namespace BenNHTetris
 16 {
 17     class Block6:Block
 18     {
 19         public Block6()
 20         {
 21             _curChangeTimes = 0;
 22             _needRows = 5;
 23             _needColumns = 5;
 24             _range = new int[5, 5]{{0,0,0,0,0},
 25                                     {0,0,1,0,0},
 26                                     {0,0,1,1,1},
 27                                     {0,0,0,0,0},
 28                                     {0,0,0,0,0}};
 29             _center = new Point(2, 2);
 30         }
 31         public override bool CanChange(int[,] arr, int rows, int cloumns)
 32         {
 33             if (_Pos.X - 2 >= 0 && _Pos.X + 2 <= rows - 1 && _Pos.Y - 2 >= 0 && _Pos.Y + 2 <= cloumns - 1)
 34             {
 35                 bool result = true;
 36                 switch (_curChangeTimes)
 37                 {
 38                     case 0:
 39                         for (int i = -2; i < 3; i++)
 40                         {
 41                             for (int j = -2; j < 3; j++)
 42                             {
 43                                 if (i == -1 && j == 0 || i == 0 && j == 0 || i == 0 && j == 1 || i == 0 && j == 2)
 44                                     continue;
 45                                 if (arr[_Pos.X + i, _Pos.Y + j] == 1)
 46                                 {
 47                                     result = false;
 48                                     goto break1;
 49                                 }
 50                             }
 51                         }
 52                         break;
 53                     case 1:
 54                         for (int i = -2; i < 3; i++)
 55                         {
 56                             for (int j = -2; j < 3; j++)
 57                             {
 58                                 if (i == 0 && j == 0 || i == 0 && j == 1 || i == 1 && j == 0 || i == 2 && j == 0)
 59                                     continue;
 60                                 if (arr[_Pos.X + i, _Pos.Y + j] == 1)
 61                                 {
 62                                     result = false;
 63                                     goto break1;
 64                                 }
 65                             }
 66                         }
 67                         break;
 68                     case 2:
 69                         for (int i = -2; i < 3; i++)
 70                         {
 71                             for (int j = -2; j < 3; j++)
 72                             {
 73                                 if (i == 0 && j == -2 || i == 0 && j == -1 || i == 0 && j == 0 || i == 1 && j == 0)
 74                                     continue;
 75                                 if (arr[_Pos.X + i, _Pos.Y + j] == 1)
 76                                 {
 77                                     result = false;
 78                                     goto break1;
 79                                 }
 80                             }
 81                         }
 82                         break;
 83                     case 3:
 84                         for (int i = -2; i < 3; i++)
 85                         {
 86                             for (int j = -2; j < 3; j++)
 87                             {
 88                                 if (i == -2 && j == 0 || i == -1 && j == 0 || i == 0 && j == -1 || i == 0 && j == 0)
 89                                     continue;
 90                                 if (arr[_Pos.X + i, _Pos.Y + j] == 1)
 91                                 {
 92                                     result = false;
 93                                     goto break1;
 94                                 }
 95                             }
 96                         }
 97                         break;
 98                     default:
 99                         break;
100                 }
101             break1: return result;
102             }
103             else
104             {
105                 return false;
106             }
107         }
108 
109         public override void Change()
110         {
111             switch (_curChangeTimes)
112             {
113                 case 0:
114                     _range = new int[5, 5]{{0,0,0,0,0},
115                                             {0,0,0,0,0},
116                                             {0,0,1,1,0},
117                                             {0,0,1,0,0},
118                                             {0,0,1,0,0}};
119                     _curChangeTimes = 1;
120                     break;
121                 case 1:
122                     _range = new int[5, 5]{{0,0,0,0,0},
123                                             {0,0,0,0,0},
124                                             {1,1,1,0,0},
125                                             {0,0,1,0,0},
126                                             {0,0,0,0,0}};
127                     _curChangeTimes = 2;
128                     break;
129                 case 2:
130                     _range = new int[5, 5]{{0,0,1,0,0},
131                                             {0,0,1,0,0},
132                                             {0,1,1,0,0},
133                                             {0,0,0,0,0},
134                                             {0,0,0,0,0}};
135                     _curChangeTimes = 3;
136                     break;
137                 case 3:
138                     _range = new int[5, 5]{{0,0,0,0,0},
139                                             {0,0,1,0,0},
140                                             {0,0,1,1,1},
141                                             {0,0,0,0,0},
142                                             {0,0,0,0,0}};
143                     _curChangeTimes = 0;
144                     break;
145                 default:
146                     break;
147             }
148         }
149 
150         public override bool CanLeftMove(int[,] arr, int rows, int columns)
151         {
152             bool result = false;
153             switch (_curChangeTimes)
154             {
155                 case 0:
156                     if (_Pos.Y > 0)
157                     {
158                         if (_Pos.X == 0)
159                         {
160                             if (arr[_Pos.X, _Pos.Y - 1] == 0)
161                                 result = true;
162                         }
163                         else
164                         {
165                             if (arr[_Pos.X, _Pos.Y - 1] == 0 && arr[_Pos.X - 1, _Pos.Y - 1] == 0)
166                                 result = true;
167                         }
168                     }
169                     break;
170                 case 1:
171                     if (_Pos.Y > 0)
172                     {
173                         if (_Pos.X == -2)
174                         {
175                             if (arr[_Pos.X + 2, _Pos.Y - 1] == 0)
176                                 result = true;
177                         }
178                         else if (_Pos.X == -1)
179                         {
180                             if (arr[_Pos.X + 2, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0)
181                                 result = true;
182                         }
183                         else
184                         {
185                             if (arr[_Pos.X + 2, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X, _Pos.Y - 1] == 0)
186                                 result = true;
187                         }
188                     }
189                     break;
190                 case 2:
191                     if (_Pos.Y > 2)
192                     {
193                         if (_Pos.X == -1)
194                         {
195                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0)
196                                 result = true;
197                         }
198                         else
199                         {
200                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X, _Pos.Y - 3] == 0)
201                                 result = true;
202                         }
203                     }
204                     break;
205                 case 3:
206                     if (_Pos.Y > 1)
207                     {
208                         if (_Pos.X == 0)
209                         {
210                             if (arr[_Pos.X, _Pos.Y - 2] == 0)
211                                 result = true;
212                         }
213                         else if (_Pos.X == 1)
214                         {
215                             if (arr[_Pos.X, _Pos.Y - 2] == 0 && arr[_Pos.X - 1, _Pos.Y - 1] == 0)
216                                 result = true;
217                         }
218                         else
219                         {
220                             if (arr[_Pos.X, _Pos.Y - 2] == 0 && arr[_Pos.X - 1, _Pos.Y - 1] == 0 && arr[_Pos.X - 2, _Pos.Y - 1] == 0)
221                                 result = true;
222                         }
223                     }
224                     break;
225                 default:
226                     break;
227             }
228             return result;
229         }
230 
231         public override bool CanRightMove(int[,] arr, int rows, int columns)
232         {
233             bool result = false;
234             switch (_curChangeTimes)
235             {
236                 case 0:
237                     if (_Pos.Y + 2 < columns - 1)
238                     {
239                         if (_Pos.X == 0)
240                         {
241                             if (arr[_Pos.X, _Pos.Y + 3] == 0)
242                                 result = true;
243                         }
244                         else
245                         {
246                             if (arr[_Pos.X, _Pos.Y + 3] == 0 && arr[_Pos.X - 1, _Pos.Y + 1] == 0)
247                                 result = true;
248                         }
249                     }
250                     break;
251                 case 1:
252                     if (_Pos.Y + 1 < columns - 1)
253                     {
254                         if (_Pos.X == -2)
255                         {
256                             if (arr[_Pos.X + 2, _Pos.Y + 1] == 0)
257                                 result = true;
258                         }
259                         else if (_Pos.X == -1)
260                         {
261                             if (arr[_Pos.X + 2, _Pos.Y + 1] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
262                                 result = true;
263                         }
264                         else
265                         {
266                             if (arr[_Pos.X + 2, _Pos.Y + 1] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0 && arr[_Pos.X, _Pos.Y + 2] == 0)
267                                 result = true;
268                         }
269                     }
270                     break;
271                 case 2:
272                     if (_Pos.Y < columns - 1)
273                     {
274                         if (_Pos.X == -1)
275                         {
276                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0)
277                                 result = true;
278                         }
279                         else
280                         {
281                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0 && arr[_Pos.X, _Pos.Y + 1] == 0)
282                                 result = true;
283                         }
284                     }
285                     break;
286                 case 3:
287                     if (_Pos.Y < columns - 1)
288                     {
289                         if (_Pos.X == 0)
290                         {
291                             if (arr[_Pos.X, _Pos.Y + 1] == 0)
292                                 result = true;
293                         }
294                         else if (_Pos.X == 1)
295                         {
296                             if (arr[_Pos.X, _Pos.Y + 1] == 0 && arr[_Pos.X - 1, _Pos.Y + 1] == 0)
297                                 result = true;
298                         }
299                         else
300                         {
301                             if (arr[_Pos.X, _Pos.Y + 1] == 0 && arr[_Pos.X - 1, _Pos.Y + 1] == 0 && arr[_Pos.X - 2, _Pos.Y + 1] == 0)
302                                 result = true;
303                         }
304                     }
305                     break;
306                 default:
307                     break;
308             }
309             return result;
310         }
311 
312         public override bool CanDownMove(int[,] arr, int rows, int columns)
313         {
314             bool result = false;
315             switch (_curChangeTimes)
316             {
317                 case 0:
318                     if (_Pos.X < rows - 1)
319                     {
320                         if (arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0 && arr[_Pos.X + 1, _Pos.Y + 2] == 0)
321                             result = true;
322                     }
323                     break;
324                 case 1:
325                     if (_Pos.X + 2 < rows - 1)
326                     {
327                         if (_Pos.X == -2)
328                         {
329                             if (arr[_Pos.X + 3, _Pos.Y] == 0)
330                                 result = true;
331                         }
332                         else
333                         {
334                             if (arr[_Pos.X + 3, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
335                                 result = true;
336                         }
337                     }
338                     break;
339                 case 2:
340                     if (_Pos.X + 1 < rows - 1)
341                     {
342                         if (arr[_Pos.X + 1, _Pos.Y - 2] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X + 2, _Pos.Y] == 0)
343                             result = true;
344                     }
345                     break;
346                 case 3:
347                     if (_Pos.X < rows - 1)
348                     {
349                         if (arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y] == 0)
350                             result = true;
351                     }
352                     break;
353                 default:
354                     break;
355             }
356             return result;
357         }
358 
359         public override int Appear()
360         {
361             int result = 0;
362             switch (_curChangeTimes)
363             {
364                 case 0:
365                     result = 0;
366                     break;
367                 case 1:
368                     result = -2;
369                     break;
370                 case 2:
371                     result = -1;
372                     break;
373                 case 3:
374                     result = -0;
375                     break;
376                 default:
377                     break;
378             }
379             return result;
380         }
381     }
382 }
View Code

Block7

  1 ///////////////////////////////////////////////////////////
  2 //  Class            :    Block7.cs
  3 //  CLRVersion        :    4.0.30319.42000
  4 //  NameSpace        :    BenNHTetris
  5 //  Created on        :    2018/5/31 11:42:44
  6 //  Original author    :    JIYONGFEI 
  7 //  JiYF笨男孩博客  :   https://www.cnblogs.com/JiYF/
  8 ///////////////////////////////////////////////////////////
  9 using System;
 10 using System.Collections.Generic;
 11 using System.Drawing;
 12 using System.Linq;
 13 using System.Text;
 14 
 15 namespace BenNHTetris
 16 {
 17     class Block7:Block
 18     {
 19         public Block7()
 20         {
 21             _curChangeTimes = 0;
 22             _needRows = 5;
 23             _needColumns = 5;
 24             _range = new int[5, 5]{{0,0,0,0,0},
 25                                     {0,0,1,0,0},
 26                                     {1,1,1,0,0},
 27                                     {0,0,0,0,0},
 28                                     {0,0,0,0,0}};
 29             _center = new Point(2, 2);
 30         }
 31         public override bool CanChange(int[,] arr, int rows, int cloumns)
 32         {
 33             if (_Pos.X - 2 >= 0 && _Pos.X + 2 <= rows - 1 && _Pos.Y - 2 >= 0 && _Pos.Y + 2 <= cloumns - 1)
 34             {
 35                 bool result = true;
 36                 switch (_curChangeTimes)
 37                 {
 38                     case 0:
 39                         for (int i = -2; i < 3; i++)
 40                         {
 41                             for (int j = -2; j < 3; j++)
 42                             {
 43                                 if (i == -1 && j == 0 || i == 0 && j == -2 || i == 0 && j == -1 || i == 0 && j == 0)
 44                                     continue;
 45                                 if (arr[_Pos.X + i, _Pos.Y + j] == 1)
 46                                 {
 47                                     result = false;
 48                                     goto break1;
 49                                 }
 50                             }
 51                         }
 52                         break;
 53                     case 1:
 54                         for (int i = -2; i < 3; i++)
 55                         {
 56                             for (int j = -2; j < 3; j++)
 57                             {
 58                                 if (i == -2 && j == 0 || i == -1 && j == 0 || i == 0 && j == 0 || i == 0 && j == 1)
 59                                     continue;
 60                                 if (arr[_Pos.X + i, _Pos.Y + j] == 1)
 61                                 {
 62                                     result = false;
 63                                     goto break1;
 64                                 }
 65                             }
 66                         }
 67                         break;
 68                     case 2:
 69                         for (int i = -2; i < 3; i++)
 70                         {
 71                             for (int j = -2; j < 3; j++)
 72                             {
 73                                 if (i == 0 && j == 0 || i == 0 && j == 1 || i == 0 && j == 2 || i == 1 && j == 0)
 74                                     continue;
 75                                 if (arr[_Pos.X + i, _Pos.Y + j] == 1)
 76                                 {
 77                                     result = false;
 78                                     goto break1;
 79                                 }
 80                             }
 81                         }
 82                         break;
 83                     case 3:
 84                         for (int i = -2; i < 3; i++)
 85                         {
 86                             for (int j = -2; j < 3; j++)
 87                             {
 88                                 if (i == 0 && j == -1 || i == 0 && j == 0 || i == 1 && j == 0 || i == 2 && j == 0)
 89                                     continue;
 90                                 if (arr[_Pos.X + i, _Pos.Y + j] == 1)
 91                                 {
 92                                     result = false;
 93                                     goto break1;
 94                                 }
 95                             }
 96                         }
 97                         break;
 98                     default:
 99                         break;
100                 }
101             break1: return result;
102             }
103             else
104             {
105                 return false;
106             }
107         }
108 
109         public override void Change()
110         {
111             switch (_curChangeTimes)
112             {
113                 case 0:
114                     _range = new int[5, 5]{{0,0,1,0,0},
115                                             {0,0,1,0,0},
116                                             {0,0,1,1,0},
117                                             {0,0,0,0,0},
118                                             {0,0,0,0,0}};
119                     _curChangeTimes = 1;
120                     break;
121                 case 1:
122                     _range = new int[5, 5]{{0,0,0,0,0},
123                                             {0,0,0,0,0},
124                                             {0,0,1,1,1},
125                                             {0,0,1,0,0},
126                                             {0,0,0,0,0}};
127                     _curChangeTimes = 2;
128                     break;
129                 case 2:
130                     _range = new int[5, 5]{{0,0,0,0,0},
131                                             {0,0,0,0,0},
132                                             {0,1,1,0,0},
133                                             {0,0,1,0,0},
134                                             {0,0,1,0,0}};
135                     _curChangeTimes = 3;
136                     break;
137                 case 3:
138                     _range = new int[5, 5]{{0,0,0,0,0},
139                                             {0,0,1,0,0},
140                                             {1,1,1,0,0},
141                                             {0,0,0,0,0},
142                                             {0,0,0,0,0}};
143                     _curChangeTimes = 0;
144                     break;
145                 default:
146                     break;
147             }
148         }
149 
150         public override bool CanLeftMove(int[,] arr, int rows, int columns)
151         {
152             bool result = false;
153             switch (_curChangeTimes)
154             {
155                 case 0:
156                     if (_Pos.Y - 2 > 0)
157                     {
158                         if (_Pos.X == 0)
159                         {
160                             if (arr[_Pos.X, _Pos.Y - 3] == 0)
161                                 result = true;
162                         }
163                         else
164                         {
165                             if (arr[_Pos.X, _Pos.Y - 3] == 0 && arr[_Pos.X - 1, _Pos.Y - 1] == 0)
166                                 result = true;
167                         }
168                     }
169                     break;
170                 case 1:
171                     if (_Pos.Y > 0)
172                     {
173                         if (_Pos.X == 0)
174                         {
175                             if (arr[_Pos.X, _Pos.Y - 1] == 0)
176                                 result = true;
177                         }
178                         else if (_Pos.X == 1)
179                         {
180                             if (arr[_Pos.X, _Pos.Y - 1] == 0 && arr[_Pos.X - 1, _Pos.Y - 1] == 0)
181                                 result = true;
182                         }
183                         else
184                         {
185                             if (arr[_Pos.X, _Pos.Y - 1] == 0 && arr[_Pos.X - 1, _Pos.Y - 1] == 0 && arr[_Pos.X - 2, _Pos.Y - 1] == 0)
186                                 result = true;
187                         }
188                     }
189                     break;
190                 case 2:
191                     if (_Pos.Y > 0)
192                     {
193                         if (_Pos.X == -1)
194                         {
195                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0)
196                                 result = true;
197                         }
198                         else
199                         {
200                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X, _Pos.Y - 1] == 0)
201                                 result = true;
202                         }
203                     }
204                     break;
205                 case 3:
206                     if (_Pos.Y > 1)
207                     {
208                         if (_Pos.X == -2)
209                         {
210                             if (arr[_Pos.X + 2, _Pos.Y - 1] == 0)
211                                 result = true;
212                         }
213                         else if (_Pos.X == -1)
214                         {
215                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X + 2, _Pos.Y - 1] == 0)
216                                 result = true;
217                         }
218                         else
219                         {
220                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X + 2, _Pos.Y - 1] == 0 && arr[_Pos.X, _Pos.Y - 2] == 0)
221                                 result = true;
222                         }
223                     }
224                     break;
225                 default:
226                     break;
227             }
228             return result;
229         }
230 
231         public override bool CanRightMove(int[,] arr, int rows, int columns)
232         {
233             bool result = false;
234             switch (_curChangeTimes)
235             {
236                 case 0:
237                     if (_Pos.Y < columns - 1)
238                     {
239                         if (_Pos.X == 0)
240                         {
241                             if (arr[_Pos.X, _Pos.Y + 1] == 0)
242                                 result = true;
243                         }
244                         else
245                         {
246                             if (arr[_Pos.X, _Pos.Y + 1] == 0 && arr[_Pos.X - 1, _Pos.Y + 1] == 0)
247                                 result = true;
248                         }
249                     }
250                     break;
251                 case 1:
252                     if (_Pos.Y + 1 < columns - 1)
253                     {
254                         if (_Pos.X == 0)
255                         {
256                             if (arr[_Pos.X, _Pos.Y + 2] == 0)
257                                 result = true;
258                         }
259                         else if (_Pos.X == 1)
260                         {
261                             if (arr[_Pos.X, _Pos.Y + 2] == 0 && arr[_Pos.X - 1, _Pos.Y + 1] == 0)
262                                 result = true;
263                         }
264                         else
265                         {
266                             if (arr[_Pos.X, _Pos.Y + 2] == 0 && arr[_Pos.X - 1, _Pos.Y + 1] == 0 && arr[_Pos.X - 2, _Pos.Y + 1] == 0)
267                                 result = true;
268                         }
269                     }
270                     break;
271                 case 2:
272                     if (_Pos.Y + 2 < columns - 1)
273                     {
274                         if (_Pos.X == -1)
275                         {
276                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0)
277                                 result = true;
278                         }
279                         else
280                         {
281                             if (arr[_Pos.X + 1, _Pos.Y + 1] == 0 && arr[_Pos.X, _Pos.Y + 3] == 0)
282                                 result = true;
283                         }
284                     }
285                     break;
286                 case 3:
287                     if (_Pos.Y < columns - 1)
288                     {
289                         if (_Pos.X == -2)
290                         {
291                             if (arr[_Pos.X + 2, _Pos.Y + 1] == 0)
292                                 result = true;
293                         }
294                         else if (_Pos.X == -1)
295                         {
296                             if (arr[_Pos.X + 2, _Pos.Y + 1] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
297                                 result = true;
298                         }
299                         else
300                         {
301                             if (arr[_Pos.X + 2, _Pos.Y + 1] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0 && arr[_Pos.X, _Pos.Y + 1] == 0)
302                                 result = true;
303                         }
304                     }
305                     break;
306                 default:
307                     break;
308             }
309             return result;
310         }
311 
312         public override bool CanDownMove(int[,] arr, int rows, int columns)
313         {
314             bool result = false;
315             switch (_curChangeTimes)
316             {
317                 case 0:
318                     if (_Pos.X < rows - 1)
319                     {
320                         if (arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X + 1, _Pos.Y - 2] == 0)
321                             result = true;
322                     }
323                     break;
324                 case 1:
325                     if (_Pos.X < rows - 1)
326                     {
327                         if (arr[_Pos.X + 1, _Pos.Y] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0)
328                             result = true;
329                     }
330                     break;
331                 case 2:
332                     if (_Pos.X + 1 < rows - 1)
333                     {
334                         if (arr[_Pos.X + 1, _Pos.Y + 2] == 0 && arr[_Pos.X + 1, _Pos.Y + 1] == 0 && arr[_Pos.X + 2, _Pos.Y] == 0)
335                             result = true;
336                     }
337                     break;
338                 case 3:
339                     if (_Pos.X + 2 < rows - 1)
340                     {
341                         if (_Pos.X == -2)
342                         {
343                             if (arr[_Pos.X + 3, _Pos.Y] == 0)
344                                 result = true;
345                         }
346                         else
347                         {
348                             if (arr[_Pos.X + 1, _Pos.Y - 1] == 0 && arr[_Pos.X + 3, _Pos.Y] == 0)
349                                 result = true;
350                         }
351                     }
352                     break;
353                 default:
354                     break;
355             }
356             return result;
357         }
358 
359         public override int Appear()
360         {
361             int result = 0;
362             switch (_curChangeTimes)
363             {
364                 case 0:
365                     result = 0;
366                     break;
367                 case 1:
368                     result = 0;
369                     break;
370                 case 2:
371                     result = -1;
372                     break;
373                 case 3:
374                     result = -2;
375                     break;
376                 default:
377                     break;
378             }
379             return result;
380         }
381     }
382 }
View Code

Blocks

 1 ///////////////////////////////////////////////////////////
 2 //  Class            :    Blocks.cs
 3 //  CLRVersion        :    4.0.30319.42000
 4 //  NameSpace        :    BenNHTetris
 5 //  Created on        :    2018/5/31 14:32:10
 6 //  Original author    :    JIYONGFEI 
 7 //  JiYF笨男孩博客  :   https://www.cnblogs.com/JiYF/
 8 ///////////////////////////////////////////////////////////
 9 using System;
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Linq;
13 using System.Text;
14 
15 namespace BenNHTetris
16 {
17     class Blocks
18     {
19         public static ArrayList BlockList = new ArrayList();
20         //随机获取一个砖块
21         public static Block GetBlock()
22         {
23             Random random = new Random();
24             int index = random.Next(7);
25             Block block;
26             switch (index)
27             {
28                 case 0:
29                     block = new Block1();
30                     break;
31                 case 1:
32                     block = new Block2();
33                     break;
34                 case 2:
35                     block = new Block3();
36                     break;
37                 case 3:
38                     block = new Block4();
39                     break;
40                 case 4:
41                     block = new Block5();
42                     break;
43                 case 5:
44                     block = new Block6();
45                     break;
46                 case 6:
47                     block = new Block7();
48                     break;
49                 default:
50                     block = new Block1();
51                     break;
52             }
53             return block;
54         }
55     }
56 }
View Code

Canvas

  1 ///////////////////////////////////////////////////////////
  2 //  Class            :    Canvas.cs
  3 //  CLRVersion        :    4.0.30319.42000
  4 //  NameSpace        :    BenNHTetris
  5 //  Created on        :    2018/5/31 11:43:28
  6 //  Original author    :    JIYONGFEI 
  7 //  JiYF笨男孩博客  :   https://www.cnblogs.com/JiYF/
  8 ///////////////////////////////////////////////////////////
  9 using System;
 10 using System.Collections.Generic;
 11 using System.Drawing;
 12 using System.Linq;
 13 using System.Text;
 14 
 15 namespace BenNHTetris
 16 {
 17     class Canvas
 18     {
 19         public int m_rows;                  //行数
 20         public int m_columns;               //列数
 21         public int[,] m_arr;                //画布二维数组
 22         public int m_score;                 //分数
 23         private Block m_curBlock = null;    //当前砖块
 24         private Block m_nextBlock = null;   //下一个砖块
 25         private int m_height;               //当前高度
 26 
 27         /// <summary>
 28         /// 构造画布
 29         /// </summary>
 30         public Canvas()
 31         {
 32             m_rows = 20;        //初始20
 33             m_columns = 20;     //初始20
 34             m_arr = new int[m_rows, m_columns];
 35             for (int i = 0; i < m_rows; i++)
 36             {
 37                 for (int j = 0; j < m_columns; j++)
 38                 {
 39                     m_arr[i, j] = 0;
 40                 }
 41             }
 42             m_score = 0;
 43             m_height = 0;
 44         }
 45 
 46         //定时器  砖块定时下降或无法下降时生成新的砖块
 47         public bool Run()
 48         {
 49             //判断是否为空
 50             lock (m_arr)
 51             {
 52                 if (m_curBlock == null && m_nextBlock == null)
 53                 {
 54                     m_curBlock = Blocks.GetBlock();
 55                     m_nextBlock = Blocks.GetBlock();
 56                     m_nextBlock.RandomShape();
 57                     m_curBlock.SetCenterPos(m_curBlock.Appear(), m_columns / 2 - 1);
 58                     SetArrayValue();
 59                 }
 60                 else if (m_curBlock == null)
 61                 {
 62                     m_curBlock = m_nextBlock;
 63                     m_nextBlock = Blocks.GetBlock();
 64                     m_nextBlock.RandomShape();
 65                     m_curBlock.SetCenterPos(m_curBlock.Appear(), m_columns / 2 - 1);
 66                     SetArrayValue();
 67                 }
 68                 else
 69                 {
 70                     if (m_curBlock.CanDownMove(m_arr, m_rows, m_columns) == true)
 71                     {
 72                         ClearCurBrick();
 73                         m_curBlock.DownMove();
 74                         SetArrayValue();
 75                     }
 76                     else
 77                     {
 78                         m_curBlock = null;
 79                         SetCurHeight();
 80                         ClearRow();
 81                     }
 82                 }
 83                 if (m_score >= 100)
 84                     return false;
 85                 if (m_height < m_rows)
 86                     return true;
 87                 else
 88                     return false;
 89             }
 90         }
 91 
 92         //根据清除当前砖块在m_arr中的值
 93         private void ClearCurBrick()
 94         {
 95             int centerX = m_curBlock._center.X;
 96             int centerY = m_curBlock._center.Y;
 97             for (int i = 0; i < m_curBlock._needRows; i++)
 98             {
 99                 for (int j = 0; j < m_curBlock._needColumns; j++)
100                 {
101                     int realX = m_curBlock._Pos.X - (centerX - i);
102                     int realY = m_curBlock._Pos.Y - (centerY - j);
103                     if (realX < 0 || realX >= m_columns || realY < 0 || realY >= m_rows)
104                     {
105                         continue;
106                     }
107                     else
108                     {
109                         if (m_curBlock._range[i, j] == 0)
110                         {
111                             continue;
112                         }
113                         else
114                         {
115                             m_arr[realX, realY] = 0;
116                         }
117                     }
118                 }
119             }
120         }
121 
122         //根据当前砖块设置m_arr的值
123         public void SetArrayValue()
124         {
125             int centerX = m_curBlock._center.X;
126             int centerY = m_curBlock._center.Y;
127             for (int i = 0; i < m_curBlock._needRows; i++)
128             {
129                 for (int j = 0; j < m_curBlock._needColumns; j++)
130                 {
131                     int realX = m_curBlock._Pos.X - (centerX - i);
132                     int realY = m_curBlock._Pos.Y - (centerY - j);
133                     if (realX < 0 || realX >= m_columns || realY < 0 || realY >= m_rows)
134                     {
135                         continue;
136                     }
137                     else
138                     {
139                         if (m_curBlock._range[i, j] == 0)
140                         {
141                             continue;
142                         }
143                         else
144                         {
145                             m_arr[realX, realY] = 1;
146                         }
147                     }
148                 }
149             }
150         }
151 
152         //判断当前有没有填满的行,有则消除、加分
153         private void ClearRow()
154         {
155             int clearrows = 0;
156             for (int i = m_rows - m_height; i < m_rows; i++)
157             {
158                 bool isfull = true;
159                 for (int j = 0; j < m_columns; j++)
160                 {
161                     if (m_arr[i, j] == 0)
162                     {
163                         isfull = false;
164                         break;
165                     }
166                 }
167                 if (isfull == true)
168                 {
169                     clearrows++;
170                     m_score++;
171                     for (int k = 0; k < m_columns; k++)
172                     {
173                         m_arr[i, k] = 0;
174                     }
175                 }
176             }
177             for (int i = m_rows - 1; i > m_rows - m_height - 1; i--)
178             {
179                 bool isfull = true;
180                 for (int j = 0; j < m_columns; j++)
181                 {
182                     if (m_arr[i, j] == 1)
183                     {
184                         isfull = false;
185                         break;
186                     }
187                 }
188                 if (isfull == true)
189                 {
190                     int n = i;
191                     for (int m = n - 1; m > m_rows - m_height - 2; m--)
192                     {
193                         if (n == 0)
194                         {
195                             for (int k = 0; k < m_columns; k++)
196                             {
197                                 m_arr[n, k] = 0;
198                             }
199                         }
200                         else
201                         {
202                             for (int k = 0; k < m_columns; k++)
203                             {
204                                 m_arr[n, k] = m_arr[m, k];
205                             }
206                             n--;
207                         }
208                     }
209                 }
210             }
211             m_height -= clearrows;
212         }
213 
214         //计算当期高度
215         private void SetCurHeight()
216         {
217             for (int i = 0; i < m_rows; i++)
218             {
219                 for (int j = 0; j < m_columns; j++)
220                 {
221                     if (m_arr[i, j] == 1)
222                     {
223                         m_height = m_rows - i;
224                         return;
225                     }
226                 }
227             }
228         }
229 
230 
231         //左移
232         public void BrickLeft()
233         {
234             lock (m_arr)
235             {
236                 if (m_curBlock != null && m_curBlock.CanLeftMove(m_arr, m_rows, m_columns) == true)
237                 {
238                     ClearCurBrick();
239                     m_curBlock.LeftMove();
240                     SetArrayValue();
241                 }
242             }
243         }
244 
245         //右移
246         public void BrickRight()
247         {
248             lock (m_arr)
249             {
250                 if (m_curBlock != null && m_curBlock.CanRightMove(m_arr, m_rows, m_columns) == true)
251                 {
252                     ClearCurBrick();
253                     m_curBlock.RightMove();
254                     SetArrayValue();
255                 }
256             }
257         }
258 
259         //下移
260         public void BrickDown()
261         {
262             lock (m_arr)
263             {
264                 if (m_curBlock != null && m_curBlock.CanDownMove(m_arr, m_rows, m_columns) == true)
265                 {
266                     ClearCurBrick();
267                     m_curBlock.DownMove();
268                     SetArrayValue();
269                 }
270             }
271         }
272 
273         //变形
274         public void BrickUp()
275         {
276             lock (m_arr)
277             {
278                 if (m_curBlock != null && m_curBlock.CanChange(m_arr, m_rows, m_columns) == true)
279                 {
280                     ClearCurBrick();
281                     m_curBlock.Change();
282                     SetArrayValue();
283                 }
284             }
285         }
286 
287         //
288         public void DrawNewxBrick(Graphics gra, float itemwidth, float itemheight)
289         {
290             int[,] arr = new int[5, 5]{{0,0,0,0,0},
291                                          {0,0,0,0,0},
292                                          {0,0,0,0,0},
293                                          {0,0,0,0,0},
294                                          {0,0,0,0,0,}};
295             switch (m_nextBlock._needColumns)
296             {
297                 case 2:
298                     arr[2, 2] = 1;
299                     arr[2, 3] = 1;
300                     arr[3, 2] = 1;
301                     arr[3, 3] = 1;
302                     break;
303                 case 3:
304                     for (int i = 1, m = 0; i < 4; i++, m++)
305                     {
306                         for (int j = 1, n = 0; j < 4; j++, n++)
307                         {
308                             arr[i, j] = m_nextBlock._range[m, n];
309                         }
310                     }
311                     break;
312                 case 5:
313                     arr = m_nextBlock._range;
314                     break;
315                 default:
316                     return;
317             }
318             for (int i = 0; i < 5; i++)
319             {
320                 for (int j = 0; j < 5; j++)
321                 {
322                     if (arr[i, j] == 1)
323                     {
324                         gra.FillRectangle(Brushes.Orange, j * itemwidth, i * itemheight, itemwidth - 2, itemheight - 2);
325                     }
326                 }
327             }
328         }
329     }
330 }
View Code

4.运行效果展示

初始主界面

游戏过程

游戏结束

程序源代码工程文件下载

 自定义窗体源码下载

posted @ 2018-09-12 09:43  JiYF  阅读(9539)  评论(4编辑  收藏  举报