C语言实现扫雷

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值