思路解释
首先我们实现扫雷需要两个棋盘,我们用棋盘mine来存放雷,0表示非雷,1表示雷。另一个棋盘show存放的是该坐标周围八个坐标中存放的雷数。这样做是为了避免搞不清1的含义,因为1可能代表该坐标是雷,也可能代表改坐标周围有1个雷。
game.h
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<time.h> //定义布置雷的数量 #define COUNT 10 //定义雷场大小 #define ROW 9 #define COL 9 //定义二维数组大小 #define ROWS ROW+2 #define COLS COL+2 //数组初始化函数的声明 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set); //数组打印函数的声明 void DisplayBoard(char board[ROWS][COLS], int row, int col); //布置雷函数的声明 void SetBoard(char board[ROWS][COLS], int row, int col); //boardm--mine--存雷排雷 //boards--show--存数展示 //排查雷函数的声明 void FindMine(char boardm[ROWS][COLS], char boards[ROWS][COLS], int row, int col); //统计坐标周围雷数量的函数声明 int counter(char board[ROWS][COLS], int x, int y);
game.c
#include"game.h" //初始化雷场函数的实现 void InitBoard(char board[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++) { board[i][j] = set; } } } //打印棋盘函数的实现 void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; for (j = 0; j <= col; j++) { 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"); } } //布置雷函数的实现 void SetBoard(char board[ROWS][COLS], int row, int col) { int count = COUNT; while (count) { int x = rand() % row + 1; int y = rand() % col + 1; //判断坐标合法性 if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } //菜单1打印函数 menu1() { printf("************************\n"); printf("**1.扫雷 2.做标记**\n"); printf("************************\n"); } //查找雷函数的声明 void FindMine(char boardm[ROWS][COLS], char boards[ROWS][COLS], int row, int col) { int count = 0; while (1) { menu1(); printf("请输入:>"); int xy = 0; scanf("%d", &xy); switch (xy) { case 1: { int x = 0; int y = 0; printf("请输入要扫雷的坐标:(先输横坐标,再输纵坐标)\n"); scanf("%d %d", &x, &y); //scanf("%d", &y); if ((x >= 1 && x <= row) && (y >= 1 && y <= row)) { if (boardm[x][y] == '1') { printf("很遗憾你被炸死了\n"); DisplayBoard(boardm, ROW, COL); break; } else { //统计周围雷数 int ret = counter(boardm, x, y); boards[x][y] = ret + '0'; count++; DisplayBoard(boards, ROW, COL); printf("----------------------\n"); //DisplayBoard(boardm, ROW, COL); if (count == ROW * COL - COUNT) { printf("恭喜你,成功了\n"); DisplayBoard(boardm, ROW, COL); break; } } } else { printf("输入的坐标非法,请重新输入\n"); } break; } case 2: { int xx = 0; int yy = 0; printf("请输入要标记的坐标:(先输横坐标,再输纵坐标)\n"); scanf("%d %d", &xx, &yy); while (1) { if ((xx >= 1 && xx <= row) && (yy >= 1 && yy <= row)) { if (boards[xx][yy] == '*') { boards[xx][yy] = '#'; printf("标记成功\n"); DisplayBoard(boards, ROW, COL); break; } else { printf("改地址已排查,无法做标记,请重新输入\n"); } } } } } } } int count = 0 //计数器函数的声明 int counter(char board[ROWS][COLS], int x, int y) { return board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] + board[x][y - 1] + board[x][y + 1] + board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1] - 8 * '0'; }
mine.c
#include"game.h" //打印菜单1函数 void menu1() { printf("*********************\n"); printf("** 1.play 0.exit **\n"); printf("*********************\n"); } //三子棋游戏函数 void play() { //创建数组 char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; //初始化雷场 InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); //打印棋盘 DisplayBoard(show, ROW, COL); printf("--------------------\n"); //DisplayBoard(mine, ROW, COL); //设置雷(雷---‘1’ 非雷--‘0’) SetBoard(mine, ROW, COL); DisplayBoard(mine, ROW, COL);//!!!!!!!!!!!!!!!!!!!!! //排查雷 FindMine(mine, show, ROW, COL); } //主函数 int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu1(); printf("请输入:>"); scanf("%d", &input); switch (input) { case 1: { printf("游戏开始,共有%d个雷。\n", COUNT); play(); break; } case 0: { printf("退出游戏。\n"); break; } default: { printf("选择错误,请重新选择\n"); } } }while(input); return 0; }
【游戏开发】