利用C语言实现扫雷游戏

这篇文章主要为大家详细介绍了利用C语言实现扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

通过一段时间的C语言学习,想必小伙伴们也想跃跃欲试的编写一些小程序,这个扫雷简易游戏,非常适合C语言初学者去实践。

实现扫雷,首先要有两个棋盘,一个棋盘放置着雷的信息,另个用于展示到屏幕上;然后就是玩家输入坐标排雷,如果输入的坐标有雷,就游戏结束,要是没有雷就在展示棋盘对应的位置,显示出周边雷的个数;最后排查完所有坐标并且不被雷炸死,就是胜利。

以上是扫雷的基本逻辑,现在开始,一步一步的实现。

1.初始化棋盘

 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; for (i = 0; i 

mine就是放雷的棋盘,show就是展示出来棋盘。我们把mine都初始化成‘0',雷为‘1',show

都初始化为‘*'。

2.布置雷

 void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT;//10个雷 while (count) { //1. 生成随机下标 int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } }

3.排查雷

排查雷是本游戏关键,也稍有难度,首先,需要将俩个棋盘都传过去,每次输入坐标都会去雷盘里找,如果是雷就游戏结束,不是雷就会使对应的展示盘的位置 展现出周围的雷数。

 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"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("很遗憾,你被炸死了\n"); DisplayBoard(mine, ROW, COL); break; } else { int count = GetMineCount(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); win++; } } else { printf("坐标非法,重新输入\n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功\n"); DisplayBoard(mine, ROW, COL); } }

看(x,y)周围雷数的函数:

 int GetMineCount(char mine[ROWS][COLS], int x, int y) { return (mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0'); }

上面使扫雷游戏一步一步完成的步骤,接下来是游戏的运行代码:

1.头文件

 #include  #include  #include  #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 //初始化棋盘 void InitBoard(char board[ROWS][COLS], int rows, int cols, 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);

2.游戏代码

 #include "game.h" void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; for (i = 0; i "); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("很遗憾,你被炸死了\n"); DisplayBoard(mine, ROW, COL); break; } else { int count = GetMineCount(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); win++; } } else { printf("坐标非法,重新输入\n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功\n"); DisplayBoard(mine, ROW, COL); } }

3.总程序

 #include "game.h" void menu() { printf("********************************\n"); printf("*********   1. play     ********\n"); printf("*********   0. exit     ********\n"); printf("********************************\n"); } void game() { char mine[ROWS][COLS] = { 0 };//存放雷的信息 char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息 //初始化一下棋盘 InitBoard(mine, ROWS, COLS, '0');//'0' InitBoard(show, ROWS, COLS, '*');//'*' //布置雷 SetMine(mine, ROW, COL); DisplayBoard(show, ROW, COL); //排查雷 FindMine(mine, show, ROW, COL); } int main() { 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("选择错误,重新选择!\n"); break; } } while (input); return 0; }

以上就是利用C语言实现扫雷游戏的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言