private void Btn1_Click(object sender, MouseEventArgs e) { MineButton btn = (MineButton)sender; Position pos = matrix[btn.X, btn.Y]; if (e.Button == MouseButtons.Left && !matrix[pos.X, pos.Y].IsBomb && !GameFinished && !matrix[pos.X, pos.Y].IsFlagged) { EnableButton(matrix[pos.X, pos.Y]); } else if (e.Button == MouseButtons.Left && !pos.IsFlagged && !GameFinished) { this.pictureBox2.Image = global::MinesTest.Properties.Resources.sad; GameFinished = true; for (int i = 0; i < this.Rows; i++) { for (int j = 0; j < this.Cols; j++) { if (matrix[i, j].IsEnabled && matrix[i, j].IsBomb) { matrix[i, j].Boton.Image = global::MinesTest.Properties.Resources.bomb; matrix[i, j].IsEnabled = false; } } } } if (e.Button == MouseButtons.Right && matrix[btn.X, btn.Y].IsFlagged && !GameFinished) { btn.Text = ""; btn.Image = null; matrix[btn.X, btn.Y].IsFlagged = false; } else if (e.Button == MouseButtons.Right && !GameFinished) { matrix[btn.X, btn.Y].IsFlagged = true; btn.Image = global::MinesTest.Properties.Resources.banderita; } }
private MineButton CreateButton(int top, int left, int x, int y, bool isBomb) { MineButton bt; bt = new MineButton { Top = top, Left = left, Text = "", Width = 30, Height = 30, X = x, Y = y }; matrix[x, y] = new Position() { X = x, Y = y, IsBomb = false, Boton = bt, IsEnabled = true }; bt.MouseUp += Btn1_Click; return(bt); }
public Mines(Level Difficulty) { InitializeComponent(); menu = new MainMenu(); filemenu = new MenuItem(); filemenu.Text = "&Options"; menu.MenuItems.Add(filemenu); MenuItem print = new MenuItem(); print.Text = "Level"; filemenu.MenuItems.Add(print); MenuItem temp = new MenuItem(); temp.Text = "Easy"; print.MenuItems.Add(temp); MenuItem temp2 = new MenuItem(); temp2.Text = "Medium"; print.MenuItems.Add(temp2); MenuItem temp3 = new MenuItem(); temp3.Text = "Hard"; print.MenuItems.Add(temp3); this.Menu = menu; temp.Click += Temp_Click; temp2.Click += Temp2_Click; temp3.Click += Temp3_Click; switch (Difficulty) { case Level.easy: this.Rows = 10; this.Cols = 10; break; case Level.medium: this.Rows = 15; this.Cols = 15; break; case Level.hard: this.Rows = 20; this.Cols = 20; break; } matrix = new Position[this.Rows, this.Cols]; this.Bombs = (int)(matrix.Length / 6.4); int startTop = 28; int startLeft = 0; int count = 0; for (int i = 0; i < this.Rows; i++) { for (int j = 0; j < this.Cols; j++) { MineButton but = CreateButton(startTop, startLeft, i, j, false); startLeft += 28; this.Controls.Add(but); count++; } startTop += 28; startLeft = 0; } this.pictureBox2.MouseClick += pictureBox2_MouseClick; PutBombs(this.Bombs); PutNumbers(); }