I'm doing a minesweeper that should have size 10 * 10 and within this I must locate 10 mines in random positions.
My problem is that when generating the random positions, these are, in some cases, repeated and therefore overwritten. That is, if in the 3rd round of the loop it takes the position (3,7) and then it takes the same position in the 5th round, the value is overwritten and therefore a mine is deducted.
How can I avoid repeating positions?
Here is the part of the code where the problem is:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 10
int main()
{
srand(time(NULL));
int i=0;
int j=0;
int count=0;
int bombs=0;
int x=0;
int map[size][size];
for(x=0; x<size; x++) //crea las minas
{
i=rand()%11;
j=rand()%11;
map[i][j]=1;
bombs++;
}
for (i=0; i<size ; i++ )
for (j=0; j<size ; j++ )
if(map[i][j]==1)
{
count++;
}
printf("bombs = %d\n",count);//verifica la cantidad de bombas
for(i=0; i<size; i++)//imprime el array
{
for(j=0; j<size; j++)
printf("%d",map[i][j]);
puts("");
}
return 0;
}
}