一晚写完基本的扫雷

#include<iostream>
#include<easyx.h>
#include<graphics.h>
using namespace std;
const int Row = 10;
const int Col = 10;
const int NUM_MINES = 10;
enum status { nevtor, unfold, attrmine };
typedef struct map {
	int pos[10][10] = { 0 };
	status Sta[10][10];
}map;
map gmap;
IMAGE img[24];
void Show();
void Click(int x, int y)
{
	char img_name[20];
	sprintf_s(img_name, "././image/%d.tif", gmap.pos[x][y]);
	loadimage(img, img_name, 32, 32);
	putimage(32 * x, 32 * y, img);
	gmap.Sta[x][y] = unfold;
}
void BOMB(int x, int y)
{
	Click(x, y);
	if (gmap.pos[x][y] == 0)
	{
		if (x - 1 >= 0 && gmap.Sta[x - 1][y] == nevtor)
			BOMB(x - 1, y);
		if (x + 1 < Row && gmap.Sta[x + 1][y] == nevtor)
			BOMB(x + 1, y);
		if (y - 1 >= 0 && gmap.Sta[x][y - 1] == nevtor)
			BOMB(x, y - 1);
		if (y + 1 < Col && gmap.Sta[x][y + 1] == nevtor)
			BOMB(x, y + 1);
		if (x - 1 >= 0 && y - 1 >= 0 && gmap.Sta[x][y + 1] == nevtor)
			BOMB(x - 1, y - 1);
		if (x - 1 >= 0 && y + 1 < Col && gmap.Sta[x][y + 1] == nevtor)
			BOMB(x - 1, y + 1);
		if (x + 1 < Row && y - 1 >= 0 && gmap.Sta[x][y + 1] == nevtor)
			BOMB(x + 1, y - 1);
		if (x + 1 < Row && y + 1 < Col && gmap.Sta[x][y + 1] == nevtor)
			BOMB(x + 1, y + 1);
	}
}
int InitGameMap()
{
	srand(unsigned(time(NULL)));
	for (int i = 0; i < NUM_MINES; i++)
	{
		int rand_1 = rand() % Row;
		int rand_2 = rand() % Col;
		if (gmap.pos[rand_1][rand_2])
		{
			i--;
			continue;
		}
		else
		{
			gmap.pos[rand_1][rand_2] = -NUM_MINES - 1;
		}
	}
	for (int i = 0; i < Row; i++)
	{
		for (int j = 0; j < Col; j++)
		{
			gmap.Sta[i][j] = nevtor;
			if (gmap.pos[i][j] < 0)
			{
				if (i > 0)
				{
					gmap.pos[i - 1][j]++;
					if (j > 0)
						gmap.pos[i - 1][j - 1]++;
					if (j < Col - 1)
						gmap.pos[i - 1][j + 1]++;
				}
				if (i < Row - 1)
				{
					gmap.pos[i + 1][j]++;
					if (j > 0)
						gmap.pos[i + 1][j - 1]++;
					if (j < Col - 1)
						gmap.pos[i + 1][j + 1]++;
				}
				if (j > 0)
					gmap.pos[i][j - 1]++;
				if (j < Col - 1)
					gmap.pos[i][j + 1]++;
			}
		}
	}
	return 0;
}
int WatchGraphCMD()
{
	for (int i = 0; i < Row; i++)
	{
		for (int j = 0; j < Col; j++)
		{
			printf("%3d ", gmap.pos[j][i]);
		}
		printf("\n");
	}
	return 0;
}
void MouseMsg()
{
	MOUSEMSG msg = GetMouseMsg();
	int x = msg.x / 32;
	int y = msg.y / 32;
	/*cout << "( " << x << " , " << y << " )" << endl;*/
	if (msg.uMsg == WM_LBUTTONDOWN)
	{
		cout << "( " << y + 1 << " , " << x + 1 << " )" << "gmap.pos[x][y] "<< gmap.pos[x][y] <<"gmap.pos[y][x] "<< gmap.pos[y][x] << endl;
		if (gmap.Sta[x][y] == nevtor)
		{
			if (gmap.pos[x][y] < 0)
			{
				Show();
				cout << "你踩中雷了" << endl;
			}
			else if (gmap.pos[x][y])
			{
				Click(x, y);
			}
			else
			{
				BOMB(x, y);
			}
		}
		else
			return;

	}
	else if (msg.uMsg == WM_RBUTTONDOWN)
	{
		if (gmap.Sta[x][y] == nevtor)
		{
			loadimage(img, "././image/9.tif", 32, 32);
			putimage(32 * x, 32 * y, img);
			gmap.Sta[x][y] = attrmine;
		}
		else if (gmap.Sta[x][y] == attrmine)
		{
			loadimage(img, "././image/-2.tif", 32, 32);
			putimage(32 * x, 32 * y, img);
			gmap.Sta[x][y] = nevtor;
		}
		else
		{
			return;
		}
	}
	else
		return;

}
void Show()
{
	for (int j = 0; j < Row; j++)
	{
		for (int i = 0; i < Col; i++)
		{
			//char filename[20] = { '\0' };
			//sprintf_s(filename, "././image/%d.tif", i);
			if (gmap.pos[i][j] < 0)
			{
				loadimage(img, "././image/-1.tif", 32, 32);
			}
			else if (gmap.pos[i][j] == 0)
			{
				loadimage(img, "././image/0.tif", 32, 32);
			}
			else
			{
				char filename[20] = { '\0' };
				sprintf_s(filename, "././image/%d.tif", gmap.pos[i][j]);
				loadimage(img, filename, 32, 32);
			}
			putimage(32 * i, 32 * j, img);
		}
	}
}
void Show_Begin()
{
	for (int i = 0; i < Row; i++)
	{
		for (int j = 0; j < Col; j++)
		{
			loadimage(img, "././image/-2.tif", 32, 32);
			putimage(32 * i, 32 * j, img);
		}
	}
}
int main(void)
{
	HWND hwnd = initgraph(320, 320, EW_SHOWCONSOLE);
	SetWindowTextA(hwnd, "扫雷");

	InitGameMap();


	WatchGraphCMD();
	Show_Begin();
	while (true)
	{
		MouseMsg();
	}
	getchar();
}

扫雷都玩过,基本原理大家也都知道,这次写主要是因为之前没有写过图形化的界面,这次就通过这个小游戏,看看图形化程序怎么写的,比较初级采用的是easyx,使用的话需要先下载,下载也十分简单可以自行去官网下载。

 以上是全部代码,因为晚上太累写的很粗糙,图这也有,想要的话拿去吧,路径看代码自己放就行。

发现图片太大上传不了,是我用PS做的,自己做也许,也可发邮件到dus51812@gmail.com,我会寄给你

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

alasnot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值