用数组初始化11*11的棋盘
打印9*9的棋盘
下雷
排查雷
//简单的扫雷程序
#include"game.h"
void menu()
{
printf("******************\n");
printf("******1.paly******\n");
printf("******2.exit******\n");
printf("******************\n");
}
//①初始化出两个11×11的棋盘(对比9×9能防止越界),棋盘1为0,棋盘2为*。
void INITBOARD(char arr[ROWS][COLS], int rows, int cols,char word)//初始化的棋盘两个棋盘
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)//************************************
{
for (j = 0; j < cols; j++)//**************************
{
arr[i][j] = word;//这里用word参数可以解决初始化棋盘写连个函数的问题,更简约了。
}
}
}
//②生成9×9的*棋盘
void DSIPLAYBOARD(char arr[ROWS][COLS],int row,int col)
{
int i = 0;
int j = 0;
for (j = 0; j <= col; j++)//打印列标
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)//打印行标
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);//打印9×9的*标
}
printf("\n");
}
}
//③布置雷
void SET_star(char star[ROWS][COLS], int row, int col)
{
int count = setboom;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (star[x][y] == '0')
{
star[x][y] = '1';
count--;
}
}
}
int seekboom(char zero[ROWS][COLS], int x, int y)
{
return (zero[x - 1][y - 1] + zero[x - 1][y] + zero[x - 1][y + 1] + zero[x][y - 1] + zero[x][y + 1]+ zero[x + 1][y - 1] + zero[x + 1][y] + zero[x + 1][y + 1] - 8 * '0');
}
//④开始扫雷 找雷
void FINDBOOM(char zero[ROWS][COLS], char star[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - setboom )
{
printf("请输入要排查的位置");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (zero[x][y] == '1')
{
printf("game over\n");
DSIPLAYBOARD(zero, ROW, COL);//打印带数字的炸弹的棋盘
break;
}
else
{
printf("good! next one\n");
int count = seekboom(zero, x, y);
star[x][y] = count+ '0';
DSIPLAYBOARD(star, ROW, COL);//
DSIPLAYBOARD(zero, ROW, COL);
win++;
}
}
else
{
printf("您输入的坐标有误,请重新输入\n");
}
}
}