<>扫雷源代码
编译环境:我用的是VS2013,更高级的版本也是可以的。
创建三个文件,头文件game.h,两个源文件game.c和test.c,头文件game.h主要是函数的各种声明,源文件game.c中主要是扫雷算法的实现,源文件test.c中主要是main函数,函数的主体部分。
test.c文件
#include "game.h" void menu() {
printf("**************************************\n"); printf("************ 1.
play **********\n"); printf("************ 0. exit *********\n");
printf("**************************************\n"); } void game() { //雷的信息存储
//1.布置好的雷的信息 char mine[ROWS][COLS] = { 0 };//11*11 //2.排查的雷的信息 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); } void test() {
int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("请选择:>");
scanf("%d", &input); switch (input) { case 1: game(); break; case 0:
printf("退出游戏\n"); break; default: printf("选择错误,请重新选择"); break; } } while
(input); } int main() { test(); return 0; }
game.h文件
#define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #include
<stdio.h> #include <stdlib.h> #include <time.h> //雷的数目 #define EASY_COUNT 10
void InitBoard(char board[ROW][COL], int row, int col, char set); void
DisplayBoard(char board[ROWS][COLS], int row, int col); void SetMine(char
board[ROWS][COLS], int row, int col); void FindMine(char mine[ROWS][COLS], char
show[ROWS][COLS], int row, int col); void SearchMine(char mine[ROWS][COLS],
char show[ROWS][COLS], int i, int j);
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 (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"); }
} void SetMine(char board[ROWS][COLS], int row, int col) { int count =
EASY_COUNT; while (count) { int x = rand() % row + 1;//1-9 int y = rand() % col
+ 1;//1-9 if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } int
get_mine_count(char mine[ROWS][COLS],int x,int y) { return mine[x - 1][y] +
mine[x - 1][y - 1] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] +
mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '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<row*col - EASY_COUNT) {
printf("请输入排查雷的坐标:>"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row &&y >= 1
&& y <= col) { //坐标合法 //1.踩雷 if (mine[x][y] == '1') { printf("很遗憾,你被炸死了!\n");
DisplayBoard(mine, row, col); break; } else//不是雷 { //计算x、y坐标周围几个雷 int count =
get_mine_count(mine, x, y); if (count == 0) { SearchMine(mine, show, x, y);
DisplayBoard(show, row, col); } else { show[x][y] = count + '0';
DisplayBoard(show, row, col); } } } else { printf("请输入合法的坐标,请重新输入!\n"); } win =
win_count(show, ROW, COL); } if (win == row*col - EASY_COUNT) {
printf("恭喜你排雷成功!\n"); DisplayBoard(mine, row, col); } } int win_count(char
show[ROWS][COLS],char row,char col) { int count = 0; int i = 0; int j = 0; for
(i = 1; i <= row; i++) { for (j = 1; j <= col; j++) { //if (show[i][j] == ' '
|| show[i][j] >= 1 || show[i][j] <= 8) if (show[i][j] != '*') { count++; } } }
return count; } //展开功能 void SearchMine(char mine[ROWS][COLS], char
show[ROWS][COLS], int i, int j) { if (i < 1 || i > ROW || j < 1 || j > COL) {
return; } show[i][j] = ' '; int posArr[8][2] = { { i - 1, j - 1 }, { i - 1, j
}, { i - 1, j + 1 }, { i, j - 1 }, { i, j + 1 }, { i + 1, j - 1 }, { i + 1, j
}, { i + 1, j + 1 } }; for (int k = 0; k < 8; k++) { int row = posArr[k][0];
int col = posArr[k][1]; //已经探索过的 if (show[row][col] >= '1' && show[row][col] <=
'8' || show[row][col] == ' ') { continue; } //1.如果此方向周围一圈有雷,则显示雷的数量,不再往下搜索
//2.如果此方向一圈没有雷,则递归继续往周边搜索 int mineNum = 0; mineNum = get_mine_count(mine, row,
col); if (mineNum > 0) { //show[i - 1][j - 1] = mineNum + '0';
//DisplayBoard(show, row, col); show[row][col] = mineNum + '0'; } else {
SearchMine(mine, show, row, col); } } }