4 * Copyright 2000 Joshua Thielen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define WIN32_LEAN_AND_MEAN
30 #include <wine/debug.h>
32 WINE_DEFAULT_DEBUG_CHANNEL(winemine
);
34 static const DWORD wnd_style
= WS_OVERLAPPEDWINDOW
& ~WS_THICKFRAME
& ~WS_MAXIMIZEBOX
;
35 static const char* registry_key
= "Software\\Microsoft\\WinMine";
38 int WINAPI
WinMain( HINSTANCE hInst
, HINSTANCE hPrevInst
, LPSTR cmdline
, int cmdshow
)
46 LoadString( hInst
, IDS_APPNAME
, appname
, sizeof(appname
));
49 wc
.lpfnWndProc
= MainProc
;
53 wc
.hIcon
= LoadIcon( hInst
, "WINEMINE" );
54 wc
.hCursor
= LoadCursor( 0, IDI_APPLICATION
);
55 wc
.hbrBackground
= (HBRUSH
) GetStockObject( BLACK_BRUSH
);
56 wc
.lpszMenuName
= "MENU_WINEMINE";
57 wc
.lpszClassName
= appname
;
59 if (!RegisterClass(&wc
)) ExitProcess(1);
60 hWnd
= CreateWindow( appname
, appname
,
62 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
65 if (!hWnd
) ExitProcess(1);
67 ShowWindow( hWnd
, cmdshow
);
70 haccel
= LoadAccelerators( hInst
, MAKEINTRESOURCE(IDA_WINEMINE
) );
71 SetTimer( hWnd
, ID_TIMER
, 1000, NULL
);
73 while( GetMessage(&msg
, 0, 0, 0) ) {
74 if (!TranslateAccelerator( hWnd
, haccel
, &msg
))
75 TranslateMessage( &msg
);
77 DispatchMessage( &msg
);
82 LRESULT WINAPI
MainProc( HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
91 board
.hInst
= ((LPCREATESTRUCT
) lParam
)->hInstance
;
94 CreateBoard( &board
);
101 WINE_TRACE("WM_PAINT\n");
102 hdc
= BeginPaint( hWnd
, &ps
);
103 hMemDC
= CreateCompatibleDC( hdc
);
105 DrawBoard( hdc
, hMemDC
, &ps
, &board
);
108 EndPaint( hWnd
, &ps
);
114 WINE_TRACE("WM_MOVE\n");
115 board
.pos
.x
= (short)LOWORD(lParam
);
116 board
.pos
.y
= (short)HIWORD(lParam
);
121 DestroyBoard( &board
);
122 PostQuitMessage( 0 );
126 if( board
.status
== PLAYING
) {
128 RedrawWindow( hWnd
, &board
.timer_rect
, 0,
129 RDW_INVALIDATE
| RDW_UPDATENOW
);
134 WINE_TRACE("WM_LBUTTONDOWN\n");
135 if( wParam
& MK_RBUTTON
)
136 msg
= WM_MBUTTONDOWN
;
137 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
142 WINE_TRACE("WM_LBUTTONUP\n");
143 if( wParam
& MK_RBUTTON
)
145 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
150 WINE_TRACE("WM_RBUTTONDOWN\n");
151 if( wParam
& MK_LBUTTON
) {
154 msg
= WM_MBUTTONDOWN
;
156 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
160 WINE_TRACE("WM_RBUTTONUP\n");
161 if( wParam
& MK_LBUTTON
)
163 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
167 WINE_TRACE("WM_MBUTTONDOWN\n");
168 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
172 WINE_TRACE("WM_MBUTTONUP\n");
173 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
178 if( (wParam
& MK_LBUTTON
) && (wParam
& MK_RBUTTON
) ) {
179 msg
= WM_MBUTTONDOWN
;
181 else if( wParam
& MK_LBUTTON
) {
182 msg
= WM_LBUTTONDOWN
;
188 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
194 switch(LOWORD(wParam
)) {
196 CreateBoard( &board
);
200 hMenu
= GetMenu( hWnd
);
201 board
.IsMarkQ
= !board
.IsMarkQ
;
203 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_CHECKED
);
205 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_UNCHECKED
);
209 SetDifficulty( &board
, BEGINNER
);
210 CreateBoard( &board
);
214 SetDifficulty( &board
, ADVANCED
);
215 CreateBoard( &board
);
219 SetDifficulty( &board
, EXPERT
);
220 CreateBoard( &board
);
224 SetDifficulty( &board
, CUSTOM
);
225 CreateBoard( &board
);
229 SendMessage( hWnd
, WM_CLOSE
, 0, 0);
233 DialogBoxParam( board
.hInst
, "DLG_TIMES", hWnd
,
234 TimesDlgProc
, (LPARAM
) &board
);
238 DialogBox( board
.hInst
, "DLG_ABOUT", hWnd
, AboutDlgProc
);
241 WINE_TRACE("Unknown WM_COMMAND command message received\n");
245 return( DefWindowProc( hWnd
, msg
, wParam
, lParam
));
248 void InitBoard( BOARD
*p_board
)
252 p_board
->hMinesBMP
= LoadBitmap( p_board
->hInst
, "mines");
253 p_board
->hFacesBMP
= LoadBitmap( p_board
->hInst
, "faces");
254 p_board
->hLedsBMP
= LoadBitmap( p_board
->hInst
, "leds");
256 LoadBoard( p_board
);
258 hMenu
= GetMenu( p_board
->hWnd
);
259 CheckMenuItem( hMenu
, IDM_BEGINNER
+ (unsigned) p_board
->difficulty
,
261 if( p_board
->IsMarkQ
)
262 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_CHECKED
);
264 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_UNCHECKED
);
265 CheckLevel( p_board
);
268 void LoadBoard( BOARD
*p_board
)
277 RegOpenKeyEx( HKEY_CURRENT_USER
, registry_key
,
278 0, KEY_QUERY_VALUE
, &hkey
);
280 size
= sizeof( p_board
->pos
.x
);
281 if( !RegQueryValueEx( hkey
, "Xpos", NULL
, (LPDWORD
) &type
,
282 (LPBYTE
) &p_board
->pos
.x
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
285 size
= sizeof( p_board
->pos
.y
);
286 if( !RegQueryValueEx( hkey
, "Ypos", NULL
, (LPDWORD
) &type
,
287 (LPBYTE
) &p_board
->pos
.y
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
290 size
= sizeof( p_board
->rows
);
291 if( !RegQueryValueEx( hkey
, "Height", NULL
, (LPDWORD
) &type
,
292 (LPBYTE
) &p_board
->rows
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
293 p_board
->rows
= BEGINNER_ROWS
;
295 size
= sizeof( p_board
->cols
);
296 if( !RegQueryValueEx( hkey
, "Width", NULL
, (LPDWORD
) &type
,
297 (LPBYTE
) &p_board
->cols
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
298 p_board
->cols
= BEGINNER_COLS
;
300 size
= sizeof( p_board
->mines
);
301 if( !RegQueryValueEx( hkey
, "Mines", NULL
, (LPDWORD
) &type
,
302 (LPBYTE
) &p_board
->mines
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
303 p_board
->mines
= BEGINNER_MINES
;
305 size
= sizeof( p_board
->difficulty
);
306 if( !RegQueryValueEx( hkey
, "Difficulty", NULL
, (LPDWORD
) &type
,
307 (LPBYTE
) &p_board
->difficulty
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
308 p_board
->difficulty
= BEGINNER
;
310 size
= sizeof( p_board
->IsMarkQ
);
311 if( !RegQueryValueEx( hkey
, "Mark", NULL
, (LPDWORD
) &type
,
312 (LPBYTE
) &p_board
->IsMarkQ
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
313 p_board
->IsMarkQ
= TRUE
;
315 for( i
= 0; i
< 3; i
++ ) {
316 wsprintf( key_name
, "Name%d", i
+1 );
317 size
= sizeof( data
);
318 if( RegQueryValueEx( hkey
, key_name
, NULL
, (LPDWORD
) &type
,
320 (LPDWORD
) &size
) == ERROR_SUCCESS
)
321 lstrcpynA( p_board
->best_name
[i
], data
, sizeof(p_board
->best_name
[i
]) );
323 LoadString( p_board
->hInst
, IDS_NOBODY
, p_board
->best_name
[i
], 16 );
326 for( i
= 0; i
< 3; i
++ ) {
327 wsprintf( key_name
, "Time%d", i
+1 );
328 size
= sizeof( p_board
->best_time
[i
] );
329 if( !RegQueryValueEx( hkey
, key_name
, NULL
, (LPDWORD
) &type
,
330 (LPBYTE
) &p_board
->best_time
[i
],
331 (LPDWORD
) &size
) == ERROR_SUCCESS
)
332 p_board
->best_time
[i
] = 999;
337 void SaveBoard( BOARD
*p_board
)
344 if( RegCreateKeyEx( HKEY_CURRENT_USER
, registry_key
,
346 REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
,
347 &hkey
, NULL
) != ERROR_SUCCESS
)
350 RegSetValueEx( hkey
, "Xpos", 0, REG_DWORD
, (LPBYTE
) &p_board
->pos
.x
, sizeof(p_board
->pos
.x
) );
351 RegSetValueEx( hkey
, "Ypos", 0, REG_DWORD
, (LPBYTE
) &p_board
->pos
.y
, sizeof(p_board
->pos
.y
) );
352 RegSetValueEx( hkey
, "Difficulty", 0, REG_DWORD
, (LPBYTE
) &p_board
->difficulty
, sizeof(p_board
->difficulty
) );
353 RegSetValueEx( hkey
, "Height", 0, REG_DWORD
, (LPBYTE
) &p_board
->rows
, sizeof(p_board
->rows
) );
354 RegSetValueEx( hkey
, "Width", 0, REG_DWORD
, (LPBYTE
) &p_board
->cols
, sizeof(p_board
->cols
) );
355 RegSetValueEx( hkey
, "Mines", 0, REG_DWORD
, (LPBYTE
) &p_board
->mines
, sizeof(p_board
->mines
) );
356 RegSetValueEx( hkey
, "Mark", 0, REG_DWORD
, (LPBYTE
) &p_board
->IsMarkQ
, sizeof(p_board
->IsMarkQ
) );
358 for( i
= 0; i
< 3; i
++ ) {
359 wsprintf( key_name
, "Name%u", i
+1 );
360 lstrcpyn( data
, p_board
->best_name
[i
], sizeof( data
) );
361 RegSetValueEx( hkey
, key_name
, 0, REG_SZ
, (LPBYTE
) data
, strlen(data
)+1 );
364 for( i
= 0; i
< 3; i
++ ) {
365 wsprintf( key_name
, "Time%u", i
+1 );
366 RegSetValueEx( hkey
, key_name
, 0, REG_DWORD
, (LPBYTE
) &p_board
->best_time
[i
], sizeof(p_board
->best_time
[i
]) );
371 void DestroyBoard( BOARD
*p_board
)
373 DeleteObject( p_board
->hFacesBMP
);
374 DeleteObject( p_board
->hLedsBMP
);
375 DeleteObject( p_board
->hMinesBMP
);
378 void SetDifficulty( BOARD
*p_board
, DIFFICULTY difficulty
)
382 if ( difficulty
== CUSTOM
)
383 if (DialogBoxParam( p_board
->hInst
, "DLG_CUSTOM", p_board
->hWnd
,
384 CustomDlgProc
, (LPARAM
) p_board
) != 0)
387 hMenu
= GetMenu( p_board
->hWnd
);
388 CheckMenuItem( hMenu
, IDM_BEGINNER
+ p_board
->difficulty
, MF_UNCHECKED
);
389 p_board
->difficulty
= difficulty
;
390 CheckMenuItem( hMenu
, IDM_BEGINNER
+ difficulty
, MF_CHECKED
);
392 switch( difficulty
) {
394 p_board
->cols
= BEGINNER_COLS
;
395 p_board
->rows
= BEGINNER_ROWS
;
396 p_board
->mines
= BEGINNER_MINES
;
400 p_board
->cols
= ADVANCED_COLS
;
401 p_board
->rows
= ADVANCED_ROWS
;
402 p_board
->mines
= ADVANCED_MINES
;
406 p_board
->cols
= EXPERT_COLS
;
407 p_board
->rows
= EXPERT_ROWS
;
409 p_board
->mines
= EXPERT_MINES
;
417 static void ShiftBetween(LONG
* x
, LONG
* y
, LONG a
, LONG b
)
429 static void MoveOnScreen(RECT
* rect
)
434 /* find the nearest monitor ... */
435 hMonitor
= MonitorFromRect(rect
, MONITOR_DEFAULTTONEAREST
);
437 /* ... and move it into the work area (ie excluding task bar)*/
438 mi
.cbSize
= sizeof(mi
);
439 GetMonitorInfo(hMonitor
, &mi
);
441 ShiftBetween(&rect
->left
, &rect
->right
, mi
.rcWork
.left
, mi
.rcWork
.right
);
442 ShiftBetween(&rect
->top
, &rect
->bottom
, mi
.rcWork
.top
, mi
.rcWork
.bottom
);
445 void CreateBoard( BOARD
*p_board
)
447 int left
, top
, bottom
, right
;
450 p_board
->mb
= MB_NONE
;
451 p_board
->boxes_left
= p_board
->cols
* p_board
->rows
- p_board
->mines
;
452 p_board
->num_flags
= 0;
454 CreateBoxes( p_board
);
456 p_board
->width
= p_board
->cols
* MINE_WIDTH
+ BOARD_WMARGIN
* 2;
458 p_board
->height
= p_board
->rows
* MINE_HEIGHT
+ LED_HEIGHT
461 /* setting the mines rectangle boundary */
462 left
= BOARD_WMARGIN
;
463 top
= BOARD_HMARGIN
* 2 + LED_HEIGHT
;
464 right
= left
+ p_board
->cols
* MINE_WIDTH
;
465 bottom
= top
+ p_board
->rows
* MINE_HEIGHT
;
466 SetRect( &p_board
->mines_rect
, left
, top
, right
, bottom
);
468 /* setting the face rectangle boundary */
469 left
= p_board
->width
/ 2 - FACE_WIDTH
/ 2;
471 right
= left
+ FACE_WIDTH
;
472 bottom
= top
+ FACE_HEIGHT
;
473 SetRect( &p_board
->face_rect
, left
, top
, right
, bottom
);
475 /* setting the timer rectangle boundary */
476 left
= BOARD_WMARGIN
;
478 right
= left
+ LED_WIDTH
* 3;
479 bottom
= top
+ LED_HEIGHT
;
480 SetRect( &p_board
->timer_rect
, left
, top
, right
, bottom
);
482 /* setting the counter rectangle boundary */
483 left
= p_board
->width
- BOARD_WMARGIN
- LED_WIDTH
* 3;
485 right
= p_board
->width
- BOARD_WMARGIN
;
486 bottom
= top
+ LED_HEIGHT
;
487 SetRect( &p_board
->counter_rect
, left
, top
, right
, bottom
);
489 p_board
->status
= WAITING
;
490 p_board
->face_bmp
= SMILE_BMP
;
493 wnd_rect
.left
= p_board
->pos
.x
;
494 wnd_rect
.right
= p_board
->pos
.x
+ p_board
->width
;
495 wnd_rect
.top
= p_board
->pos
.y
;
496 wnd_rect
.bottom
= p_board
->pos
.y
+ p_board
->height
;
497 AdjustWindowRect(&wnd_rect
, wnd_style
, TRUE
);
499 /* Make sure the window is completely on the screen */
500 MoveOnScreen(&wnd_rect
);
501 MoveWindow( p_board
->hWnd
, wnd_rect
.left
, wnd_rect
.top
,
502 wnd_rect
.right
- wnd_rect
.left
,
503 wnd_rect
.bottom
- wnd_rect
.top
,
505 RedrawWindow( p_board
->hWnd
, NULL
, 0,
506 RDW_INVALIDATE
| RDW_UPDATENOW
| RDW_ERASE
);
510 void CheckLevel( BOARD
*p_board
)
512 if( p_board
->rows
< BEGINNER_ROWS
)
513 p_board
->rows
= BEGINNER_ROWS
;
515 if( p_board
->rows
> MAX_ROWS
)
516 p_board
->rows
= MAX_ROWS
;
518 if( p_board
->cols
< BEGINNER_COLS
)
519 p_board
->cols
= BEGINNER_COLS
;
521 if( p_board
->cols
> MAX_COLS
)
522 p_board
->cols
= MAX_COLS
;
524 if( p_board
->mines
< BEGINNER_MINES
)
525 p_board
->mines
= BEGINNER_MINES
;
527 if( p_board
->mines
> p_board
->cols
* p_board
->rows
- 1 )
528 p_board
->mines
= p_board
->cols
* p_board
->rows
- 1;
532 void CreateBoxes( BOARD
*p_board
)
537 srand( (unsigned) time( NULL
) );
539 /* Create the boxes...
540 * We actually create them with an empty border,
541 * so special care doesn't have to be taken on the edges
544 for( col
= 0; col
<= p_board
->cols
+ 1; col
++ )
545 for( row
= 0; row
<= p_board
->rows
+ 1; row
++ ) {
546 p_board
->box
[col
][row
].IsPressed
= FALSE
;
547 p_board
->box
[col
][row
].IsMine
= FALSE
;
548 p_board
->box
[col
][row
].FlagType
= NORMAL
;
549 p_board
->box
[col
][row
].NumMines
= 0;
554 while( (unsigned) i
< p_board
->mines
) {
555 col
= (int) (p_board
->cols
* (float) rand() / RAND_MAX
+ 1);
556 row
= (int) (p_board
->rows
* (float) rand() / RAND_MAX
+ 1);
558 if( !p_board
->box
[col
][row
].IsMine
) {
560 p_board
->box
[col
][row
].IsMine
= TRUE
;
565 * Now we label the remaining boxes with the
566 * number of mines surrounding them.
569 for( col
= 1; col
< p_board
->cols
+ 1; col
++ )
570 for( row
= 1; row
< p_board
->rows
+ 1; row
++ ) {
571 for( i
= -1; i
<= 1; i
++ )
572 for( j
= -1; j
<= 1; j
++ ) {
573 if( p_board
->box
[col
+ i
][row
+ j
].IsMine
) {
574 p_board
->box
[col
][row
].NumMines
++ ;
580 void DrawMines ( HDC hdc
, HDC hMemDC
, BOARD
*p_board
)
584 hOldObj
= SelectObject (hMemDC
, p_board
->hMinesBMP
);
586 for( row
= 1; row
<= p_board
->rows
; row
++ ) {
587 for( col
= 1; col
<= p_board
->cols
; col
++ ) {
588 DrawMine( hdc
, hMemDC
, p_board
, col
, row
, FALSE
);
591 SelectObject( hMemDC
, hOldObj
);
594 void DrawMine( HDC hdc
, HDC hMemDC
, BOARD
*p_board
, unsigned col
, unsigned row
, BOOL IsPressed
)
596 MINEBMP_OFFSET offset
= BOX_BMP
;
598 if( col
== 0 || col
> p_board
->cols
|| row
== 0 || row
> p_board
->rows
)
601 if( p_board
->status
== GAMEOVER
) {
602 if( p_board
->box
[col
][row
].IsMine
) {
603 switch( p_board
->box
[col
][row
].FlagType
) {
608 offset
= EXPLODE_BMP
;
616 switch( p_board
->box
[col
][row
].FlagType
) {
618 offset
= QUESTION_BMP
;
630 WINE_TRACE("Unknown FlagType during game over in DrawMine\n");
634 } else { /* WAITING or PLAYING */
635 switch( p_board
->box
[col
][row
].FlagType
) {
638 offset
= QUESTION_BMP
;
655 WINE_TRACE("Unknown FlagType while playing in DrawMine\n");
660 if( p_board
->box
[col
][row
].FlagType
== COMPLETE
661 && !p_board
->box
[col
][row
].IsMine
)
662 offset
= (MINEBMP_OFFSET
) p_board
->box
[col
][row
].NumMines
;
665 (col
- 1) * MINE_WIDTH
+ p_board
->mines_rect
.left
,
666 (row
- 1) * MINE_HEIGHT
+ p_board
->mines_rect
.top
,
667 MINE_WIDTH
, MINE_HEIGHT
,
668 hMemDC
, 0, offset
* MINE_HEIGHT
, SRCCOPY
);
671 void DrawLeds( HDC hdc
, HDC hMemDC
, BOARD
*p_board
, int number
, int x
, int y
)
680 led
[0] = count
/ 100 ;
681 count
-= led
[0] * 100;
684 led
[0] = 10; /* negative sign */
688 count
-= led
[1] * 10;
692 for( i
= 0; i
< 3; i
++ )
696 /* use unlit led if not playing */
697 if( p_board
->status
== WAITING
)
698 for( i
= 0; i
< 3; i
++ )
701 hOldObj
= SelectObject (hMemDC
, p_board
->hLedsBMP
);
703 for( i
= 0; i
< 3; i
++ ) {
715 SelectObject( hMemDC
, hOldObj
);
719 void DrawFace( HDC hdc
, HDC hMemDC
, BOARD
*p_board
)
723 hOldObj
= SelectObject (hMemDC
, p_board
->hFacesBMP
);
726 p_board
->face_rect
.left
,
727 p_board
->face_rect
.top
,
730 hMemDC
, 0, p_board
->face_bmp
* FACE_HEIGHT
, SRCCOPY
);
732 SelectObject( hMemDC
, hOldObj
);
736 void DrawBoard( HDC hdc
, HDC hMemDC
, PAINTSTRUCT
*ps
, BOARD
*p_board
)
740 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->counter_rect
) )
741 DrawLeds( hdc
, hMemDC
, p_board
, p_board
->mines
- p_board
->num_flags
,
742 p_board
->counter_rect
.left
,
743 p_board
->counter_rect
.top
);
745 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->timer_rect
) )
746 DrawLeds( hdc
, hMemDC
, p_board
, p_board
->time
,
747 p_board
->timer_rect
.left
,
748 p_board
->timer_rect
.top
);
750 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->face_rect
) )
751 DrawFace( hdc
, hMemDC
, p_board
);
753 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->mines_rect
) )
754 DrawMines( hdc
, hMemDC
, p_board
);
758 void TestBoard( HWND hWnd
, BOARD
*p_board
, int x
, int y
, int msg
)
766 if( PtInRect( &p_board
->mines_rect
, pt
) && p_board
->status
!= GAMEOVER
767 && p_board
->status
!= WON
)
768 TestMines( p_board
, pt
, msg
);
770 UnpressBoxes( p_board
,
773 p_board
->press
.x
= 0;
774 p_board
->press
.y
= 0;
777 if( p_board
->boxes_left
== 0 ) {
778 p_board
->status
= WON
;
780 if (p_board
->num_flags
< p_board
->mines
) {
781 for( row
= 1; row
<= p_board
->rows
; row
++ ) {
782 for( col
= 1; col
<= p_board
->cols
; col
++ ) {
783 if (p_board
->box
[col
][row
].IsMine
&& p_board
->box
[col
][row
].FlagType
!= FLAG
)
784 p_board
->box
[col
][row
].FlagType
= FLAG
;
788 p_board
->num_flags
= p_board
->mines
;
790 RedrawWindow( p_board
->hWnd
, NULL
, 0,
791 RDW_INVALIDATE
| RDW_UPDATENOW
);
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 WINE_TRACE("Unknown message type received in TestMines\n");
864 RedrawWindow( p_board
->hWnd
, NULL
, 0,
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
, 0,
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
);