C#掃雷代碼:
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Frm1 : Form
- {
- #region 變量
- public int qishu;
- public static int ls=20, hs=11;
- public int lgs = 30; //列數,行數。
- public int bl=0; //是否重新布雷
- public int time ;
- public int[,] bulei = new int[ls, hs]; //bulei數組的值0代表無雷,1-8代表雷的個數,9代表雷
- public int ceshi = 0;
- public int[,] click = new int[ls, hs]; //10表示要清除既是單擊的 11表示右擊一次,12表示右擊兩次 16表示以清除過了
- public int sum = 0;
- public int timerEnable = 0; //游戲所處的狀態 0是游戲中
- #endregion
- public Frm1()
- {
- InitializeComponent();
- }
- ///
- /// 標識雷的個數 和可插的旗數
- ///
- public void Qishu()
- {
- if (qishu <= lgs)
- {
- gamesource.Text = "00" + (lgs - qishu)。ToString();
- }
- else
- {
- gamesource.Text = "00" + (lgs - qishu)。ToString();
- }
- }
- ///
- /// 畫方格
- ///
- /// <實例對象>
- /// <要畫圖的位置>
- /// <要畫圖的位置>
- /// <畫圖使用的顏色>
- public void Dline(Graphics dline,int mx1,int my1,Pen mypen)
- {
- dline.DrawLine(mypen , mx1, my1, mx1 + 20, my1);
- dline.DrawLine(mypen, mx1, my1, mx1, my1 + 20);
- dline.DrawLine(new Pen(Color.Black), mx1 + 20, my1, mx1 + 20, my1 + 20);
- dline.DrawLine(new Pen(Color.Black), mx1, my1 + 20, mx1 + 20, my1 + 20);
- }
- ///
- /// 看是否可以向周圍移動
- ///
- /// <移動的基本X坐標>
- /// <移動的基本Y坐標>
- public void PanjIE(int x,int y)
- {
- Pl(x, y);
- if (y - 1 >= 0)
- {
- Pl(x,y-1);
- if (x - 1 >= 0)
- {
- Pl(x-1,y-1);
- }
- if (x + 1
- {
- Pl(x + 1, y - 1);
- }
- }
- if (y + 1 < hs)
- {
- Pl(x,y+1);
- if (x - 1 >= 0)
- {
- Pl(x - 1, y + 1);
- }
- if (x + 1 < ls)
- {
- Pl(x + 1, y +1);
- }
- }
- if (x - 1 >= 0)
- {
- Pl(x - 1 ,y);
- }
- if (x + 1 < ls)
- {
- Pl(x + 1,y);
- }
- }
- ///
- /// 判斷是否要清清除
- ///
- /// <要判斷的基本X坐標>
- /// <要判斷的基本Y坐標>
- public void Pl(int x, int y)
- {
- if (bulei[x, y] == 0 && click[x, y] != 10 && click[x, y] != 11 && click[x, y] != 16)
- {
- click[x, y] = 10;
- PanjIE(x,y);
- }
- if (bulei[x, y] > 0 && bulei[x, y] < 9 && click[x, y] != 10 && click[x, y] != 11 && click[x, y] != 16)
- {
- click[x, y] = 10;
- }
- }
- ///
- /// 初始化數組,布雷,判斷周圍的雷數
- ///
- public void Blpl()
- {
- sum = ls * hs;
- qishu = 0;
- int mx,my;
- //初始化數組 9表示雷數
- int mx1,my1;
- int sum1=0;
- for (mx = 0; mx < ls; mx++)
- {
- for (my = 0; my
- {
- bulei[mx, my] = 0;
- click[mx, my] = 0;
- }
- }
- while(sum1
- //決定雷的個數和雷德位置
- {
- sum1= 0;
- Random r = new Random((int)DateTime.Now.Ticks);
- mx = r.Next(ls-1);
- my = r.Next(hs-1);
- bulei[mx,my] = 9;
- for (mx1 = 0; mx1 < ls; mx1++)
- {
- for (my1 = 0; my1 < hs; my1++)
- {
- sum1 += bulei[mx1, my1];
- }
- }
- }
- //判斷周圍雷的個數
- for (mx = 0; mx < ls; mx++)
- {
- for (my = 0; my < hs; my++)
- {
- if (bulei[mx,my] != 9)
- {
- if (mx - 1 >= 0)
- {
- if (bulei[mx - 1, my] == 9)
- {
- bulei[mx, my]++;
- }
- if (my - 1 >= 0 && bulei[mx - 1, my - 1] == 9)
- {
- bulei[mx, my]++;
- }
- if ( my + 1 < hs && bulei[mx - 1, my + 1] == 9)
- {
- bulei[mx , my]++;
- }
- }
- if (mx +1
- {
- if (bulei[mx + 1, my] == 9)
- {
- bulei[mx, my]++;
- }
- if (my- 1 >= 0 && bulei[mx+ 1, my - 1] == 9)
- {
- bulei[mx, my]++;
- }
- if (my + 1 < hs && bulei[mx + 1, my + 1] == 9)
- {
- bulei[mx, my]++;
- }
- }
- if(my-1>=0 && bulei[mx,my-1]==9)
- {
- bulei[mx, my]++;
- }
- if (my +1
- {
- bulei[mx, my]++;
- }
- }
- }
- }
- }
- ///
- /// 右擊的重繪
- ///
- /// >
- public void Rdraw(Graphics dline)
- {
- string text;
- int mx,my;
- int mx1, my1;
- for (mx = 0;mx < ls; mx++)
- {
- for (my = 0; my < hs; my++)
- {
- mx1 = mx * 2 + mx * 20 + 4;
- my1 = my * 2 + my * 20 + 4;
- if ( click[mx, my] == 11)
- {
- //第一次右擊畫旗
- dline.DrawLine(new Pen(Color.White), mx1, my1, mx1 + 20, my1 + 20);
- dline.DrawLine(new Pen(Color.White), mx1, my1, mx1, my1 + 20);
- dline.DrawLine(new Pen(Color.Black), mx1 + 20, my1, mx1 + 20, my1 + 20);
- dline.DrawLine(new Pen(Color.Black), mx1, my1 + 20, mx1 + 20, my1 + 20);
- dline.FillRectangle(new SolidBrush(Color.FromArgb(224, 224, 224)), mx1 + 1, my1 + 1, 18, 18);
- text = "P";
- dline.DrawString(text, new Font("宋體", 15), new SolidBrush(Color.Red), mx1 + 2, my1 + 2);
- }
- if ( click[mx, my] == 12)
- {
- //第二次右擊畫問號
- dline.DrawLine(new Pen(Color.White), mx1, my1, mx1 + 20, my1 + 20);
- dline.DrawLine(new Pen(Color.White), mx1, my1, mx1, my1 + 20);
- dline.DrawLine(new Pen(Color.Black), mx1 + 20, my1, mx1 + 20, my1 + 20);
- dline.DrawLine(new Pen(Color.Black), mx1, my1 + 20, mx1 + 20, my1 + 20);
- dline.FillRectangle(new SolidBrush(Color.FromArgb(224, 224, 224)), mx1 + 1, my1 + 1, 18, 18);
- text = "?";
- dline.DrawString(text, new Font("宋體", 15), new SolidBrush(Color.Black), mx1 + 2, my1 + 2);
- }
- if (click[mx, my] == 13)
- {
- //第三次右擊 恢復原樣
- click[mx, my] = 2;
- dline.DrawLine(new Pen(Color.White), mx1, my1, mx1 + 20, my1 + 20);
- dline.DrawLine(new Pen(Color.White), mx1, my1, mx1, my1 + 20);
- dline.DrawLine(new Pen(Color.Black), mx1 + 20, my1, mx1 + 20, my1 + 20);
- dline.DrawLine(new Pen(Color.Black), mx1, my1 + 20, mx1 + 20, my1 + 20);
- dline.FillRectangle(new SolidBrush(Color.FromArgb(224, 224, 224)), mx1 + 1, my1 + 1, 18, 18);
- }
- }
- }
- }
- ///
- /// 初始化屏幕 (包還所有的)
- ///
- public void Startscr()
- {
- Graphics dline = labdraw.CreateGraphics();
- time = 0;
- Blpl();
- timer1.Enabled = false;
- Color c = Color.FromArgb(224, 224, 224);
- labdraw.CreateGraphics()。Clear(c);
- Drawscreen(dline);
- timerEnable = 0;
- labtime.Text = ("000");
- }
- ///
- /// 畫界面
- ///
- /// >
- public void Drawscreen(Graphics dline)
- {
- sum = 0; //得到沒清楚的格子數
- int mx, my;
- int mx1 = 0, my1 = 0; //得到要畫圖的坐標
- string text;
- for (mx = 0; mx < ls; mx++)
- {
- for (my = 0; my < hs; my++)
- {
- //畫圖地位置 mx,my分別是屏幕x,y軸
- mx1 = mx * 2 + mx * 20 + 4;
- my1 = my * 2 + my * 20 + 4;
- if (click[mx, my] != 10 && click[mx, my] != 12 && click[mx, my] != 16 && click[mx, my] != 15)
- {
- //下面用於畫立體形矩形;
- sum++;
- Pen mypen = new Pen(Color.Gray);
- Dline(dline, mx1, my1, mypen);
- dline.FillRectangle(new SolidBrush(Color.FromArgb(224, 224, 224)), mx1 + 1, my1 + 1, 18, 18);
- }
- if ((click[mx, my] == 10 || click[mx, my] == 16))
- {
- click[mx, my] = 16; //清除的格子
- //提示周圍的雷數
- if (bulei[mx, my] <= 8 && bulei[mx, my] > 0)
- {
- text = bulei[mx, my].ToString();
- dline.DrawString(text, new Font("宋體", 15), new SolidBrush(Color.Black), mx1 + 2, my1 + 2);
- }
- //顯示雷
- if ((bulei[mx, my] == 9 || bulei[mx, my] == 100))
- {
- text = "*";
- dline.DrawString(text, new Font("宋體", 15), new SolidBrush(Color.Black), mx1 + 2, my1 + 2);
- if (bulei[mx, my] == 100)
- {
- //被單擊的雷上畫上叉號
- dline.DrawLine(new Pen(Color.Red), mx1, my1, mx1 + 20, my1 + 20);
- dline.DrawLine(new Pen(Color.Red), mx1, my1 + 20, mx1 + 20, my1);
- }
- }
- }
- }
- //用於畫旗 和問號
- Rdraw(dline);
- }
- }
- ///
- /// 當為雷時的算法
- ///
- /// >
- /// >
- public void Panlei(int x, int y)
- {
- if (bulei[x, y] != 0)
- {
- if (bulei[x, y] == 9)
- {
- timer1.Enabled = false; //使計時器停止
- timerEnable = 1; //使游戲出游結束狀態
- bulei[x, y] = 100;
- int i, j;
- for (i = 0; i < ls; i++)
- {
- for (j = 0; j < hs; j++)
- {
- if (bulei[i, j] == 9 || bulei[i, j] ==100)
- {
- click[i, j] = 10; //顯示所有的雷
- }
- }
- }
- }
- if (bulei[x, y] > 0 && bulei[x, y] < 9)
- {
- click[x, y] = 10;
- }
- }
- else
- {
- PanjIE(x, y);
- }
- }
- ///
- /// 回到游戲開始的界面
- ///
- ///
- ///
- private void btnreplay_Click(object sender, EventArgs e)
- {
- //初始化一切 包括布雷 ,界面, click數組
- Startscr();
- }
- ///
- /// 計算游戲所用的時間
- ///
- ///
- ///
- private void timer1_Tick(object sender, EventArgs e)
- {
- time++;
- if (time <= 99)
- {
- if (time < 10)
- {
- labtime.Text = ("00" + time.ToString());
- }
- else
- {
- labtime.Text = ("0" + time.ToString());
- }
- }
- else
- {
- if (time < 99)
- {
- labtime.Text = (time.ToString());
- }
- else
- {
- timer1.Enabled = false;
- MessageBox.Show("RIME OVER ");
- time = 0;
- timerEnable = 1;
- }
- }
- }
- ///
- /// 在啟動的游戲時初始化界面
- ///
- ///
- ///
- private void labdraw_Paint(object sender, PaintEventArgs e)
- {
- Graphics dline = e.Graphics;
- if (bl == 0)
- {
- Blpl();
- Drawscreen(dline);
- }
- else
- {
- Drawscreen(dline);
- }
- }
- ///
- /// //單擊時觸發的事件
- ///
- ///
- ///
- private void labdraw_MouseClick(object sender, MouseEventArgs e)
- {
- bl = 10;
- int i=0, j=0;
- if (timerEnable == 0) //判斷游戲是否處於結束狀態
- {
- timer1.Enabled = true;
- Graphics dline = labdraw.CreateGraphics(); //將LAB1建立成圖形對象
- int mx = 0, my = 0;
- int mx2 = 0, my2 = 0;
- int mx3 = 0, my3 = 0;
- //判斷鼠標所點位置
- mx2 = e.X;
- my2 = e.Y;
- for (i = 0; i < ls; i++)
- {
- for (j = 0; j < hs; j++)
- {
- mx = 4 + i * 2 + i * 20;
- my = 4 + j * 2 + j * 20;
- //找到單擊位置代表的數組
- if (mx < mx2 && mx + 20+4 > mx2 && my < my2 && my2 < my + 20+4 )
- {
- mx3 = i;
- my3 = j;
- break;
- }
- }
- if (j < 9)
- {
- break;
- }
- }
- //判斷是左擊還是右擊
- if (e.Button == MouseButtons.Right && click[mx3, my3] != 10 && mx3 >= 0 && my3 >= 0 && click[mx3, my3] != 16)
- {
- //第2次右擊
- if (click[mx3, my3] == 11 && click[mx3, my3] != 10)
- {
- click[mx3, my3] = 12;
- --qishu;
- }
- else
- {
- //第1次右擊
- if (click[mx3, my3] != 12 && click[mx3, my3] != 10)
- {
- if (qishu
- {
- click[mx3, my3] = 11;
- qishu++;
- }
- }
- if (click[mx3, my3] == 12 && click[mx3, my3] != 10)
- {
- click[mx3, my3] = 13;
- }
- }
- //右擊的繪圖事件
- Rdraw(dline);
- Qishu();
- }
- else
- {
- //左擊
- if (click[mx3, my3] != 11 && click[mx3, my3] != 16)
- {
- click[mx3, my3] = 15;
- Color c = Color.FromArgb(224, 224, 224);
- //清屏 用背景顏色
- labdraw.CreateGraphics()。Clear(c);
- Panlei (mx3, my3);
- Drawscreen(dline);
- }
- if (bulei[mx3, my3] == 100)
- {
- MessageBox.Show("GAME OVER");
- timer1.Enabled = false;
- timerEnable = 1;
- }
- }
- if (sum == lgs)
- {
- MessageBox.Show("GAME WIN");
- timer1.Enabled = false;
- timerEnable = 1;
- }
- }
- }
- private void Frm1_Load(object sender, EventArgs e)
- {
- Qishu(); //輸出雷的個數
- // this.Height = hs * 22 + 20;
- // this.Width = ls * 22 + 8;
- }
- }
- }
【編輯推薦】