#### text.c:用来存放主体
#include "game.h"
void game()
{
char arr1[ROWS][COLS];
char arr2[ROWS][COLS];
init(arr1, ROWS, COLS, '0');
init(arr2, ROWS, COLS, '*');
ment(arr1,ROW,COL);
check(arr1,arr2, ROW, COL);
}
void menu()
{
srand((unsigned int)time(NULL));
printf("****************************\n");
printf("********* 1.play *********\n");
printf("********* 2.exit *********\n");
printf("****************************\n");
}
void text()
{
int inpt;
menu();
do
{
scanf("%d", &inpt);
switch(inpt){
case 1:
game();
break;
case 0:
printf("游戏结束\n");
break;
default:
printf("输入错误请重新输入\n");
break;
}
} while (inpt);
}
int main()
{
text();
}
#### game.c用来放需要用到的库函数和定义的常量并且对需要用到的函数进行声明
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#define mine_count 10
#define ROWS 11
#define COLS 11
#define ROW 9
#define COL 9
#define step ROW*COL-mine_count
void init(char arr[ROWS][COLS], int rows, int cols, char c);
void print(char arr[ROWS][COLS], int rows, int cols);
void ment(char arr[ROWS][COLS], int row, int col);
void check(char arr1[ROWS][COLS], char arr2[ROWS][COLS], int row, int col);
#### game.c:对函数进行定义
#include "game.h"
void init(char arr[ROWS][COLS], int rows, int cols, char c)
{
int i;
for (i = 0; i < ROWS; i++)
{
int j;
for (j = 0; j < COLS; j++)
{
arr[i][j] = c;
}
}
}
void print(char arr[ROWS][COLS], int row, int col)
{
int i;
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <=row; i++)
{
int j=1;
printf("%d ", i);
for (j = 1; j <=col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
void ment(char arr[ROWS][COLS], int row, int col)
{
int count = mine_count;
while (count >0)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}
int stst(char arr1[ROWS][COLS],int x,int y)
{
return arr1[x - 1][y - 1] + arr1[x - 1][y] + arr1[x - 1][y + 1]
+ arr1[x][y - 1] + arr1[x][y + 1] + arr1[x + 1][y - 1] +
arr1[x + 1][y] + arr1[x + 1][y + 1] - 8 * '0';
}
void check(char arr1[ROWS][COLS], char arr2[ROWS][COLS], int row, int col)
{
int x, y;
int i = step;
while (i>0)
{
printf("请输入要排查的坐标\n");
scanf("%d %d", &x, &y);
if (arr1[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
print(arr1, ROW, COL);
break;
}
else
{
int count = stst(arr1, x, y);
arr2[x][y] = count + '0';
print(arr2, ROW, COL);
i--;
}
}
if (i == 0)
{
printf("你赢了\n");
}
}