文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>扫雷

扫雷

时间:2010-09-14  来源:阿司匹林

扫雷代码

 

   1 using System;

  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 
  9 namespace SaoLei
 10 {
 11     public partial class Form1 : Form
 12     {
 13         ClsBoomSet boomset = ClsBoomSet.GetBoomSet();
 14         static Button[,] btn = new Button[10, 10];
 15 
 16         /// <summary>
 17         /// 保存随机数
 18         /// </summary>
 19         private static int[] arrayRand;
 20 
 21         /// <summary>
 22         /// 标记地雷位置
 23         /// </summary>
 24         /// 
 25         private static string[,] str;
 26 
 27         /// <summary>
 28         /// 标记周围地雷数
 29         /// </summary>
 30         private static string[,] arrayBoomNum = new string[10, 10];
 31 
 32         /// <summary>
 33         /// 标记周围标记数
 34         /// </summary>
 35         private static int[,] arrayBoomNumCheck = new int[10, 10];
 36 
 37         /// <summary>
 38         /// 控制递归
 39         /// </summary>
 40         private static int sum = 0;
 41 
 42         public Form1()
 43         {
 44             InitializeComponent();
 45         }
 46 
 47         private void Form1_Load(object sender, EventArgs e)
 48         {
 49 
 50         }
 51 
 52         /// <summary>
 53         /// 放置地雷
 54         /// </summary>
 55         /// <param name="sender"></param>
 56         /// <param name="e"></param>
 57         private void button1_Click(object sender, EventArgs e)
 58         {
 59             //生成雷区
 60             str = new string[boomset.Number, boomset.Number];
 61             arrayRand = ClsBoomSet.BoomSet();
 62             BoomBuild(0, 29);
 63             SetBoom();
 64             button1.Enabled = false;
 65             BoomInitialise();
 66         }
 67 
 68         /// <summary>
 69         /// 动态生成按钮
 70         /// </summary>
 71         /// <param name="l"></param>
 72         /// <param name="w"></param>
 73         private void BoomBuild(int l, int w)
 74         {
 75             for ( int i = 0 ; i < 10 ; i++ )
 76             {
 77                 for ( int j = 0 ; j < 10 ; j++ )
 78                 {
 79                     btn[i, j] = new Button();
 80                     btn[i, j].Location = new Point(12 + w, 48 + l);
 81                     btn[i, j].Size = new Size(30, 30);
 82                     btn[i, j].TabStop = false;
 83 
 84                     btn[i, j].Name = ( i == 0 ) ? j.ToString() : i.ToString() + j.ToString();
 85 
 86                     btn[i, j].Text = "";
 87                     btn[i, j].Visible = true;
 88                     btn[i, j].Image = imageList1.Images[1];
 89                     this.Controls.Add(btn[i, j]);
 90                     w += 29;
 91                     int iNum = BoomNumber(i, j);
 92                     if ( iNum == 0 )
 93                         btn[i, j].Text = "";
 94                     if ( iNum != 0 )
 95                         btn[i, j].Text = iNum.ToString();
 96                 }
 97                 l += 29;
 98                 w = 29;
 99             }
100         }
101 
102         /// <summary>
103         /// 设置地雷
104         /// </summary>
105         private void SetBoom()
106         {
107             foreach ( Button button in btn )
108             {
109                 for ( int i = 0 ; i < boomset.Number ; i++ )
110                 {
111                     if ( button.Name == arrayRand[i].ToString() )
112                         button.Text = " ";
113                 }
114             }
115             for ( int i = 0 ; i < 10 ; i++ )
116             {
117                 for ( int j = 0 ; j < 10 ; j++ )
118                 {
119                     btn[i, j].MouseClick += new MouseEventHandler(btn_Click);
120                     btn[i, j].MouseUp += new MouseEventHandler(btn_RightClick);
121                     btn[i, j].MouseDoubleClick += new MouseEventHandler(btn_doubleClick);
122 
123                 }
124             }
125         }
126 
127         /// <summary>
128         /// 标记地雷位置
129         /// </summary>
130         private static void BoomInitialise()
131         {
132             for ( int i = 0 ; i < 10 ; i++ )
133                 for ( int j = 0 ; j < 10 ; j++ )
134                     str[i, j] = "0";
135             int x, y;
136             for ( int i = 0 ; i < arrayRand.Length ; i++ )
137             {
138                 x = arrayRand[i] / 10;
139                 y = arrayRand[i] % 10;
140                 str[x, y] = "1";
141             }
142             foreach ( Button b1 in btn )
143             {
144                 int i, j;
145                 i = Convert.ToInt32(b1.Name) / 10;
146                 j = Convert.ToInt32(b1.Name) % 10;
147                 if ( b1.Text == "" )
148                     arrayBoomNum[i, j] = ( BoomNumber(i, j) == 0 ) ? "" : BoomNumber(i, j).ToString();
149             }
150             for ( int i = 0 ; i < 10 ; i++ )
151                 for ( int j = 0 ; j < 10 ; j++ )
152                     arrayBoomNumCheck[i, j] = BoomNumCheck(i, j);
153         }
154 
155         /// <summary>
156         /// 计算周围标记数
157         /// </summary>
158         /// <param name="x"></param>
159         /// <param name="y"></param>
160         /// <returns></returns>
161         private static int BoomNumCheck(int x, int y)
162         {
163             int[] a = { 1, -1, 0 };
164             int[] b = { 1, -1, 0 };
165             int n = 0;
166             for ( int i = 0 ; i < 3 ; i++ )
167             {
168                 for ( int j = 0 ; j < 3 ; j++ )
169                 {
170                     if ( ( x + a[i] ) < 0 || ( x + a[i] ) > 9 || ( y + b[j] ) < 0 || ( y + b[j] ) > 9 )
171                         continue;
172                     if ( a[i] == 0 && b[j] == 0 )
173                         continue;
174                     else if ( str[x + a[i], y + b[j]] == "1" )
175                     {
176                         if ( arrayBoomNum[x + a[i], y + b[j]] == "" )
177                             continue;
178                         else
179                             n += Convert.ToInt32(arrayBoomNum[x + a[i], y + b[j]]);
180                     }
181                     else
182                         continue;
183                 }
184             }
185             return n;
186         }
187 
188         /// <summary>
189         /// 排雷事件
190         /// </summary>
191         /// <param name="sender"></param>
192         /// <param name="e"></param>
193         void btn_Click(object sender, MouseEventArgs e)
194         {
195             Button b = (Button)sender;
196             if ( e.Button == MouseButtons.Left )
197             {
198                 int i, j;
199                 i = Convert.ToInt32(b.Name) / 10;
200                 j = Convert.ToInt32(b.Name) % 10;
201                 if ( b.Text == "" )
202                     b.Text = arrayBoomNum[i, j];
203                 if ( b.Text == " " )
204                 {
205                     MessageBox.Show("You lose!");
206                     BoomClear();
207                 }
208                 BoomBreak(b);
209             }
210 
211 
212         }
213 
214         /// <summary>
215         /// 插旗
216         /// </summary>
217         /// <param name="sender"></param>
218         /// <param name="e"></param>
219         void btn_RightClick(object sender, MouseEventArgs e)
220         {
221             Button b = (Button)sender;
222             if ( e.Button == MouseButtons.Right )
223             {
224                 Button button = (Button)sender;
225                 button.Image = imageList1.Images[2];
226             }
227 
228 
229         }
230 
231         /// <summary>
232         ///双击事件 
233         /// </summary>
234         /// <param name="sender"></param>
235         /// <param name="e"></param>
236         void btn_doubleClick(object sender, EventArgs e)
237         {
238             //Button b = (Button)sender;
239             //int i, j;
240             //i = Convert.ToInt32(b.Name) / 10;
241             //j = Convert.ToInt32(b.Name) % 10;
242             //    b.Text = "1";
243             //if ( str[i, j] == "1" )
244             //    return;
245             //if ( arrayBoomNum[i, j] != "" )
246             //{
247             //    btn[i, j].Text = arrayBoomNum[i, j];
248             //    btn[i, j].Enabled = false;
249             //    btn[i, j].Image = null;
250             //    return;
251             //}
252         }
253 
254         /// <summary>
255         /// 清块
256         /// </summary>
257         /// <param name="i"></param>
258         /// <param name="j"></param>
259         private static void BoomBreakWork(int i, int j)
260         {
261             if ( i < 0 || i > 9 || j < 0 || j > 9 )
262                 return;
263             if ( str[i, j] == "1" )
264                 return;
265             if ( arrayBoomNum[i, j] != "" )
266             {
267                 btn[i, j].Text = arrayBoomNum[i, j];
268                 btn[i, j].Enabled = false;
269                 btn[i, j].Image = null;
270                 return;
271             }
272             if ( btn[i, j].Enabled == false )
273                 return;
274             if ( arrayBoomNumCheck[i, j] >= 3 )
275                 return;
276             else
277             {
278                 if ( sum == 4 )
279                     return;
280                 btn[i, j].Enabled = false;
281                 btn[i, j].Image = null;
282                 BoomBreakWork(i - 1, j - 1);
283                 BoomBreakWork(i, j - 1);
284                 BoomBreakWork(i + 1, j - 1);
285                 BoomBreakWork(i + 1, j);
286                 BoomBreakWork(i + 1, j + 1);
287                 BoomBreakWork(i, j + 1);
288                 BoomBreakWork(i - 1, j + 1);
289                 BoomBreakWork(i - 1, j);
290             }
291 
292         }
293         private static void BoomBreak(Button b)
294         {
295             int i, j;
296             i = Convert.ToInt32(b.Name) / 10;
297             j = Convert.ToInt32(b.Name) % 10;
298             BoomBreakWork(i, j);
299         }
300 
301         /// <summary>
302         /// 雷数计算
303         /// </summary>
304         /// <param name="x"></param>
305         /// <param name="y"></param>
306         /// <returns></returns>
307         private static int BoomNumber(int x, int y)
308         {
309             int[] a = { 1, -1, 0 };
310             int[] b = { 1, -1, 0 };
311             int n = 0;
312             for ( int i = 0 ; i < 3 ; i++ )
313             {
314                 for ( int j = 0 ; j < 3 ; j++ )
315                 {
316                     if ( ( x + a[i] ) < 0 || ( x + a[i] ) > 9 || ( y + b[j] ) < 0 || ( y + b[j] ) > 9 )
317                         continue;
318                     if ( a[i] == 0 && b[j] == 0 )
319                         continue;
320                     else if ( str[x + a[i], y + b[j]] == "1" )
321                         n++;
322                     else
323                         continue;
324                 }
325             }
326             return n;
327         }
328 
329         /// <summary>
330         /// 游戏结束,显示地雷位置
331         /// </summary>
332         private void BoomClear()
333         {
334             int i, j;
335             foreach ( Button b in btn )
336             {
337                 i = Convert.ToInt32(b.Name) / 10;
338                 j = Convert.ToInt32(b.Name) % 10;
339                 if ( b.Text == "" )
340                 {
341                     b.Enabled = false;
342                     b.Image = null;
343                     b.Text = ( BoomNumber(i, j) == 0 ) ? "" : BoomNumber(i, j).ToString();
344                 }
345                 if ( b.Text == " " )
346                 {
347                     b.Image = imageList1.Images[0];
348                 }
349             }
350         }
351 
352         /// <summary>
353         /// 重新开始
354         /// </summary>
355         /// <param name="sender"></param>
356         /// <param name="e"></param>
357         private void button2_Click_1(object sender, EventArgs e)
358         {
359             if ( btn != null )
360             {
361                 foreach ( Button b in btn )
362                     this.Controls.Remove(b);
363             }
364             button1.Enabled = true;
365         }
366 
367         private void button3_Click_1(object sender, EventArgs e)
368         {
369             this.Close();
370         }
371 
372         private void 简单ToolStripMenuItem_Click_1(object sender, EventArgs e)
373         {
374             boomset.Number = 10;
375             if ( btn != null )
376             {
377                 foreach ( Button b in btn )
378                     this.Controls.Remove(b);
379             }
380             button1.Enabled = true;
381         }
382 
383         private void 不简单ToolStripMenuItem_Click_1(object sender, EventArgs e)
384         {
385             boomset.Number = 15;
386             if ( btn != null )
387             {
388                 foreach ( Button b in btn )
389                     this.Controls.Remove(b);
390             }
391             button1.Enabled = true;
392         }
393 
394         private void 变态ToolStripMenuItem_Click_1(object sender, EventArgs e)
395         {
396             boomset.Number = 30;
397             if ( btn != null )
398             {
399                 foreach ( Button b in btn )
400                     this.Controls.Remove(b);
401             }
402             button1.Enabled = true;
403         }
404     }
405 }
406 
407 
408 
409 using System;
410 using System.Collections.Generic;
411 using System.Text;
412 
413 namespace SaoLei
414 {
415     class ClsBoomSet
416     {
417         private static string[,] arrayBtn;
418 
419         private static int iNum;//标记雷区大小
420 
421         private static ClsBoomSet clsboomset;
422 
423         public int Number
424         {
425             set
426             {
427                 iNum = value;
428             }
429             get
430             {
431                 return iNum;
432             }
433         }
434 
435         public ClsBoomSet()
436         {
437 
438         }
439 
440         public static ClsBoomSet GetBoomSet()
441         {
442             if ( clsboomset == null )
443             {
444                 clsboomset = new ClsBoomSet();
445                 iNum = 10;
446             }
447             return clsboomset;
448         }
449 
450         /// <summary>
451         /// 设置哪个坐标点有地雷
452         /// </summary>
453         /// <returns></returns>
454         public static int[] BoomSet()
455         {
456             arrayBtn = new string[iNum, iNum];
457             for ( int i = 0 ; i < iNum ; i++ )
458                 for ( int j = 0 ; j < iNum ; j++ )
459                     arrayBtn[i, j] = i.ToString() + j.ToString();
460             Random rand = new Random();
461             int[] arrayBoomSet = new int[iNum];
462             for ( int i = 0 ; i < arrayBoomSet.Length ; i++ )
463                 arrayBoomSet[i] = rand.Next(0, 99);
464             return arrayBoomSet;
465         }
466 
467 
468     }
469 }
470 

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载