Mail.ruПочтаМой МирОдноклассникиВКонтактеИгрыЗнакомстваНовостиКалендарьОблакоЗаметкиВсе проекты

Индекс находился вне границ массива

Александр Бананов Ученик (145), на голосовании 2 месяца назад
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
int width = 10;
int hight = 10;
int distance = 35;
ButtonExtended[,] allButtons;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
allButtons = new ButtonExtended[width, hight];
Random rng = new Random();
for (int i = 10; i - 10 < width * distance; i += distance)
{
for (int j = 10; j - 10 < hight * distance; j += distance)
{
ButtonExtended button = new ButtonExtended();
button.Location = new Point(i, j);
button.Size = new Size(30, 30);
if ( rng.Next (0,101) < 20)
{
button.isBomb = true;
}
allButtons[i - 10 / distance, j - 10 / distance] = button;
Controls.Add(button);
button.Click += new EventHandler(FieldClick);
}
}
}
private void FieldClick(object sender, EventArgs e)
{
ButtonExtended button = (ButtonExtended)sender;
if (button.isBomb)
{
Explode(button);
}
else
{
EmptyFieldClick(button);
}
}
void Explode(ButtonExtended button)
{
for (int i = 0; i < width; i++)
{
for (int j = 0; j < width; j++)
{
if (allButtons[i, j].isBomb)
{
allButtons[i, j].Text = "@";
}
}
}
MessageBox.Show("Game Over");
}
void EmptyFieldClick(ButtonExtended button)
{
for(int i = 0; i < width; i++)
{
for (int j = 0; j < width; j++)
{
if (allButtons[i,j] == button)
{
button.Text = "" + CountBombsAround(i,j);
}
}
}
}
int CountBombsAround(int iB, int jB)
{
int bombCount = 0;
for (int i = iB - 1; i <= iB + 1; i++)
{
for (int j = jB - 1; j <= jB + 1; j++)
{
if(i >= 0 && i < width && j >= 0 && j < hight)
{
if (allButtons[i, j].isBomb)
{
bombCount++;
}
}
}
}
return bombCount;
}
}
class ButtonExtended:Button
{
public bool isBomb;
}
}
Дополнен 3 месяца назад
Это сапер, только без пустого пространства
Голосование за лучший ответ
Похожие вопросы