1
Good afternoon I’m having some trouble using the method random
to try to appear only 10 bombs in my minefield, only that it appears without limits. someone knows where I am missing?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Camp
{
class Program
{
static int[,] matriz = new int[10, 10];
static void Main(string[] args)
{
PreencherMatriz();
}
static void Bomba()
{
Random rand = new Random();
int linhaRand = 0;
int colunaRand = 0;
//bool minValue = false;
bool maxValue = false;
int bomba = 0;
do
{
bomba ++;
linhaRand = rand.Next(1, matriz.GetLength(0) - 1);
colunaRand = rand.Next(1, matriz.GetLength(1) - 1);
if ( matriz[linhaRand, colunaRand] == '9')
{
maxValue = true;
}
}while(!maxValue && bomba <= 1);
matriz[linhaRand, colunaRand] =0;
LerMatriz();
Bomba();
}
static void LerMatriz()
{
Console.Clear();
for (int i = 0; i < matriz.GetLength(0); i++)
{
for (int j = 0; j < matriz.GetLength(1); j++)
{
Console.Write("{0} ", matriz[i, j]);
}
Console.WriteLine("");
}
}
static void PreencherMatriz()
{
for (int i = 0; i < matriz.GetLength(0); i++)
{
for (int j = 0; j < matriz.GetLength(1); j++)
{
if(j == 0 || j == matriz.GetLength(1) - 1)
{
matriz[i, j] = 5;
}
else if(i == 0 || i == matriz.GetLength(0) - 1)
{
matriz[i, j] = 5;
}
else
{
matriz[i, j] = 9;
}
Console.Write("{0} ", matriz[i, j]);
}
Console.WriteLine("");
}
Console.ReadKey();
Bomba();
}
}
}
At the end of the function
Bomba
you are calling her again, which causes the code to run non-stop.– carlosfigueira
But your retreat there, it doesn’t work, what can I do? can you guide me pfv
– Rafael Soares
As it was your teacher who passed you should study a little more and check what you are doing with your code debugging line to line mainly in your method Bomb me(); inside your 'if ( matrix[lineRand, columnRand] == 9)' you should change the value 9 for your 10.. tip.
– Marco Souza
Obg by the tips of all able to run the code working perfectly.
– Rafael Soares