< 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)//如果触发到fail为1,则说明爆炸过了,就失败了 。{printf("请选择是否再来一把\n");}else{printf("标记的雷有误,请仔细检查,重新标记\n");//到这里就是标记了至少一个安全区域,需要重新排雷goto again;//goto语句直接跳到重新排雷的地方}}
源码
该部分代码为game.h文件中的
#pragma once#include#include#include#define ROW 9#define COL 9#define ROWS ROW+2#define COLS COL+2#define AMOUNT 10//菜单void menu();//棋盘初始化void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch);//展示棋盘void Display(char board[ROWS][COLS], int row, int col);//布置雷void SetMine(char board[ROWS][COLS], int row, int col);//选择坐标区域雷的数量int AmountMine(char board[ROWS][COLS], int row, int col);//扫雷void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//标记雷void FlagMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col, int x, int y);//取消标记void FlagCancel(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col, int x, int y);//排查选择坐标区域周围是否安全void Spread(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);//返回被标记的雷的个数int ClearMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
该部分代码为game.c文件中的
#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"void menu(){printf("************************\n");printf("*****1.play*****\n");printf("*****0.exit*****\n");printf("************************\n");}void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch){int i = 0, j = 0;for (i = 0; i < rows; i++){for (j = 0; j < cols; j++){board[i][j] = ch;}}}void Display(char board[ROWS][COLS], int row, int col){printf("-------------------\n");int i = 0, j = 0;for (j = 0; j <= col; j++)//这个for循环是打印棋盘的列坐标{printf("%d ", j);}printf("\n");for (i = 1; i <= row; i++){printf("%d ", i);//这一行语句是打印棋盘的行坐标for (j = 1; j <= col; j++){printf("%c ", board[i][j]);}printf("\n");}printf("-------------------\n");}void SetMine(char board[ROWS][COLS], int row, int col){int x = 0, y = 0;int count = AMOUNT;while (count){x = rand() % row + 1;//(0~8)+1==1~9y = rand() % col + 1;//(0~8)+1==1~9if (board[x][y] == '0'){board[x][y] = '1';count--;}}}int AmountMine(char board[ROWS][COLS], int x, int y){return (board[x - 1][y] +board[x - 1][y - 1] +board[x][y - 1] +board[x + 1][y - 1] +board[x + 1][y] +board[x + 1][y + 1] +board[x][y + 1] +board[x - 1][y + 1] - 8 * '0');}void FlagMine(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col, int x, int y){if (x >= 1 && x <= row && y >= 1 && y <= col){show[x][y] = '!';}}void FlagCancel(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col, int x, int y){if (show[x][y] == '!'){show[x][y] = '*';}}void Spread(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y){int around_x = 0;int around_y = 0;int count = 0;//坐标合法if (x >= 1 && x
- 聊一聊我在 B站 上自学编程的经历吧!
- 2 Transformer综述大全【A Survey of Visual Tr
- 【浏览器】Ajax 是什么? Ajax的基本流程?
- 人类文字的发展史:文字是怎么样发展的
- 尼龙长袜如何诞生的?它曾是上流社会身份标识
- 中国原本岭北行省被称为世界中心,可现在只剩下一半
- 因为活祭?迈诺安文明的消逝之谜
- 【GNURadio实验报告】实验3
- 【MYSQL】比前一天高的温度
- 【工具|FoFa 查询工具】