007 【编程之路扫雷游戏】(C语言实现)( 四 )

<= 9 && y >= 1 && y <= 9){//遍历周围坐标for (around_x = -1; around_x <= 1; around_x++){for (around_y = -1; around_y <= 1; around_y++){//如果这个坐标不是雷if (mine[x + around_x][y + around_y] == '0'){//统计周围雷的个数count = AmountMine(mine, x + around_x, y + around_y);if (count == 0){if (show[x + around_x][y + around_y] == '*'){show[x + around_x][y + around_y] = ' ';Spread(mine, show, x + around_x, y + around_y);}}else{show[x + around_x][y + around_y] = count + '0';}}}}}}int ClearMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col){int i = 0, j = 0;int end = 0;for (i = 1; i <= row; i++){for (j = 1; j <= col; j++){if (mine[i][j] == '1' && show[i][j] == '!')end++;}}return end;}void FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col){int n = 0, x = 0, y = 0;int win = 0;int flag = 0;int fail = 0;again:while (win < AMOUNT){printf("功能 1:排查雷 2:标记雷 3:取消标记\n");printf("请选择功能和排查的坐标(功能 横 纵)\n");scanf("%d %d %d", &n, &x, &y);switch (n){case 1:break;case 2:{FlagMine(show, mine, row, col, x, y);win++;break; }case 3:{FlagCancel(show, mine, row, col, x, y); flag = 1;win--;break;}}if (x >= 1 && x <= row && y >= 1 && y <= col){if (mine[x][y] == '1' && show[x][y] == '*' && flag == 0){printf("你被炸死了,下面是雷区分布图 1为雷0非雷\n");Display(mine, ROW, COL);fail = 1;break;}else if (show[x][y] == '!')Display(show, ROW, COL);else if (flag == 1)Display(show, ROW, COL);else {int count = AmountMine(mine, x, y);if (count == 0){show[x][y] = ' ';Spread(mine, show, x, y);Display(show, ROW, COL);}else{show[x][y] = count + '0';Display(show, ROW, COL);}}}else{printf("输入错误,请重新输入");}}if (AMOUNT == ClearMine(mine,show,row,col)){printf("恭喜你,扫雷成功\n");}else if(fail == 1){printf("请选择是否再来一把\n");}else{printf("标记的雷有误,请仔细检查,重新标记\n");goto again;}}
该部分代码为test.c文件中的
#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"void game(){//存放雷的棋盘雷为'1',非雷为'0'这里是字符1,0char mine[ROWS][COLS] = { 0 };//展示排查后的棋盘未排查的区域为'*',排查后显示周围雷的数字(也是字符)char show[ROWS][COLS] = { 0 };InitBoard(mine, ROWS, COLS, '0');InitBoard(show, ROWS, COLS, '*');SetMine(mine, ROW, COL);Display(show, ROW, COL);//Display(mine, ROW, COL);FindMine(show, mine, ROW, COL);}int main(){srand((unsigned int)time(NULL));int choice = 0;do{printf("游戏介绍:\n");printf("输入 1 为开始游戏,输入 0 为退出游戏\n");printf("游戏最终的胜利即为把全部的雷标记出来\n");menu();printf("请选择\n");scanf("%d", &choice);switch (choice){case 1:{game();break;}case 0:{printf("游戏退出\n");break;}default:{printf("选择错误,重新选择\n");break;}}} while (choice);return 0;}
【007【编程之路扫雷游戏】(C语言实现)】希望这个扫雷游戏可以帮到你 。有什么错误和建议也希望大家可以在评论区发出来 。