#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = ret;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("\n-------扫雷-------\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("\n");
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
//布置10个雷
int count = EASY;
while (count)
{
//生成随机下标
int x = rand() % row + 1;
int y = rand() % col + 1;;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
//static 作用:修饰全局变量,修饰局部变量, 修饰函数
static int get_mine_count(char mine[ROWS][COLS], 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';
}
//void ZhangKai(char mine[ROWS][COLS], char show[ROW][COL], int x, int y)
//{
// int i = 0;
// int j = 0;
// for (i = -1; i <= 1; i++)
// {
// for (j = -1; j <= -1; j++)
// {
// if (i == 0 && j == 0)
// {
// continue;
// }
// else
// {
// if (mine[x + i][y + j] == '0')
// {
// get_mine_count(mine, x, y);
// ZhangKai(mine, show, x, y);
// }
// else
// {
// get_mine_count(mine, x, y);
// }
// }
// }
// }
//}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
//1、输入排查的坐标
//2、检查坐标处是不是雷
//如果是雷,游戏结束
//不是雷,统计坐标周围有几个雷 - 存储排查雷的信息到show数组 游戏继续
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY)
{
printf("请输入要排查的坐标:> ");
scanf("%d %d", &x, &y);//x_1-9 y_1-9
//判断坐标的合法性
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("游戏结束\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
//不是雷的情况下,统计x,y坐标周围有几个雷
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
/* if (show[x][y] == '0')
{
ZhangKai(mine, show, x, y);
}*/
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("坐标不合法,请重新输入\n");
}
}
if (win == row * col - EASY)
{
printf("\n恭喜你,游戏胜利!\n");
DisplayBoard(mine, ROW, COL);
}
}
有一段是想写展开的,稍微构思了一下,但是函数不可行,此问题便留到下次解决