关于扫雷小游戏


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<graphics.h>
#include<conio.h>
#include<easyx.h>
#include<time.h>
#define MYMINE 10
#define picWidth 16
#define picHeight 16
void DFS(int x, int y);
void BFS(int r,int c);
void initMap(void);
void release();
int getMouseEvent();
void printInitMap(void);
int dfsMap[50][50] = { 0 };
int mineMap[50][50] = { 0 };
int rMap[50][50] = { 0 };
int sumclick = 0;
int minewin;
//一轮游戏结束按回车回到主界面
int dirVector[2][8] =
{
	{ -1, -1, -1, 0, 1, 1,  1,  0 },
	{ -1,  0,  1, 1, 1, 0, -1, -1 }
};//方向数组
int mineNum , mineCount = 0;
int COL = 0;
int ROW;
int main(void)
{
	while (1)
	{
		int fff = 1;
		memset(dfsMap, 0, sizeof(dfsMap));
		memset(mineMap, 0, sizeof(mineMap));
		memset(rMap, 0, sizeof(rMap));
		sumclick = 0;
		mineCount = 0;

		initgraph(400, 600);
		setbkcolor(DARKGRAY);
		cleardevice();
		while (fff)
		{
			outtextxy(185, 200, "游戏难度");
			outtextxy(200, 250, "初级");
			outtextxy(200, 300, "中级");
			outtextxy(200, 350, "高级");
			outtextxy(185, 400, "退出游戏");
			MOUSEMSG mmsg;
			mmsg = GetMouseMsg();
			if (mmsg.x >= 200 && mmsg.x <= 220 && mmsg.y >= 250 && mmsg.y <= 265)
				if (mmsg.uMsg == WM_LBUTTONDOWN)
				{
					ROW = 10;
					COL = 15;
					mineNum = 15;
					fff = 0;
				}
			if (mmsg.x >= 200 && mmsg.x <= 220 && mmsg.y >= 300 && mmsg.y <= 315)
				if (mmsg.uMsg == WM_LBUTTONDOWN)
				{
					ROW = 20;
					COL = 25;
					mineNum = 30;
					fff = 0;
				}
			if (mmsg.x >= 200 && mmsg.x <= 220 && mmsg.y >= 350 && mmsg.y <= 365)
				if (mmsg.uMsg == WM_LBUTTONDOWN)
				{
					ROW = 30;
					COL = 35;
					mineNum = 40;
					fff = 0;
				}
			if (mmsg.x >= 200 && mmsg.x <= 220 && mmsg.y >= 400 && mmsg.y <= 415)
				if (mmsg.uMsg == WM_LBUTTONDOWN)
				{
					fff = 0;
					return 0;
				}
		}
		minewin = mineNum;
		if (COL == 0)
		{
			closegraph();
			return 0;
		}
		release();
		initgraph(picWidth * COL, picHeight * ROW + 32);
		setbkcolor(DARKGRAY);
		cleardevice();
		initMap();
		printInitMap();
		getMouseEvent();
		system("pause");
	}
}

void wfinish()
{
	outtextxy(picWidth*COL / 2 - 9, picHeight*ROW / 2 + 32, "你赢了");
}
void ffinish()
{
	IMAGE img;
	loadimage(&img, _T("E:\\bmp410.bmp"), 0, 0);
	int i, j;
	for (i = 0; i < ROW; i++)
		for (j = 0; j < COL; j++)
		{
			if (mineMap[i][j] == 10)
				putimage(j * picWidth, i * picHeight+32, picWidth, picHeight, &img, 0, 16 * (15 - mineMap[i][j]));
			else
			if(rMap[i][j]==1)
			putimage(j * picWidth, i * picHeight+32, picWidth, picHeight, &img, 0, 16 * (15 -11));
		}
	outtextxy(picWidth*COL/2-9, picHeight*ROW/2+32, "你输了");

	
}
int queueArr[2][200000] = { 0 }, queueHead = 0, queueEnd = 0;
void DFS(int x, int y)
{
	int i, j, k;
	IMAGE img;
	loadimage(&img, _T("E:\\bmp410.bmp"), 0, 0);
	dfsMap[x][y] = 1;
	sumclick++;
	for (k = 0; k < 8; k++)
	{
		i = x + dirVector[0][k];
		j = y + dirVector[1][k];

		if (mineMap[i][j] == 0 && dfsMap[i][j] == 0 && i >= 0 && j >= 0 && i < ROW && j < COL)
			DFS(i, j);
		if(i>=0&&j>=0)
		putimage(j * picWidth, i * picHeight+32, picWidth, picHeight, &img, 0, 16 * (15 - mineMap[i][j]));
		dfsMap[i][j] = 1;
		sumclick++;
	}
	if (i >= 0 && j >= 0)
	putimage(y * picWidth, x * picHeight+32, picWidth, picHeight, &img, 0, 16 * (15 - mineMap[x][y]));
	return;
}
void release()
{
	memset(dfsMap, 0, sizeof(dfsMap));
	memset(mineMap, 0,sizeof(mineMap));
	memset(rMap, 0, sizeof(rMap));
	sumclick = 0;
	mineCount = 0;
	initMap();
	printInitMap();
}
void BFS(int r, int c)//广度

