基於.NET平台網路編程入門實戰系列 二

來源:互聯網
上載者:User

今天的主要任務就是把單擊版的搞定,這是過渡到網路版的必備過程。

如果沒有玩過掃雷的,建議先去體驗一下;體驗完後,自己嘗試寫出掃雷的演算法;經過思考揣摩推敲才能有收穫。不建議一上來就下載源碼剖析,跟蹤代碼!

現在我來說下我的思路,如果有更優雅的演算法,歡迎大家分享------競爭引發技術進步,分享推動技術進步!

【一】單機版掃雷劃為兩層

第一層為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         }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.