4 * Copyright 2000 Joshua Thielen <jt85296@ltu.edu>
5 * To be distributed under the Wine License
17 /* Work around a Wine bug which defines handles as UINT rather than LPVOID */
19 #define NULL_HANDLE NULL
26 #define DEBUG(x) fprintf(stderr,x)
31 int WINAPI
WinMain( HINSTANCE hInst
, HINSTANCE hPrevInst
, LPSTR cmdline
, int cmdshow
)
39 LoadString( hInst
, IDS_APPNAME
, appname
, sizeof(appname
));
42 wc
.lpfnWndProc
= MainProc
;
46 wc
.hIcon
= LoadIcon( hInst
, appname
);
47 wc
.hCursor
= LoadCursor( NULL_HANDLE
, IDI_APPLICATION
);
48 wc
.hbrBackground
= (HBRUSH
) GetStockObject( BLACK_BRUSH
);
49 wc
.lpszMenuName
= "MENU_WINEMINE";
50 wc
.lpszClassName
= appname
;
52 if (!RegisterClass(&wc
)) exit(1);
53 hWnd
= CreateWindow( appname
, appname
,
54 WS_OVERLAPPEDWINDOW
& ~WS_THICKFRAME
& ~WS_MAXIMIZEBOX
,
55 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
56 NULL_HANDLE
, NULL_HANDLE
, hInst
, NULL
);
60 ShowWindow( hWnd
, cmdshow
);
63 haccel
= LoadAccelerators( hInst
, appname
);
64 SetTimer( hWnd
, ID_TIMER
, 1000, NULL
);
66 while( GetMessage(&msg
, NULL_HANDLE
, 0, 0) ) {
67 if (!TranslateAccelerator( hWnd
, haccel
, &msg
))
68 TranslateMessage( &msg
);
70 DispatchMessage( &msg
);
75 LRESULT WINAPI
MainProc( HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
84 board
.hInst
= ((LPCREATESTRUCT
) lParam
)->hInstance
;
87 CreateBoard( &board
);
95 hdc
= BeginPaint( hWnd
, &ps
);
96 hMemDC
= CreateCompatibleDC( hdc
);
98 DrawBoard( hdc
, hMemDC
, &ps
, &board
);
101 EndPaint( hWnd
, &ps
);
108 board
.pos
.x
= (unsigned) LOWORD(lParam
);
109 board
.pos
.y
= (unsigned) HIWORD(lParam
);
114 DestroyBoard( &board
);
115 PostQuitMessage( 0 );
119 if( board
.status
== PLAYING
) {
121 RedrawWindow( hWnd
, &board
.timer_rect
, NULL_HANDLE
,
122 RDW_INVALIDATE
| RDW_UPDATENOW
);
127 DEBUG("WM_LBUTTONDOWN\n");
128 if( wParam
& MK_RBUTTON
)
129 msg
= WM_MBUTTONDOWN
;
130 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
135 DEBUG("WM_LBUTTONUP\n");
136 if( wParam
& MK_RBUTTON
)
138 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
143 DEBUG("WM_RBUTTONDOWN\n");
144 if( wParam
& MK_LBUTTON
) {
147 msg
= WM_MBUTTONDOWN
;
149 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
153 DEBUG("WM_RBUTTONUP\n");
154 if( wParam
& MK_LBUTTON
)
156 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
161 if( (wParam
& MK_LBUTTON
) && (wParam
& MK_RBUTTON
) ) {
162 msg
= WM_MBUTTONDOWN
;
164 else if( wParam
& MK_LBUTTON
) {
165 msg
= WM_LBUTTONDOWN
;
171 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
177 switch(LOWORD(wParam
)) {
179 CreateBoard( &board
);
183 hMenu
= GetMenu( hWnd
);
184 board
.IsMarkQ
= !board
.IsMarkQ
;
186 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_CHECKED
);
188 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_UNCHECKED
);
192 SetDifficulty( &board
, BEGINNER
);
193 CreateBoard( &board
);
197 SetDifficulty( &board
, ADVANCED
);
198 CreateBoard( &board
);
202 SetDifficulty( &board
, EXPERT
);
203 CreateBoard( &board
);
207 SetDifficulty( &board
, CUSTOM
);
208 CreateBoard( &board
);
212 SendMessage( hWnd
, WM_CLOSE
, 0, 0);
216 DialogBoxParam( board
.hInst
, "DLG_TIMES", hWnd
,
217 TimesDlgProc
, (LPARAM
) &board
);
221 DialogBox( board
.hInst
, "DLG_ABOUT", hWnd
, AboutDlgProc
);
224 DEBUG("Unknown WM_COMMAND command message received\n");
228 return( DefWindowProc( hWnd
, msg
, wParam
, lParam
));
231 void InitBoard( BOARD
*p_board
)
235 p_board
->hMinesBMP
= LoadBitmap( p_board
->hInst
, "mines");
236 p_board
->hFacesBMP
= LoadBitmap( p_board
->hInst
, "faces");
237 p_board
->hLedsBMP
= LoadBitmap( p_board
->hInst
, "leds");
239 LoadBoard( p_board
);
241 if( p_board
->pos
.x
< (unsigned) GetSystemMetrics( SM_CXFIXEDFRAME
))
242 p_board
->pos
.x
= GetSystemMetrics( SM_CXFIXEDFRAME
);
244 if( p_board
->pos
.x
> (unsigned) (GetSystemMetrics( SM_CXSCREEN
)
245 - GetSystemMetrics( SM_CXFIXEDFRAME
))) {
246 p_board
->pos
.x
= GetSystemMetrics( SM_CXSCREEN
)
247 - GetSystemMetrics( SM_CXFIXEDFRAME
);
250 if( p_board
->pos
.y
< (unsigned) (GetSystemMetrics( SM_CYMENU
)
251 + GetSystemMetrics( SM_CYCAPTION
)
252 + GetSystemMetrics( SM_CYFIXEDFRAME
))) {
253 p_board
->pos
.y
= GetSystemMetrics( SM_CYMENU
) +
254 GetSystemMetrics( SM_CYCAPTION
) +
255 GetSystemMetrics( SM_CYFIXEDFRAME
);
258 if( p_board
->pos
.y
> (unsigned) (GetSystemMetrics( SM_CYSCREEN
)
259 - GetSystemMetrics( SM_CYFIXEDFRAME
))) {
260 p_board
->pos
.y
= GetSystemMetrics( SM_CYSCREEN
)
261 - GetSystemMetrics( SM_CYFIXEDFRAME
);
264 hMenu
= GetMenu( p_board
->hWnd
);
265 CheckMenuItem( hMenu
, IDM_BEGINNER
+ (unsigned) p_board
->difficulty
,
267 if( p_board
->IsMarkQ
)
268 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_CHECKED
);
270 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_UNCHECKED
);
271 CheckLevel( p_board
);
274 void LoadBoard( BOARD
*p_board
)
284 RegOpenKeyEx( HKEY_LOCAL_MACHINE
, "Software\\Wine\\WineMine",
285 0, KEY_QUERY_VALUE
, &hkey
);
287 size
= sizeof( data
);
288 if( RegQueryValueEx( hkey
, "Xpos", NULL
, (LPDWORD
) &type
,
289 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
) {
290 p_board
->pos
.x
= atoi( data
);
293 p_board
->pos
.x
= GetSystemMetrics( SM_CXFIXEDFRAME
);
295 size
= sizeof( data
);
296 if( RegQueryValueEx( hkey
, "Ypos", NULL
, (LPDWORD
) &type
,
297 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
298 p_board
->pos
.y
= atoi( data
);
300 p_board
->pos
.y
= GetSystemMetrics( SM_CYMENU
)
301 + GetSystemMetrics( SM_CYCAPTION
)
302 + GetSystemMetrics( SM_CYFIXEDFRAME
);
304 size
= sizeof( data
);
305 if( RegQueryValueEx( hkey
, "Rows", NULL
, (LPDWORD
) &type
,
306 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
307 p_board
->rows
= atoi( data
);
309 p_board
->rows
= BEGINNER_ROWS
;
311 size
= sizeof( data
);
312 if( RegQueryValueEx( hkey
, "Cols", NULL
, (LPDWORD
) &type
,
313 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
314 p_board
->cols
= atoi( data
);
316 p_board
->cols
= BEGINNER_COLS
;
318 size
= sizeof( data
);
319 if( RegQueryValueEx( hkey
, "Mines", NULL
, (LPDWORD
) &type
,
320 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
321 p_board
->mines
= atoi( data
);
323 p_board
->rows
= BEGINNER_ROWS
;
325 size
= sizeof( data
);
326 if( RegQueryValueEx( hkey
, "Difficulty", NULL
, (LPDWORD
) &type
,
327 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
328 p_board
->difficulty
= (DIFFICULTY
) atoi( data
);
330 p_board
->difficulty
= BEGINNER
;
332 size
= sizeof( data
);
333 if( RegQueryValueEx( hkey
, "MarkQ", NULL
, (LPDWORD
) &type
,
334 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
335 p_board
->IsMarkQ
= atoi( data
);
337 p_board
->IsMarkQ
= TRUE
;
339 for( i
= 0; i
< 3; i
++ ) {
340 wsprintf( key_name
, "Name%d", i
);
341 size
= sizeof( data
);
342 if( RegQueryValueEx( hkey
, key_name
, NULL
, (LPDWORD
) &type
,
344 (LPDWORD
) &size
) == ERROR_SUCCESS
)
345 strncpy( p_board
->best_name
[i
], data
, sizeof( data
) );
347 wsprintf( p_board
->best_name
[i
], "Nobody");
350 for( i
= 0; i
< 3; i
++ ) {
351 wsprintf( key_name
, "Time%d", i
);
352 size
= sizeof( data
);
353 if( RegQueryValueEx( hkey
, key_name
, NULL
, (LPDWORD
) &type
,
355 (LPDWORD
) &size
) == ERROR_SUCCESS
)
356 p_board
->best_time
[i
] = atoi( data
);
358 p_board
->best_time
[i
] = 999;
363 void SaveBoard( BOARD
*p_board
)
367 SECURITY_ATTRIBUTES sa
;
372 if( RegCreateKeyEx( HKEY_LOCAL_MACHINE
,
373 "Software\\Wine\\WineMine", 0, NULL
,
374 REG_OPTION_NON_VOLATILE
, KEY_WRITE
, &sa
,
375 &hkey
, &disp
) != ERROR_SUCCESS
)
378 wsprintf( data
, "%d", p_board
->pos
.x
);
379 RegSetValueEx( hkey
, "Xpos", 0, REG_SZ
, (LPBYTE
) data
,
382 wsprintf( data
, "%d", p_board
->pos
.x
);
383 RegSetValueEx( hkey
, "Ypos", 0, REG_SZ
, (LPBYTE
) data
,
386 wsprintf( data
, "%d", (int) p_board
->difficulty
);
387 RegSetValueEx( hkey
, "Difficulty", 0, REG_SZ
, (LPBYTE
) data
,
390 wsprintf( data
, "%d", p_board
->rows
);
391 RegSetValueEx( hkey
, "Rows", 0, REG_SZ
, (LPBYTE
) data
,
394 wsprintf( data
, "%d", p_board
->cols
);
395 RegSetValueEx( hkey
, "Cols", 0, REG_SZ
, (LPBYTE
) data
,
398 wsprintf( data
, "%d", p_board
->mines
);
399 RegSetValueEx( hkey
, "Mines", 0, REG_SZ
, (LPBYTE
) data
,
402 wsprintf( data
, "%d", (int) p_board
->IsMarkQ
);
403 RegSetValueEx( hkey
, "MarkQ", 0, REG_SZ
, (LPBYTE
) data
,
406 for( i
= 0; i
< 3; i
++ ) {
407 wsprintf( key_name
, "Name%u", i
);
408 strncpy( data
, p_board
->best_name
[i
], sizeof( data
) );
409 RegSetValueEx( hkey
, key_name
, 0, REG_SZ
, (LPBYTE
) data
,
413 for( i
= 0; i
< 3; i
++ ) {
414 wsprintf( key_name
, "Time%u", i
);
415 wsprintf( data
, "%d", p_board
->best_time
[i
] );
416 RegSetValueEx( hkey
, key_name
, 0, REG_SZ
, (LPBYTE
) data
,
422 void DestroyBoard( BOARD
*p_board
)
424 DeleteObject( p_board
->hFacesBMP
);
425 DeleteObject( p_board
->hLedsBMP
);
426 DeleteObject( p_board
->hMinesBMP
);
429 void SetDifficulty( BOARD
*p_board
, DIFFICULTY difficulty
)
431 HMENU hMenu
= GetMenu( p_board
->hWnd
);
433 CheckMenuItem( hMenu
, IDM_BEGINNER
+ p_board
->difficulty
, MF_UNCHECKED
);
434 p_board
->difficulty
= difficulty
;
435 CheckMenuItem( hMenu
, IDM_BEGINNER
+ difficulty
, MF_CHECKED
);
437 switch( difficulty
) {
439 p_board
->cols
= BEGINNER_COLS
;
440 p_board
->rows
= BEGINNER_ROWS
;
441 p_board
->mines
= BEGINNER_MINES
;
445 p_board
->cols
= ADVANCED_COLS
;
446 p_board
->rows
= ADVANCED_ROWS
;
447 p_board
->mines
= ADVANCED_MINES
;
451 p_board
->cols
= EXPERT_COLS
;
452 p_board
->rows
= EXPERT_ROWS
;
453 p_board
->mines
= EXPERT_MINES
;
457 DialogBoxParam( p_board
->hInst
, "DLG_CUSTOM", p_board
->hWnd
,
458 CustomDlgProc
, (LPARAM
) p_board
);
463 void CreateBoard( BOARD
*p_board
)
465 int left
, top
, bottom
, right
, wnd_x
, wnd_y
, wnd_width
, wnd_height
;
467 p_board
->mb
= MB_NONE
;
468 p_board
->boxes_left
= p_board
->cols
* p_board
->rows
- p_board
->mines
;
469 p_board
->num_flags
= 0;
471 CreateBoxes( p_board
);
473 p_board
->width
= p_board
->cols
* MINE_WIDTH
+ BOARD_WMARGIN
* 2;
475 p_board
->height
= p_board
->rows
* MINE_HEIGHT
+ LED_HEIGHT
478 wnd_x
= p_board
->pos
.x
- GetSystemMetrics( SM_CXFIXEDFRAME
);
479 wnd_y
= p_board
->pos
.y
- GetSystemMetrics( SM_CYMENU
)
480 - GetSystemMetrics( SM_CYCAPTION
)
481 - GetSystemMetrics( SM_CYFIXEDFRAME
);
482 wnd_width
= p_board
->width
483 + GetSystemMetrics( SM_CXFIXEDFRAME
) * 2;
484 wnd_height
= p_board
->height
485 + GetSystemMetrics( SM_CYMENU
)
486 + GetSystemMetrics( SM_CYCAPTION
)
487 + GetSystemMetrics( SM_CYFIXEDFRAME
) * 2;
489 /* setting the mines rectangle boundary */
490 left
= BOARD_WMARGIN
;
491 top
= BOARD_HMARGIN
* 2 + LED_HEIGHT
;
492 right
= left
+ p_board
->cols
* MINE_WIDTH
;
493 bottom
= top
+ p_board
->rows
* MINE_HEIGHT
;
494 SetRect( &p_board
->mines_rect
, left
, top
, right
, bottom
);
496 /* setting the face rectangle boundary */
497 left
= p_board
->width
/ 2 - FACE_WIDTH
/ 2;
499 right
= left
+ FACE_WIDTH
;
500 bottom
= top
+ FACE_HEIGHT
;
501 SetRect( &p_board
->face_rect
, left
, top
, right
, bottom
);
503 /* setting the timer rectangle boundary */
504 left
= BOARD_WMARGIN
;
506 right
= left
+ LED_WIDTH
* 3;
507 bottom
= top
+ LED_HEIGHT
;
508 SetRect( &p_board
->timer_rect
, left
, top
, right
, bottom
);
510 /* setting the counter rectangle boundary */
511 left
= p_board
->width
- BOARD_WMARGIN
- LED_WIDTH
* 3;
513 right
= p_board
->width
- BOARD_WMARGIN
;
514 bottom
= top
+ LED_HEIGHT
;
515 SetRect( &p_board
->counter_rect
, left
, top
, right
, bottom
);
517 p_board
->status
= WAITING
;
518 p_board
->face_bmp
= SMILE_BMP
;
521 MoveWindow( p_board
->hWnd
, wnd_x
, wnd_y
, wnd_width
, wnd_height
, TRUE
);
522 RedrawWindow( p_board
->hWnd
, NULL
, NULL_HANDLE
,
523 RDW_INVALIDATE
| RDW_UPDATENOW
| RDW_ERASE
);
527 void CheckLevel( BOARD
*p_board
)
529 if( p_board
->rows
< BEGINNER_ROWS
)
530 p_board
->rows
= BEGINNER_ROWS
;
532 if( p_board
->rows
> MAX_ROWS
)
533 p_board
->rows
= MAX_ROWS
;
535 if( p_board
->cols
< BEGINNER_COLS
)
536 p_board
->cols
= BEGINNER_COLS
;
538 if( p_board
->cols
> MAX_COLS
)
539 p_board
->cols
= MAX_COLS
;
541 if( p_board
->mines
< BEGINNER_MINES
)
542 p_board
->mines
= BEGINNER_MINES
;
544 if( p_board
->mines
> p_board
->cols
* p_board
->rows
- 1 )
545 p_board
->mines
= p_board
->cols
* p_board
->rows
- 1;
549 void CreateBoxes( BOARD
*p_board
)
554 srand( (unsigned) time( NULL
) );
556 /* Create the boxes...
557 * We actually create them with an empty border,
558 * so special care doesn't have to be taken on the edges
561 for( col
= 0; col
<= p_board
->cols
+ 1; col
++ )
562 for( row
= 0; row
<= p_board
->rows
+ 1; row
++ ) {
563 p_board
->box
[col
][row
].IsPressed
= FALSE
;
564 p_board
->box
[col
][row
].IsMine
= FALSE
;
565 p_board
->box
[col
][row
].FlagType
= NORMAL
;
566 p_board
->box
[col
][row
].NumMines
= 0;
571 while( (unsigned) i
< p_board
->mines
) {
572 col
= (int) (p_board
->cols
* (float) rand() / RAND_MAX
+ 1);
573 row
= (int) (p_board
->rows
* (float) rand() / RAND_MAX
+ 1);
575 if( !p_board
->box
[col
][row
].IsMine
) {
577 p_board
->box
[col
][row
].IsMine
= TRUE
;
582 * Now we label the remaining boxes with the
583 * number of mines surrounding them.
586 for( col
= 1; col
< p_board
->cols
+ 1; col
++ )
587 for( row
= 1; row
< p_board
->rows
+ 1; row
++ ) {
588 for( i
= -1; i
<= 1; i
++ )
589 for( j
= -1; j
<= 1; j
++ ) {
590 if( p_board
->box
[col
+ i
][row
+ j
].IsMine
) {
591 p_board
->box
[col
][row
].NumMines
++ ;
597 void DrawMines ( HDC hdc
, HDC hMemDC
, BOARD
*p_board
)
601 hOldObj
= SelectObject (hMemDC
, p_board
->hMinesBMP
);
603 for( row
= 1; row
<= p_board
->rows
; row
++ ) {
604 for( col
= 1; col
<= p_board
->cols
; col
++ ) {
605 DrawMine( hdc
, hMemDC
, p_board
, col
, row
, FALSE
);
608 SelectObject( hMemDC
, hOldObj
);
611 void DrawMine( HDC hdc
, HDC hMemDC
, BOARD
*p_board
, unsigned col
, unsigned row
, BOOL IsPressed
)
613 MINEBMP_OFFSET offset
= BOX_BMP
;
615 if( col
== 0 || col
> p_board
->cols
|| row
== 0 || row
> p_board
->rows
)
618 if( p_board
->status
== GAMEOVER
) {
619 if( p_board
->box
[col
][row
].IsMine
) {
620 switch( p_board
->box
[col
][row
].FlagType
) {
625 offset
= EXPLODE_BMP
;
631 switch( p_board
->box
[col
][row
].FlagType
) {
633 offset
= QUESTION_BMP
;
645 DEBUG("Unknown FlagType during game over in DrawMine\n");
649 } else { /* WAITING or PLAYING */
650 switch( p_board
->box
[col
][row
].FlagType
) {
653 offset
= QUESTION_BMP
;
670 DEBUG("Unknown FlagType while playing in DrawMine\n");
675 if( p_board
->box
[col
][row
].FlagType
== COMPLETE
676 && !p_board
->box
[col
][row
].IsMine
)
677 offset
= (MINEBMP_OFFSET
) p_board
->box
[col
][row
].NumMines
;
680 (col
- 1) * MINE_WIDTH
+ p_board
->mines_rect
.left
,
681 (row
- 1) * MINE_HEIGHT
+ p_board
->mines_rect
.top
,
682 MINE_WIDTH
, MINE_HEIGHT
,
683 hMemDC
, 0, offset
* MINE_HEIGHT
, SRCCOPY
);
686 void DrawLeds( HDC hdc
, HDC hMemDC
, BOARD
*p_board
, int number
, int x
, int y
)
695 led
[0] = count
/ 100 ;
696 count
-= led
[0] * 100;
699 led
[0] = 10; /* negative sign */
703 count
-= led
[1] * 10;
707 for( i
= 0; i
< 3; i
++ )
711 /* use unlit led if not playing */
712 if( p_board
->status
== WAITING
)
713 for( i
= 0; i
< 3; i
++ )
716 hOldObj
= SelectObject (hMemDC
, p_board
->hLedsBMP
);
718 for( i
= 0; i
< 3; i
++ ) {
730 SelectObject( hMemDC
, hOldObj
);
734 void DrawFace( HDC hdc
, HDC hMemDC
, BOARD
*p_board
)
738 hOldObj
= SelectObject (hMemDC
, p_board
->hFacesBMP
);
741 p_board
->face_rect
.left
,
742 p_board
->face_rect
.top
,
745 hMemDC
, 0, p_board
->face_bmp
* FACE_HEIGHT
, SRCCOPY
);
747 SelectObject( hMemDC
, hOldObj
);
751 void DrawBoard( HDC hdc
, HDC hMemDC
, PAINTSTRUCT
*ps
, BOARD
*p_board
)
755 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->counter_rect
) )
756 DrawLeds( hdc
, hMemDC
, p_board
, p_board
->mines
- p_board
->num_flags
,
757 p_board
->counter_rect
.left
,
758 p_board
->counter_rect
.top
);
760 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->timer_rect
) )
761 DrawLeds( hdc
, hMemDC
, p_board
, p_board
->time
,
762 p_board
->timer_rect
.left
,
763 p_board
->timer_rect
.top
);
765 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->face_rect
) )
766 DrawFace( hdc
, hMemDC
, p_board
);
768 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->mines_rect
) )
769 DrawMines( hdc
, hMemDC
, p_board
);
773 void TestBoard( HWND hWnd
, BOARD
*p_board
, unsigned x
, unsigned y
, int msg
)
780 if( PtInRect( &p_board
->mines_rect
, pt
) && p_board
->status
!= GAMEOVER
781 && p_board
->status
!= WON
)
782 TestMines( p_board
, pt
, msg
);
784 UnpressBoxes( p_board
,
787 p_board
->press
.x
= 0;
788 p_board
->press
.y
= 0;
791 if( p_board
->boxes_left
== 0 ) {
792 p_board
->status
= WON
;
794 if( p_board
->difficulty
!= CUSTOM
&&
795 p_board
->time
< p_board
->best_time
[p_board
->difficulty
] ) {
796 p_board
->best_time
[p_board
->difficulty
] = p_board
->time
;
798 DialogBoxParam( p_board
->hInst
, "DLG_CONGRATS", hWnd
,
799 CongratsDlgProc
, (LPARAM
) p_board
);
801 DialogBoxParam( p_board
->hInst
, "DLG_TIMES", hWnd
,
802 TimesDlgProc
, (LPARAM
) p_board
);
805 TestFace( p_board
, pt
, msg
);
808 void TestMines( BOARD
*p_board
, POINT pt
, int msg
)
813 col
= (pt
.x
- p_board
->mines_rect
.left
) / MINE_WIDTH
+ 1;
814 row
= (pt
.y
- p_board
->mines_rect
.top
) / MINE_HEIGHT
+ 1;
818 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
) {
820 p_board
->press
.x
, p_board
->press
.y
);
821 p_board
->press
.x
= col
;
822 p_board
->press
.y
= row
;
823 PressBox( p_board
, col
, row
);
829 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
)
831 p_board
->press
.x
, p_board
->press
.y
);
832 p_board
->press
.x
= 0;
833 p_board
->press
.y
= 0;
834 if( p_board
->box
[col
][row
].FlagType
!= FLAG
)
835 p_board
->status
= PLAYING
;
836 CompleteBox( p_board
, col
, row
);
840 PressBoxes( p_board
, col
, row
);
845 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
)
846 UnpressBoxes( p_board
,
847 p_board
->press
.x
, p_board
->press
.y
);
848 p_board
->press
.x
= 0;
849 p_board
->press
.y
= 0;
850 CompleteBoxes( p_board
, col
, row
);
854 AddFlag( p_board
, col
, row
);
855 p_board
->status
= PLAYING
;
858 DEBUG("Unknown message type received in TestMines\n");
864 RedrawWindow( p_board
->hWnd
, NULL
, NULL_HANDLE
,
865 RDW_INVALIDATE
| RDW_UPDATENOW
);
870 void TestFace( BOARD
*p_board
, POINT pt
, int msg
)
872 if( p_board
->status
== PLAYING
|| p_board
->status
== WAITING
) {
873 if( msg
== WM_LBUTTONDOWN
|| msg
== WM_MBUTTONDOWN
)
874 p_board
->face_bmp
= OOH_BMP
;
875 else p_board
->face_bmp
= SMILE_BMP
;
877 else if( p_board
->status
== GAMEOVER
)
878 p_board
->face_bmp
= DEAD_BMP
;
879 else if( p_board
->status
== WON
)
880 p_board
->face_bmp
= COOL_BMP
;
882 if( PtInRect( &p_board
->face_rect
, pt
) ) {
883 if( msg
== WM_LBUTTONDOWN
)
884 p_board
->face_bmp
= SPRESS_BMP
;
886 if( msg
== WM_LBUTTONUP
)
887 CreateBoard( p_board
);
890 RedrawWindow( p_board
->hWnd
, &p_board
->face_rect
, NULL_HANDLE
,
891 RDW_INVALIDATE
| RDW_UPDATENOW
);
895 void CompleteBox( BOARD
*p_board
, unsigned col
, unsigned row
)
899 if( p_board
->box
[col
][row
].FlagType
!= COMPLETE
&&
900 p_board
->box
[col
][row
].FlagType
!= FLAG
&&
901 col
> 0 && col
< p_board
->cols
+ 1 &&
902 row
> 0 && row
< p_board
->rows
+ 1 ) {
903 p_board
->box
[col
][row
].FlagType
= COMPLETE
;
905 if( p_board
->box
[col
][row
].IsMine
) {
906 p_board
->face_bmp
= DEAD_BMP
;
907 p_board
->status
= GAMEOVER
;
909 else if( p_board
->status
!= GAMEOVER
)
910 p_board
->boxes_left
--;
912 if( p_board
->box
[col
][row
].NumMines
== 0 )
914 for( i
= -1; i
<= 1; i
++ )
915 for( j
= -1; j
<= 1; j
++ )
916 CompleteBox( p_board
, col
+ i
, row
+ j
);
922 void CompleteBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
924 unsigned numFlags
= 0;
927 if( p_board
->box
[col
][row
].FlagType
== COMPLETE
) {
928 for( i
= -1; i
<= 1; i
++ )
929 for( j
= -1; j
<= 1; j
++ ) {
930 if( p_board
->box
[col
+i
][row
+j
].FlagType
== FLAG
)
934 if( numFlags
== p_board
->box
[col
][row
].NumMines
) {
935 for( i
= -1; i
<= 1; i
++ )
936 for( j
= -1; j
<= 1; j
++ ) {
937 if( p_board
->box
[col
+i
][row
+j
].FlagType
!= FLAG
)
938 CompleteBox( p_board
, col
+i
, row
+j
);
945 void AddFlag( BOARD
*p_board
, unsigned col
, unsigned row
)
947 if( p_board
->box
[col
][row
].FlagType
!= COMPLETE
) {
948 switch( p_board
->box
[col
][row
].FlagType
) {
950 if( p_board
->IsMarkQ
)
951 p_board
->box
[col
][row
].FlagType
= QUESTION
;
953 p_board
->box
[col
][row
].FlagType
= NORMAL
;
954 p_board
->num_flags
--;
958 p_board
->box
[col
][row
].FlagType
= NORMAL
;
962 p_board
->box
[col
][row
].FlagType
= FLAG
;
963 p_board
->num_flags
++;
969 void PressBox( BOARD
*p_board
, unsigned col
, unsigned row
)
975 hdc
= GetDC( p_board
->hWnd
);
976 hMemDC
= CreateCompatibleDC( hdc
);
977 hOldObj
= SelectObject (hMemDC
, p_board
->hMinesBMP
);
979 DrawMine( hdc
, hMemDC
, p_board
, col
, row
, TRUE
);
981 SelectObject( hMemDC
, hOldObj
);
983 ReleaseDC( p_board
->hWnd
, hdc
);
987 void PressBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
991 for( i
= -1; i
<= 1; i
++ )
992 for( j
= -1; j
<= 1; j
++ ) {
993 p_board
->box
[col
+ i
][row
+ j
].IsPressed
= TRUE
;
994 PressBox( p_board
, col
+ i
, row
+ j
);
997 for( i
= -1; i
<= 1; i
++ )
998 for( j
= -1; j
<= 1; j
++ ) {
999 if( !p_board
->box
[p_board
->press
.x
+ i
][p_board
->press
.y
+ j
].IsPressed
)
1000 UnpressBox( p_board
, p_board
->press
.x
+ i
, p_board
->press
.y
+ j
);
1003 for( i
= -1; i
<= 1; i
++ )
1004 for( j
= -1; j
<= 1; j
++ ) {
1005 p_board
->box
[col
+ i
][row
+ j
].IsPressed
= FALSE
;
1006 PressBox( p_board
, col
+ i
, row
+ j
);
1009 p_board
->press
.x
= col
;
1010 p_board
->press
.y
= row
;
1014 void UnpressBox( BOARD
*p_board
, unsigned col
, unsigned row
)
1020 hdc
= GetDC( p_board
->hWnd
);
1021 hMemDC
= CreateCompatibleDC( hdc
);
1022 hOldObj
= SelectObject( hMemDC
, p_board
->hMinesBMP
);
1024 DrawMine( hdc
, hMemDC
, p_board
, col
, row
, FALSE
);
1026 SelectObject( hMemDC
, hOldObj
);
1028 ReleaseDC( p_board
->hWnd
, hdc
);
1032 void UnpressBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
1036 for( i
= -1; i
<= 1; i
++ )
1037 for( j
= -1; j
<= 1; j
++ ) {
1038 UnpressBox( p_board
, col
+ i
, row
+ j
);