#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#define EASY_GOAL 10 //设置雷的数量
#define ROW 9 //设置游戏的规模
#define COL 9 //设置游戏的规模
#define ROWS (ROW+2)
#define COLS (COL+2)
void menu();
void game();
void Initboard(char arr[ROWS][COLS], int row, int col, char set); //初始化二维数组
void set_mine(char arr[ROWS][COLS], int row, int col); //随机设置雷的位置
void Printboard(char arr[ROWS][COLS], int row, int col); //打印游戏界面
void Judge(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col); //判断游戏结果
int Count(char arr[ROWS][COLS], int x, int y); //判断所选点位周围累的数量
void Unfold(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y); //展开周围没有雷的坐标
void menu()
{
printf("=====================\n");
printf("===== 1.play =====\n");
printf("===== 0.exit =====\n");
printf("=====================\n");
}
void Initboard(char arr[ROWS][COLS], int row, int col, char set)
{
memset(&arr[0][0], set, row*col);
}
void set_mine(char arr[ROWS][COLS], int row, int col)
{
int x, y;
int count = EASY_GOAL;
while (count)
{
x = (rand() % (row - 2)) + 1;
y = (rand() % (col - 2)) + 1;
if (arr[x][y] != '1')
{
arr[x][y] = '1';
count--;
}
}
}
void Printboard(char arr[ROWS][COLS], int row, int col)
{
int i, j;
for (i = 0; i <= row - 2; i++)
{
printf("- ", i);
for (j = 1; j <= col - 2; j++)
{
if (i == 0)
printf("- ", j);
else
printf(", ", arr[i][j]);
}
printf("\n");
}
}
void Judge(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y;
int count = 0;
int count1 = 0;
while (count<ROW*COL - EASY_GOAL)
{
count1++;
printf("输入排查的坐标:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= ROW && y >= 1 && y <= ROW)
{
if ((count1 == 1) && (mine[x][y] == '1')) //判断第一次选择是否为雷,是则将雷移至别处
{
int j, k;
mine[x][y] = '0';
while (1)
{
j = (rand() % (row - 2)) + 1;
k = (rand() % (col - 2)) + 1;
if (mine[j][k] == '0')
{
mine[j][k] = '1';
break;
}
}
Unfold(mine, show, x, y);
Printboard(show, ROWS, COLS);
count++;
continue;
}
else if (mine[x][y] == '1')
{
Printboard(mine, ROWS, COLS);
printf("游戏失败!\n");
break;
}
else
{
Unfold(mine, show, x, y);
Printboard(show, ROWS, COLS);
count++;
}
}
else
printf("坐标非法!");
}
if (count == ROW*COL - EASY_GOAL)
{
printf("扫雷成功,666!\n");
}
}
int Count(char arr[ROWS][COLS], int x, int y)
{
return arr[x - 1][y - 1] +
arr[x - 1][y] +
arr[x - 1][y + 1] +
arr[x][y - 1] +
arr[x][y + 1] +
arr[x + 1][y - 1] +
arr[x + 1][y] +
arr[x + 1][y + 1] - 8 * '0';
}
void Unfold(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
if (x >= 1 && x <= ROW && y >= 1 && y <= ROW)
{
if (Count(mine, x, y) != 0)
{
show[x][y] = Count(mine, x, y) + '0';
}
if (Count(mine, x, y) == 0)
{
show[x][y] = ' ';
if (show[x - 1][y - 1] == '*')
Unfold(mine, show, x - 1, y - 1);
if (show[x - 1][y] == '*')
Unfold(mine, show, x - 1, y);
if (show[x - 1][y + 1] == '*')
Unfold(mine, show, x - 1, y + 1);
if (show[x][y - 1] == '*')
Unfold(mine, show, x, y - 1);
if (show[x][y + 1] == '*')
Unfold(mine, show, x, y + 1);
if (show[x + 1][y - 1] == '*')
Unfold(mine, show, x + 1, y - 1);
if (show[x + 1][y] == '*')
Unfold(mine, show, x + 1, y);
if (show[x + 1][y + 1] == '*')
Unfold(mine, show, x + 1, y + 1);
}
}
return 0;
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
Initboard(mine, ROWS, COLS, '0');
Initboard(show, ROWS, COLS, '*');
set_mine(mine, ROWS, COLS);
Printboard(show, ROWS, COLS);
Judge(mine, show, ROWS, COLS);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("输入错误,请重新选择:\n");
break;
}
} while (input);
system("pause");
return 0;
}