您的位置:首页 > 其它

windows程序设计 扫雷源码(非MFC)

2013-06-11 13:52 357 查看
这是大一的时候写的,代码质量不是很高,仅供学习。关于扫雷怎么玩,代码思想,网上很多,要我写也写不出来他们那么好的,所有这方面的百度下

源码地址http://pan.baidu.com/share/link?shareid=1925887460&uk=2065228996
#include<windows.h>
#include<stdlib.h>
#include"resource.h"
int XUNIT = GetSystemMetrics(SM_CXICON);
int YUNIT = GetSystemMetrics(SM_CYICON);
int NUM  = 80;
int CXNUM   =       20;
int CYNUM   =       20;
int Dead = 0 ;
//只能设置成全局变量
static  int minestate[100][100], mineflag[100][100] ;
HDC     hdc, hdcmem3;/*hdcmem1,hdcmem2, hdcmem4;*/
int x,y;
int cxmine , cymine;
static int      cySource3, cxSource3;// cxSource1,cxSource2,cxSource3,cxSource4,
//  cySource1,cySource2,cySource3,cySource4;
int     width = CXNUM*XUNIT;
int     hight = CYNUM*(YUNIT) + +13+GetSystemMetrics(SM_CYBORDER);
LRESULT CALLBACK MineProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
static TCHAR szMineName [] = TEXT ("mineswap");
HWND        hwnd;
MSG     msg;

WNDCLASS        wc;
wc.cbClsExtra       =   0;
wc.cbWndExtra       =   0;
wc.hbrBackground        =   (HBRUSH)GetStockObject (GRAY_BRUSH);
wc.hCursor      =   LoadCursor (NULL, IDC_ARROW);
wc.hIcon        =   LoadIcon (hInstance, MAKEINTRESOURCE(10));
wc.hInstance        =   hInstance;
wc.lpfnWndProc      =   MineProc;
wc.lpszClassName        =   szMineName ;
wc.lpszMenuName     =   szMineName;

wc.style        =   CS_HREDRAW | CS_VREDRAW;

//  RegisterClass(&wc);
if (!RegisterClass (&wc))
{
MessageBox (NULL, TEXT ("Program requires Windows NT!"),
szMineName, MB_ICONERROR) ;
return 0  ;
}

hwnd =  CreateWindow(szMineName,
TEXT("MINESWAP"),
WS_OVERLAPPED |
WS_DLGFRAME | WS_SYSMENU  | WS_MINIMIZEBOX,
CW_USEDEFAULT,CW_USEDEFAULT,
width, hight,
//XUNIT*CXNUM,YUNIT*(CYNUM + 1)+13+GetSystemMetrics(SM_CYBORDER),
NULL,NULL,hInstance,NULL);

ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}