{
	IMAGE img;
	loadimage(&img, _T("E:\\bmp410.bmp"), 0, 0);
	queueArr[0][queueEnd] = r;
	queueArr[1][queueEnd] = c;
	queueEnd++;
	int queueR, queueC;
	while (queueHead < queueEnd)
	{
		queueR = queueArr[0][queueHead];
		queueC = queueArr[1][queueHead];
		queueHead++;
		if (queueC>= 0 && queueR >= 0)
		putimage(queueC *picWidth, queueR*picHeight+32, picWidth, picHeight, &img, 0, (15 - 0)*picHeight);
		dfsMap[queueR][queueC] = 1;
		int tmpR, tmpC;
		for (int i = 0; i < 8; i++)
		{
			tmpR = queueR + dirVector[0][i];
			tmpC = queueC + dirVector[1][i];
			if (tmpR >= 0 && tmpR < ROW && tmpC >= 0 && tmpC < COL && dfsMap[tmpR][tmpC] == 0)
			{
				if (mineMap[tmpR][tmpC] == 0)
				{
					//Dfsmap(tmpR, tmpC);
					queueArr[0][queueEnd] = tmpR;
					queueArr[1][queueEnd] = tmpC;
					queueEnd++;
				}
				else
					if (queueC >= 0 && queueR >= 0)
					putimage(tmpC*picWidth, tmpR*picHeight + 32, picWidth, picHeight, &img, 0, (15 - mineMap[tmpR][tmpC])*picHeight);
				dfsMap[tmpR][tmpC] = 1;
			}
	}
	}
}
void initMap(void)
{
	srand((unsigned)time(NULL));
	int mineRow, mineCol;
	while (mineCount < mineNum)
	{
		mineRow = rand() % ROW;
		mineCol = rand() % COL;
		if (mineMap[mineRow][mineCol] != MYMINE)
		{
			mineMap[mineRow][mineCol] = MYMINE;
			for (int k = 0; k < 8; k++)
			{
				int tmpRow, tmpCol;
				tmpRow = mineRow + dirVector[0][k];
				tmpCol = mineCol + dirVector[1][k];
				if (tmpRow >= 0 && tmpCol >= 0)
					if (mineMap[tmpRow][tmpCol] != MYMINE)
						mineMap[tmpRow][tmpCol]++;
			}
			mineCount++;
		}
	}
}
int getMouseEvent()
{
	IMAGE img;
	loadimage(&img, _T("E:\\bmp410.bmp"), 0, 0);
	char xystr[50];
	
	while (1)
	{
		MOUSEMSG mmsg;
		mmsg = GetMouseMsg();
		if (mmsg.x >= picWidth*COL / 2 - 21 && mmsg.x <= picWidth*COL / 2 + 21 && mmsg.y >= 0 && mmsg.y <= 32)
			if (mmsg.uMsg == WM_LBUTTONDOWN)
			{
				release();
				mmsg = GetMouseMsg();
			}
		int col, row;
		col = mmsg.x / picWidth;
		row = (mmsg.y-32)/ picHeight;
		switch (mmsg.uMsg)
		{
			if (minewin == 0)
			{
				wfinish();
				return 0;
			}
		   case WM_LBUTTONDOWN:
			   if (mineMap[row][col] == 10&& rMap[row][col] == 0)
			   {
				   ffinish();
				   putimage(col*picWidth, row*picHeight+32, picWidth, picHeight, &img, 0, (15 - 12) * picHeight);
				   return 0;
			   }
			if (rMap[row][col] == 0)
			{
				if (mineMap[row][col] == 0)
				{
					BFS(row, col);
				}
				else
				putimage(col*picWidth, row*picHeight+32, picWidth, picHeight, &img, 0, (15 - mineMap[row][col]) * picHeight);
				dfsMap[row][col] = 1;
				sumclick++;

			}
			break;
		 case WM_RBUTTONDOWN:
			 if(dfsMap[row][col]!=1)
			if (rMap[row][col] == 0)
			{
				putimage(col*picWidth, row*picHeight+32, picWidth, picHeight, &img, 0, (15 - 14) * picHeight);
				rMap[row][col] = 1;
				if (mineMap[row][col] == 10)
					minewin--;
				mineCount--;
				sprintf_s(xystr, "雷数:%d", mineCount);
				outtextxy(0, 0, xystr);
			}
			else
			{
				if (rMap[row][col] == 1)
				{
					putimage(col*picWidth, row*picHeight+32, picWidth, picHeight, &img, 0, (15 - 13) * picHeight);
					rMap[row][col] = 2;
					mineCount++;
					if (mineMap[row][col] == 10)
						minewin++;
					sprintf_s(xystr, "雷数:%d", mineCount);
					outtextxy(0, 0, xystr);
				}
				else
					if (rMap[row][col] == 2)
					{
						putimage(col*picWidth, row*picHeight+32, picWidth, picHeight, &img, 0, (15 - 15) * picHeight);
						rMap[row][col] = 0;
		
					}
			}

			break;
		default:
			break;
		}
	}
}
void printInitMap(void)
{
	int i, j;
	IMAGE img,img1;
	char xystr[50];
	sprintf_s(xystr, "雷数:%d", mineCount);
	outtextxy(0, 0, xystr);
	loadimage(&img1, _T("E:\\timg.jpg"), 0, 0);
	putimage(picWidth*COL/2-21, 0,  &img1);
	loadimage(&img, _T("E:\\bmp410.bmp"), 0, 0);
	for (i = 0; i < ROW; i++)
		for (j = 0; j < COL; j++)
			putimage(j * picWidth, i * picHeight+32, picWidth, picHeight, &img, 0, 16 * (15 - 15));
	return;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值