Начал писать игру "Сапер", не могу решить проблему с ограниченным созданием бомб. Вообще программа работает, но как то коряво, потому что я обошел проблему созданием процентного соотношения. Но мне не нравится! И вот как эту проблему решить?
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
int width = 10;
int height = 10;
int offset = 30;
int bombPercent = 30;
FieldButton[,] field;
public void Generater()
{
Random random = new Random();
for (int x = 0; x < height; x++)
{
for (int y = 0; y < width; y++)
{
FieldButton newButton = new FieldButton();
newButton.Location = new Point(x * offset, (y + 1) * offset);
newButton.Size = new Size(offset, offset);
if (random.Next(0, 100) <= bombPercent)
{
newButton.isBomb = true;
}
Controls.Add(newButton);
newButton.MouseUp += new MouseEventHandler(GGorWin);
field[x, y] = newButton;
}
}
}
public void GGorWin(object sender, MouseEventArgs e)
{
FieldButton clickedButton = (FieldButton)sender;
if (e.Button == MouseButtons.Left)
{
if (clickedButton.isBomb)
{
GG();
}
else
{
LetsGO(clickedButton);
}
}
if (e.Button == MouseButtons.Right)
{
clickedButton.Text = "Ф";
}
}
public void GG()
{
foreach (FieldButton button in field)
{
if (button.isBomb)
{
button.Text = "*";
}
}
MessageBox.Show("Вы проиграли");
Application.Restart();
}
void LetsGO(FieldButton clickedButton)
{
int bombsAround = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (field[x, y] == clickedButton)
{
bombsAround = BombsAround(x, y);
}
}
}
if (bombsAround == 0)
{
}
else
{
clickedButton.Text = "" + bombsAround;
}
clickedButton.Enabled = false;
}
int BombsAround(int x1, int y1)
{
int bombsAround = 0;
for (int x = x1 - 1; x <= x1 + 1; x++)
{
for (int y = y1 - 1; y <= y1 + 1; y++)
{
if (x >= 0 && x < width && y >= 0 && y < height)
{
if (field[x, y].isBomb == true)
{
bombsAround++;
}
}
}
}
return bombsAround;
}
private void новаяИграToolStripMenuItem_Click(object sender, EventArgs e)
{
field = new FieldButton[width, height];
Generater();
}
}
public class FieldButton : Button
{
public bool isBomb ;
}