int IsInMineArea(int row,int col)//查看有没超出范围
{
return (row >= 0 && row < CXNUM && col >= 0 && col < CYNUM);
}
int GetAroundNum(int row, int col)//得到周围个数,
{
int i, j;
int around = 0;
int minRow = ( row == 0 ) ? 0 : row  - 1;
int maxRow = row + 2;
int minCol = ( col == 0 ) ? 0 : col - 1 ;
int maxCol = col + 2;
for (i = minRow; i < maxRow; i++)
{
for (j = minCol; j < maxCol; j++)
{
if (minestate[i][j] == 5) around++;
}
}
return around;
}
void ExpandMines(int row, int col)//扫描,如果是已经被按下且雷数为0,显示它周围的八个格,c参见程序一
{
int i, j;
int minRow = (row == 0) ? 0 : row - 1;
int maxRow = row + 2;
int minCol = (col ==  0) ? 0 : col - 1;
int maxCol = col + 2;
int around = GetAroundNum(row, col);
if (mineflag[row][col] == 1)   //标志是1才赋值
{
minestate[row][col] = 15 - around;
}
//对周围一个雷都没有的空白区域
if (around == 0)
{
for (i = minRow; i < maxRow; i++)
{
for (j = minCol; j < maxCol; j++)
{//对于周围可以拓展的区域进行的规拓展

if ((!(i == row && j == col) )&& (minestate[i][j] == 0) && mineflag[x][y] == 1)
{
if (!IsInMineArea (i, j) )continue;
ExpandMines(i, j);

}
}
}
}
}
LRESULT CALLBACK MineProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT     ps;
HINSTANCE   hInstance;
static HBITMAP  hBm3;   //hBm1,hBm2,hBm3,hBm4;
BITMAP  bitmap3;//  bimap1,bitmap2,bitmap3,bitmap4;
//若设置此函数的全局变量,没个WM_结束后不会值改变,其实是局部变量
//因为该函数其实就是下面的变形
/*
int fun ()
{
int a, b , c;
int num ;
switch()
{

}
return NUM ;
}
*/
int j;
HMENU   hmenu ;
static int Select = ID_FIRST;
switch(message)
{

case    WM_CREATE:
hInstance       =       ((LPCREATESTRUCT)lParam)->hInstance;

//hBm1  =   LoadBitmap (hInstance, MAKEINTRESOURCE(1));
//hBm2  =   LoadBitmap (hInstance, MAKEINTRESOURCE(2));
hBm3    =   LoadBitmap (hInstance, MAKEINTRESOURCE(3));
//hBm4  =   LoadBitmap (hInstance, MAKEINTRESOURCE(4));

//GetObject(hBm1,sizeof(BITMAP),&bitmap1);

//GetObject(hBm2,sizeof(BITMAP),&bitmap2);

GetObject(hBm3,sizeof(BITMAP),&bitmap3);
cxSource3   =   bitmap3.bmWidth;
cySource3   =   bitmap3.bmHeight/16;

for (x = 0 ; x < CXNUM ; x ++)
for (y = 0 ; y < CXNUM ; y ++)
{
minestate[x][y] = 0 ;   //初始化雷区状态
mineflag[x][y] = 1;// 初始化标志
}
//随机布雷
srand((unsigned)GetCurrentTime());
for( j = 1; j <= NUM; )
{
cxmine = abs (rand()%(400));
cymine = abs (rand()%(400));

if ( cxmine < CXNUM && cymine < CYNUM &&  minestate[cxmine][cymine] != 5 )
{
minestate[cxmine][cymine] = 5 ;//属性置雷
j++;
}
else continue ;
}
//GetObject(hBm4,sizeof(BITMAP),&bitmap4);
return  0 ;

case  WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
hdcmem3 = CreateCompatibleDC (hdc) ;
SelectObject (hdcmem3, hBm3) ;

//画初始化的雷和重画的雷
for (x = 0 ; x < CXNUM ; x ++)
{
for (y = 0 ; y < CYNUM ; y ++)
{

switch (minestate[x][y])
{
case  0 :
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, (mineflag[x][y] - 1)*cySource3,cxSource3, cySource3, MERGECOPY);
continue ;
case  2 :
case  4 :
case  3 :
case 6 :
case  7 :
case  8 :
case  9 :
case  10 :
case  11:
case  12:
case  13 :
case  14:
case  15:
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, minestate[x][y] * cySource3,cxSource3, cySource3, MERGECOPY);
continue ;

case  5 :
switch(Dead)
{
case 0 :
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, (mineflag[x][y] - 1)*cySource3,cxSource3, cySource3, MERGECOPY);
continue;
case 1 :
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, 5*cySource3,cxSource3, cySource3, MERGECOPY);
continue;
}
}

}
}
DeleteDC (hdcmem3) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case    WM_LBUTTONDOWN:
x = LOWORD(lParam)/XUNIT;
y = HIWORD(lParam)/YUNIT;

//雷属性赋值
if(Dead == 1)break;//结束置dead为一
else
{
hdc = GetDC(hwnd);
hdcmem3 = CreateCompatibleDC (hdc) ;
SelectObject (hdcmem3, hBm3) ;
if(mineflag[x][y] == 1)
{
switch (minestate[x][y] )
{
case 0 :
if(GetAroundNum (x, y) == 0 )
{
ExpandMines(x, y);//周围无雷时扩展无雷区

}
else
{
minestate[x][y] = 15 - GetAroundNum (x, y);
}
break;

case 5: //失败的时候
Dead = 1;
minestate[x][y] = 4 ;
break;
case 1:
case 2:
case 4:
case 3:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
break ;
}
}
//画雷
for (x = 0 ; x < CXNUM ; x ++)
{
for (y = 0 ; y < CYNUM ; y ++)
{

switch (minestate[x][y])
{
case  0 :
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, (mineflag[x][y] - 1)*cySource3,cxSource3, cySource3, MERGECOPY);
continue ;
case  3 :
case  2 :
case  4 :
case 6 :
case  7 :
case  8 :
case  9 :
case  10 :
case  11:
case  12:
case  13 :
case  14:
case  15:
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, minestate[x][y] * cySource3,cxSource3, cySource3, MERGECOPY);
continue ;

case  5 :
switch(Dead)
{
case 0 :
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, (mineflag[x][y] - 1) * cySource3,cxSource3, cySource3, MERGECOPY);
break ;
case 1:
if(mineflag[x][y]== 1)
StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, 5 * cxSource3,cxSource3, cySource3, MERGECOPY);
else StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, (mineflag[x][y] - 1) * cxSource3,cxSource3, cySource3, MERGECOPY);
break;
return 0;
}
}
}

}
//此处必须这样,放外面会使重画时失效
DeleteDC (hdcmem3) ;
ReleaseDC(hwnd, hdc);

}

return 0;
case    WM_RBUTTONDOWN:
x = LOWORD(lParam)/XUNIT;
y = HIWORD(lParam)/YUNIT;
hdc = GetDC(hwnd);
hdcmem3 = CreateCompatibleDC (hdc) ;
SelectObject (hdcmem3, hBm3) ;

