hi i am new in programming.i want to write a function “minesweeper(int, int, int )” that takes 3 arguments a, b, and p and produces an a-by-b Boolean array where each entry is occupied with probability p (e.g. if the randomly generated number is less than or equal to p, the cell is referred as empty else occupied) . In the minesweeper game, occupied cells represent bombs and empty cells represent safe cells. Print out the array using an asterisk for bombs and a period for safe cells. Then, replace each safe square with the number of neighboring bombs (above, below, left, right, or diagonal).kindly help and guide me

Edited by dureadan

5 Years
Discussion Span
Last Post by Lucaci Andrew

So, you need a function with three parameters. The 1st two parameters are the sizes of the matrix, and the last is the probability. I do have some questions related to your project. For instance, do you randomly generate numbers between 0 and 1? From my knowledges of probability, p must take a value between 0 and 1, so I figure, your random number generator should take also values from 0 to 1. If that's the case, than you'll need a rand() funtion which would generated double numbers, not ints. For that, here's this:

double drand (double low, double high){
    return ((double)rand()*(high-low))/(double)RAND_MAX+low;

In your case you should use it like drand (0, 1);.
Also, you'll need to dynamically allocate a bool matrix of aXb.

    bool** matr = new bool* [a];
    for (int i=0;i<a;i++) matr[i] = new bool [b];

You'll need to have a number, randomly genedated, and compare it with the probability. Also, this should be done in a loop, so that if the number is lower, to mark the space in the matrix as free (true), or as occupied (false) if the number is greater. Here's a quick pseudocode:

for i from 0 to a do
    for j from 0 to b do
        num <- randomly generated number between 0 and 1 (using drand)
        if num < p then
            matr[i][j] <- true
        else matr[i][j] <- false

This function will fill up your newly allocated bool matrix with true/false values (free/occupied).
As for the printing part, that's easy:

for i from 0 to a do
    for j from 0 to b do
        if matr[i][j] = true then
            print "."
        else print "*"
        if j = a-1 then
            print new line

These are just some tips for your program. Having them, try to think how you could actually solve your problem. Good luck.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.