#include "game.h"
#define _CRT_SECURE_NO_WARNINGS 1
void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
arr[i][j] = set;
}
}
}
void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
printf("******** 扫雷 *******\n");
for (i = 0; i <= row; i++)
{
printf("%-3d", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d", i);
int j = 0;
for (j = 1; j <= col; j++)
{
printf("%3c", arr[i][j]);
}
printf("\n");
}
printf("******** 扫雷 *******\n");
}
void SetMine(char arr[ROWS][COLS], int row, int col)
{
int count = EASY_CONUNT;
while (count)
{
int x = rand() % row + 1; //0-9随机
int y = rand() % col + 1; //0-9随机
if (arr[x][y] == '0') //判断该处是否为雷
{
arr[x][y] = '1';
count--;
}
}
}
static int GetMineCount(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 FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_CONUNT)
{
printf("需要排查的坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("你被炸死了,Game Over!!!!!\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int n = GetMineCount(mine,x,y);
show[x][y] = n + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("输入坐标不合法,Game Over!!!!!\n");
}
if (win == row * col - EASY_CONUNT)
{
printf("You ara Win !!!!!\n");
DisplayBoard(mine, ROW, COL);
}
}
}