1.设置2个11*11的棋盘,用字符型数组来表示。
2.其中棋盘一个用于随机分布雷
3.另外一个用于显示周围雷的个数
4.玩家输入坐标
5.写一个函数用于判断该坐标是否为雷
6.继续或结束
....
附代码
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" int main() { srand((unsigned)time(NULL)); menu(); int input = 0; char Board[ROWS][COLS] = { 0 }; char arr[ROWS][COLS] = { 0 }; int i = 0; do { printf("请选择:>\n"); scanf("%d", &input); switch (input) { case 1: init_Board(Board,ROWS,COLS); init_char(arr, ROWS, COLS); set_Mine(Board, ROW, COL); //print_arr(arr, ROW, COL); while (1) { i=judge_Mine(Board, ROW, COL, arr); print_arr(arr, ROW, COL); if (i == 0) break; if (i == 71) { printf("congratulations!win!\n"); break; } } break; case 0: printf("BYE\n"); break; default: printf("无效选择:>\n"); } } while (input); return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" //菜单 void menu(void) { printf("*******************************\n"); printf("************ 1.paly ***********\n"); printf("************ 0.exit ***********\n"); printf("*******************************\n"); } //重置棋盘 void init_Board(char Board[ROWS][COLS], int row, int col) { int m = 0; int n = 0; for (m = 0; m < row; m++) { for (n = 0; n < col; n++) { Board[m][n] = '0'; } } } //重置棋盘 void init_char(char arr[ROWS][COLS], int row, int col) { int m = 0; int n = 0; for (m = 0; m < row; m++) { for (n = 0; n < col; n++) { arr[m][n] = '*'; } } } //布置雷 void set_Mine(char Board[ROWS][COLS], int row, int col) { int x = 1; int y = 1; int c = 0; while (c <= count) { x = rand() % 9 + 1; y = rand() % 9 + 1; if (Board[x][y] == '0') { Board[x][y] = '1'; c++; } } } //失败时打印雷的分布情况 void print_Mine(char Board[ROWS][COLS], int row, int col) { int x = 0, y = 0,i=0; for (i = 0; i <= row; i++) { printf("%d ", i); } printf("\n"); for (x = 1; x <= row; x++) { printf("%d ", x); for (y = 1; y <= col; y++) { printf("%c ", Board[x][y]); } printf("\n"); } } //显示周围雷的情况 int count_mine(char Board[ROWS][COLS], int x, int y) { return (Board[x - 1][y] + Board[x + 1][y] + Board[x - 1][y - 1] + Board[x - 1][y + 1] + Board[x][y - 1] + Board[x][y + 1] + Board[x + 1][y - 1] + Board[x + 1][y + 1] - 8 * '0'); } //判断是否为雷 int judge_Mine(char Board[ROWS][COL], int row, int col,char arr[ROWS][COLS]) { printf("请输入坐标\n"); int x = 0; int y = 0; int ret = 0; static int k = 0; scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (Board[x][y] != '1') { ret = count_mine(Board, x, y); arr[x][y] = ret + '0'; k++; return k; } else { printf("defate\n"); print_Mine(Board, ROW, COL); return 0; } } } //打印棋盘 void print_arr(char arr[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int i = 0; for (i = 0; i <= row; i++) { printf("%d ", i); } printf("\n"); for (x = 1; x <= col; x++) { printf("%d ", x); for (y = 1; y <= col; y++) { printf("%c ", arr[x][y]); } printf("\n"); } }