上面的这个就是扫雷,这应该也是一代人的记忆
不,别想了,我没玩过,我还小,我也是查过资料才知道这个咋玩的。 傲娇.jpg
上次我写了一个三子棋,没看到的朋友感兴趣可以去看下(等啥呢,赶紧去看!!!)
下面我给大家介绍扫雷,比较简陋不要介意,毕竟本人也知识浅薄,有疏漏的地方欢迎指正。
正题:
工具:VS2015
这个游戏也分为三个文件 头文件 : head.h 源文件: test.c body.c
一、头文件
head.h
#ifndef _GAMH_H_
#define _GAME_H_
#include<stdio.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#define COUNT 10
#define ROWS 12
#define COLS 12
extern char show[ROWS][COLS];//展示数组
extern char mine[ROWS][COLS];//布雷数组
void ResetBoard();//初始化扫雷地图
void SetMine();//布雷函数
int CountMine();//计算坐标周围地雷数量
void PrintPlayerBoard();//打印玩家地图
void PrintMine();//打印设计者地图
void FirstProtect();//第一次不被炸死
int SweepMine();//扫雷函数
int countnumber();//判断扫雷完成函数
void ExpandArea(int row,int col);//周围无雷区域展开
#endif//_GAME_H_
二、源文件
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"head.h"
void menu()
{
printf("******************************************\n");
printf("********* 1.start 0.quit **********\n");
printf("******************************************\n");
}
void game()
{
int ret = 0;
ResetBoard();
SetMine();
PrintMine();
printf("\n");
PrintPlayerBoard();
FirstProtect();
while (1)
{
int ret = SweepMine();
if (countnumber() == COUNT)
{
PrintMine();
printf("真遗憾电脑输了!\n\n");
break;
}
if (ret)
{
printf("恭喜你被雷炸死了!\n");
PrintMine();
break;
}
PrintPlayerBoard();
}
}
void test()
{
int input = 0;
do {
menu();
printf("请选择你要进行的操作:");
scanf("%d", &input);
switch (input)
{
case 1:
printf("\n游戏载入中......\n\n");
game();
break;
case 0:
printf("\n游戏已登出......\n\n");
break;
default:
printf("\n无效操作\n\n");
break;
}
} while (1);
}
int main()
{
test();
return 0;
}
body.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"head.h"
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
void ResetBoard()//初始化扫雷地图
{
int i = 0, j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
show[i][j] = '*';
mine[i][j] = '0';
}
}
}
void SetMine()//布雷函数
{
int x = 0, y = 0;
int count = COUNT;
while (count)
{ //生成随机坐标
x = rand() % 10 + 1;
y = rand() % 10 + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';//放置地雷
count--;
}
}
}
void PrintPlayerBoard()//打印玩家地图
{
int i = 0;
int j = 0;
printf("0 ");
for (i = 1; i <ROWS - 1; i++)
{
printf("%d ", i);//打印横标(0--10)
}
printf("\n");
for (i = 1; i <ROWS - 2; i++)//打印竖标(1--10)
{
printf("%d ", i);
for (j = 1; j < COLS - 1; j++)
{
printf("%c ", show[i][j]);//玩家棋盘数组
}
printf("\n");
}
printf("10 ");//开始打印最后一行
for (i = 1; i < ROWS - 1; i++)
{
printf("%c ", show[10][i]);
}
printf("\n");
}
void PrintMine()//打印设计者棋盘
{
int i = 0;
int j = 0;
printf("0 ");
for (i = 1; i <ROWS - 1; i++)
{
printf("%d ", i);//打印横标
}
printf("\n");
for (i = 1; i <ROWS - 2; i++)//打印竖标
{
printf("%d ", i);
for (j = 1; j < COLS - 1; j++)
{
printf("%c ", mine[i][j]);
}
printf("\n");
}
printf("10 ");//开始打印最后一行
for (i = 1; i < ROWS - 1; i++)
{
printf("%c ", mine[10][i]);
}
printf("\n");
}
void FirstProtect()
{
int x = 0;
int y = 0;
if (mine[x][y] == '1')
{
mine[x][y] = '0';//如果第一次踩到雷,调整此雷的位置
while (1)//重新安置此雷
{
x = rand() % 10 + 1;
y = rand() % 10 + 1;
if (mine[x][y] != '1')
{
mine[x][y] = '0';
break;
}
}
}
}
int CountMine(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 SweepMine()//玩家扫雷位置
{
int x = 0, y = 0;
int count = 0;
printf("请输入扫雷坐标(X,Y):X = ");
scanf("%d", &x);
printf("Y = ");
scanf("%d", &y);
if ((x >= 1 && x <= 10) && (y >= 1 && y <= 10))//判断输入坐标是否有误,输入错误重新输入
{
if (mine[x][y] == '0')//没踩到雷
{
char ch = CountMine( x, y);
show[x][y] = ch + '0';//ASCII值
ExpandArea(x,y);
if (countnumber() == COUNT)//判断输赢
{
PrintMine(mine);
printf("真遗憾电脑输了!\n\n");
return 0;
}
}
else if (mine[x][y] == '1')//踩到雷
{
return 1;
}
}
else
{
printf("输入错误重新输入\n");
}
return 0;//没踩到雷
}
int countnumber()//判断扫雷剩余数量
{
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;
}
void ExpandArea(int x,int y)//周围无雷区域展开
{
if (mine[x - 1][y - 1] == '0')
{
show[x - 1][y - 1] = CountMine( x - 1, y - 1) + '0';//显示该坐标周围雷数
}
if (mine[x - 1][y] == '0')
{
show[x - 1][y] = CountMine(x - 1, y) + '0';//显示该坐标周围雷数
}
if (mine[x - 1][y + 1] == '0')
{
show[x - 1][y + 1] = CountMine(x - 1, y + 1) + '0';//显示该坐标周围雷数
}
if (mine[x][y - 1] == '0')
{
show[x][y - 1] = CountMine(x, y - 1) + '0';//显示该坐标周围雷数
}
if (mine[x][y + 1] == '0')
{
show[x][y + 1] = CountMine(x, y + 1) + '0';//显示该坐标周围雷数
}
if (mine[x + 1][y - 1] == '0')
{
show[x + 1][y - 1] = CountMine(x + 1, y - 1) + '0';//显示该坐标周围雷数
}
if (mine[x + 1][y] == '0')
{
show[x + 1][y] = CountMine(x + 1, y) + '0';//显示该坐标周围雷数
}
if (mine[x + 1][y + 1] == '0')
{
show[x + 1][y + 1] = CountMine(x + 1, y + 1) + '0';//显示该坐标周围雷数
}
}
这些代码都比较简单,在很多地方我也给出了批注方便阅读。
自己敲代码进行实现的时候先把这个游戏的过程缕清,然后逐个函数进行实现记好了。