public void GenerateField() { Random random = new Random(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { FieldButton newButton = new FieldButton(); newButton.Location = new Point(x * offset, y * offset); newButton.Size = new Size(offset, offset); newButton.isClickable = true; if (random.Next(0, 100) <= bombPercent) { newButton.isBomb = true; bombs++; } newButton.xPosition = x; newButton.yPosition = y; Controls.Add(newButton); newButton.MouseUp += new MouseEventHandler(FieldButtonClick); field[x, y] = newButton; } } }
void OpenRegion(int xPosition, int yPosition, FieldButton clickedButton) { Queue <FieldButton> queue = new Queue <FieldButton>(); queue.Enqueue(clickedButton); clickedButton.wasAdded = true; while (queue.Count > 0) { FieldButton currentCell = queue.Dequeue(); OpenCell(currentCell.xPosition, currentCell.yPosition, currentCell); Opened++; if (CountBombsAround(currentCell.xPosition, currentCell.yPosition) == 0) { for (int y = currentCell.yPosition - 1; y <= currentCell.yPosition + 1; y++) { for (int x = currentCell.xPosition - 1; x <= currentCell.xPosition + 1; x++) // функция автоматического раскрытия всех соседних пустых клеток { if (x == currentCell.xPosition && y == currentCell.yPosition) { continue; } if (x >= 0 && x < width && y < height && y >= 0) { if (!field[x, y].wasAdded) { queue.Enqueue(field[x, y]); field[x, y].wasAdded = true; } } } } } } }
void EmptyFieldButtonClick(FieldButton clickedButton) { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (field[x, y] == clickedButton) { OpenRegion(x, y, clickedButton); } } } }
private void FillMineField(int fieldSize) { for (int i = 0; i < fieldSize; i++) { for (int j = 0; j < fieldSize; j++) { var b = new FieldButton(i, j); b.Location = new System.Drawing.Point(ButtonSize * i, ButtonSize * j); b.Click += b_Click; b.MouseDown += b_MouseDown; this.Controls.Add(b); } } }
internal void Init(short boardWidth, short boardHeight, GameTimer gameTimer) { gameTimer.SetLabel(lblTime); this.buttons = new FieldButton[boardWidth, boardHeight]; for (int i = 0; i < boardWidth; i++) { for (int j = 0; j < boardHeight; j++) { buttons[i, j] = new FieldButton(i, j); buttons[i, j].MouseDown += OnFieldClick; buttons[i, j].SetBounds(margin + offsetX * i, margin + offsetY * j, buttonSize, buttonSize); Controls.Add(buttons[i, j]); } } }
void OpenCell(int x, int y, FieldButton clickedButton) { int bombsAround = CountBombsAround(x, y); if (bombsAround == 0) { clickedButton.Text = ""; } else { clickedButton.Text = "" + bombsAround; } clickedButton.Enabled = false; CheckWin(); }
private void SetUpFields() { for (int i = 1; i < 4; i++) { for (int j = 1; j < 4; j++) { var coords = new Coordinates(i, j); var field = new FieldButton(coords, _game); _fields.Add(field); } } foreach (var fieldButton in _fields) { Controls.Add(fieldButton); } }
void FieldButtonClick(object sender, MouseEventArgs e) { FieldButton clickedButton = (FieldButton)sender; if (e.Button == MouseButtons.Left && clickedButton.isClickable) { if (clickedButton.isBomb) { if (FirstClick) { clickedButton.isBomb = false; FirstClick = false; // условие проверки первого хода, чтобы первое нажатие всегда было на пустую клетку OpenRegion(clickedButton.xPosition, clickedButton.yPosition, clickedButton); } else { Explode(); } } else { EmptyFieldButtonClick(clickedButton); } FirstClick = false; } if (e.Button == MouseButtons.Right) { clickedButton.isClickable = !clickedButton.isClickable; // Метка бомбы правой кнопкой мыши if (!clickedButton.isClickable) { clickedButton.Text = "🏲"; } else { clickedButton.Text = ""; } } CheckWin(); }