#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void exercise_1() {
int row =
0, col =
0, cnt =
0, num =
0;
int arr[
10][
10] = {
0};
int delta[][
2] = {-
1, -
1, -
1,
0, -
1,
1,
0, -
1,
0,
1,
1, -
1,
1,
0,
1,
1};
/******************************************************************************************
* 产生地雷部分
* *****************************************************************************************/
srand(time(
0));
do {
row = rand() %
9;
col = rand() %
9;
if(!arr[row][col]) {
arr[row][col] = -
1;
cnt++;
}
}
while(cnt <
10);
/*****************************************************************************
* 地雷周边填充数字部分
* ****************************************************************************/
for(row =
0; row <=
9; row++) {
for(col =
0; col <=
9; col++) {
if(arr[row][col] != -
1) {
continue;
}
for(num =
0 ;num <=
7; num++) {
int tmp_row = row + delta[num][
0];
int tmp_col = col + delta[num][
1];
if(tmp_row <
0 || tmp_row >
9) {
continue;
}
if(tmp_col <
0 || tmp_col >
9 ) {
continue;
}
if( arr[tmp_row][tmp_col] == -
1) {
continue;
}
arr[tmp_row][tmp_col]++;
}
}
}
/*******************************************************************************
* 显示部分
* ******************************************************************************/
for(row =
0; row <=
9; row++) {
for(col =
0; col <=
9; col++) {
if(!arr[row][col]) {
printf(
"O ");
}
else if(arr[row][col] == -
1) {
printf(
"X ");
}
else {
printf(
"%d ", arr[row][col]);
}
}
printf(
"\n");
}
}
int main() {
exercise_1();
return 0;
}