简单易懂的扫雷c代码

1,头文件函数的声明
MS.h

#ifndef GAME_H
#define GAME_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define row 12
#define col 12
#define COUNT 10 //棋盘中的总雷数
extern char show_mine[row][col];//展示数组
extern char real_mine[row][col];//布雷数组
void muen();//菜单函数
void init_mine();//初始化数组函数
void set_mine();//布雷函数
int count_mine();//统计周围雷的个数
void print_player();//打印玩家棋盘
void print_mine();//打印设计者棋盘
int sweep_mine();//扫雷函数
void open_mine(int x, int y);//展开函数
int count_show_mine();//判断玩家棋盘剩余未知区域的个数
#endif //GAME_H_

2,主函数
MS.c

#define _CRT_SECURE_NO_WARNINGS
#include"MS.h"
void game()
{
int ret = 0;
init_mine();//初始化玩家棋盘和设计者棋盘
set_mine();//给设计者棋盘布雷
print_player();//打印玩家棋盘
if (count_show_mine() == COUNT)//一步就赢的情况
{
print_mine();
printf(" 玩家赢!\n\n");
return ;
}
while (1)//循环扫雷
{
int ret = sweep_mine();//扫雷,踩到雷返回1,没有踩到雷返回2;
if (count_show_mine() == COUNT)//若玩家棋盘的‘*’个数为雷数时,游戏胜利
{
print_mine();//打印设计者棋盘
printf(“玩家赢\n\n”);
break;
}
if (ret)//判断是否踩到雷
{
printf(“被雷炸死\n\n”);
print_mine();//打印设计者雷阵查看雷阵的分布
break;
}
print_player();//打印玩家棋盘
}
}
int main()
{
srand((unsigned int)time(NULL));//产生随机数生成器
int input = 0;
muen();//菜单
do
{
scanf("%d", &input);
switch (input)
{
case 1:game();
break;
case(0) : exit(1);
break;
default:
printf(“输入错误,重新输入\n”);
break;
}
muen();
printf(“contiue?\n”);
} while (1);
system(“pause”);
return 0;
}

3;函数体
twst.c

