Win32简易版扫雷

社团的一群小孩都默默的写好了扫雷,嘿嘿,我也打算凑个热闹 

最近学windows编程,就写一个win32简易版扫雷吧 //不支持右键

感觉写的挺乱的╮(╯▽╰)╭啊,不要在意这些细节

点到雷会退出程序 //果然热爱各种退出,关机╮(╯▽╰)╭

代码链接:http://yunpan.cn/cAwIeSpUpSwLA (提取码:cc0c)

(发现有点问题。。我对扫雷理解错了。。考完试来改吧)

___________________________________________________________

// MineSweeping.cpp : 定义应用程序的入口点。
//VS2012
//呆 2014.11.15


#include "stdafx.h"
#include "MineSweeping.h"
#include "stdlib.h"
#include "time.h"




#define MAX_LOADSTRING 100
#define DIVISIONS 10


// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名


static BOOL mine[DIVISIONS][DIVISIONS];//雷
static BOOL open[DIVISIONS][DIVISIONS];//是否打开
static BOOL check[DIVISIONS][DIVISIONS];//确认是否已检查过为0
HDC hdc;
static int cxBlock,cyBlock;//一个格子的长度,宽度
TCHAR szText[10];//给minenumber的缓冲区
int minenumber;//当前x,y位置的数字//九宫格内的雷数


// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);


int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);


  // TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;


// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MINESWEEPING, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);


// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}


hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MINESWEEPING));


// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}


return (int) msg.wParam;
}






//
//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;


wcex.cbSize = sizeof(WNDCLASSEX);


wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc= WndProc;
wcex.cbClsExtra= 0;
wcex.cbWndExtra= 0;
wcex.hInstance= hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MINESWEEPING));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName= MAKEINTRESOURCE(IDC_MINESWEEPING);
wcex.lpszClassName= szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));


return RegisterClassEx(&wcex);
}


//
//   函数: InitInstance(HINSTANCE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;


   hInst = hInstance; // 将实例句柄存储在全局变量中


   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, 500, 500, NULL, NULL, hInstance, NULL);


   if (!hWnd)
   {
      return FALSE;
   }


   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);


   return TRUE;
}


int countnumber(int x,int y)
{
int minenumber=0;
for(int i=x-1;i<=x+1;i++)
for(int j=y-1;j<=y+1;j++)
{
if(i>=0&&i<=9&&j>=0&&j<=9)
{
if(mine[i][j])
minenumber++;
}
}
return minenumber;
}


void checkzero(int x,int y)
{
check[x][y]=1;
if(!(minenumber=countnumber(x,y)))
{
open[x][y]=1;
TextOut(hdc,x*cxBlock+cxBlock/3,y*cyBlock+cyBlock/3,TEXT("0"),1);
for(int i=x-1;i<=x+1;i++)
for(int j=y-1;j<=y+1;j++)
{
if(i>=0&&i<=9&&j>=0&&j<=9&&check[i][j]==0)
checkzero(i,j);
}     
}
}


//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的: 处理主窗口的消息。
//
//  WM_COMMAND - 处理应用程序菜单
//  WM_PAINT - 绘制主窗口
//  WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
RECT rect;
int x,y,randx,randy,randcount=1;//格子的坐标,布雷初始化时的随机坐标


int wmId, wmEvent;
PAINTSTRUCT ps;


switch (message)
{
case WM_CREATE:
srand(time(NULL));
while(randcount<=20)
{
randx=rand()%10;
randy=rand()%10;
if(mine[randx][randy]==0)
{
mine[randx][randy]=1;
randcount++;
}
}
return 0;


case WM_SIZE:
cxBlock=LOWORD(lParam)/DIVISIONS;
cyBlock=HIWORD(lParam)/DIVISIONS;
return 0;

case WM_LBUTTONDOWN:
x=LOWORD(lParam)/cxBlock;
y=HIWORD(lParam)/cyBlock;
open[x][y]=1;


if(x {
rect.left=x*cxBlock;
rect.top=y*cyBlock;
rect.right=(x+1)*cxBlock;
rect.bottom=(y+1)*cyBlock;


InvalidateRect(hWnd,NULL,FALSE);
}
else
MessageBeep(0);
return 0;


case WM_COMMAND:
wmId    = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
for(x=0;x for(y=0;y {
Rectangle(hdc,x*cxBlock,y*cyBlock,(x+1)*cxBlock,(y+1)*cyBlock);
if(open[x][y])
{
if(mine[x][y])
{
TextOut(hdc,x*cxBlock+cxBlock/3,y*cyBlock+cyBlock/3,TEXT("*"),1);
TextOut(hdc,230,230,TEXT("LOSE"),4);
PostQuitMessage(0);
break;
}
else
{
if(minenumber=countnumber(x,y))
TextOut(hdc,x*cxBlock+cxBlock/3,y*cyBlock+cyBlock/3,szText,wsprintf(szText, TEXT("%d"), minenumber));
else
checkzero(x,y);
//TextOut(hdc,x*cxBlock+cxBlock/3,y*cyBlock+cyBlock/3,szText,wsprintf(szText, TEXT("%d"), minenumber));
}
}
}
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}


// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;


case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}

你可能感兴趣的:(Windows,vs2012,win32,扫雷)