Minefield, random bombs

1

Good afternoon I'm having a problem using the random method to try to show only 10 bombs in my mine field, except that it appears without limits. Does anyone know where I'm going wrong?

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();
        }
    }
}
    
asked by anonymous 05.11.2015 / 19:03

1 answer

1

I do not quite understand how your logic works, but the truth is that the problem lies in how you want to position the bombs. Your array passing in method PreencherMatriz() exits with the following result:

5 5 5 5 5 5 5 5 5 5 
5 9 9 9 9 9 9 9 9 5 
5 9 9 9 9 9 9 9 9 5 
5 9 9 9 9 9 9 9 9 5 
5 9 9 9 9 9 9 9 9 5 
5 9 9 9 9 9 9 9 9 5 
5 9 9 9 9 9 9 9 9 5 
5 9 9 9 9 9 9 9 9 5 
5 9 9 9 9 9 9 9 9 5 
5 5 5 5 5 5 5 5 5 5 

If he is responsible for the number of pumps inserted then there is the error, and has nothing to do with Random .

And if the Bomba method is the one who should make any changes to this array to mark the positions of the bombs, it is not assigning any value inside its loop that I imagine would be there that it should be counting how many bombs were inserted.

@edit:

static void Bomba()
{
    Random rand = new Random();
    int linhaRand = 0;
    int colunaRand = 0;
    int bomba = 0;
    do
    {
        linhaRand = rand.Next(1, matriz.GetLength(0) - 1);
        colunaRand  = rand.Next(1, matriz.GetLength(1) - 1);
        if ( matriz[linhaRand, colunaRand] == '9')
        {
            //adotando que onde é 0 é bomba
            matriz[linhaRand, colunaRand] = 0;
            bomba ++;
        }
    }while(bomba < 10);
    LerMatriz();
}
    
05.11.2015 / 20:03