-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
434 lines (345 loc) · 11 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/***************************************************************************\
* N-PUZZLE 0.2
* Desenvolvido por Abner Cordeiro
* Iniciado em: 19/03/2013 23:17
* corrigido em: 20/09/2014 22:35
\***************************************************************************/
//#define WINVER 0x0410
#define UNICODE
#include <windows.h >
#include <Windowsx.h>
#include <commctrl.h>
#include <stdio.h >
#include <time.h >
#include "resource.h"
#define POSX_BOARD 50//50
#define POSY_BOARD 50
HINSTANCE g_hInst;
int iBoard[3][3];
int g_xPosMouse,g_yPosMouse;
int g_iMinutos ,g_iSegundos;
int g_iRetanguloLargura = 60;
int g_retanguloAltura = 60;
BOOL bJogando = FALSE;
wchar_t g_wcTempo[MAX_PATH];
HBRUSH g_hBrushFundoBoard;
HBRUSH g_hBrushInativoDesbloqueado;
HBRUSH g_hBrushSelecionado;
HBRUSH g_hBrushBloqueado;
HBRUSH g_hFundoPreto;
HFONT g_hFonte1;
HFONT g_hFonte2;
HPEN g_hPen1;
HMENU g_hMenu;
BOOL VerificaPossibilitade(int x,int y)
{
if(iBoard[x][y + 1] == 0 && y < 2) return TRUE;
if(iBoard[x][y - 1] == 0 && y > 0) return TRUE;
if(iBoard[x + 1][y] == 0 && x < 2) return TRUE;
if(iBoard[x - 1][y] == 0 && x > 0) return TRUE;
return FALSE;
}
void IniciaBoard()
{
int x,y,z = 1;
for(x = 0; x < 3; x++)
for(y = 0; y < 3; y++)
iBoard[x][y] = z,z++;
iBoard[2][2] = 0;
}
BOOL VerificaFimGame(HWND hwnd)
{
if((iBoard[0][0] == 1 && iBoard[0][1] == 2 && iBoard[0][2] == 3) &&
(iBoard[1][0] == 4 && iBoard[1][1] == 5 && iBoard[1][2] == 6) &&
(iBoard[2][0] == 7 && iBoard[2][1] == 8 && iBoard[2][2] == 0))
{
KillTimer(hwnd, 1);
wchar_t wcBuffer[MAX_PATH];
wsprintf(wcBuffer,L"Voce Ganhou no tempo de %.2d:%.2d minutos...",g_iMinutos ,g_iSegundos);
MessageBox(NULL,wcBuffer,L"Parabens!!!",MB_OK);
IniciaBoard();
bJogando = FALSE;
return TRUE;
}
return FALSE;
}
void MoveBloco(HWND hwnd)
{
int x,y;
for(x = 0; x < 3; x++)
{
for(y = 0; y < 3; y++)
{
int iLeft,iTop,iRight,iBottom;
iLeft = (y * g_iRetanguloLargura) + 1;
iTop = (x * g_retanguloAltura) + 1 ;
iRight = (y * g_iRetanguloLargura) + g_iRetanguloLargura + 1 ;
iBottom = (x * g_retanguloAltura) + g_retanguloAltura + 1;
if(( (g_xPosMouse - POSX_BOARD ) > iLeft && (g_xPosMouse - POSX_BOARD) < iRight) &&
( (g_yPosMouse - POSY_BOARD) > iTop && (g_yPosMouse - POSY_BOARD ) < iBottom))
{
if(iBoard[x][y + 1] == 0 && y < 2)
{
iBoard[x][y + 1] = iBoard[x][y],iBoard[x][y] = 0,VerificaFimGame(hwnd);
}
if(iBoard[x][y - 1] == 0 && y > 0)
{
iBoard[x][y - 1] = iBoard[x][y],iBoard[x][y] = 0,VerificaFimGame(hwnd);
}
if(iBoard[x + 1][y] == 0 && x < 2)
{
iBoard[x + 1][y] = iBoard[x][y],iBoard[x][y] = 0,VerificaFimGame(hwnd);
}
if(iBoard[x - 1][y] == 0 && x > 0)
{
iBoard[x - 1][y] = iBoard[x][y],iBoard[x][y] = 0,VerificaFimGame(hwnd);
}
return;
}
}
}
}
void MostraBoard(HDC hdc,int pos_x,int pos_y)
{
register int x;
register int y;
register int iBitmapWidth;
register int iBitmapHeight;
RECT textRect;
wchar_t wcBuff[12];
iBitmapWidth = (3 * g_iRetanguloLargura) + 4;
iBitmapHeight = (3 * g_retanguloAltura) + 4;
// 15,20,360,270
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP memBM = CreateCompatibleBitmap(hdc,iBitmapWidth ,iBitmapHeight);
SelectObject(memDC,memBM);
SelectObject(memDC,g_hBrushFundoBoard);
HGDIOBJ hGDI_Obj = SelectObject(memDC,g_hPen1);
Rectangle(memDC,1,1,iBitmapWidth,iBitmapHeight);
SelectObject(memDC,hGDI_Obj);
SelectObject(memDC,g_hBrushInativoDesbloqueado);
for(x = 0; x < 3; x++)
{
for(y = 0; y < 3; y++)
{
int iLeft,iTop,iRight,iBottom;
iLeft = (y * g_iRetanguloLargura) + 2;
iTop = (x * g_retanguloAltura) + 2;
iRight = (y * g_iRetanguloLargura) + g_iRetanguloLargura+ 2;
iBottom = (x * g_retanguloAltura) + g_retanguloAltura+ 2;
if(iBoard[x][y] != 0)
{
if(bJogando)
{
if(( (g_xPosMouse - pos_x - 17) > iLeft && (g_xPosMouse - pos_x - 17) < iRight) &&
( (g_yPosMouse - pos_y - 22) > iTop && (g_yPosMouse - pos_y - 22) < iBottom) &&
VerificaPossibilitade(x,y))
SelectObject(memDC,g_hBrushSelecionado);
else if(!VerificaPossibilitade(x,y))
SelectObject(memDC,g_hBrushBloqueado);
else
SelectObject(memDC,g_hBrushInativoDesbloqueado);
}
else
{
SelectObject(memDC,g_hBrushInativoDesbloqueado);
}
RoundRect(memDC,iLeft,iTop,iRight,iBottom,5,5);
SetRect(&textRect, iLeft, iTop, iRight,iBottom);
wsprintf(wcBuff,L"%d",iBoard[x][y]);
SelectObject(memDC,g_hFonte1);
SetBkMode(memDC, TRANSPARENT);
DrawText(memDC,wcBuff,-1,&textRect, DT_CENTER | DT_NOCLIP | DT_VCENTER | DT_SINGLELINE);
}
}
}
BitBlt(hdc,pos_x,pos_x,iBitmapWidth + 1,iBitmapHeight + 1,memDC,0,0,SRCCOPY);
DeleteDC (memDC);
DeleteObject(memBM);
}
int *GeraTabuleiro(int iQtaNumeros)
{
register int iPosNum = 0;
int *Array = (int*)malloc( (sizeof(int) * iQtaNumeros) + 1 );
while(iPosNum <= iQtaNumeros)
{
register int iGen = (rand() % (iQtaNumeros + 1));
register int iCount = 0;
for(; (Array[iCount] != iGen && iCount < iPosNum); iCount++);
if(iCount == iPosNum)
{
Array[iPosNum] = iGen;
iPosNum++;
}
}
return Array;
}
void NovoJogo(HWND hwnd)
{
g_iMinutos = g_iSegundos = 0;
wsprintf(g_wcTempo,L"Tempo: %.2d:%.2d",0,0);
KillTimer(hwnd, 1);
int *pArray = GeraTabuleiro(8);
int x,y,r = 0;
for(x = 0; x < 3; x++)
{
for(y = 0; y < 3; y++)
{
iBoard[x][y] = pArray[r];
r++;
}
}
free(pArray);
bJogando = TRUE;
g_iSegundos++;
SetTimer(hwnd,1,1000,(TIMERPROC) NULL);
}
BOOL CALLBACK DlgAbout(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
return TRUE;
case WM_CLOSE:
{
DestroyWindow(hwnd);
}
return TRUE;
}
return FALSE;
}
void ShowGame(HDC hdc )
{
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP memBM = CreateCompatibleBitmap(hdc,340 ,250);
SelectObject(memDC,memBM);
HGDIOBJ hGDI_Obj = SelectObject(memDC,g_hFundoPreto);
Rectangle(memDC,15,20,340,260);
SelectObject(memDC,hGDI_Obj);
MostraBoard(memDC,POSX_BOARD - 15,POSY_BOARD - 20);
hGDI_Obj = SelectObject(memDC,g_hFonte2);
SetBkMode(memDC, TRANSPARENT);
COLORREF CorOriginal = SetTextColor(memDC,RGB(255,255,255));
TextOut(memDC,230,40,g_wcTempo,lstrlenW(g_wcTempo));
SetTextColor(memDC,CorOriginal);
SelectObject(memDC,hGDI_Obj);
BitBlt(hdc,15,20,340,250,memDC,0,0,SRCCOPY);
DeleteDC(memDC);
DeleteObject(memBM);
}
BOOL CALLBACK DlgMain(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
g_hBrushFundoBoard = CreateSolidBrush(RGB(150,150,150));
g_hBrushInativoDesbloqueado = CreateSolidBrush(RGB(255,255,255));
g_hBrushSelecionado = CreateSolidBrush(RGB(100,255,100));
g_hBrushBloqueado = CreateSolidBrush(RGB(100,100,255));
g_hFundoPreto = CreateSolidBrush(RGB(0,0,0));
g_hPen1 = CreatePen( PS_SOLID,2,RGB(255,255,255));
wsprintf(g_wcTempo,L"Tempo: %.2d:%.2d",0,0);
IniciaBoard();
g_hFonte1 = CreateFont(35,0,0,0,FW_DONTCARE,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, VARIABLE_PITCH,/*L"Times New Roman"*/L"Stencil");
g_hFonte2 = CreateFont(15,0,0,0,FW_DONTCARE,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY, VARIABLE_PITCH,/*L"Times New Roman"*/L"Stencil");
SetMenu(hwnd,g_hMenu);
}
return TRUE;
case WM_TIMER:
switch (wParam)
{
case 1:
{
wsprintf(g_wcTempo,L"Tempo: %.2d:%.2d",g_iMinutos,g_iSegundos);
g_iSegundos++;
if(g_iSegundos >= 60)
{
g_iSegundos = 0;
g_iMinutos++;
}
// melhor prevenir...
if(g_iMinutos >= 60)
{
g_iSegundos = 0;
g_iMinutos = 0;
}
HDC hdc = GetDC(hwnd);
ShowGame( hdc );
ReleaseDC(hwnd,hdc);
}
return 0;
}
return TRUE;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd,&ps);
ShowGame( hdc );
EndPaint(hwnd,&ps);
}
return TRUE;
case WM_LBUTTONDOWN:
{
g_xPosMouse = GET_X_LPARAM(lParam);
g_yPosMouse = GET_Y_LPARAM(lParam);
HDC hdc = GetDC(hwnd);
if(bJogando)
MoveBloco(hwnd);
ShowGame( hdc );
ReleaseDC(hwnd,hdc);
}
return TRUE;
case WM_MOUSEMOVE:
{
g_xPosMouse = GET_X_LPARAM(lParam);
g_yPosMouse = GET_Y_LPARAM(lParam);
HDC hdc = GetDC(hwnd);
ShowGame( hdc );
ReleaseDC(hwnd,hdc);
}
return TRUE;
case WM_CLOSE:
{
DeleteObject(g_hBrushFundoBoard);
DeleteObject(g_hBrushInativoDesbloqueado);
DeleteObject(g_hBrushSelecionado);
DeleteObject(g_hBrushBloqueado);
DeleteObject(g_hFundoPreto);
DeleteObject(g_hFonte1);
DeleteObject(g_hFonte2);
DeleteObject(g_hPen1);
DestroyMenu(g_hMenu);
KillTimer(hwnd, 1);
EndDialog(hwnd, 0);
}
return TRUE;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDM_NOVO_JOGO1:
NovoJogo(hwnd);
break;
case IDM_SAIR1:
SendMessage(hwnd,WM_CLOSE,0,0);
break;
case IDM_SOBRE_O_AUTOR1:
DialogBox(g_hInst, MAKEINTRESOURCE(IDD_DIALOG2), NULL, (DLGPROC)DlgAbout);
break;
}
}
return TRUE;
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
g_hInst = hInstance;
InitCommonControls();
g_hMenu = LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU1));
srand(time(NULL));
return DialogBox(g_hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DlgMain);
}