void GenerateGame(int width, int height, int diff, int maxMine) { allFields = new ButtonExtend[width, height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { ButtonExtend button = new ButtonExtend(); button.Location = new Point(x * padding + 10, y * padding + 45); button.Size = new Size(30, 30); button.isClickable = true; button.isAdded = false; button.FlatStyle = FlatStyle.Flat; button.BackColor = Color.Silver; button.value = 9; button.toCheck = true; if (random.Next(0, 100) <= diff && minesCount < maxMine) { button.isBomb = true; minesCount++; //button.BackgroundImage = Resource1.bomb; //button.BackgroundImageLayout = ImageLayout.Stretch; } button.xPosition = x; button.yPosition = y; allFields[x, y] = button; Controls.Add(button); button.MouseUp += new MouseEventHandler(FieldClick); lblMinesCounter.Text = $"Mines: {minesCount}"; } } }
void OpenRegion(ButtonExtend button) { Queue <ButtonExtend> emptyFields = new Queue <ButtonExtend>(); emptyFields.Enqueue(button); button.isAdded = true; while (emptyFields.Count > 0) { ButtonExtend currentField = emptyFields.Dequeue(); OpenField(currentField); openFieldsCount++; if (FindBombsAround(currentField.xPosition, currentField.yPosition) == 0) { for (int y = currentField.yPosition - 1; y <= currentField.yPosition + 1; y++) { for (int x = currentField.xPosition - 1; x <= currentField.xPosition + 1; x++) { if (x >= 0 && x < width && y < height && y >= 0) { if (!allFields[x, y].isAdded) { emptyFields.Enqueue(allFields[x, y]); allFields[x, y].isAdded = true; } } } } } } }
void FieldClick(object sender, MouseEventArgs e) { ButtonExtend button = (ButtonExtend)sender; if (e.Button == MouseButtons.Left && button.isClickable) { if (button.isBomb) { if (isFirstClick) { button.isBomb = false; minesCount--; isFirstClick = false; OpenRegion(button); } else { button.BackColor = Color.Red; Explode(); } } else { OpenRegion(button); } isFirstClick = false; } if (e.Button == MouseButtons.Right) { button.isClickable = !button.isClickable; if (!button.isClickable) { button.BackgroundImage = Resource1.flag; button.BackgroundImageLayout = ImageLayout.Stretch; button.BackColor = Color.White; button.value = -1; } else { button.BackgroundImage = null; button.BackgroundImageLayout = ImageLayout.None; button.BackColor = Color.Silver; button.value = 9; } } timer1.Start(); IsTheGameWon(); }
void flagAround(ButtonExtend field) { mouseArgs = new MouseEventArgs(MouseButtons.Right, 1, 1, 1, 0); for (int y = field.yPosition - 1; y <= field.yPosition + 1; y++) { for (int x = field.xPosition - 1; x <= field.xPosition + 1; x++) { if (x >= 0 && x < width && y >= 0 && y < height) { if (allFields[x, y].value == 9) { FieldClick(allFields[x, y], mouseArgs); } } } } }
void OpenField(ButtonExtend field) { int bombsAround = FindBombsAround(field.xPosition, field.yPosition); if (bombsAround != 0) { field.Text = bombsAround.ToString(); field.value = bombsAround; } else { field.value = 0; } field.BackColor = Color.White; field.Enabled = false; field.BackgroundImage = null; }