#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,我会寄给你