整体代码
整体代码如下,当然还有很多值得优化的地方,
(甚至还有一两个小bug,技术力不够啊(T⌓T) ),
大家有什么改进的办法可以交流一下。
game.h
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> #include<time.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 //初始化棋盘 void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set); //打印棋盘 void DisplayBoard(char arr[ROWS][COLS], int row, int col); //随机布置雷 void SetMine(char arr[ROWS][COLS], int row, int col, int N_COUNT); void StartBoard(char arr[ROWS][COLS], int row, int col, int N_COUNT); //查找雷或清除雷 void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int N_COUNT); //拓展功能(一次性扫一大片) void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y); //判断合法输入 int judge(int n);
test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> #include"game.h" void menu() { printf("********************\n"); printf("**** 1.play ****\n"); printf("**** 0.exit ****\n"); printf("********************\n"); } /* 1.存放数据的类型太多,容易产生歧义 1.创建两个数组, 2.一个雷的信息,一个存放排查雷的信息 2.在统计一个坐标周围雷的个数的时候,可能会越界 */ void game(int N_COUNT) { char mine[ROWS][COLS]; char show[ROWS][COLS]; //初始化棋盘 InitBoard(mine, ROWS, COLS, '0');// '0' InitBoard(show, ROWS, COLS, '*');// '*' //打印棋盘 //DisplayBoard(show,ROW,COL); //DisplayBoard(mine,ROW,COL); //1.布置雷 SetMine(mine,ROW,COL,N_COUNT); StartBoard(mine, ROW, COL, N_COUNT); //DisplayBoard(mine, ROW, COL); //2.排查雷 FindMine(mine, show, ROW, COL, N_COUNT); } int main() { int N_COUNT = 0; int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("请输入:"); input = judge(input); switch (input) { case 1: printf("请输入你要扫雷的数量(不要太多哦!!!)\n"); N_COUNT = judge(N_COUNT); int i = 5 + N_COUNT; printf("你有%d的次数清除雷\n", i); if (N_COUNT > 50) { printf("你的雷有点多啊!\n"); printf("请重新开始吧!\n"); break; }else if(N_COUNT <= 0) { printf("你的雷不能是非正数啊!\n"); printf("请重新开始吧!\n"); break; } game(N_COUNT); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,重新选择\n"); break; } } while (input); return 0; }
game.c
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> #include"game.h" #include<time.h> #include<stdlib.h> void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set) { int i = 0; for (i = 0; i < rows; i++) { int j = 0; for (j = 0; j < cols; j++) { arr[i][j] = set; } } } void StartBoard(char arr[ROWS][COLS], int row, int col, int N_COUNT) { int i = 1; printf("*********扫雷*********\n"); int count = N_COUNT; printf("***你需要找出%d个雷***\n", N_COUNT); //先打印列号 for (i = 0; i <= row; i++) { printf("%d ", i); }printf("\n"); for (i = 1; i <= row; i++) { int j = 0; printf("%d ", i);//再打印行号 for (j = 1; j <= col; j++) { printf("* "); } printf("\n"); } } void DisplayBoard(char arr[ROWS][COLS], int row, int col) { int i = 1; printf("*********扫雷*********\n"); //先打印列号 for (i = 0; i <= row; i++) { printf("%d ", i); }printf("\n"); for (i = 1; i <= row; i++) { int j = 0; printf("%d ", i);//再打印行号 for (j = 1; j <= col; j++) { printf("%c ", arr[i][j]); } printf("\n"); } } void SetMine(char arr[ROWS][COLS], int row, int col, int N_COUNT) { //布置1-10个雷 int count = N_COUNT;//用count接收雷的数量 while (count) { //布置雷 int x = rand() % row + 1;//1-9的值 int y = rand() % col + 1;//1-9的值 //布置成功一个雷count--,还需要布置雷的数量 if (arr[x][y] = '0') { arr[x][y] = '1'; count--; } } } static GetMineCount(char mine[ROWS][COLS], int x, int y) { return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x][y + 1] + mine[x + 1][y + 1] + mine[x - 1][y + 1] - 8*'0'; } void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y) { int temp = 0; int j = 0, i = 0; int n = GetMineCount(mine, x, y);//统计坐标周围有几个雷 if (x >= 1 && x <= ROW && y >= 1 && y <= COL) { if (n == 0 && mine[x][y] == '0') { show[x][y] = '0'; for (j = x - 1; j <= x + 1; j++) { for (i = y - 1; i <= y + 1; i++) { if (show[i][j] == '*' && i > 0 && i <= col && j > 0 && j <= row) { Expand(mine, show, row, col, i, j); } } } } else if (mine[x][y] == '1' && mine[x][y] != '#') { show[x][y] = '*'; } else { show[x][y] = n + '0'; } } } int judge(int n) { while (scanf("%d", &n) == 0 || getchar() != '\n') { printf("输入了非法字符,请重新输入:"); while (getchar() != '\n'); // 清除缓存区 } return n; } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int N_COUNT) { int t = 0, x = 0, y = 0, i = 5 + N_COUNT; int arr1[10][10] = { 0 }; while (1) { int reinput = 0; printf("0.排查雷,1.清除雷,2.游戏结束\n"); printf("请输入2或1或0:"); reinput = judge(reinput); switch (reinput) { case 2: { goto h; } case 1: printf("请输入要消除的坐标(请输入数字):\n"); printf("请输入纵坐标:");x = judge(x); printf("请输入横坐标:");y = judge(y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (arr1[x][y] == 0) { arr1[x][y] = 1; if (mine[x][y] == '1') { i--; t = t + 1; show[x][y] = '#'; mine[x][y] = '#'; DisplayBoard(show, ROW, COL); printf("清除成功,请再次选择\n"); if (t == N_COUNT) { printf("恭喜你扫雷成功,游戏结束\n"); goto h; } } else { i--; if (i == 0) { printf("你的次数用尽了!!!"); goto h; } printf("清除失败,你还有%d次机会,请重新选择\n", i); } } else { printf("你已经尝试过清除了,请重新选择\n"); } } break; case 0: printf("请输入要排查的坐标(请输入数字):\n"); printf("请输入纵坐标:"); x = judge(x); printf("请输入横坐标:"); y = judge(y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (show[x][y] == '#') { printf("你已经尝试过清除了,请重新选择\n"); } else if (mine[x][y] == '1') { printf("很遗憾,你被炸死了\n"); DisplayBoard(mine, row, col); goto h; } else { Expand(mine, show, row, col, x, y); DisplayBoard(show, row, col); } } else { printf("坐标非法请重新输入\n"); break; } } } h: printf("游戏结束"); system("pause"); }