我的第一篇博客(重点说明我实现的排雷时展开一片的效果)
1.开始游戏:打印一个菜单提示用户输入(函数仅仅是打包了代码没有返回值)
void Menu()
{
printf("******************\n");
printf("****1:游玩扫雷****\n");
printf("****0:退出扫雷****\n");
printf("******************\n");
}
定义一个整型变量来存放用户的选择。(代码略)
在用户选择开始游戏时运行游戏(整个游戏流程建议打包成函数)扫雷游戏需要 两个数组 来存放 雷的位置(JiSuan 数组) 和 显示给用户看的雷区(XianShi 数组) (俩个数组坐标一一对应)(11*11的二维数组中 打印雷区,排查,布置雷,只用到内部的9*9的空间,周围的一圈是用来防止排查雷时越界访问数组外的东西。)
#define BianChang 9
#define BianChang_s 11
void game()
{
char XianShi[BianChang_s][BianChang_s], JiSuan[BianChang_s][BianChang_s];
ChuShiHua(JiSuan, BianChang_s, '0');
ChuShiHua(XianShi, BianChang_s, '*');
DaYin(XianShi, BianChang);
BuZhiLei(JiSuan, BianChang);
PaiChaLei(JiSuan, XianShi, BianChang);
}
在排查雷函数中当玩家未踩雷时,传递坐标并计算周围雷的个数。把这个数字存放在 XianShi 数组相应的坐标处用于向玩家展示。但是!当周围雷的个数为零时,程序应该自动排查周围的格子,并再次填入雷的个数,直到所有为零的格子周围全部被自动排查。
所以我们定义一个函数 ZhanKaiYiPian 嵌套在 PaiChaLei 函数中(当玩家输入的排雷坐标未踩雷时先改变 * 为周围雷的个数,之后执行ZhanKaiYiPian 函数)
ZhanKaiYiPian 函数需要操作两个数组并传入一个坐标来进行自动排查。(bianchang变量保证函数随着雷区大小的改变依旧有效)//所传参数的作用
进行一个bianchang*bianchang次的循环。
循环中如果传入坐标处XianShi 数组值为 '0' 或'o'(之后说明)时,用两个循环遍历传入坐标周围的坐标(用if+continue排除越界坐标)并改变每个坐标处XianShi 数组的值为: 对应坐标处周围雷的个数(调用自定义函数根据 雷的位置:(JiSuan 数组)来计算)。
再次用两个循环遍历整个XianShi 数组每次遇到'0'就把它标记为(已查看)'o',并且将传入坐标改为它。
然后循环回去。//保证把每一个'0'作为传入坐标探索。
结束bianchang*bianchang次的循环。吧XianShi 数组中的每一个‘o’改成‘0’。
void ZhanKaiYiPian(char arr[BianChang_s][BianChang_s],
char XianShi[BianChang_s][BianChang_s],
int bianchang,
int a,int b)
{
int i = 0, j = 0;
for (int hehe = 1; hehe <= bianchang * bianchang; hehe++)
{
if (XianShi[a][b] == '0'|| XianShi[a][b] == 'o')
{
for (i = a - 1; i <= a + 1; i++)
{
if (i == 0 || i == BianChang_s - 1 )
{
continue;
}
for (j = b - 1; j <= b + 1; j++)
{
if ( j == 0 || j == BianChang_s - 1)
{
continue;
}
if (XianShi[i][j] == '*')//用于保证排雷数真实可靠
{
XianShi[i][j] = JiSuanZhouWeiLeiDeGeShu(arr, i, j);
PaiLeiShu++;//用于判断是否排完所有雷,作用在PaiChaLei函数中
}
}
}
}
for (int s = 1; s <= bianchang; s++)
{
for (int d = 1; d <= bianchang; d++)
{
if (XianShi[s][d] == '0')
{
a = s;
b = d;
XianShi[s][d] = 'o';
goto biaoqian;
}
}
}
biaoqian:;//break两层
}
for (int s = 1; s <= bianchang; s++)
{
for (int d = 1; d <= bianchang; d++)
{
if (XianShi[s][d] == 'o')
{
XianShi[s][d] = '0';
}
}
}
}
谢谢观看,恳请大家为我提出博客书写 或是其他方面的改进意见!
下面是所有的代码
#pragma once
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#define BianChang 9
#define BianChang_s 11
#define THE_NUMBER_OF_THE_THUNDER 6
void ChuShiHua(char arr[BianChang_s][BianChang_s], int bianchang_s, char a);
void DaYin(char arr[BianChang_s][BianChang_s], int bianchang);
void BuZhiLei(char arr[BianChang_s][BianChang_s],int bianchang);
void PaiChaLei(char arr[BianChang_s][BianChang_s], char XianShi[BianChang_s][BianChang_s], int bianchang);
int JiSuanZhouWeiLeiDeGeShu(char arr[BianChang_s][BianChang_s],int a,int b);
void TongBuLeiDeFenBu(char arr[BianChang_s][BianChang_s], char XianShi[BianChang_s][BianChang_s], int bianchang);
void ZhanKaiYiPian(char arr[BianChang_s][BianChang_s], char XianShi[BianChang_s][BianChang_s], int bianchang, int a, int b);
int PaiLeiShu;
#define _CRT_SECURE_NO_WARNINGS 1
#include "TWJ.h"
void Menu()
{
printf("******************\n");
printf("****1:游玩扫雷****\n");
printf("****0:退出扫雷****\n");
printf("******************\n");
}
void game()
{
char XianShi[BianChang_s][BianChang_s], JiSuan[BianChang_s][BianChang_s];
ChuShiHua(JiSuan, BianChang_s, '0');
ChuShiHua(XianShi, BianChang_s, '*');
DaYin(XianShi, BianChang);
BuZhiLei(JiSuan, BianChang);
PaiChaLei(JiSuan, XianShi, BianChang);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
input = 0;
Menu();
scanf("%d", &input);
switch (input)
{
case 1:
{
system("cls");
game();
break;
}
case 0:
{
printf("退出游戏");
break;
}
default:
printf("输入错误");
break;
}
} while (input);
}
#define _CRT_SECURE_NO_WARNINGS 1
#include "TWJ.h"
void ChuShiHua(char arr[BianChang_s][BianChang_s], int bianchang_s, char a)
{
for ( int i=0; i< bianchang_s; i++)
{
for (int j = 0; j < BianChang_s; j++)
{
arr[i][j] = a;
}
}
}
void DaYin(char arr[BianChang_s][BianChang_s], int bianchang)
{
int g=0;
int xs = THE_NUMBER_OF_THE_THUNDER;
printf("总共%d个雷", xs);
printf("\n*扫雷*\n");
for (int s= bianchang; s >= 0;s--)
{
printf("%d ",g);
g++;
}
printf("\n");
for (int i = 1; i <= bianchang; i++)
{
printf("%d ",i);
for (int j = 1; j <= bianchang; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
printf("*扫雷*\n\n");
}
void BuZhiLei(char arr[BianChang_s][BianChang_s], int bianchang)
{
for (int i = 1; i <= THE_NUMBER_OF_THE_THUNDER; i++)
{
int a=0, b=0;
a = (rand() % bianchang) + 1;
b = (rand() % bianchang) + 1;
if (arr[a][b]== '0')
{
arr[a][b] = '1';
}
else
{
i--;
}
}
}
void PaiChaLei(char arr[BianChang_s][BianChang_s],char XianShi[BianChang_s][BianChang_s],int bianchang)
{
int a = 0, b = 0,i=0;
for (PaiLeiShu=0; PaiLeiShu<( BianChang* BianChang- THE_NUMBER_OF_THE_THUNDER); )
{
i = 0;
do
{
i++;
if (i > 1)
{
printf("输入错误\n");
DaYin(XianShi, BianChang);
}
if (i > 3)
{
printf("输入错误\n");
goto biaoji;//对抗未知错误【输入`(或许也可是其他什么东西)】造成的死循环
}
printf("请输入要排查哪一行的雷:");
scanf("%d", &a);
printf("请输入要排查哪一列的雷:");
scanf("%d", &b);
system("cls");
} while (a<1 || a>bianchang || b<1 || b>bianchang);
if (arr[a][b] == '1')
{
printf("boom的爆炸声!\n");
Sleep(1000);
system("cls");
printf("什么嘛,只是玩具摔炮。\n");
printf("游戏结束!雷都藏在这呢(@表示雷)\n");
TongBuLeiDeFenBu(arr, XianShi, bianchang);
DaYin(XianShi, BianChang);
break;
}
else
{
XianShi[a][b] = JiSuanZhouWeiLeiDeGeShu(arr, a, b);
ZhanKaiYiPian(arr, XianShi, bianchang, a, b);//展开一片!!!
DaYin(XianShi, BianChang);
PaiLeiShu++;
}
}
if (PaiLeiShu >= (BianChang * BianChang - THE_NUMBER_OF_THE_THUNDER))
{
printf("排雷成功!\n\n");
}
biaoji:;//对抗未知错误造成的死循环
}
int JiSuanZhouWeiLeiDeGeShu(char arr[BianChang_s][BianChang_s], int a, int b)
{
return arr[a - 1][b - 1] + arr[a - 1][b] + arr[a - 1][b + 1] +
arr[a][b - 1] + arr[a][b] + arr[a][b + 1] +
arr[a + 1][b - 1] + arr[a + 1][b] + arr[a + 1][b + 1]-'0'*8;
}
void TongBuLeiDeFenBu(char arr[BianChang_s][BianChang_s], char XianShi[BianChang_s][BianChang_s], int bianchang)
{
for (int i = 1; i <= bianchang; i++)
{
for (int j = 1; j <= bianchang; j++)
{
if (arr[i][j] == '1')
{
XianShi[i][j] = '@';
}
else
{
XianShi[i][j] = '*';
}
}
}
}
void ZhanKaiYiPian(char arr[BianChang_s][BianChang_s], char XianShi[BianChang_s][BianChang_s], int bianchang, int a,int b)
{
int i = 0, j = 0;
for (int hehe = 1; hehe <= bianchang * bianchang; hehe++)
{
if (XianShi[a][b] == '0'|| XianShi[a][b] == 'o')
{
for (i = a - 1; i <= a + 1; i++)
{
if (i == 0 || i == BianChang_s - 1 )
{
continue;
}
for (j = b - 1; j <= b + 1; j++)
{
if ( j == 0 || j == BianChang_s - 1)
{
continue;
}
if (XianShi[i][j] == '*')//用于保证排雷数真实可靠
{
XianShi[i][j] = JiSuanZhouWeiLeiDeGeShu(arr, i, j);
PaiLeiShu++;//用于判断是否排完所有雷,作用在PaiChaLei函数中
}
}
}
}
for (int s = 1; s <= bianchang; s++)
{
for (int d = 1; d <= bianchang; d++)
{
if (XianShi[s][d] == '0')
{
a = s;
b = d;
XianShi[s][d] = 'o';
goto biaoqian;
}
}
}
biaoqian:;//break两层
}
for (int s = 1; s <= bianchang; s++)
{
for (int d = 1; d <= bianchang; d++)
{
if (XianShi[s][d] == 'o')
{
XianShi[s][d] = '0';
}
}
}
}