C语言-扫雷游戏
C语言扫雷的实现主要分为1.棋盘数组初始化2.打印菜单及棋盘3.放雷4.排雷四个阶段涉及对函数数组的应用1.h#define _CRT_SECURE_NO_WARNINGS #include stdio.h void menu (void); #define ROW 9 #define COL 9 #define ROWS ROW2 #define COLS COL2 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set); void display_board(char print[ROWS][COLS], int row, int col); void set_mine(char mine[ROWS][COLS], int row, int col); #define EASY_COUNT 10 #include stdlib.h #include time.h void found_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col); int GetMineCount(char mine[ROWS][COLS], int x, int y);1.c#define _CRT_SECURE_NO_WARNINGS #include stdio.h #include 1.h //菜单 void menu (void) { printf(************\n); printf(****1.play****\n); printf(****0.exit****\n); printf(************\n); printf(请输入); } //初始化 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int a 0; for (a 0; a rows; a) { int b 0; for (b 0; b cols; b) { board[a][b] set; } } } //打印棋盘 void display_board(char print[ROWS][COLS], int row, int col ) { int a 0; printf(-----扫雷游戏-----\n); for (a 0; a row; a) { printf(%d, a); } printf(\n); for (a 1; a row; a) { int b 0; printf(%d , a); for (b 1; b col; b) { char c print[a][b]; printf(%c, c); } printf(\n); } } //放雷 void set_mine(char mine[ROWS][COLS], int row, int col) { //生成10个随机的坐标放雷 int count EASY_COUNT; while (count0) { int x rand() % row 1; int y rand() % col 1; if (mine[x][y] 0) { mine[x][y] 1; count--; } } } //统计周围类书 int 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 1][y 1] mine[x][y 1] mine[x - 1][y 1] - 8 * 0); } //排雷 void found_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x 0; int y 0; int win 0; while (win row * col - EASY_COUNT) { printf(请输入要排查的坐标格); scanf(%d %d, x, y); if (x 1 y 1 x row y col) { if (mine[x][y] 1) { printf(很遗憾你死了\n); display_board(mine, ROW, COL); break; } else { //该位置不是雷就统计周围的雷数 int count GetMineCount(mine, x, y); show[x][y] count 0; display_board(show, ROW, COL); win; } } else { printf(字符非法请重新输入); } } if (win row * col - EASY_COUNT) { printf(YOU WIN); display_board(mine, ROW, COL); } }test.c#define _CRT_SECURE_NO_WARNINGS #include stdio.h #include 1.h void game(void) { char mine[ROWS][COLS];//存放雷的 char show[ROWS][COLS];//展示存放排查出的雷的 //初始化 InitBoard(mine, ROWS, COLS, 0); InitBoard(show, ROWS, COLS, *); //打印棋盘 //display_board(mine, ROW, COL); display_board(show, ROW, COL); //放雷 set_mine(mine,ROW,COL); //display_board(mine, ROW, COL); //排雷 found_mine(mine,show,ROW,COL); } int main() { int input 0; srand((unsigned int)time(NULL)); do { menu(); scanf(%d, input); switch (input) { case 1: game(); break; case 0: printf(退出游戏\n); break; default: printf(选择错误请重新选择\n); break; } }while (input); return 0; }