#include <stdio.h>
#include <time.h>
int main()
{
int arr[10][10]={0};//10*10=100个格子
int row,col;//纵向与横向的循环变量
int count=0;//统计随机地雷的数量
srand((unsigned)time(NULL)); //随机种子,要配合随机数使用
do
{
row=rand()%10;
col=rand()%10;
if(arr[row][col]==0)
{
arr[row][col]=-1;//标记为有地雷
count++;
}
}while(count<10);
for(row=0;row<10;row++)
{
for(col=0;col<10;col++)
{
if(arr[row][col]!=-1)//标记为没有地雷
printf("□");
else
printf("■");
}
printf("\n");
}
return 0;
利用两个for循环控制扫雷区域的大小,地雷的产生用随机数来控制,在使用rand之前,要设定随机种子,两者必须结合使用!