0

I'm trying to code the game Minesweeper and for that I need to randomize the bombs locations.

Can someone help change the function a bit so that placing of bombs is randomized?

THE CODE:

void Createbomb(int*** bord, int size)//size+1
{
    //bomb num.1
    (*bord)[size - 5][size - 1] = 9;
    //bomb num.2
    (*bord)[size - 4][size - 4] = 9;
    //bomb num.3
    (*bord)[size - 3][size - 2] = 9;
    //bomb num.4
    (*bord)[size - 2][size - 2] = 9;
    //bomb num.5
    (*bord)[size - 1][size - 5] = 9;
}
ghost
  • 1,107
  • 3
  • 12
  • 31
  • 3
    `rand() % size` gives you a "random" number in the range `0` till `size-1` inclusive. – mch Jun 04 '20 at 12:04
  • @mch Not really, at least if you use the generated values for something important. If `RAND_MAX` is not divisible by `size` there will be some skewing towards the lower values. And more importantly, many PRNGs have very poor randomness in the low order bits. See [**Random integers in C, how bad is rand()%N compared to integer arithmetic? What are its flaws?**](https://stackoverflow.com/questions/49880304/random-integers-in-c-how-bad-is-randn-compared-to-integer-arithmetic-what-a) – Andrew Henle Jun 04 '20 at 12:36

3 Answers3

0

Suppose that the number of bombs you need is 5. Also, let initially board contain default value of 1 for all cells, and 9 only if there is a bomb present in the cell. Then you can do something like:

void createBomb(int*** board, int size){
    srand(time(0));
    int numOfBombsPlaced = 0;
    while(numOfBombsPlaced < 5){
        int x = rand()%size;
        int y = rand()%size;
        if ((*board)[x][y]==9):
             continue;
        numOfBombsPlaced++;
        (*board)[x][y] = 9;
    }
}

Note that the numbers generated by rand are not truly random, and depend on the initial seed. The seed is provided by srand. Using time for seed is a good option to randomise.

ghost
  • 1,107
  • 3
  • 12
  • 31
0

Do not use the modulus to get a pseudo-random number, since with a few trials, we can easily figure out which is going to be the next number. There are several ways of randomize in a good way, this is one way, that is better that using the modulus: (If you have interest, there are more complex ways to generate random numbers)

#include <stdlib.h>

int random_num(int inf, int sup)
{
    if(inf < 0 || sup-inf < 0) return -1; /*Only positive numbers because it is an array */

    return inf+(int)(((sup-inf+1.0)*rand())/(RAND_MAX + 1.0));
}

RAND_MAX is a constant value defined in stdlib.h, roughly speaking it is a huge integer constant, i.e, a big integer number.

So, in your program you just have to call:

int x = random_num(0, size); /*Gets a new random number in the desired range*/ 
0

I wrote a ncurses based Minesweeper a long time ago (around 1993). It's opensource and it's now in Github, If you want hints about how to implement some part of it, you have full source code there. You are free to download, clone, and even to show me better ways to implement something if you get some.

My approach was to use a matrix for the field cells, and to store on them the number of neighbour bombs, while one bit stored the presence of a bomb. I'm afraid it was written so long ago that I have not had a time to translate all the comments and identifiers into english... but probably this is a good time to do so.... let's think on it.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31