# C# - How to place a given bulb in a vein in the mass

• I'm just saying I'm new at c#. I've got a job to do some kind of saper, but it's the one that opens the door.

``````static void Main(string[] args)
{
Console.Write("Введите ширину поля: ");
Console.Write("Введите длину поля: ");

Console.Write("Введите число мин: ");

Random rand = new Random();

var charArray = new char[q, w];
var intArray = new int[q, w];
for (int i = 0; i &lt; q; i++)
{
for (int j = 0; j &lt; w; j++)
{
intArray[i, j] = rand.Next(2);
charArray[i, j] = intArray[i, j] == 0 ? '_' : '*';
Console.Write(charArray[i, j]);
}
Console.WriteLine();
}

}

}

}
``````

Two matrices should be identified. The first must be closed, i.e. only symbols: _ and

0 is a place without mines, I replaced them with a symbol _

1 is mined spaces, I replaced them with a star symbol, but they don't accept the number of mines used by the user. And I need the "" symbols to be as many as mine.

And in the second matrix, there should be an open decision of the game. I mean, the cells with which the mines are near must accept the number that means the number of mines next to this cell.

• In fact, the whole task is to generate `c` random unique numbers. And then using these numbers to set up a unit in the body. The algorithms of generation and placement can be implemented in different ways, and the way depends on your knowledge and context of the assignment. There is one guaranteed way to do this effectively if the size of the matrix is small, say less than a million elements.

We need to take a mass of length into the number of elements of the matrix, fill it with the indexes of these elements and mix it up, then take the necessary number of elements from the beginning of the mass. The task is very simple with Linq, but I'll try without it.

``````int rows = 10;
int cols = 10;
int minesCount = 5;
int[] indexes = new int[rows * cols];
// заполняю индексами
for (int i = 0; i < indexes.Length; i++)
{
indexes[i] = i;
}
Random rnd = new Random();
// перемешиваю
for (int i = 0; i < indexes.Length; i++)
{
int randomIndex = rnd.Next(indexes.Length);
int x = indexes[randomIndex];
indexes[randomIndex] = indexes[i];
indexes[i] = x;
}
``````

Now it can be assumed that the mass `indexes` contains unique random numbers. Algorithm is described in good shape. https://ru.wikipedia.org/wiki/%D0%A2%D0%B0%D1%81%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5_%D0%A4%D0%B8%D1%88%D0%B5%D1%80%D0%B0_%E2%80%94_%D0%99%D0%B5%D1%82%D1%81%D0%B0 ♪

In order to obtain the coordinates in the matrix, a formula should be applied whereby the row number is the result of the index split into the number of columns per whole, and the column number is the balance of this separation.

``````int[,] matrix = new int[rows, cols];
for (int i = 0; i < minesCount; i++)
{
int index = indexes[i];
int row = index / cols;
int col = index % cols;
matrix[row, col] = 1;
}
``````

Now you can take the matrix to the console.

``````for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write(matrix[i, j]);
}
Console.WriteLine();
}
``````

Example of withdrawal

``````0000001000
0000000010
0000000000
0000000000
0000000000
0000000000
0000000000
0100000000
1000000000
0000000010
``````

In doing so, you do not need to store a symbol matrix, it will duplicate the numbers of the matrices, and duplication of data in the annex is always bad. It's a source of extra mistakes. You can just do that to get the stars off the line.

``````Console.Write(matrix[i, j] == 1 ? '*' : '_');
``````

Either a matrix with symbols may be stored in the first place. But if you're going to make a sapper, it might not be very convenient.

Pay attention to the condition that `x` - it's a horizontal coordinate, `matrix[x, y]` It's a wrong note, loyal. `matrix[y, x]`♪ Because the first index points to the row number and the second to the column number, and the lines are in height, one by one.

To avoid confusion, I suggest you do what I do. `matrix[row, col]`♪ In your example `q` - it's a width, right? No, you're counting the width, but you're putting it up like a height, and vice versa. Try to get different. `q` and `w`e.g. 5 and 10, and you'll make sure in practice.

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2