今天的主要任務就是把單擊版的搞定,這是過渡到網路版的必備過程。
如果沒有玩過掃雷的,建議先去體驗一下;體驗完後,自己嘗試寫出掃雷的演算法;經過思考揣摩推敲才能有收穫。不建議一上來就下載源碼剖析,跟蹤代碼!
現在我來說下我的思路,如果有更優雅的演算法,歡迎大家分享------競爭引發技術進步,分享推動技術進步!
【一】單機版掃雷劃為兩層
第一層為Button,Button蓋在Lable上,Lable被隱藏在Button下面;
第二層為Lable, Lable上的背景圖片改成地雷圖案表示有雷。
雷區的大家我定為20*20.
private const int Xcount = 20;
private const int Ycount = 20;
添加Lable的代碼如下:
1 private Label[] labels2 = new Label[Xcount * Ycount];
2 private Label[,] labels = new Label[Xcount, Ycount];
3 int indexOfLable = 0;
4 private void CreateLable()
5 {
6 int[] ints = new int[400];
7 Random rd = new Random();
8 for (int i = 0; i < 400; i++)
9 {
10
11 ints[i] = rd.Next(1, 400);
12 }
13 for (int i = 0; i < Xcount; i++)
14 {
15 for (int j = 0; j < Ycount; j++)
16 {
17 Label lb = new Label();
18 lb.Location = new Point(i * 30, 30 * j);
19 lb.Size = new Size(30, 30);
20 lb.BorderStyle = BorderStyle.Fixed3D;
21 this.Controls.Add(lb);
22 labels[i, j] = lb;
23 labels2[indexOfLable] = lb;
24 indexOfLable++;
25 }
26 }
27 for (int i = 0; i < ints.Length; i++)
28 {
29 if (ints[i] < 40)
30 {
31 labels2[i].Image = Properties.Resources.標準地雷;
32 }
33 }
34 }