扫雷是我们非常熟悉的游戏,可以说是家喻户晓。今天我们用c语言的知识复现一下这个游戏
首先我们新建三个项目
它们三个的用途分别是 函数测试 主函数 还有头文件声明
首先我们先打印一个菜单
int main() {
int input=0;
rand((unsigned)time(NULL));
do
{
meau();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出\n");
break;
default:
printf("选择错误,重新选择");
break;
}
} while (input);
return 0;
}
注意不要把do while循环放在scanf下面不然会死循环。
void meau() {
printf("*********************\n");
printf("******1. play ******\n");
printf("******0. ESC ******\n");
printf("*********************\n");
printf("*********************\n");
}
菜单函数
这样我们基本的逻辑就已经完成
第二步我们打印棋盘
完成game函数
void game() {
char mine[ROWS][LOSS];
char show[ROWS][LOSS];
Intborad(mine, ROWS, LOSS,'0');
Intborad(show, ROWS, LOSS,'*');
#define ROW 9
#define LOS 9
#define LOSS LOS+2
#define ROWS ROW+2
先初始化棋盘
为什么有mine show两个棋盘呢
因为我们要一个拿来布置雷一个拿来给玩游戏的人看 (看不到雷)
ROW LOSS初始化棋牌 为了防止越界访问我们设置11 11 的棋盘 实际为9×9
disborad(show, ROW, LOS);
disborad(mine, ROW, LOS);
Intborad(char borad[ROWS][LOSS], int row, int los,char set) {
int i;
for (i = 0; i < row; i++) {
int j ;
for (j = 0; j < los; j++) {
borad[i][j] = set;
}
}
}
disborad(char borad[ROWS][LOSS], int row, int los) {
int i;
printf("-------扫雷—————\n");
for (i = 0; i <=los; i++) {
printf("%d ", i);
}
printf("\n");
for (int i=1; i <=row; i++) {
printf("%d ", i);
int j;
for (int j=1; j <=los; j++) {
printf("%c ", borad[i][j]);
}
printf("\n");
}
}
接下来我们打印这个棋盘 为了方便寻找我们在棋盘上打印数字方便输入坐标
接下来我们会得到
接下来我们来布置雷
为了让雷随机布置我们用rand函数
rand((unsigned)time(NULL)); 时间戳空指针 强制类型转换无符号整数
setmine(mine, ROW, LOS);雷只要布置在mine棋盘上
setmine(char borad[ROWS][LOSS], int row, int los) {
int count = LEI;
while (count)
{
int x = rand() % row + 1;
int y = rand() % los + 1;
if (borad[x][y] == '0') {
borad[x][y] = '1';
count--;
}
}
}
%几就可以得到0到几的数据 所以这里是 0到9 所以+1
10个雷布置一个减一个
接下来我们来查找雷
int Findmine(char mine[ROWS][LOSS], 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';
}
getmine(char show[ROWS][LOSS], char mine[ROWS][LOSS], int row, int los)
{
int win = 0;
int x=0;
int y=0;
while (win<row*los-LEI)
{
printf("请输入你要排查的坐标");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= los) {
if (mine[x][y] == '1') {
printf("你被炸死了");
disborad(mine,ROW,LOS);
break;
}
else {
int count = Findmine(mine, x, y);
show[x][y] = count + '0';
disborad(show, ROW, LOS);
win++;
}
}
else
{
printf("输入错误");
}
}
if (win == row * los - LEI) {
printf("排雷成功");
disborad(mine, ROW, LOS);
}
}
我们要判断两个 函数所以形参 要两个数组
count +“字符0”
因为阿斯特码值可以转译为数字
findmine函数是查找周围的雷 九宫阁上其他八个的位置 减去字符数得到数字
接下来我们设置一下能展开一片的函数 这里用到了递归
int win = 0;
void arr(char mine[ROWS][LOSS], char show[ROWS][LOSS], int x, int y)
{
if (show[x][y] == '*' && x >= 1 && x <= ROW && y >= 1 && y <= ROW)
{
int count = Findmine(mine, x, y);
show[x][y] = count + '0';
win++;
if (count == 0)
{
arr(mine, show, x - 1, y);
arr(mine, show, x - 1, y - 1);
arr(mine, show, x - 1, y + 1);
arr(mine, show, x, y - 1);
arr(mine, show, x, y + 1);
arr(mine, show, x + 1, y - 1);
arr(mine, show, x + 1, y + 1);
arr(mine, show, x + 1, y);
}
}
}
源代码
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
Intborad(char borad[ROWS][LOSS], int row, int los,char set) {
int i;
for (i = 0; i < row; i++) {
int j ;
for (j = 0; j < los; j++) {
borad[i][j] = set;
}
}
}
disborad(char borad[ROWS][LOSS], int row, int los) {
int i;
printf("-------扫雷—————\n");
for (i = 0; i <=los; i++) {
printf("%d ", i);
}
printf("\n");
for (int i=1; i <=row; i++) {
printf("%d ", i);
int j;
for (int j=1; j <=los; j++) {
printf("%c ", borad[i][j]);
}
printf("\n");
}
}
setmine(char borad[ROWS][LOSS], int row, int los) {
int count = LEI;
while (count)
{
int x = rand() % row + 1;
int y = rand() % los + 1;
if (borad[x][y] == '0') {
borad[x][y] = '1';
count--;
}
}
}
int Findmine(char mine[ROWS][LOSS], 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';
}
int win = 0;
void arr(char mine[ROWS][LOSS], char show[ROWS][LOSS], int x, int y)
{
if (show[x][y] == '*' && x >= 1 && x <= ROW && y >= 1 && y <= ROW)
{
int count = Findmine(mine, x, y);
show[x][y] = count + '0';
win++;
if (count == 0)
{
arr(mine, show, x - 1, y);
arr(mine, show, x - 1, y - 1);
arr(mine, show, x - 1, y + 1);
arr(mine, show, x, y - 1);
arr(mine, show, x, y + 1);
arr(mine, show, x + 1, y - 1);
arr(mine, show, x + 1, y + 1);
arr(mine, show, x + 1, y);
}
}
}
getmine(char show[ROWS][LOSS], char mine[ROWS][LOSS], int row, int los)
{
int win = 0;
int x=0;
int y=0;
while (win<row*los-LEI)
{
printf("请输入你要排查的坐标");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= los) {
if (mine[x][y] == '1') {
printf("你被炸死了");
disborad(mine,ROW,LOS);
break;
}
else {
int count = Findmine(mine, x, y);
arr(mine, show, x, y);
show[x][y] = count + '0';
disborad(show, ROW, LOS);
win++;
}
}
else
{
printf("输入错误");
}
}
if (win == row * los - LEI) {
printf("排雷成功");
disborad(mine, ROW, LOS);
}
}
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void meau() {
printf("*********************\n");
printf("******1. play ******\n");
printf("******0. ESC ******\n");
printf("*********************\n");
printf("*********************\n");
}
void game() {
char mine[ROWS][LOSS];
char show[ROWS][LOSS];
Intborad(mine, ROWS, LOSS,'0');
Intborad(show, ROWS, LOSS,'*');
disborad(show, ROW, LOS);
//disborad(mine, ROW, LOS);
setmine(mine, ROW, LOS);
//disborad(mine, ROW, LOS);
getmine(show, mine, ROW,LOS);
}
int main() {
int input=0;
rand((unsigned)time(NULL));
do
{
meau();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出\n");
break;
default:
printf("选择错误,重新选择");
break;
}
} while (input);
return 0;
}
#pragma once
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 9
#define LOS 9
#define LOSS LOS+2
#define ROWS ROW+2
#define LEI 10
Intborad(char borad[ROWS][LOSS],int row,int los,char set);
disborad(char borad[ROWS][LOSS], int row, int los);
setmine(char borad[ROWS][LOSS], int row, int los);
getmine(char show[ROWS][LOSS], char mine[ROWS][LOSS], int row, int los);