#include <windows.h>
#include <stdio.h>
#include “main.hpp”
#include “field.hpp”
#include “resource.h”
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK BtnProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
char WinClass[] = “Minegame”;
char WinTitle[] = “Mine Game”;
HINSTANCE hInst;
HWND hwnd;
HWND fraHwnd;
HWND btnHwnd[BLOCKS*BLOCKS];
HBITMAP hbitBomb, hbitFlag;
int cxClient, cyClient, cxOrigin, cyOrigin;
int **field, fieldstate[BLOCKS][BLOCKS];
int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode)
{
MSG msg;
WNDCLASSEX wcl;
wcl.cbSize = sizeof(WNDCLASSEX);
wcl.lpszClassName = WinClass;
wcl.lpfnWndProc = WinProc;
wcl.hInstance = hThisInst;
wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
wcl.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
wcl.style = CS_HREDRAW | CS_VREDRAW;
wcl.lpszMenuName = NULL;
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;
if(!RegisterClassEx(&wcl))
exit(1);
field = new int *[BLOCKS];
for(int i=0; i<BLOCKS; i++)
field[i] = new int [BLOCKS];
hInst = hThisInst;
hwnd = CreateWindow(WinClass, WinTitle, WS_SYSMENU | WS_CAPTION | WS_VISIBLE | WS_BORDER,
CW_USEDEFAULT, CW_USEDEFAULT, (BLOCKS+2)*BLOCKSIZE, (BLOCKS+3)*BLOCKSIZE,
HWND_DESKTOP, NULL, hThisInst, 0);
ShowWindow(hwnd, nWinMode);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc, memdc;
PAINTSTRUCT ps;
HFONT hfont;
RECT rcClient;
RECT rcText;
int i, j, k;
char number[25];
static int NFlags, NCorrect;
switch(msg){
case WM_CREATE:
for(i=0; i<BLOCKS; i++)
for(j=0; j<BLOCKS; j++)
fieldstate[i][j] = 0;
ClearField(field, BLOCKS);
InitializeField(field, BLOCKS);
for(i=0; i<BLOCKS; i++)
for(j=0; j<BLOCKS; j++)
if(IsBomb(field[i][j]))
fieldstate[i][j] |= F_ISBOMB;
NCorrect = 0;
NFlags = 0;
GetClientRect(hwnd, &rcClient);
cxClient = rcClient.right;
cyClient = rcClient.bottom;
cxOrigin = (cxClient-BLOCKS*BLOCKSIZE)/2;
cyOrigin = cyClient-(BLOCKS+1)*BLOCKSIZE;
fraHwnd = CreateWindowEx(WS_EX_STATICEDGE, “STATIC”, NULL, WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
cxOrigin, cyOrigin, BLOCKS*BLOCKSIZE, BLOCKS*BLOCKSIZE,
hwnd, NULL, hInst, 0);
for(i=0; i<BLOCKS; i++){
for(j=0; j<BLOCKS; j++){
btnHwnd[i*BLOCKS+j] = CreateWindowEx(WS_EX_STATICEDGE, “BUTTON”, NULL,
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_BITMAP | BS_NOTIFY,
cxOrigin+i*BLOCKSIZE, cyOrigin+j*BLOCKSIZE, BLOCKSIZE, BLOCKSIZE,
hwnd, (HMENU)(IDB_FIRST+i*BLOCKS+j), hInst, 0);
}
}
hbitBomb = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BOMB));
hbitFlag = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_FLAG));
break;
case WM_COMMAND:
switch(HIWORD(wParam)){
case BN_CLICKED: //
DestroyWindow(btnHwnd[LOWORD(wParam)-IDB_FIRST]);
i = (LOWORD(wParam)-IDB_FIRST) / BLOCKS;
j = (LOWORD(wParam)-IDB_FIRST) % BLOCKS;
fieldstate[i][j] |= F_ISOPEN;
if(IsBomb(field[i][j])){
for(k=0; k<BLOCKS*BLOCKS; k++)
DestroyWindow(btnHwnd[k]);
MessageBox(hwnd, “Game over!”, “Mine Game”, MB_OK);
if(MessageBox(hwnd, “Start another game?”, “Mine Game”, MB_YESNO)==IDNO)
SendMessage(hwnd, WM_DESTROY, 0, 0);
else{
for(k=0; k<BLOCKS*BLOCKS; k++)
DestroyWindow(btnHwnd[k]);
SendMessage(hwnd, WM_CREATE, 0, 0);
}
}
else if(IsZero(field[i][j])){
int OpenedThisRun;
do{
OpenedThisRun = 0;
for(k=0; k<BLOCKS*BLOCKS; k++)
if(!(fieldstate[k/BLOCKS][k%BLOCKS] & F_ISOPEN) && CheckCellForOpen(k/BLOCKS, k%BLOCKS)){
DestroyWindow(btnHwnd[k]);
fieldstate[k/BLOCKS][k%BLOCKS] |= F_ISOPEN;
OpenedThisRun++;
}
}while(OpenedThisRun);
}
break;
}
break;
case WM_PARENTNOTIFY:
switch(LOWORD(wParam)){
case WM_RBUTTONDOWN:
i = (LOWORD(lParam)-cxOrigin) / BLOCKSIZE;
j = (HIWORD(lParam)-cyOrigin) / BLOCKSIZE;
if(fieldstate[i][j] & F_ISFLAG){
fieldstate[i][j] = fieldstate[i][j] & (~F_ISFLAG);
SendMessage(btnHwnd[i*BLOCKS+j], BM_SETIMAGE, 0, NULL);
NFlags–;
if(fieldstate[i][j] & F_ISBOMB)
NCorrect–;
}
else{
fieldstate[i][j] |= F_ISFLAG;
SendMessage(btnHwnd[i*BLOCKS+j], BM_SETIMAGE, 0, (LPARAM)hbitFlag);
NFlags++;
if(fieldstate[i][j] & F_ISBOMB)
NCorrect++;
}
if(NCorrect==BLOCKS && NCorrect==NFlags){
for(k=0; k<BLOCKS*BLOCKS; k++)
if(!(fieldstate[k/BLOCKS][k%BLOCKS] & F_ISFLAG))
DestroyWindow(btnHwnd[k]);
MessageBox(hwnd, “Mission Accomplished!”, “Mine Game”, MB_OK);
if(MessageBox(hwnd, “Start another game?”, “Mine Game”, MB_YESNO)==IDNO)
SendMessage(hwnd, WM_DESTROY, 0, 0);
else{
for(k=0; k<BLOCKS*BLOCKS; k++)
DestroyWindow(btnHwnd[k]);
SendMessage(hwnd, WM_CREATE, 0, 0);
}
}
break;
}
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
for(i=0; i<BLOCKS; i++){ //horizontal
MoveToEx(hdc, cxOrigin+i*BLOCKSIZE, cyOrigin, NULL);
LineTo(hdc, cxOrigin+i*BLOCKSIZE, cyOrigin+BLOCKS*BLOCKSIZE);
}
for(i=0; i<BLOCKS; i++){ //vertical
MoveToEx(hdc, cxOrigin, cyOrigin+i*BLOCKSIZE, NULL);
LineTo(hdc, cxOrigin+BLOCKS*BLOCKSIZE, cyOrigin+i*BLOCKSIZE);
}
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(0, 0, 100));
hfont = CreateFont(5*BLOCKSIZE/6, 0, 0, 0, FW_SEMIBOLD, 0, 0, 0,
ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, “New Times Roman”);
SelectObject(hdc, hfont);
memdc = CreateCompatibleDC(hdc);
SelectObject(memdc, hbitBomb);
for(i=0; i<BLOCKS; i++){
for(j=0; j<BLOCKS; j++){
if(IsBomb(field[i][j]))
BitBlt(hdc, cxOrigin+i*BLOCKSIZE, cxOrigin+j*BLOCKSIZE-4,
cxOrigin+(i+1)*BLOCKSIZE, cxOrigin+(j+1)*BLOCKSIZE,
memdc, 0, 0, SRCCOPY);
else if(!IsZero(field[i][j])){
SetRect(&rcText, cxOrigin+i*BLOCKSIZE, cxOrigin+j*BLOCKSIZE, cxOrigin+(i+1)*BLOCKSIZE, cxOrigin+(j+1)*BLOCKSIZE);
sprintf(number, “%d”, field[i][j]);
DrawText(hdc, number, strlen(number), &rcText, DT_CENTER);
}
}
}
DeleteObject(hfont);
DeleteObject(hbitBomb);
DeleteDC(memdc);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
for(i=0; i<BLOCKS; i++)
delete [] field[i];
delete [] field;
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
bool CheckCellForOpen(int i, int j)
{
for(int m=max(i-1, 0); m<=min(i+1, BLOCKS-1); m++)
for(int l=max(j-1, 0); l<=min(j+1, BLOCKS-1); l++)
if(!(m==i && l==j) && IsZero(field[m][l]) && (fieldstate[m][l] & F_ISOPEN))
return true;
return false;
}
|
دیدگاهتان را بنویسید