void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("----------------------------");
printf("n");
for (i = 0; i <= row; i++)
{
printf("%d ", i);//这里是为了标出列数,便于定位
}
printf("n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);//这里是在每行开头标出行数,便于定位
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("n");
}
printf("----------------------------");
printf("n");
}
3、造炸弹
这里咱们在头文件定义炸弹数,以后想玩多点炸弹,修改一个数就行,方便快捷
void SetBoard(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int num = EASY;
while (num)
{
x = rand() % ROW + 1;
y = rand() % COL + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
num--;
}
}
}
4、扫描函数
进入游戏,玩家只有选择了要检查的点才能继续,这里有三种情况,因为要有很多次选择,所以采用循环
(1)选中雷区,那么直接跳出循环,游戏结束
(2)没选中雷区,电脑会扫描周围的区域,把周围无雷的点展开,展开周围有雷的点
这里还要说一下mine数组为什么要用‘0'和‘1'来做标记,因为0和1这两个字符在ascII码表里是连续的,一会在电脑扫描周围时可以直接通过减法算出周围的雷数
void CheckBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int ret = 0;
int x = 0;
int y = 0;
int num = 0;
while (ret < ROW*COL - EASY)
{
printf("输入排查坐标n");
scanf("%d%d", &x, &y);
if (x > 0 && x <= row && y > 0 && y <= col)
{
if (mine[x][y] == '1')//选中雷区,游戏结束
{
printf("炸死n");
DisplayBoard(mine, row, col);//展示mine区域
break;//跳出循环
}
else//没踩中雷区
{
ZeroLine(mine, show, x, y);//展开周围的区域
DisplayBoard(show, row, col);
ret++;
}
}
else
{
printf("input wrongn");
}
}
if (ret == ROW * COL - EASY)
{
printf("胜利n");
}
}
void ZeroLine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int ret = 0;
ret = AroundNum(mine, x, y);//扫描函数,扫描该点周围雷数
if (x >= 0 && y >= 0 && x < ROWS && y < COLS)
{
if (ret == 0)
{
show[x][y] = ' ';//无雷则为空白
if (mine[x][y + 1] == '0' && show[x][y + 1] == '*')
{
ZeroLine(mine, show, x, y + 1);
}
if (mine[x][y - 1] == '0' && show[x][y - 1] == '*')
{
ZeroLine(mine, show, x, y - 1);
}
if (mine[x - 1][y] == '0' && show[x - 1][y] == '*')
{
ZeroLine(mine, show, x - 1, y);
}
if (mine[x + 1][y] == '0' && show[x + 1][y] == '*')
{
ZeroLine(mine, show, x + 1, y);
}
if (mine[x + 1][y + 1] == '0' && show[x + 1][y + 1] == '*')
{
ZeroLine(mine, show, x + 1, y + 1);
}
if (mine[x - 1][y - 1] == '0' && show[x - 1][y - 1] == '*')
{
ZeroLine(mine, show, x - 1, y - 1);
}
if (mine[x + 1][y - 1] == '0' && show[x + 1][y - 1] == '*')
{
ZeroLine(mine, show, x + 1, y - 1);
}
if (mine[x - 1][y + 1] == '0' && show[x - 1][y - 1] == '*')
{
ZeroLine(mine, show, x - 1, y + 1);
}
}
else
{
show[x][y] = ret + '0';
}
}
}
int AroundNum(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y - 1] + mine[x][y - 1] + mine[x - 1][y] +
mine[x + 1][y + 1] + mine[x][y + 1] + mine[x + 1][y] +
mine[x - 1][y + 1] + mine[x + 1][y - 1] - 8 * mine[x][y];
}










