I've made an string-array (called a) of 10 rows and 10 columns. I fill the array with zero's. Next I place 10 bombs at random places. The corner has to remain empty of bombs. I figured out how to pick a number from 1 to 8 (because the corner has to remain empty of bombs!), and assign this random number to een variable "row" and "column". Problem: when the place a[row][column] already has a bomb (sign:"*") the proces of picking a random number from 1 to 8 has to be started all over again. That's why it works so slow.
For as far as I know minesweeper of Windows works much faster!
//the array a is already filled with zero's
int rows=sizeof(a)/sizeof(a[0]);
int columns=sizeof(a[0])/sizeof(a[0][0]);
int numberBombs=10;int counting=0;
while(numberBombs > counting)
{bool go_on=false;
long random_row;long random_column;
long test_value;
while(!go_on)
{srand(time(0));//set random number
test_value=rand();
random_row=test_value%(rows-2)+1;//pick a number from 1 to 8
srand(time(0));
test_value=rand();
random_row=test_value%(columns-2)+1;
if(a[random_row][random_column]!="*"){go_on=true;}
}
a[random_row][random_column]="*";//"*" is a bomb
counting++;
}
That's right. I see now. When I copied the code in this forum, I changed the names of some vars (I had my own Dutch names), and that was the moment when I mistypied that.
But that isn't the case in the real code on my computer.
helios already said it, srand should be called only once. Not just once for each mine, once for the whole snippet. Call srand before the for loop (the outer while).
1. The way you're doing it, not only the corners, but all the rim is free of bombs...
2. You have to call srand(time(NULL)) only once at the beginning of the program, because time(NULL) changes every second. In your implementation, you have to wait one second until rand() gives a new number.
And in your implementation random_row and random_column are only different when time(NULL) changes between line 11and line 12. But you are looking for 10 pairs out of 8 numbers, you need this seldom occurance twice...
Anyhow, i would suggest to have one array with 100 entries rather than a two-dimensional array, but it should work this way, too