game.h:函数声明
test.c:运行测试
game.c:游戏核心代码
#include
#include"game.h"void menu(){printf("********************************n");printf("********** 1.play ***********n");printf("********** 0.exit ***********n");printf("********************************n");}void game(){system("cls");//清屏char mine[ROWS][COLS] = { 0 };//存放布置好雷的信息char show[ROWS][COLS] = { 0 };//存放排查出雷的信息//初始化棋盘initboard(mine, ROWS, COLS, '0');initboard(show, ROWS, COLS, '*');//打印棋盘//displayboard(mine, ROW, COL);displayboard(show, ROW, COL);//布置雷setmine(mine, ROW, COL);//displayboard(mine, ROW, COL);//排查雷findmine(mine, show, ROW, COL);}int main(){int input;srand((unsigned int)time(NULL));do{menu();printf("请选择:>n");scanf("%d", &input);switch (input){case 1:printf("开始游戏n");game();break;case 0:printf("退出游戏n");break;default:printf("选择错误,请重新选择n");break;}} while (input);}
game.c#include"game.h"void initboard(char board[ROWS][COLS], int rows, int cols, char set){int i, j;for (i = 0; i < rows; i++){for (j = 0; j < cols; j++){board[i][j] = set;}}}void displayboard(char board[ROWS][COLS], int row, int col){int i, j;printf("----------扫雷游戏-----------n");for (i = 0; i <= col; i++){printf("%d ", i);}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){//布置10个雷int count = easy_count;//产生随机下标while (count){int x = rand() % row + 1;int y = rand() % col + 1;//判断当前坐标是否有雷if (board[x][y] == '0'){board[x][y] = '1';count--;}}}int get_mine_count(char mine[ROWS][COLS], int x, int y){char num = '0';int i, j;for (i = -1; i <= 1; i++){for (j = -1; j <= 1; j++){num += mine[x + i][y + j];}}return num - 10 * '0';}void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col){int x = 0;int y = 0;int win = 0;while(win