public void InitMap() { int n, m; double mFactor = 0.15; switch (listBox1.SelectedIndex) { case 0: mFactor = 0.08; break; case 1: mFactor = 0.166; break; case 2: mFactor = 0.333; break; case 3: mFactor = 0.666; break; default: mFactor = 0.08; break; } bcount = 0; count = 0; if (!(buttons is null)) { for (n = 0; n < onumy; n++) { for (m = 0; m < onumx; m++) { buttons[n, m].Visible = false; this.Controls.Remove(buttons[n, m]); } } } buttons = new ButtonVars[numy, numx]; for (n = 0; n < numy; n++) { for (m = 0; m < numx; m++) { buttons[n, m] = new ButtonVars(); buttons[n, m].x = m; buttons[n, m].y = n; buttons[n, m].Location = new Point(m * 20 + 20, n * 20 + 40); buttons[n, m].Width = 19; buttons[n, m].Height = 19; buttons[n, m].Visible = true; buttons[n, m].Name = "btn" + Convert.ToString(n) + Convert.ToString(m); buttons[n, m].Click += new System.EventHandler(this.btn_Click); buttons[n, m].Paint += new System.Windows.Forms.PaintEventHandler(this.btn_Paint); buttons[n, m].Text = " "; this.Controls.Add(buttons[n, m]); if (rand.NextDouble() < mFactor) { buttons[n, m].bomb = 'B'; bcount++; } else { buttons[n, m].bomb = ' '; } } } }
private void btn_Click(object sender, EventArgs e) { ButtonVars btn = (ButtonVars)sender; int x = btn.x; int y = btn.y; if (btn.bomb == 'B') { btn.Text = "*"; MessageBox.Show("Boom!", "MineSweeper", MessageBoxButtons.OK); } else { btn.Text = CountSurroundingBombs(buttons[y, x]).ToString(); if (y > 0 & x > 0) { buttons[y - 1, x - 1].Text = CountSurroundingBombs(buttons[y - 1, x - 1]).ToString(); count++; } if (y > 0) { buttons[y - 1, x].Text = CountSurroundingBombs(buttons[y - 1, x]).ToString(); count++; } if (y > 0 & x < 8) { buttons[y - 1, x + 1].Text = CountSurroundingBombs(buttons[y - 1, x + 1]).ToString(); count++; } if (x > 0) { buttons[y, x - 1].Text = CountSurroundingBombs(buttons[y, x - 1]).ToString(); count++; } if (x < 8) { buttons[y, x + 1].Text = CountSurroundingBombs(buttons[y, x + 1]).ToString(); count++; } if (y < 8 & x > 0) { buttons[y + 1, x - 1].Text = CountSurroundingBombs(buttons[y + 1, x - 1]).ToString(); count++; } if (y < 8) { buttons[y + 1, x].Text = CountSurroundingBombs(buttons[y + 1, x]).ToString(); count++; } if (y < 8 & x < 8) { buttons[y + 1, x + 1].Text = CountSurroundingBombs(buttons[y + 1, x + 1]).ToString(); count++; } count++; if (bcount + count >= 81) { MessageBox.Show("Complete!", "MineSweeper", MessageBoxButtons.OK); } } }
private void btn_Paint(object sender, PaintEventArgs e) { ButtonVars btn = (ButtonVars)sender; if (btn.Text == "*") { RectangleF destRect = new RectangleF(0, 0, 21, 21); RectangleF srcRect = new RectangleF(tick * imgExplosion.Width / 13.0f, 0f, imgExplosion.Width / 13.0f, imgExplosion.Height); e.Graphics.DrawImage(imgExplosion, destRect, srcRect, GraphicsUnit.Pixel); if (tick > 11) { btn.Text = " "; timer1.Enabled = false; } } //else //{ // e.Graphics.DrawString(btn.Text, new Font("Courier New", 8), Brushes.Black, 0, 0); //} }
public int CountSurroundingBombs(ButtonVars btn) { int sum = 0; int y = btn.y; int x = btn.x; if (y > 0) { if (x > 0) { sum += buttons[y - 1, x - 1].bomb == 'B' ? 1 : 0; } sum += buttons[y - 1, x].bomb == 'B' ? 1 : 0; if (x < numx - 1) { sum += buttons[y - 1, x + 1].bomb == 'B' ? 1 : 0; } } if (x > 0) { sum += buttons[y, x - 1].bomb == 'B' ? 1 : 0; } if (x < numx - 1) { sum += buttons[y, x + 1].bomb == 'B' ? 1 : 0; } if (y < numy - 1) { if (x > 0) { sum += buttons[y + 1, x - 1].bomb == 'B' ? 1 : 0; } sum += buttons[y + 1, x].bomb == 'B' ? 1 : 0; if (x < numx - 1) { sum += buttons[y + 1, x + 1].bomb == 'B' ? 1 : 0; } } return(sum); }
public Form2() { int n, m; System.Random rand = new System.Random(); InitializeComponent(); bcount = 0; buttons = new ButtonVars[9, 9]; for (n = 0; n < 9; n++) { for (m = 0; m < 9; m++) { buttons[n, m] = new ButtonVars(); buttons[n, m].x = m; buttons[n, m].y = n; buttons[n, m].Location = new Point(m * 20, n * 20); buttons[n, m].Width = 19; buttons[n, m].Height = 19; buttons[n, m].Visible = true; buttons[n, m].Name = "btn" + Convert.ToString(n) + Convert.ToString(m); buttons[n, m].Click += new System.EventHandler(this.btn_Click); this.Controls.Add(buttons[n, m]); if (rand.NextDouble() < 0.125) { buttons[n, m].bomb = 'B'; bcount++; } else { buttons[n, m].bomb = ' '; } } } count = 0; }
private void btn_Click(object sender, EventArgs e) { ButtonVars btn = (ButtonVars)sender; int x = btn.x; int y = btn.y; if (btn.bomb == 'B') { btn.Text = "*"; tick = 0; timer1.Enabled = true; Score(); //MessageBox.Show("Boom!", "MineSweeper", MessageBoxButtons.OK); } else { btn.Text = CountSurroundingBombs(buttons[y, x]).ToString(); if (y > 0 & x > 0) { count += buttons[y - 1, x - 1].Text == " " ? 1 : 0; buttons[y - 1, x - 1].Text = CountSurroundingBombs(buttons[y - 1, x - 1]).ToString(); } if (y > 0) { count += buttons[y - 1, x].Text == " " ? 1 : 0; buttons[y - 1, x].Text = CountSurroundingBombs(buttons[y - 1, x]).ToString(); } if (y > 0 & x < numx - 1) { count += buttons[y - 1, x + 1].Text == " " ? 1 : 0; buttons[y - 1, x + 1].Text = CountSurroundingBombs(buttons[y - 1, x + 1]).ToString(); } if (x > 0) { count += buttons[y, x - 1].Text == " " ? 1 : 0; buttons[y, x - 1].Text = CountSurroundingBombs(buttons[y, x - 1]).ToString(); } if (x < numx - 1) { count += buttons[y, x + 1].Text == " " ? 1 : 0; buttons[y, x + 1].Text = CountSurroundingBombs(buttons[y, x + 1]).ToString(); } if (y < numy - 1 & x > 0) { count += buttons[y + 1, x - 1].Text == " " ? 1 : 0; buttons[y + 1, x - 1].Text = CountSurroundingBombs(buttons[y + 1, x - 1]).ToString(); } if (y < numy - 1) { count += buttons[y + 1, x].Text == " " ? 1 : 0; buttons[y + 1, x].Text = CountSurroundingBombs(buttons[y + 1, x]).ToString(); } if (y < numy - 1 & x < numx - 1) { count += buttons[y + 1, x + 1].Text == " " ? 1 : 0; buttons[y + 1, x + 1].Text = CountSurroundingBombs(buttons[y + 1, x + 1]).ToString(); } count++; if (bcount + count >= numx * numy) { for (y = 0; y < numy; y++) { for (x = 0; x < numx; x++) { if (buttons[y, x].bomb == 'B') { buttons[y, x].Text = "*"; } else { buttons[y, x].count = CountSurroundingBombs(buttons[y, x]); buttons[y, x].Text = CountSurroundingBombs(buttons[y, x]).ToString(); } } } MessageBox.Show("Complete!", "MineSweeper", MessageBoxButtons.OK); Score(); } } }