Minefield, random bombs

Asked

Viewed 1,059 times

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();
        }
    }
}
  • 4

    At the end of the function Bomba you are calling her again, which causes the code to run non-stop.

  • But your retreat there, it doesn’t work, what can I do? can you guide me pfv

  • 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.

  • Obg by the tips of all able to run the code working perfectly.

1 answer

1

I don’t quite understand how your logic works, but the truth is, the problem is how you want to position the bombs. Your matrix when passing the 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 quantity of bombs inserted then there is the error, and has nothing to do with the Random.

And if the method Bomba is who should perform any modification in this matrix to mark the positions of the bombs, it is not assigning any value inside its loop that I imagine it would be there that 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();
}
  • Professor asked me a very simple thing, 5 would be the empty border 9 is missing I introduce 10 pumps Random without going over the edge. When doing this only looks like 1 bomb. and Obs the bombs have to appear randomly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.