如图可以得到如下思路:
- 设计两个二维数组,mine[ 11][ 11],show[ 9][9 ]; show[ ][ ]用来展示扫雷界面,mine[ ][ ]数组用来布置雷;输入坐标: 是雷就炸死 ;不是雷就告诉你周围八个坐标有多少个雷,直到把所有非雷的坐 标都排查出来,游戏胜利。
代码分为三个部分:
一、测试游戏的逻辑:#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void menu() { printf("*************************n"); printf("******* 1:play *****n"); printf("******* 0:exit *****n"); printf("*************************n"); } void game() { //定义棋盘 char mine[ROWS][COLS] = { 0 }; //'0' char show[ROWS][COLS] = { 0 }; //'*' //初始化棋盘 Init_board(mine, ROWS, COLS, '0'); Init_board(show, ROWS, COLS, '*'); //布置雷 set_mine(mine, ROW, COL); //打印棋盘 show_board(mine, ROW, COL); //show_board(show, ROW, COL); //排雷 find_mine(mine, show, ROW, COL); } int main() { srand((unsigned int)time(NULL)); int input = 0; do { menu(); printf("请选择:"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏n"); break; default : printf("选择错误,请重新输入n"); } } while (input); return 0; }二、 游戏的实现:
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" //初始化棋盘 void Init_board(char arr[ROWS][COLS], int rows, int cols, char set) { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { arr[i][j] = set; } } } //打印棋盘 void show_board(char arr[ROWS][COLS], int row, int col) { int i = 0; int j = 0; 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 ", arr[i][j]); } printf("n"); } printf("-------扫雷---------nnn"); } //布置雷 void set_mine(char mine[ROWS][COLS], int row, int col) { int count = ECOUNT; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } } int get_mine(char mine[ROWS][COLS], int x, int y) { int i = 0; int j = 0; int sum = 0; for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { sum += mine[x + i][y + j]; } } return sum - '0' * 9; } //排雷 void find_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-ECOUNT) { printf("请输入要排查的坐标:"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("很遗憾,你被炸死了n"); show_board(mine, ROW, COL); break; } else { int count = get_mine(mine, x, y); show[x][y] = count + '0'; show_board(show, ROW, COL); win++; } } else { printf("坐标非法,请重新输入n"); } } if (win == row * col - ECOUNT) { printf("恭喜你,排雷成功n"); show_board(mine, ROW, COL); } }三、函数的声明和符号的定义:
#include#include #include #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define ECOUNT 10 //初始化棋盘 void Init_board(char arr[ROWS][COLS], int rows, int cols, char set); //打印棋盘 void show_board(char arr[ROWS][COLS], int row, int col); //布置雷 void set_mine(char mine[ROWS][COLS], int row, int col); //排雷 void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)