#define _CRT_SECURE_NO_WARNINGS
#include"MS.h"
char show_mine[row][col] = { 0 };
char real_mine[row][col] = { 0 };
void muen()
{
printf(“\n ");
printf("1.play 0.exit
\n");
printf("
** \n”);
}
void init_mine()//初始化两个棋盘
{
int i = 0;
int j = 0;
for (int i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
show_mine[i][j] = ‘’;
real_mine[i][j] = ‘0’;
}
}
printf("\n");
}
void print_player()//打印玩家棋盘
{
int i = 0;
int j = 0;
printf(“0 “);
for (i = 1; i <row - 1; i++)
{
printf(”%3d “, i);//打印横标(0–10)
}
printf(”\n”);
for (i = 1; i <row - 1; i++)//打印竖标(1–10)
{
printf("%2d", i);
for (j = 1; j < col - 1; j++)
{
printf("%3c “, show_mine[i][j]);//玩家棋盘数组
}
printf(”\n");
}
}
void print_mine()//打印设计者棋盘
{
int i = 0;
int j = 0;
printf(“0 “);
for (i = 1; i <row - 1; i++)
{
printf(”%3d “, i);//打印横标(0–10)
}
printf(”\n”);
for (i = 1; i <row - 2; i++)//打印竖标(1–10)
{
printf("%2d “, i);
for (j = 1; j < col - 1; j++)
{
printf(”%3c “, real_mine[i][j]);
}
printf(”\n");
}
printf("\n");
}
void set_mine()//给设计者棋盘布雷
{
int x = 0;
int y = 0;
int count = COUNT;//雷总数
while (count)//雷布完后跳出循环
{
int x = rand() % 10 + 1;//产生1到10的随机数,在数组下标为1到10的范围内布雷
int y = rand() % 10 + 1;//产生1到10的随机数,在数组下标为1到10的范围内布雷
if (real_mine[x][y] == ‘0’)//找不是雷的地方布雷
{
real_mine[x][y] = ‘1’;
count–;
}
}
}
int count_mine(int x, int y)//检测周围8个区域雷的个数
{
int count = 0;
if (real_mine[x - 1][y - 1] == ‘1’)
count++;
if (real_mine[x - 1][y] == ‘1’)
count++;
if (real_mine[x - 1][y + 1] == ‘1’)
count++;
if (real_mine[x][y - 1] == ‘1’)
count++;
if (real_mine[x][y + 1] == ‘1’)
count++;
if (real_mine[x + 1][y - 1] == ‘1’)
count++;
if (real_mine[x + 1][y] == ‘1’)
count++;
if (real_mine[x + 1][y + 1] == ‘1’)
count++;
return count;
}
int sweep_mine()//扫雷函数,踩到雷返回1,没有踩到雷返回0
{
int x = 0;
int y = 0;
int count = 0;
printf(“输入坐标扫雷 “);
scanf_s(”%d%d”, &x, &y);//只能输入1到10
if ((x >= 1 && x <= 10) && (y >= 1 && y <= 10))//判断输入坐标是否有误,输入错误重新输入
{
if (real_mine[x][y] == ‘0’)//没踩到雷
{
char ch = count_mine(x, y);
show_mine[x][y] = ch + ‘0’;//数字对应的ASCII值和数字字符对应的ASCII值相差48,即’0’的ASCII值
open_mine(x, y);
if (count_show_mine() == COUNT)//判断剩余未知区域的个数,个数为雷数时玩家赢
{
print_mine();
printf("玩家赢! ");
return 0;
}
}
else if (real_mine[x][y] == ‘1’)//踩到雷
{
return 1;
}
}
else
{
printf("输入错误重新输入 ");
}
return 0;//没踩到雷
}
void open_mine(int x, int y)//坐标周围展开函数
{
if (real_mine[x - 1][y - 1] == ‘0’)
{
show_mine[x - 1][y - 1] = count_mine(x - 1, y - 1) + ‘0’;//显示该坐标周围雷数
}
if (real_mine[x - 1][y] == ‘0’)
{
show_mine[x - 1][y] = count_mine(x - 1, y) + ‘0’;//显示该坐标周围雷数
}
if (real_mine[x - 1][y + 1] == ‘0’)
{
show_mine[x - 1][y + 1] = count_mine(x - 1, y + 1) + ‘0’;//显示该坐标周围雷数
}
if (real_mine[x][y - 1] == ‘0’)
{
show_mine[x][y - 1] = count_mine(x, y - 1) + ‘0’;//显示该坐标周围雷数
}
if (real_mine[x][y + 1] == ‘0’)
{
show_mine[x][y + 1] = count_mine(x, y + 1) + ‘0’;//显示该坐标周围雷数
}
if (real_mine[x + 1][y - 1] == ‘0’)
{
show_mine[x + 1][y - 1] = count_mine(x + 1, y - 1) + ‘0’;//显示该坐标周围雷数
}
if (real_mine[x + 1][y] == ‘0’)
{
show_mine[x + 1][y] = count_mine(x + 1, y) + ‘0’;//显示该坐标周围雷数
}
if (real_mine[x + 1][y + 1] == ‘0’)
{
show_mine[x + 1][y + 1] = count_mine(x + 1, y + 1) + ‘0’;//显示该坐标周围雷数
}
}
int count_show_mine()//判断剩余未知区域的个数,个数为雷数时玩家赢
{
int count = 0;
int i = 0;
int j = 0;
for (i = 1; i <= row - 2; i++)
{
for (j = 1; j <= col - 2; j++)
{
if (show_mine[i][j] == '
’)
{
count++;
}
}
}
return count;
}

猜你喜欢

转载自blog.csdn.net/belongHWL/article/details/90262819