扫雷 - main.c C




#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define Row 9
#define Col 9
#define Rows Row+2
#define Cols Col+2
#define Easy_count 10
#define Difficulty  Easy_count


void menu()
{
	printf("*****************\n");
	printf("*****1.play******\n");
	printf("*****0.exit******\n");
	printf("*****************\n");
	printf("请选择:>");
}
void Init(char main[Rows][Cols], char show[Rows][Cols], int rows, int cols)
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			main[i][j] = '0';
			show[i][j] = '*';
		}
	}
}
void Display(char crr[Rows][Cols],int row,int col)
{
	printf("---------扫雷--------\n");

	for (int i = 0; i <= row; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (int j = 1; j <= col; j++)
		{
			printf("%c ", crr[i][j]);
		}
		printf("\n");
	}
	printf("---------扫雷--------\n");
}
void Setmain(char main[Rows][Cols], int row, int col)
{
	int count = Easy_count;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (main[x][y] == '0')
		{
			main[x][y] = '1';
			count--;
		}
	}
}
char Count(char main[Rows][Cols], int x, int y)//统计周围雷的个数
{
	int ret = 0;
	for (int i = -1; i <= 1; i++)
	{
		for (int j = -1; j <= 1; j++)
		{
			if (main[x + i][y + j] == '1')
			{
				ret++;
			}
		}
	}
	char c = ret + '0';
	return c;
}
void find(char main[Rows][Cols], char show[Rows][Cols], int x, int y,int* win)
{
	if (show[x][y] == '*')
	{
		(*win)++;
		show[x][y] = Count(main, x, y);
		if (Count(main, x, y) == '0')
		{
			for (int i = -1; i <= 1; i++)
			{
				for (int j = -1; j <= 1; j++)
				{
					if (x + i >= 1 && x + i <= Row && y + j >= 1 && y + j <= Col)
					{
						find(main, show, x + i, y + j, win);
					}
				}
			}
		}
	}
}
void Findmain(char main[Rows][Cols], char show[Rows][Cols], int row, int col)
{
	int x = 0;
	int y = 0;
	int n = 0;
	int win=0;
	while (win<Row*Col- Difficulty)
	{
		Display(show, Row, Col);
		printf("插旗标记输入:1 x y \n");
		printf("扫雷输入:2 x y \n");
		printf("请输入要排查的坐标:>");
		scanf("%d %d %d",&n, &x, &y);
		if (n == 1)
		{
			if (show[x][y] == '/')
			{
				show[x][y] = '*';
			}
			show[x][y] = '/';
		}
		else if (n == 2)
		{
			if (main[x][y] == '1')
			{
				printf("很遗憾你被炸死了\n");
				Display(main, Row, Col);
				break;
			}
			else
			{
				find(main, show, x, y, &win);
			}
		}
		else
		{
			printf("输入错误,请重新输入\n");
		}
	}
	if (win == Row * Col - Difficulty)
	{
		printf("恭喜你,排雷成功!");
	}
}
void game()
{
	char main[Rows][Cols] = { 0 };
	char show[Rows][Cols] = { 0 };
	Init(main, show, Rows, Cols);//初始化
	//Display(show,Row,Col);
	Setmain(main,Row,Col);//布置雷
	//Display(main, Row, Col);
	Findmain(main,show,Row,Col);//排查雷
}

int main()
{
	srand((unsigned int)time(NULL));
	int input = 0;
	
	do
	{ 
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新选择\n");
			break;
		}
	} while (input);
	return 0;
}