扫雷游戏(C语言)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <windows.h>
#define xx 51
#define yy 51
int a[xx][yy];
char show[xx][yy];
int boom[xx][yy];
int hang,lie;
double begin, finish;
int oo;
void menu(void);
void set(int boom[xx][yy]);
void display(char show[xx][yy]);
int sweep(int boom[xx][yy], char show[xx][yy]);
int num(int boom[xx][yy], char show[xx][yy], int x, int y);
void nandu();
//主函数如下
int main()
{
int m, n;
kaishi:
for (m = 0; m<xx; m++)
{
for (n = 0; n<yy; n++)
{
show[m][n] = ‘\0’;
boom[m][n] = 0;
a[m][n] = 0;//为数组进行初始化
}
}
menu();
int f;
scanf("%d", &f);
if (f == 1)
{
system(“cls”);
nandu();
for (m = 0, n = 0; n<lie; n++)//设置边界标识
a[m][n] = 1;
for (m = (hang-1), n = 0; n<lie; n++)//设置边界标识
a[m][n] = 1;
for (m = 0, n = 0; m<hang; m++)//设置边界标识
a[m][n] = 1;
for (m = 0, n = (lie-1); m<hang; m++)//设置边界标识
a[m][n] = 1;
system(“cls”);
set(boom);
display(show);
sweep(boom, show);
printf(“按任意键返回主菜单\n”);
system(“pause”);
system(“cls”);
goto kaishi;
}
return 0;
}
void menu()
{
printf(“\n");
printf(" 小韩专属 \n");
printf(" 主菜单 \n");
printf(" 1.进入游戏 \n");
printf(" 2.退出游戏 \n");
printf("\n”);
printf("(输入对应序号进入)\n");
}
//选择难度
void nandu()//难度调节
{tou:
//color(7);
printf(“1.简单模式\n”);
//color(9);
printf(“2.普通模式\n”);
//color(4);
printf(“3.困难模式\n”);
//color(5);
printf(“4.噩梦模式\n”);
//color(7);
printf(“5.自定义(行列小于50)\n”);
int mm;
scanf("%d", &mm);
switch (mm)
{
case 1:oo = 10; hang = 11; lie = 11; break;
case 2:oo = 40; hang = 18; lie = 18; break;
case 3:oo = 99; hang = 23; lie = 23; break;
case 4:oo = 120; hang = 23; lie = 23; break;
case 5: printf(“行 列 雷\n”);
scanf("%d %d %d",&hang,&lie,&oo);
hang=hang+2;
lie=lie+2;
break;
default:{printf(“错误,请重新选择”);
goto tou; system("cls"); } }
}
void set(int boom[xx][yy]) //设雷
{
int count = oo;
int x = 0;
int y = 0;
srand((unsigned)time(NULL));
while (count)
{
x = rand() % (hang-3) + 1;
y = rand() % (lie-3) + 1;
if (boom[x][y] == 0)
{
boom[x][y] = 1;
count–;
}
}
}
void display(char show[xx][yy])
{
int i = 0;
int j = 0;
printf(" “);
for (i = 1; i<hang - 1; i++)
{
printf(” %2d “, i);
}
printf(”\n");
for (i = 1; i < hang - 1; i++)
{
printf(" “);
for (j = 1; j<lie - 1; j++)
{
printf(”|—");
}
printf("|\n");
printf("%2d", i);
for (j = 1; j < lie - 1; j++)
{
printf("| %c “, show[i][j]);
}
printf(”|\n");
}
printf(" “);
for (j = 1; j < lie - 1; j++)
{
printf(”|—", show[i][j]);
}
printf("|\n");
}
int sweep(int boom[xx][yy], char show[xx][yy])
{
int num(int boom[xx][yy], char show[xx][yy], int x, int y);
int x, y, count = 0, l, k, win = 0;
begin = clock();
while (1)
{
start:
win = 0;
for (l = 1; l<hang - 1; l++)
{
for (k = 1; k<lie - 1; k++)
if (show[l][k] >= 48 && show[l][k]<57)//胜利条件检测
win++;
}
if (win == ((hang - 2)(lie - 2) - oo))//胜利条件判断
break;
printf(“请输入坐标,输入坐标的相反数可以标记为雷区\n”);
scanf("%d %d", &x, &y);
if (x>(hang-2) || y>(lie-2)||x<-(hang-2)||y<-(lie-2)||!((x>0&&y>0)||(x<0&&y<0)))
{
system(“cls”);
display(show);
printf(“输入错误,请重新输入\n”);
goto start;
}
if (x<0)
{
if (show[-x][-y] == ‘\0’)
show[-x][-y] = ‘!’;
else
if(show[-x][-y] == ‘!’)
show[-x][-y] = ‘\0’;
else {goto start;
}
system(“cls”);
display(show);
continue;
}
if (count == 0)//防止第一次踩雷
{
if (boom[x][y] == 1)//count在这里是计步作用 记录操作次数
{
boom[x][y] = 0;
l = rand() % (hang-3)+ 1;
k = rand() % (lie-3) + 1;
if (boom[l][k] == 0)
{
boom[l][k] = 1;
}
}
}
if (boom[x][y] == 1){
for (l = 1; l<hang - 1; l++){
for (k = 1; k<lie - 1; k++)
if (boom[l][k] == 1)
show[l][k] = '’;//失败时雷区显示
}
system(“cls”);
display(show);
finish = clock();
printf(“你踩到地雷了!\n”);
printf(“游戏结束,用时%d秒\n”, (int)(finish - begin) / 1000);
return 0;//游戏失败
}
else
{
num(boom, show, x, y);
system(“cls”);
display(show);
count++;
}
}
finish = clock();
printf(“你赢了!!\n”);
printf(“游戏结束,用时%d秒\n”, (int)(finish - begin) / 1000);
printf(“输入任意数字返回主菜单\n”);
return 0;
}
int num(int boom[xx][yy], char show[xx][yy], int x, int y)//查看一点周围雷的个数
{
int nnn = 0, p, q;
if (a[x][y] == 0)
{
if (boom[x - 1][y - 1] == 1)
nnn++;
if (boom[x - 1][y] == 1)
nnn++;
if (boom[x - 1][y + 1] == 1)
nnn++;
if (boom[x][y - 1] == 1)
nnn++;
if (boom[x][y + 1] == 1)
nnn++;
if (boom[x + 1][y - 1] == 1)
nnn++;
if (boom[x + 1][y] == 1)
nnn++;
if (boom[x + 1][y + 1] == 1)
nnn++;
show[x][y] = nnn + ‘0’;
if (nnn == 0){//连锁反应递归
a[x][y] = 1;
for (p = x - 1; p<x + 2; p++)
{
for (q = y - 1; q<y + 2; q++)
num(boom, show, p, q);
}
}
}
else return 0;
}