private void OpenEmptySurrounding(Minefiled parent, Field field) { int LowXbounds = parent.X - 1 < 0 ? 0 : parent.X - 1; int HighXbounds = parent.X + 1 >= field._tableC ? field._tableC - 1 : parent.X + 1; int LowYbounds = parent.Y - 1 < 0 ? 0 : parent.Y - 1; int HighYbounds = parent.Y + 1 >= field._tableR ? field._tableR - 1 : parent.Y + 1; for (int i = LowYbounds; i <= HighYbounds; i++) { for (int j = LowXbounds; j <= HighXbounds; j++) { if (i == parent.Y && j == parent.X) { continue; } if (field._mat[i, j].IsOpen) { continue; } field._mat[i, j].OpenField(field, ""); } } }
public void FormMinesweeper_MouseDown(object sender, MouseEventArgs e) { if (!timer.Enabled) { timer.Start(); _startTime = DateTime.Now; } Minefiled minefiled = sender as Minefiled; if (e.Button == MouseButtons.Left) { minefiled.OpenField(_field, sender.ToString()); if (_field.winCondition != 0) { timer.Stop(); } if (_field.winCondition == 1) { MessageBox.Show("VI VON ZULUL"); return; } if (_field.winCondition == -1) { MessageBox.Show("You lost!"); return; } } else if (e.Button == MouseButtons.Right) { if (minefiled.IsOpen) { return; } if (minefiled.IsFlag) { minefiled.IsFlag = false; minefiled.Image = Image.FromFile("Resources\\closedField-50x50.png"); } else { minefiled.IsFlag = true; minefiled.Image = Image.FromFile("Resources\\flag-50x50.png"); } } ActiveControl = null; }
public Field(int tableR, int tableC, int tableBombs, int tableSafe, FormMinesweeper parent) { this._tableR = tableR; this._tableC = tableC; this._tableBombs = tableBombs; this._tableSafe = tableSafe; _mat = new Minefiled[_tableR, _tableC]; int X = 2; int Y = 0; var size = new Size(X + 50 * _tableC + 20, Y + 50 * _tableR + 67); parent.Size = size; var random = new Random(); for (int i = 0; i < _tableR; i++) { for (int j = 0; j < _tableC; j++) { _mat[i, j] = new Minefiled { Size = new Size(50, 50), BackColor = Color.LightGray, Anchor = AnchorStyles.Top | AnchorStyles.Left, Location = new Point(X, Y), Image = Image.FromFile("Resources\\closedField-50x50.png"), X = j, Y = i, IsOpen = false, BombsAround = 0 }; _mat[i, j].MouseDown += parent.FormMinesweeper_MouseDown; parent.panelGame.Controls.Add(_mat[i, j]); X += 50; } Y += 50; X = 2; } if (_tableBombs > _tableR * _tableC) { _tableBombs = _tableR * _tableC; } while (true) { int i = random.Next(0, _tableR); int j = random.Next(0, _tableC); if (!_mat[i, j].IsBomb) { _mat[i, j].IsBomb = true; _mat[i, j].BackgroundImage = Image.FromFile("Resources\\mine-clipart-50x50-inactive.png"); int LowYbounds = i - 1 < 0 ? 0 : i - 1; int HighYbounds = i + 1 >= _tableR ? _tableR - 1 : i + 1; int LowXbounds = j - 1 < 0 ? 0 : j - 1; int HighXbounds = j + 1 >= _tableC ? _tableC - 1 : j + 1; for (int ia = LowYbounds; ia <= HighYbounds; ia++) { for (int ja = LowXbounds; ja <= HighXbounds; ja++) { _mat[ia, ja].BombsAround++; } } if (--_tableBombs == 0) { return; } } } }