project did not solve the error.I did minesweeper game but row and column gives the error.
link to my project
https://app.box.com/s/3go54y99ewfd3jdoa1um[
^]
List<Box> ListOfNeighbours(int Row, int Column)
{
List<Box> TheList = new List<Box>();
for (int r = -1; r <= 1; r++)
{
for (int c = -1; c <= 1; c++)
{
if ((r == 0 && c == 0) || (Column + c < 0) || (Row + r < 0) || ((Column + c) >= RowCount) || (Row + r >= ColumnCount))
{
continue;
}
TheList.Add(ListOfBoxes[Row + r, Column + c]);
}
}
return TheList;
}
[EDIT - posting OP's code from solution which will disappear soon]
Consider all projects
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Cet.MineSweeperGame
{
public partial class MineGame : Form
{
public MineGame()
{
InitializeComponent();
}
public int RowCount;
public int ColumnCount;
public int BombCount;
bool FirstClick = true;
Box[,] ListOfBoxes;
int timeCount= 0;
int flagCount;
private void timer_Tick(object sender, EventArgs e)
{
txtTime.Text = timeCount++.ToString();
}
private void btnRenew_Click(object sender, EventArgs e)
{
if (numTotalBomb.Value < numRowCount.Value * numColumnCount.Value)
{
timer.Stop();
FirstClick = true;
ResetBoxBoard();
GenerateGame();
}
else
{
MessageBox.Show("Total bomb should be less than " + (numRowCount.Value * numColumnCount.Value).ToString());
}
}
private void ResetBoxBoard()
{
BoxBoard.Enabled = true;
BoxBoard.Controls.Clear();
}
private void MineGame_Load(object sender, EventArgs e)
{
GenerateGame();
}
private void GenerateGame()
{
RowCount = Convert.ToInt32(numRowCount.Value);
ColumnCount = Convert.ToInt32(numColumnCount.Value);
BombCount = Convert.ToInt32(numTotalBomb.Value);
timeCount = 0;
txtTime.Text = "0";
flagCount = Convert.ToInt32(numTotalBomb.Value);
txtFlag.Text = flagCount.ToString();
ListOfBoxes = new Box[RowCount, ColumnCount];
BoxBoard.Width = RowCount * Box.BOX_WIDTH;
this.Width = RowCount * Box.BOX_WIDTH + 13;
BoxBoard.Height = RowCount * Box.BOX_HEIGHT;
this.Height = BoxBoard.Height + BoxBoard.Top + 35;
for (int r = 0; r < RowCount; r++)
{
for (int c = 0; c < ColumnCount; c++)
{
Box b = new Box();
ListOfBoxes[r, c] = b;
b.Column = c;
b.Row = r;
b.Top = r * Box.BOX_HEIGHT;
b.Left = c * Box.BOX_WIDTH;
b.MouseUp += new MouseEventHandler(b_MouseUp);
BoxBoard.Controls.Add(b);
}
}
int currentBomb = 0;
Random rnd = new Random();
do
{
int random = rnd.Next(RowCount * ColumnCount);
int rndR = random / RowCount;
int rndC = random % ColumnCount;
if (!ListOfBoxes[rndR, rndC].isBomb)
{
ListOfBoxes[rndR, rndC].isBomb = true;
currentBomb++;
foreach (Box neighbour in ListOfNeighbours(rndR, rndC)) { neighbour.NeighboursBombCount++; }
}
} while (currentBomb < BombCount);
}
void b_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Box clickedBox = sender as Box;
if (!clickedBox.isFlagged)
{
if (clickedBox.isBomb)
{
if (FirstClick == false)
{
timer.Stop();
BoxBoard.Enabled = false;
ShowAllBombs();
clickedBox.BackColor = System.Drawing.Color.Red;
}
else
{
MessageBox.Show("Unlucky! Game will be renewed.");
ResetBoxBoard();
GenerateGame();
}
}
else
{
OpenBox(clickedBox);
if (timeCount == 0)
{
timeCount++;
timer.Start();
FirstClick = false;
}
if (isFinished())
{
timer.Stop();
BoxBoard.Enabled = false;
MessageBox.Show("Finished!");
}
}
}
}
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
Box b = (Box)sender;
if (!b.isFlagged)
{
b.isFlagged = true;
b.BackgroundImage = Image.FromFile(Application.StartupPath + "\\Flag.png");
b.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
flagCount--;
txtFlag.Text = flagCount.ToString();
}
else
{
b.isFlagged = false;
b.BackgroundImage = null;
flagCount++;
txtFlag.Text = flagCount.ToString();
}
if (isFinished())
{
timer.Stop();
BoxBoard.Enabled = false;
MessageBox.Show("Bitti.");
}
}
}
private void OpenBox(Box clickedBox)
{
if (clickedBox.NeighboursBombCount > 0)
{
clickedBox.Enabled = false;
clickedBox.Text = clickedBox.NeighboursBombCount.ToString();
clickedBox.isOpen = true;
}
else
{
clickedBox.Visible=false;
clickedBox.isOpen = true;
foreach (Box b in ListOfNeighbours(clickedBox.Row, clickedBox.Column))
{
if (!b.isOpen && !b.isFlagged) { OpenBox(b); }
}
}
}
List<box> ListOfNeighbours(int Row, int Column)
{
List<box> TheList = new List<box>();
for (int r = -1; r <= 1; r++)
{
for (int c = -1; c <= 1; c++)
{
if ((r == 0 && c == 0) || (Column + c < 0) || (Row + r < 0) || ((Column + c) >= RowCount) || (Row + r >= ColumnCount))
{
continue;
}
TheList.Add(ListOfBoxes[Row + r, Column + c]);
}
}
return TheList;
}
private void ShowAllBombs()
{
for (int r = 0; r < RowCount; r++)
{
for (int c = 0; c < ColumnCount; c++)
{
if (ListOfBoxes[r, c].isBomb && !ListOfBoxes[r, c].isFlagged)
{
ListOfBoxes[r, c].BackgroundImage = Image.FromFile(Application.StartupPath + "\\Bomb.png");
ListOfBoxes[r, c].BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
}
if (!ListOfBoxes[r, c].isBomb && ListOfBoxes[r, c].isFlagged)
{
ListOfBoxes[r, c].BackgroundImage = Image.FromFile(Application.StartupPath + "\\WrongFlag.png");
}
}
}
}
private bool isFinished()
{
bool result = true;
for (int r = 0; r < RowCount; r++)
{
for (int c = 0; c < ColumnCount; c++)
{
if (ListOfBoxes[r, c].isBomb && !ListOfBoxes[r, c].isFlagged)
{
result = false;
}
if (!ListOfBoxes[r, c].isBomb && !ListOfBoxes[r, c].isOpen)
{
result = false;
}
}
}
return result;
}
}
}
</box></box></box>