C语言实现扫雷

#include
#include
#include
#include
#include
#define width 9
#define heith 9 
#define lei 10
int cho;
void menu()
{
	printf("\n\n                           ****************************************\n                                  *扫雷小游戏*\n");
	printf("                                  *1.开始游戏*\n                                  *2.游戏须知*\n                                  *3.退出游戏*\n");
	printf("                           ****************************************\n");
	printf("                                  请选择\n                                   ");
	scanf_s("%d", &cho);
	if (cho == 1)
	{
		system("cls");
		return;
	}
		
	if (cho == 2)
	{
		system("cls");
		printf("no have anything\n");
		Sleep(30000);
		exit(0);
	}
	if (cho == 3)
		exit(0);
	else
		exit(0);
}
void setmap(char showmap[heith][width], char gkmap[heith][width])
{
	int row, col, n = lei;
	for (row = 0; row < heith; row++)
	{
		for (col = 0; col < width; col++)
		{
			showmap[row][col] = '*';//'■';
		}
	}
	for (row = 0; row < heith; row++)
	{
		for (col = 0; col < width; col++)
		{
			gkmap[row][col] = '0';
		}
	}
	while (n > 0)
	{
		row = rand() % 9;
		col = rand() % 9;
		if (gkmap[row][col] == '1')
		{
			continue;
		}
		gkmap[row][col] = '1';
		n--;
	}
}
void drawmap(char themap[heith][width], int g)
{
	int col, row;
	printf("========================================\n");
	printf("yx|");
	for (col = 0; col < width; col++)
	{
		printf(" %d", col);
	}
	printf("\n--+--------------------------\n");
	for (row = 0; row < heith; row++)
	{
		printf(" %d|", row);
		for (col = 0; col < width; col++)
		{
			printf(" %c", themap[row][col]);
		}
		printf("\n");
	}
	printf("========================================\n已经检查坐标数量 : %d ,未检查数量 : %d,共有10个地雷", g, 81-g);
}
void show(char showmap[heith][width], char gkmap[heith][width], int row, int col)
{
	int n, m, t = 0;
	for (n = row - 1; n <= row + 1; n++)
	{
		for (m = col - 1; m <= col + 1; m++)
		{
			if (n < 0 || n >= heith || m < 0 || m >= width)
			{
				continue;
			}
			if (gkmap[n][m] == '1')
				t++;
		}
	}
	printf("(%d,%d)附近有%d个地雷,请注意!!!\n", col, row, t);
	showmap[row][col] = t + '0';
}
void play()
{
	char showmap[heith][width] = { 0 }, gkmap[heith][width] = { 0 };
	setmap(showmap, gkmap);
	int f = 0;
	while (1)
	{
		int x, y;
		//drawmap(gkmap, f);//需要检查游戏是否正确运行时将此代码插入程序,正常游戏时关闭它。
		drawmap(showmap, f);
		printf("\n请输入您要检查的坐标(x,y) : ");
		scanf_s("%d %d", &x, &y);
		if (x < 0 || x >= width || y < 0 || y >= heith || showmap[y][x] != '*')
		{
			system("cls");
			printf("(%d,%d)坐标非法,请重新输入!!!\n\n", x, y);
			continue;
		}
		if (gkmap[y][x] == '1')
		{
			system("cls");
			printf("(%d,%d)boom--\n\nboom-- boom-- boom--\n\n", x, y);
			drawmap(gkmap, f);
			printf("\n\nboom-- boom-- boom--\n\n");
			break;
		}
		system("cls");
		show(showmap, gkmap, y, x);
		f++;
		if (f == width * heith - lei)
		{
			system("cls");
			printf("nice!!!\n");
			drawmap(gkmap, f);
			break;
		}
	}
}
int main()
{
	system("mode con cols=98 lines=38");
	SetConsoleTitleA("扫雷"); //设置窗口
	srand((unsigned)time(NULL));
	menu();
	play();
	system("pause");
	return 0;
}

 

你可能感兴趣的:(c,c++)