{
int position_x = rand.Next(hang); int position_y = rand.Next(lie);
if (button[position_x, position_y].Youlei == 0) {
button[position_x, position_y].Youlei = 1;
} else
i = i - 1; }
}
//判断点开的这个按钮周围8个中有几个地雷
private int Getdilei(int row, int col)//x代表行D,y代表列 {
int i, j; int around = 0;
int minRow = (row == 0) ? 0 : row - 1; int maxRow = row + 2;
int minCol = (col == 0) ? 0 : col - 1; int maxCol = col + 2;
for (i = minRow; i < maxRow; i++) {
for (j = minCol; j < maxCol; j++) {
if (!(i >= 0 && i
continue;
if (button[i, j].Youlei == 1) around++; } }
return around; }
//以下递归扫雷,向周围八个发散的递归查找 private void Saolei(int row, int col) {
int minrow1 = (row == 0) ? 0 : row - 1; int mincol1 = (col == 0) ? 0 : col - 1; int maxrow1 = row + 2;
24
int maxcol1 = col + 2;
int leishuliang = Getdilei(row, col); if (leishuliang == 0) {
button[row, col].Enabled = false; for (int m = minrow1; m < maxrow1; m++) {
for (int n = mincol1; n < maxcol1; n++) {
if (!(m >= 0 && m < hang && n >= 0 && n < lie)) continue;
if (!(m == row && n == col) && button[m, n].Enabled == true) Saolei(m, n);
button[m, n].Enabled = false;
button[m, n].Text = Getdilei(m, n).ToString(); if (button[m, n].Text == \) button[m, n].Text = string.Empty; }
} }
}
// 判断是否扫完地雷 private bool Win() {
int zongshu = 0;
for (int i = 0; i < hang; i++) {
for (int j = 0; j < lie; j++) {
if (button[i, j].Youlei == 1 && Convert.ToInt16(button[i, j].Tag) == 1) zongshu++;
}
}
if (zongshu == leishu) return true; else
return false;
}
25
//private void Showlei() //{
// for (int i = 0; i < hang; i++) // {
// for (int j = 0; j < lie; j++) // if (button[i, j].Youlei == 1) // {
// button[i, j].BackgroundImage = Image.FromFile(dileiimage); // }
// }
//}
private void xianshi() {
int l = 0;
for (int i = 0; i < hang; i++) {
for (int j = 0; j < lie; j++) {
if (button[i, j].Youlei == 1) {
button[i, j].BackgroundImage = Image.FromFile(shibai); l++;
//MessageBox.Show(button[i,j].Youlei.ToString()); } } }
}
//每次按开始按钮时所有方块复原- public void Fuyuan() {
for (int i = 0; i < hang; i++) {
for (int j = 0; j < lie; j++) {
button[i, j].Tag = 0; button[i, j].Enabled = true; button[i, j].Text = string.Empty; button[i, j].BackgroundImage = null; if (button[i, j].Youlei == 1)
26
button[i, j].Youlei = 0; }
} Bulei(); yongshi = 0; over = false; restlie = leishu; timer1.Start(); label4.Text = \秒?\;
lei.Text = restlie.ToString() + \颗?\;
button1.BackgroundImage = Image.FromFile(xiaolian); }
//定义timer组件的Tick事件
private void timer1_Tick(object sender, EventArgs e) {
yongshi++;
label4.Text = yongshi.ToString() + \秒?\; if (yongshi >= 60) timer1.Enabled = false; }
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) {
this.Close(); }
private void 中级ToolStripMenuItem_Click(object sender, EventArgs e) {
foreach (Button i in button) {
this.Controls.Remove(i); //i.Dispose(); }
start(40,16,16); ci = 1; zong = 5; fuli(); Fuyuan(); lei.Left=290; button1.Left = 150;
this.Size = new Size(330,433);
27
}
private void 初级ToolStripMenuItem_Click(object sender, EventArgs e) {
foreach (Button i in button) {
this.Controls.Remove(i); //i.Dispose(); }
start(10,9,9); ci = 1; zong = 5; fuli(); Fuyuan();
this.Size = new Size(190,293); }
private void 高级ToolStripMenuItem_Click(object sender, EventArgs e) {
foreach (Button i in button) {
this.Controls.Remove(i); //i.Dispose(); }
start(99,23,23); ci = 1; zong = 2; fuli();
lei.Left = 430; button1.Left =218; Fuyuan();
this.Size = new Size(470, 433); }
private void start(int a,int b,int c) {
restlie =Leishu=a; lie = b; hang = c;
label4.Text = \秒?\;
lei.Text = restlie.ToString() + \颗?\; timer1.Enabled = true;
button1.BackgroundImage = Image.FromFile(xiaolian); Leizheng(); Bulei();
28