//标志赋值
if((IsInMineArea(x, y) && minestate[x][y] == 0 || minestate[x][y] == 5 )&& Dead == 0)
{
mineflag[x][y] = mineflag[x][y] % 3 + 1;   //实现循环

StretchBlt (hdc, x*XUNIT, y*YUNIT, XUNIT, YUNIT, hdcmem3, 0, (mineflag[x][y] - 1)*cySource3,cxSource3, cySource3, MERGECOPY);
}
else
MessageBeep(0);
DeleteDC (hdcmem3) ;
ReleaseDC(hwnd, hdc);
return 0;
case    WM_COMMAND://command非commend
hmenu = GetMenu (hwnd);
switch(LOWORD (wParam))
{
case ID_FIRST://初级时
CheckMenuItem(hmenu,Select, MF_UNCHECKED);
Select = LOWORD(wParam);
CheckMenuItem(hmenu,Select, MF_CHECKED);
NUM  = 60;
for (x = 0 ; x < CXNUM ; x ++)
for (y = 0 ; y < CXNUM ; y ++)
{
minestate[x][y] = 0 ;   //初始化雷区状态
mineflag[x][y] = 1;
}
//随机布雷
srand((unsigned)GetCurrentTime());
for( j = 1; j <= NUM; )
{
cxmine = abs (rand()%(400));
cymine = abs (rand()%(400));

if ( cxmine < CXNUM && cymine < CYNUM &&  minestate[cxmine][cymine] != 5 )
{
minestate[cxmine][cymine] = 5 ;
j++;
Dead = 0;//重生
}
else continue ;
}
InvalidateRect(hwnd, NULL,TRUE);
break;
case ID_SECOND: //中级
CheckMenuItem(hmenu,Select, MF_UNCHECKED);
Select = LOWORD(wParam) ;// 对于前后要用的值得一用
CheckMenuItem(hmenu, Select, MF_CHECKED);
NUM  = 100;
for (x = 0 ; x < CXNUM ; x ++)
for (y = 0 ; y < CXNUM ; y ++)
{
minestate[x][y] = 0 ;   //初始化雷区状态
mineflag[x][y] = 1;
}
//随机布雷
srand((unsigned)GetCurrentTime());
for( j = 1; j <= NUM; )
{
cxmine = abs (rand()%(400));
cymine = abs (rand()%(400));

if ( cxmine < CXNUM && cymine < CYNUM &&  minestate[cxmine][cymine] != 5)
{
minestate[cxmine][cymine] = 5 ;
j++;
Dead = 0 ;
}
else continue ;
}
InvalidateRect(hwnd, NULL,TRUE);//重画
break;
case ID_MAX://高级
CheckMenuItem(hmenu,Select, MF_UNCHECKED);
Select = LOWORD(wParam) ;// 对于前后要用的值得一用
CheckMenuItem(hmenu, Select, MF_CHECKED);
NUM  = 140;
for (x = 0 ; x < CXNUM ; x ++)
for (y = 0 ; y < CXNUM ; y ++)
{
minestate[x][y] = 0 ;   //初始化雷区状态
mineflag[x][y] = 1;
}
//随机布雷
srand((unsigned)GetCurrentTime());
for( j = 1; j <= NUM; )
{
cxmine = abs (rand()%(400));
cymine = abs (rand()%(400));

if ( cxmine < CXNUM && cymine < CYNUM &&  minestate[cxmine][cymine] != 5 )
{
minestate[cxmine][cymine] = 5 ;
j++;

Dead = 0;
}
else continue ;
}
InvalidateRect(hwnd, NULL,TRUE);
break ;

case ID_EXIT://退出
SendMessage (hwnd, WM_CLOSE, 0, 0) ;
break;

case ID_START://重开始
for (x = 0 ; x < CXNUM ; x ++)
for (y = 0 ; y < CXNUM ; y ++)
{
minestate[x][y] = 0 ;   //初始化雷区状态
mineflag[x][y] = 1;
}
//随机布雷
srand((unsigned)GetCurrentTime());
for( j = 1; j <= NUM; )
{
cxmine = abs (rand()%(400));
cymine = abs (rand()%(400));

if ( cxmine < CXNUM && cymine < CYNUM &&  minestate[cxmine][cymine] != 5 )
{
minestate[cxmine][cymine] = 5 ;
j++;
Dead = 0;
}
else continue ;
}
InvalidateRect(hwnd, NULL,TRUE);
break;
}
return 0 ;

case    WM_DESTROY:

//  DeleteObject(hBm1);
//  DeleteObject(hBm2);
PostQuitMessage(0);
DeleteObject(hBm3);

//  DeleteObject(hBm4);

return  0;
}
return DefWindowProc(hwnd,message, wParam, lParam);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息