赞
踩
哈喽,大家好,上次我们已经学习了三子棋小游戏,今天我们来学习扫雷小游戏了。
目录
想必大家都玩过扫雷这款小游戏吧?
在一个棋盘上随机摆放数个雷,这些雷用户无法看见,只能一个个将雷排查出来,用户点击一个格子,如果这个格子含有雷,游戏就以失败告终,如果这个格子不是雷,就会在这个格子上显示周围8个格子共有多少个雷,如下图:
今天我们就用C语言来实现这个扫雷小游戏
下面是游戏要用到的函数:
- //初始化棋盘
- 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);
- //统计雷
- int GetMineCount(char mine[ROWS][COLS], int x, int y);
- //递归,展开一片区域
- void DfsMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col);
菜单非常简洁,用户输入1表示开始游戏,输入0则退出小游戏
- void menu()
- {
- printf("********************\n");
- printf("***** 1.play *****\n");
- printf("***** 0.exit *****\n");
- printf("********************\n");
- }
game()函数包含一个游戏的执行过程,将一些函数组合了起来,形成游戏的完整过程。在这儿我们定义了两个数组,mine数组表示存放的雷,show数组表示存放排查出雷的信息。
- void game()
- {
- char mine[ROWS][COLS];//存放的雷
- char show[ROWS][COLS];//存放盘查出雷的信息
- //初始化棋盘
- //mine数组最开始全是‘0’
- //show数组最开始全是‘*’
- InitBoard(mine, ROWS, COLS, '0');
- InitBoard(show, ROWS, COLS, '*');
- DisPlayBoard(show, ROW, COL);
-
- //布置雷
- SetMine(mine, ROW, COL);
- //排查雷
- FindMine(mine, show, ROW, COL);
- }
main函数是整个程序中必不可少的一个部分。
- int main()
- {
- int input = 0;
- srand((unsigned int)time(NULL));
- do
- {
- menu();
- printf("请选择:>");
- scanf("%d", &input);
- switch (input)
- {
- case 1:
- game();
- break;
- case 0:
- system("cls");
- printf("退出游戏\n");
- break;
- default:
- printf("选择错误,重新选择\n");
- break;
- }
- } while (input);
- }
初始化棋盘将两个数组初始化。mine数组全部初始化为‘0’,表示最开始还没有存放雷。show数组最开始初始化为‘*’。
- void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
- {
- int i = 0;
- for (i = 0; i < rows; i++)
- {
- int j = 0;
- for (j = 0; j < cols; j++)
- {
- board[i][j] = set;
- }
- }
- }
打印棋盘这个步骤很简单,但为了方便玩家,我们顺便把棋盘的行号和列号也打印出来。如下图:
- void DisPlayBoard(char board[ROWS][COLS], int row, int col)
- {
- int i = 0;
- printf("--------扫雷--------\n");
- for (i = 0; i <= col; i++)
- {
- printf("%d ", i);
- }
- printf("\n");
- for (i = 1; i <= row; i++)
- {
- int j = 0;
- printf("%d ", i);
- for (j = 1; j <= col; j++)
- {
- printf("%c ", board[i][j]);
- }
- printf("\n");
- }
- }
布置雷就是让电脑随机生成10个不重复的坐标,这些坐标就是雷的位置
- void SetMine(char board[ROWS][COLS], int row, int col)
- {
- //布置十个雷
- //随机生成坐标
- int count = ESAY_COUNT;
- while(count)
- {
- int x = rand() % row + 1;
- int y = rand() % col + 1;
- if (board[x][y] == '0')
- {
- board[x][y] = '1';
- count--;
- }
- }
- }
用户输入要排查的雷的坐标,如果该坐标是雷,就输出"很遗憾,你被炸死了“,否则统计这个位置周围的八个位置雷的数量,存到show数组对应坐标位置里。同时这里扩展了一个递归展开,可以展开一片非雷区域。
- void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
- {
- int x, y;
- int win = 0;
- while (win < row * col - ESAY_COUNT)
- {
- system("cls");
- DisPlayBoard(show, ROW, COL);
- printf("请输入要排查雷的坐标:>");
- scanf("%d%d", &x, &y);
- if (x >= 1 && x <= row && y >= 1 && y <= col)
- {
- if (mine[x][y] == '1')
- {
- printf("很遗憾,你被炸死了\n");
- break;
- }
- else
- {
- //该位置不是雷,就统计这个坐标周围有几个雷
- int count = GetMineCount(mine, x, y);
- show[x][y] = count + '0';
- if (count == 0)
- {
- //递归,展开一片区域
- DfsMine(mine, show, x, y, ROW, COL);
- }
- show[x][y] = count + '0';
- DisPlayBoard(show, ROW, COL);
- win++;
- }
- }
- else
- {
- printf("坐标非法,重新输入\n");
- }
- }
- if (win == row * col - ESAY_COUNT)
- {
- printf("恭喜你,排雷成功\n");
- }
- DisPlayBoard(mine, ROW, COL);
- }
这个函数只要返回坐标(x,y)周围雷的数量即可。
- int GetMineCount(char mine[ROWS][COLS], int x, int y)
- {
- return (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] + mine[x - 1][y] - 8 * '0');
- }
这块可以说是一个扩展的内容了,当我们点到一个周围没有地雷的格子,这些区域就会自动展开。
这里需要用到函数递归来实现,可以将其看作是一个深度优先搜索
- void DfsMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col)
- {
- int count= GetMineCount(mine, x, y);
- show[x][y] = count + '0';
- if (count != 0)
- {
- return;
- }
- int xx[] = { 1,1, 1 ,-1,-1,-1,0, 0 };
- int yy[] = { 1,0,-1, 0, 1, -1 ,1,-1};
- int i = 0;
- for (i = 0; i < 8; i++)
- {
- int dx = x + xx[i];
- int dy = y + yy[i];
- if (dx >= 1 && dx <= row && dy >= 1 && dy <= col && mine[dx][dy]=='0' && show[dx][dy] == '*')
- {
- DfsMine(mine, show, dx, dy, ROW, COL);
- }
- }
- }
game.h文件
- #pragma once
-
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- #include<Windows.h>
-
-
- #define ROW 9
- #define COL 9
- #define ROWS ROW+2
- #define COLS COL+2
- #define ESAY_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);
- //统计雷
- int GetMineCount(char mine[ROWS][COLS], int x, int y);
- //递归,展开一片区域
- void DfsMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col);
-
game.c文件
- #include"game.h"
-
- //初始化棋盘
- void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
- {
- int i = 0;
- for (i = 0; i < rows; i++)
- {
- int j = 0;
- for (j = 0; j < cols; j++)
- {
- board[i][j] = set;
- }
- }
- }
-
- //打印棋盘
- void DisPlayBoard(char board[ROWS][COLS], int row, int col)
- {
- int i = 0;
- printf("--------扫雷--------\n");
- for (i = 0; i <= col; i++)
- {
- printf("%d ", i);
- }
- printf("\n");
- for (i = 1; i <= row; i++)
- {
- int j = 0;
- 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 = ESAY_COUNT;
- while(count)
- {
- int x = rand() % row + 1;
- int y = rand() % col + 1;
- if (board[x][y] == '0')
- {
- board[x][y] = '1';
- count--;
- }
- }
- }
-
- //排查雷
- void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
- {
- int x, y;
- int win = 0;
- while (win < row * col - ESAY_COUNT)
- {
- system("cls");
- DisPlayBoard(show, ROW, COL);
- printf("请输入要排查雷的坐标:>");
- scanf("%d%d", &x, &y);
- if (x >= 1 && x <= row && y >= 1 && y <= col)
- {
- if (mine[x][y] == '1')
- {
- printf("很遗憾,你被炸死了\n");
- break;
- }
- else
- {
- //该位置不是雷,就统计这个坐标周围有几个雷
- int count = GetMineCount(mine, x, y);
- show[x][y] = count + '0';
- if (count == 0)
- {
- //递归,展开一片区域
- DfsMine(mine, show, x, y, ROW, COL);
- }
- show[x][y] = count + '0';
- DisPlayBoard(show, ROW, COL);
- win++;
- }
- }
- else
- {
- printf("坐标非法,重新输入\n");
- }
- }
- if (win == row * col - ESAY_COUNT)
- {
- printf("恭喜你,排雷成功\n");
- }
- DisPlayBoard(mine, ROW, COL);
- }
-
- //递归,展开一片区域
- void DfsMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col)
- {
- int count= GetMineCount(mine, x, y);
- show[x][y] = count + '0';
- if (count != 0)
- {
- return;
- }
- int xx[] = { 1,1, 1 ,-1,-1,-1,0, 0 };
- int yy[] = { 1,0,-1, 0, 1, -1 ,1,-1};
- int i = 0;
- for (i = 0; i < 8; i++)
- {
- int dx = x + xx[i];
- int dy = y + yy[i];
- if (dx >= 1 && dx <= row && dy >= 1 && dy <= col && mine[dx][dy]=='0' && show[dx][dy] == '*')
- {
- DfsMine(mine, show, dx, dy, ROW, COL);
- }
- }
- }
-
- //统计雷
- int GetMineCount(char mine[ROWS][COLS], int x, int y)
- {
- return (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] + mine[x - 1][y] - 8 * '0');
- }
test.c文件
- #include"game.h"
-
- void menu()
- {
- printf("********************\n");
- printf("***** 1.play *****\n");
- printf("***** 2.exit *****\n");
- printf("********************\n");
- }
-
- void game()
- {
- char mine[ROWS][COLS];//存放的雷
- char show[ROWS][COLS];//存放盘查出雷的信息
- //初始化棋盘
- //mine数组最开始全是‘0’
- //show数组最开始全是‘*’
- InitBoard(mine, ROWS, COLS, '0');
- InitBoard(show, ROWS, COLS, '*');
- DisPlayBoard(show, ROW, COL);
-
- //布置雷
- SetMine(mine, 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 2:
- system("cls");
- printf("退出游戏\n");
- break;
- default:
- printf("选择错误,重新选择\n");
- break;
- }
- } while (input);
-
- }
游戏效果展示:(为了方便截图,已注释了清屏的代码)
哈哈哈,这次游戏疏忽大意导致游戏失败了~
总结:今天我们学习了扫雷小游戏,再次使用多文件编程的形式,同时也体验了玩自己所写的游戏,如果我写的有什么的不好之处,请在文章下方给出你宝贵的意见。如果觉得我写的好的话请点个赞赞和关注哦~
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/748843
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。