扫雷游戏相信大家都有玩过吧,游戏简单但也很经典。在这里我为大家演示一下用C语言编写一个简单的扫雷程序,代码如下,每个函数部分都有详细的解释。
game.h文件中声明各种函数:
#ifndef __GAME_H__ #define __GAME_H__ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> #define ROWS 12//定义雷盘行数 #define COLS 12//定义雷盘列数 #define COUNT 10//定义雷数 void init_board(char mine[ROWS][COLS], char show[ROWS][COLS]);//初始化雷盘 void display_board(char show[ROWS][COLS]);//打印玩家雷盘 void print_board(char mine[ROWS][COLS]);//打印设计者雷盘 void set_board(char mine[ROWS][COLS]);//布雷函数 int get_board(char mine[ROWS][COLS], int x, int y);//统计周围雷数 void safe_board(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);//判断第一个雷是否安全函数 int open_board(char mine[ROWS][COLS],char [ROWS][COLS], int x, int y);//展开周围函数 int sweep_board(char real_mine[ROWS][COLS], char show_mine[ROWS][COLS], int x, int y);//扫雷函数 int count_show_mine(char show[ROWS][COLS]);//判断雷盘中剩余未排位置的个数,返回值等于设置好的雷时游戏胜利 #endif
game.c文件包含各种游戏函数:
#include"game.h"void init_board(char mine[ROWS][COLS], char show[ROWS][COLS])//初始化雷盘{ int i = 0; int j = 0; for (i = 1; i < ROWS - 1; i++) { for (j = 1; j < COLS - 1; j++) { mine[i][j] = '0'; } } for (i = 1; i < ROWS - 1; i++) { for (j = 1; j < COLS - 1; j++) { show[i][j] = '*'; } }
}
void print_board(char mine[ROWS][COLS])//打印设计者雷盘{ int i, j; printf(" "); for (i = 1; i < COLS - 1; i++) printf("%d ", i); printf("\n"); for (i = 1; i < ROWS - 1; i++) { printf("%2d ", i); for (j = 1; j < COLS - 1; j++) { printf("%c ", mine[i][j]); } printf("\n"); }}
void display_board(char show[ROWS][COLS])//打印玩家雷盘{ int i, j; printf(" "); for (i = 1; i < COLS - 1; i++) printf("%d ", i); printf("\n"); for (i = 1; i < ROWS - 1; i++) { printf("%2d ", i); for (j = 1; j < COLS - 1; j++) { printf("%c ", show[i][j]); } printf("\n"); }}
void set_board(char mine[ROWS][COLS])//布雷函数{ int x = 0; int y = 0; int count = COUNT; srand((unsigned)time(NULL)); while (count) { x = rand() % (ROWS - 2) + 1; y = rand() % (COLS - 2) + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } }}
int get_board(char mine[ROWS][COLS], int x, int y)//判断坐标周围雷数{ int count = 0; if (mine[x - 1][y - 1] == '1') count++; if (mine[x - 1][y] == '1') count++; if (mine[x - 1][y + 1] == '1') count++; if (mine[x][y - 1] == '1') count++; if (mine[x][y + 1] == '1') count++; if (mine[x + 1][y - 1] == '1') count++; if (mine[x + 1][y] == '1') count++; if (mine[x + 1][y + 1] == '1') count++; return count;}
void safe_board(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y){ /*int x = 0; int y = 0;*/ char ch = 0; int count = 0; int ret = 1; printf("请输入坐标扫雷1:>"); while (1) { scanf_s("%d%d", &x, &y); if ((x >= 1 && x <= 10) && (y >= 1 && y <= 10)) { if (mine[x][y] == '1') { mine[x][y] = '0'; char ch = get_board(mine, x, y); show[x][y] = ch + '0'; get_board(mine, x, y); display_board(show); while (ret)//在其余有空的地方设置一个雷 { int x = rand() % (ROWS - 2) + 1;//产生1到10的随机数,在数组下标为1到10的范围内布雷 int y = rand() % (COLS - 2) + 1;//产生1到10的随机数,在数组下标为1到10的范围内布雷 if (mine[x][y] == '0')//找不是雷的地方布雷 { mine[x][y] = '1'; ret--; break; } }break;//跳出此函数
} if (mine[x][y] == '0') { char ch = get_board(mine,x, y); show[x][y] = ch + '0';//数字对应的ASCII值和数字字符对应的ASCII值相差48,即'0'的ASCII值 get_board(mine, x, y); display_board(show); break; }
} else { printf("输入坐标错误,请重新输入:>"); } }}
int open_board(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)//坐标周围展开函数{ if (mine[x - 1][y - 1] == '0') { show[x - 1][y - 1] = get_board(mine, x - 1, y - 1) + '0';//显示该坐标周围雷数 } if (mine[x - 1][y] == '0') { show[x - 1][y] = get_board(mine, x - 1, y) + '0';//显示该坐标周围雷数 } if (mine[x - 1][y + 1] == '0') { show[x - 1][y + 1] = get_board(mine, x - 1, y + 1) + '0';//显示该坐标周围雷数 } if (mine[x][y - 1] == '0') { show[x][y - 1] = get_board(mine, x, y - 1) + '0';//显示该坐标周围雷数 } if (mine[x][y + 1] == '0') { show[x][y + 1] = get_board(mine, x, y + 1) + '0';//显示该坐标周围雷数 } if (mine[x + 1][y - 1] == '0') { show[x + 1][y - 1] = get_board(mine, x + 1, y - 1) + '0';//显示该坐标周围雷数 } if (mine[x + 1][y] == '0') { show[x + 1][y] = get_board(mine, x + 1, y) + '0';//显示该坐标周围雷数 } if (mine[x + 1][y + 1] == '0') { show[x + 1][y + 1] = get_board(mine, x + 1, y + 1) + '0';//显示该坐标周围雷数 } }
int sweep_board(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)//扫雷函数,踩到雷返回1,没有踩到雷返回0{ /*int x = 0; int y = 0;*/ int count = 0; printf("输入坐标扫雷2:>\n"); scanf_s("%d%d", &x, &y);//只能输入1到10 if ((x >= 1 && x <= 10) && (y >= 1 && y <= 10))//判断输入坐标是否有误,输入错误重新输入 { if (mine[x][y] == '0')//没踩到雷 { char ch = get_board( mine, x, y); show[x][y] = ch + '0';//数字对应的ASCII值和数字字符对应的ASCII值相差48,即'0'的ASCII值 open_board(mine,show, x, y); display_board(show); if (count_show_mine(show) == COUNT)//判断剩余未知区域的个数,个数为雷数时玩家赢 { display_board(show); printf("玩家赢!\n\n"); return 0; } } else if (mine[x][y] == '1')//踩到雷 {
return 1; }
} else { printf("输入错误,请重新输入\n"); } return 0;//没踩到雷}
int count_show_mine(char show[ROWS][COLS])//判断剩余未知区域的个数,个数为雷数时玩家赢{ int count = 0; int i = 0; int j = 0; for (i = 1; i <= ROWS- 2; i++) { for (j = 1; j <= COLS - 2; j++) { if (show[i][j] == '*') { count++; } }
} return count;}
test.c文件调用game.c文件中的游戏函数,实现扫雷游戏:
#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() { int x = 0; int y = 0; char mine[ROWS][COLS]; char show[ROWS][COLS]; int ret = 0; init_board(mine, show);//初始化玩家棋盘和设计者棋盘 set_board(mine);//给棋盘布雷 display_board(show);//打印玩家棋盘 printf("\n"); print_board(mine);//打印设计者棋盘,可不打印,将此行代码注释掉即不打印设计者棋盘 safe_board(mine, show, x, y);//避免第一次被炸死 if (count_show_mine(show) == COUNT)//一步就赢的情况 { print_board(mine); printf("玩家赢!\n\n"); return; } while (1)//循环扫雷 { int ret = sweep_board(mine, show, x, y);//扫雷,踩到雷返回1,没有踩到雷返回0 if (count_show_mine(show) == COUNT )//若玩家棋盘的'*'个数为雷数时,扫雷完成,游戏胜利 { print_board(show);//打印设计者棋盘 printf("玩家赢!\n\n"); break; } if (ret)//判断是否踩到雷 { printf("被雷炸死\n"); print_board(mine);//打印设计者雷阵查看雷的分布 break; } } } int main() { srand((unsigned int)time(NULL));//产生随机数生成器 int input = 0; menu();//菜单 do { scanf("%d", &input); switch (input) { case 1:game(); break; case 0:exit(1);//退出游戏 break; default: printf("输入错误,重新输入\n"); break; } menu(); printf("contiue?\n"); } while (1);//循环玩游戏 system("pause"); return 0; }