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
31 #include <wine/debug.h>
33 WINE_DEFAULT_DEBUG_CHANNEL(winemine
);
35 static const DWORD wnd_style
= WS_OVERLAPPEDWINDOW
& ~WS_THICKFRAME
& ~WS_MAXIMIZEBOX
;
36 static const WCHAR registry_key
[] = {'S','o','f','t','w','a','r','e','\\',
37 'M','i','c','r','o','s','o','f','t','\\',
38 'W','i','n','M','i','n','e',0};
40 static const WCHAR xposW
[] = {'X','p','o','s',0};
41 static const WCHAR yposW
[] = {'Y','p','o','s',0};
42 static const WCHAR heightW
[] = {'H','e','i','g','h','t',0};
43 static const WCHAR widthW
[] = {'W','i','d','t','h',0};
44 static const WCHAR minesW
[] = {'M','i','n','e','s',0};
45 static const WCHAR difficultyW
[] = {'D','i','f','f','i','c','u','l','t','y',0};
46 static const WCHAR markW
[] = {'M','a','r','k',0};
47 static const WCHAR nameW
[] = {'N','a','m','e','%','u',0};
48 static const WCHAR timeW
[] = {'T','i','m','e','%','u',0};
50 void CheckLevel( BOARD
*p_board
)
52 if( p_board
->rows
< BEGINNER_ROWS
)
53 p_board
->rows
= BEGINNER_ROWS
;
55 if( p_board
->rows
> MAX_ROWS
)
56 p_board
->rows
= MAX_ROWS
;
58 if( p_board
->cols
< BEGINNER_COLS
)
59 p_board
->cols
= BEGINNER_COLS
;
61 if( p_board
->cols
> MAX_COLS
)
62 p_board
->cols
= MAX_COLS
;
64 if( p_board
->mines
< BEGINNER_MINES
)
65 p_board
->mines
= BEGINNER_MINES
;
67 if( p_board
->mines
> ( p_board
->cols
- 1 ) * ( p_board
->rows
- 1 ) )
68 p_board
->mines
= ( p_board
->cols
- 1 ) * ( p_board
->rows
- 1 );
71 static void LoadBoard( BOARD
*p_board
)
76 WCHAR data
[MAX_PLAYER_NAME_SIZE
+1];
80 RegOpenKeyExW( HKEY_CURRENT_USER
, registry_key
, 0, KEY_QUERY_VALUE
, &hkey
);
82 size
= sizeof( p_board
->pos
.x
);
83 if( !RegQueryValueExW( hkey
, xposW
, NULL
, &type
,
84 (LPBYTE
) &p_board
->pos
.x
, &size
) == ERROR_SUCCESS
)
87 size
= sizeof( p_board
->pos
.y
);
88 if( !RegQueryValueExW( hkey
, yposW
, NULL
, &type
,
89 (LPBYTE
) &p_board
->pos
.y
, &size
) == ERROR_SUCCESS
)
92 size
= sizeof( p_board
->rows
);
93 if( !RegQueryValueExW( hkey
, heightW
, NULL
, &type
,
94 (LPBYTE
) &p_board
->rows
, &size
) == ERROR_SUCCESS
)
95 p_board
->rows
= BEGINNER_ROWS
;
97 size
= sizeof( p_board
->cols
);
98 if( !RegQueryValueExW( hkey
, widthW
, NULL
, &type
,
99 (LPBYTE
) &p_board
->cols
, &size
) == ERROR_SUCCESS
)
100 p_board
->cols
= BEGINNER_COLS
;
102 size
= sizeof( p_board
->mines
);
103 if( !RegQueryValueExW( hkey
, minesW
, NULL
, &type
,
104 (LPBYTE
) &p_board
->mines
, &size
) == ERROR_SUCCESS
)
105 p_board
->mines
= BEGINNER_MINES
;
107 size
= sizeof( p_board
->difficulty
);
108 if( !RegQueryValueExW( hkey
, difficultyW
, NULL
, &type
,
109 (LPBYTE
) &p_board
->difficulty
, &size
) == ERROR_SUCCESS
)
110 p_board
->difficulty
= BEGINNER
;
112 size
= sizeof( p_board
->IsMarkQ
);
113 if( !RegQueryValueExW( hkey
, markW
, NULL
, &type
,
114 (LPBYTE
) &p_board
->IsMarkQ
, &size
) == ERROR_SUCCESS
)
115 p_board
->IsMarkQ
= TRUE
;
117 for( i
= 0; i
< 3; i
++ ) {
118 wsprintfW( key_name
, nameW
, i
+1 );
119 size
= sizeof( data
);
120 if( RegQueryValueExW( hkey
, key_name
, NULL
, &type
,
121 (LPBYTE
) data
, &size
) == ERROR_SUCCESS
)
122 lstrcpynW( p_board
->best_name
[i
], data
, sizeof(p_board
->best_name
[i
])/sizeof(WCHAR
) );
124 LoadStringW( p_board
->hInst
, IDS_NOBODY
, p_board
->best_name
[i
], MAX_PLAYER_NAME_SIZE
+1 );
127 for( i
= 0; i
< 3; i
++ ) {
128 wsprintfW( key_name
, timeW
, i
+1 );
129 size
= sizeof( p_board
->best_time
[i
] );
130 if( !RegQueryValueExW( hkey
, key_name
, NULL
, &type
,
131 (LPBYTE
) &p_board
->best_time
[i
], &size
) == ERROR_SUCCESS
)
132 p_board
->best_time
[i
] = 999;
137 static void InitBoard( BOARD
*p_board
)
141 p_board
->hMinesBMP
= LoadBitmapW( p_board
->hInst
, MAKEINTRESOURCEW(IDI_MINES
));
142 p_board
->hFacesBMP
= LoadBitmapW( p_board
->hInst
, MAKEINTRESOURCEW(IDI_FACES
));
143 p_board
->hLedsBMP
= LoadBitmapW( p_board
->hInst
, MAKEINTRESOURCEW(IDI_LEDS
));
145 LoadBoard( p_board
);
147 hMenu
= GetMenu( p_board
->hWnd
);
148 CheckMenuItem( hMenu
, IDM_BEGINNER
+ (unsigned) p_board
->difficulty
,
150 if( p_board
->IsMarkQ
)
151 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_CHECKED
);
153 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_UNCHECKED
);
154 CheckLevel( p_board
);
157 static void SaveBoard( BOARD
*p_board
)
161 WCHAR data
[MAX_PLAYER_NAME_SIZE
+1];
164 if( RegCreateKeyExW( HKEY_CURRENT_USER
, registry_key
,
166 REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
,
167 &hkey
, NULL
) != ERROR_SUCCESS
)
170 RegSetValueExW( hkey
, xposW
, 0, REG_DWORD
, (LPBYTE
) &p_board
->pos
.x
, sizeof(p_board
->pos
.x
) );
171 RegSetValueExW( hkey
, yposW
, 0, REG_DWORD
, (LPBYTE
) &p_board
->pos
.y
, sizeof(p_board
->pos
.y
) );
172 RegSetValueExW( hkey
, difficultyW
, 0, REG_DWORD
, (LPBYTE
) &p_board
->difficulty
, sizeof(p_board
->difficulty
) );
173 RegSetValueExW( hkey
, heightW
, 0, REG_DWORD
, (LPBYTE
) &p_board
->rows
, sizeof(p_board
->rows
) );
174 RegSetValueExW( hkey
, widthW
, 0, REG_DWORD
, (LPBYTE
) &p_board
->cols
, sizeof(p_board
->cols
) );
175 RegSetValueExW( hkey
, minesW
, 0, REG_DWORD
, (LPBYTE
) &p_board
->mines
, sizeof(p_board
->mines
) );
176 RegSetValueExW( hkey
, markW
, 0, REG_DWORD
, (LPBYTE
) &p_board
->IsMarkQ
, sizeof(p_board
->IsMarkQ
) );
178 for( i
= 0; i
< 3; i
++ ) {
179 wsprintfW( key_name
, nameW
, i
+1 );
180 lstrcpynW( data
, p_board
->best_name
[i
], sizeof(data
)/sizeof(WCHAR
) );
181 RegSetValueExW( hkey
, key_name
, 0, REG_SZ
, (LPBYTE
) data
, (lstrlenW(data
)+1) * sizeof(WCHAR
) );
184 for( i
= 0; i
< 3; i
++ ) {
185 wsprintfW( key_name
, timeW
, i
+1 );
186 RegSetValueExW( hkey
, key_name
, 0, REG_DWORD
, (LPBYTE
) &p_board
->best_time
[i
], sizeof(p_board
->best_time
[i
]) );
191 static void DestroyBoard( BOARD
*p_board
)
193 DeleteObject( p_board
->hFacesBMP
);
194 DeleteObject( p_board
->hLedsBMP
);
195 DeleteObject( p_board
->hMinesBMP
);
198 static void SetDifficulty( BOARD
*p_board
, DIFFICULTY difficulty
)
202 if ( difficulty
== CUSTOM
)
203 if (DialogBoxParamW( p_board
->hInst
, MAKEINTRESOURCEW(DLG_CUSTOM
), p_board
->hWnd
,
204 CustomDlgProc
, (LPARAM
) p_board
) != 0)
207 hMenu
= GetMenu( p_board
->hWnd
);
208 CheckMenuItem( hMenu
, IDM_BEGINNER
+ p_board
->difficulty
, MF_UNCHECKED
);
209 p_board
->difficulty
= difficulty
;
210 CheckMenuItem( hMenu
, IDM_BEGINNER
+ difficulty
, MF_CHECKED
);
212 switch( difficulty
) {
214 p_board
->cols
= BEGINNER_COLS
;
215 p_board
->rows
= BEGINNER_ROWS
;
216 p_board
->mines
= BEGINNER_MINES
;
220 p_board
->cols
= ADVANCED_COLS
;
221 p_board
->rows
= ADVANCED_ROWS
;
222 p_board
->mines
= ADVANCED_MINES
;
226 p_board
->cols
= EXPERT_COLS
;
227 p_board
->rows
= EXPERT_ROWS
;
229 p_board
->mines
= EXPERT_MINES
;
237 static void ShiftBetween(LONG
* x
, LONG
* y
, LONG a
, LONG b
)
249 static void MoveOnScreen(RECT
* rect
)
254 /* find the nearest monitor ... */
255 hMonitor
= MonitorFromRect(rect
, MONITOR_DEFAULTTONEAREST
);
257 /* ... and move it into the work area (ie excluding task bar)*/
258 mi
.cbSize
= sizeof(mi
);
259 GetMonitorInfoW(hMonitor
, &mi
);
261 ShiftBetween(&rect
->left
, &rect
->right
, mi
.rcWork
.left
, mi
.rcWork
.right
);
262 ShiftBetween(&rect
->top
, &rect
->bottom
, mi
.rcWork
.top
, mi
.rcWork
.bottom
);
265 static void CreateBoard( BOARD
*p_board
)
267 int left
, top
, bottom
, right
;
271 p_board
->mb
= MB_NONE
;
272 p_board
->boxes_left
= p_board
->cols
* p_board
->rows
- p_board
->mines
;
273 p_board
->num_flags
= 0;
275 /* Create the boxes...
276 * We actually create them with an empty border,
277 * so special care doesn't have to be taken on the edges
279 for( col
= 0; col
<= p_board
->cols
+ 1; col
++ )
280 for( row
= 0; row
<= p_board
->rows
+ 1; row
++ ) {
281 p_board
->box
[col
][row
].IsPressed
= FALSE
;
282 p_board
->box
[col
][row
].IsMine
= FALSE
;
283 p_board
->box
[col
][row
].FlagType
= NORMAL
;
284 p_board
->box
[col
][row
].NumMines
= 0;
287 p_board
->width
= p_board
->cols
* MINE_WIDTH
+ BOARD_WMARGIN
* 2;
289 p_board
->height
= p_board
->rows
* MINE_HEIGHT
+ LED_HEIGHT
292 /* setting the mines rectangle boundary */
293 left
= BOARD_WMARGIN
;
294 top
= BOARD_HMARGIN
* 2 + LED_HEIGHT
;
295 right
= left
+ p_board
->cols
* MINE_WIDTH
;
296 bottom
= top
+ p_board
->rows
* MINE_HEIGHT
;
297 SetRect( &p_board
->mines_rect
, left
, top
, right
, bottom
);
299 /* setting the face rectangle boundary */
300 left
= p_board
->width
/ 2 - FACE_WIDTH
/ 2;
302 right
= left
+ FACE_WIDTH
;
303 bottom
= top
+ FACE_HEIGHT
;
304 SetRect( &p_board
->face_rect
, left
, top
, right
, bottom
);
306 /* setting the timer rectangle boundary */
307 left
= BOARD_WMARGIN
;
309 right
= left
+ LED_WIDTH
* 3;
310 bottom
= top
+ LED_HEIGHT
;
311 SetRect( &p_board
->timer_rect
, left
, top
, right
, bottom
);
313 /* setting the counter rectangle boundary */
314 left
= p_board
->width
- BOARD_WMARGIN
- LED_WIDTH
* 3;
316 right
= p_board
->width
- BOARD_WMARGIN
;
317 bottom
= top
+ LED_HEIGHT
;
318 SetRect( &p_board
->counter_rect
, left
, top
, right
, bottom
);
320 p_board
->status
= WAITING
;
321 p_board
->face_bmp
= SMILE_BMP
;
324 wnd_rect
.left
= p_board
->pos
.x
;
325 wnd_rect
.right
= p_board
->pos
.x
+ p_board
->width
;
326 wnd_rect
.top
= p_board
->pos
.y
;
327 wnd_rect
.bottom
= p_board
->pos
.y
+ p_board
->height
;
328 AdjustWindowRect(&wnd_rect
, wnd_style
, TRUE
);
330 /* Make sure the window is completely on the screen */
331 MoveOnScreen(&wnd_rect
);
332 MoveWindow( p_board
->hWnd
, wnd_rect
.left
, wnd_rect
.top
,
333 wnd_rect
.right
- wnd_rect
.left
,
334 wnd_rect
.bottom
- wnd_rect
.top
,
336 RedrawWindow( p_board
->hWnd
, NULL
, 0,
337 RDW_INVALIDATE
| RDW_UPDATENOW
| RDW_ERASE
);
341 /* Randomly places mines everywhere except the selected box. */
342 static void PlaceMines ( BOARD
*p_board
, int selected_col
, int selected_row
)
347 srand( (unsigned) time( NULL
) );
349 /* Temporarily place a mine at the selected box until all the other
350 * mines are placed, this avoids checking in the mine creation loop. */
351 p_board
->box
[selected_col
][selected_row
].IsMine
= TRUE
;
355 while( (unsigned) i
< p_board
->mines
) {
356 col
= (int) (p_board
->cols
* (float) rand() / RAND_MAX
+ 1);
357 row
= (int) (p_board
->rows
* (float) rand() / RAND_MAX
+ 1);
359 if( !p_board
->box
[col
][row
].IsMine
) {
361 p_board
->box
[col
][row
].IsMine
= TRUE
;
365 /* Remove temporarily placed mine for selected box */
366 p_board
->box
[selected_col
][selected_row
].IsMine
= FALSE
;
369 * Now we label the remaining boxes with the
370 * number of mines surrounding them.
372 for( col
= 1; col
< p_board
->cols
+ 1; col
++ )
373 for( row
= 1; row
< p_board
->rows
+ 1; row
++ ) {
374 for( i
= -1; i
<= 1; i
++ )
375 for( j
= -1; j
<= 1; j
++ ) {
376 if( p_board
->box
[col
+ i
][row
+ j
].IsMine
) {
377 p_board
->box
[col
][row
].NumMines
++ ;
383 static void DrawMine( HDC hdc
, HDC hMemDC
, BOARD
*p_board
, unsigned col
, unsigned row
, BOOL IsPressed
)
385 MINEBMP_OFFSET offset
= BOX_BMP
;
387 if( col
== 0 || col
> p_board
->cols
|| row
== 0 || row
> p_board
->rows
)
390 if( p_board
->status
== GAMEOVER
) {
391 if( p_board
->box
[col
][row
].IsMine
) {
392 switch( p_board
->box
[col
][row
].FlagType
) {
397 offset
= EXPLODE_BMP
;
405 switch( p_board
->box
[col
][row
].FlagType
) {
407 offset
= QUESTION_BMP
;
419 WINE_TRACE("Unknown FlagType during game over in DrawMine\n");
423 } else { /* WAITING or PLAYING */
424 switch( p_board
->box
[col
][row
].FlagType
) {
427 offset
= QUESTION_BMP
;
444 WINE_TRACE("Unknown FlagType while playing in DrawMine\n");
449 if( p_board
->box
[col
][row
].FlagType
== COMPLETE
450 && !p_board
->box
[col
][row
].IsMine
)
451 offset
= (MINEBMP_OFFSET
) p_board
->box
[col
][row
].NumMines
;
454 (col
- 1) * MINE_WIDTH
+ p_board
->mines_rect
.left
,
455 (row
- 1) * MINE_HEIGHT
+ p_board
->mines_rect
.top
,
456 MINE_WIDTH
, MINE_HEIGHT
,
457 hMemDC
, 0, offset
* MINE_HEIGHT
, SRCCOPY
);
460 static void DrawMines ( HDC hdc
, HDC hMemDC
, BOARD
*p_board
)
464 hOldObj
= SelectObject (hMemDC
, p_board
->hMinesBMP
);
466 for( row
= 1; row
<= p_board
->rows
; row
++ ) {
467 for( col
= 1; col
<= p_board
->cols
; col
++ ) {
468 DrawMine( hdc
, hMemDC
, p_board
, col
, row
, FALSE
);
471 SelectObject( hMemDC
, hOldObj
);
474 static void DrawLeds( HDC hdc
, HDC hMemDC
, BOARD
*p_board
, int number
, int x
, int y
)
483 led
[0] = count
/ 100 ;
484 count
-= led
[0] * 100;
487 led
[0] = 10; /* negative sign */
491 count
-= led
[1] * 10;
495 for( i
= 0; i
< 3; i
++ )
499 hOldObj
= SelectObject (hMemDC
, p_board
->hLedsBMP
);
501 for( i
= 0; i
< 3; i
++ ) {
513 SelectObject( hMemDC
, hOldObj
);
517 static void DrawFace( HDC hdc
, HDC hMemDC
, BOARD
*p_board
)
521 hOldObj
= SelectObject (hMemDC
, p_board
->hFacesBMP
);
524 p_board
->face_rect
.left
,
525 p_board
->face_rect
.top
,
528 hMemDC
, 0, p_board
->face_bmp
* FACE_HEIGHT
, SRCCOPY
);
530 SelectObject( hMemDC
, hOldObj
);
534 static void DrawBoard( HDC hdc
, HDC hMemDC
, PAINTSTRUCT
*ps
, BOARD
*p_board
)
538 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->counter_rect
) )
539 DrawLeds( hdc
, hMemDC
, p_board
, p_board
->mines
- p_board
->num_flags
,
540 p_board
->counter_rect
.left
,
541 p_board
->counter_rect
.top
);
543 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->timer_rect
) )
544 DrawLeds( hdc
, hMemDC
, p_board
, p_board
->time
,
545 p_board
->timer_rect
.left
,
546 p_board
->timer_rect
.top
);
548 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->face_rect
) )
549 DrawFace( hdc
, hMemDC
, p_board
);
551 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->mines_rect
) )
552 DrawMines( hdc
, hMemDC
, p_board
);
556 static void AddFlag( BOARD
*p_board
, unsigned col
, unsigned row
)
558 if( p_board
->box
[col
][row
].FlagType
!= COMPLETE
) {
559 switch( p_board
->box
[col
][row
].FlagType
) {
561 if( p_board
->IsMarkQ
)
562 p_board
->box
[col
][row
].FlagType
= QUESTION
;
564 p_board
->box
[col
][row
].FlagType
= NORMAL
;
565 p_board
->num_flags
--;
569 p_board
->box
[col
][row
].FlagType
= NORMAL
;
573 p_board
->box
[col
][row
].FlagType
= FLAG
;
574 p_board
->num_flags
++;
580 static void UnpressBox( BOARD
*p_board
, unsigned col
, unsigned row
)
586 hdc
= GetDC( p_board
->hWnd
);
587 hMemDC
= CreateCompatibleDC( hdc
);
588 hOldObj
= SelectObject( hMemDC
, p_board
->hMinesBMP
);
590 DrawMine( hdc
, hMemDC
, p_board
, col
, row
, FALSE
);
592 SelectObject( hMemDC
, hOldObj
);
594 ReleaseDC( p_board
->hWnd
, hdc
);
598 static void UnpressBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
602 for( i
= -1; i
<= 1; i
++ )
603 for( j
= -1; j
<= 1; j
++ ) {
604 UnpressBox( p_board
, col
+ i
, row
+ j
);
609 static void PressBox( BOARD
*p_board
, unsigned col
, unsigned row
)
615 hdc
= GetDC( p_board
->hWnd
);
616 hMemDC
= CreateCompatibleDC( hdc
);
617 hOldObj
= SelectObject (hMemDC
, p_board
->hMinesBMP
);
619 DrawMine( hdc
, hMemDC
, p_board
, col
, row
, TRUE
);
621 SelectObject( hMemDC
, hOldObj
);
623 ReleaseDC( p_board
->hWnd
, hdc
);
627 static void PressBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
631 for( i
= -1; i
<= 1; i
++ )
632 for( j
= -1; j
<= 1; j
++ ) {
633 p_board
->box
[col
+ i
][row
+ j
].IsPressed
= TRUE
;
634 PressBox( p_board
, col
+ i
, row
+ j
);
637 for( i
= -1; i
<= 1; i
++ )
638 for( j
= -1; j
<= 1; j
++ ) {
639 if( !p_board
->box
[p_board
->press
.x
+ i
][p_board
->press
.y
+ j
].IsPressed
)
640 UnpressBox( p_board
, p_board
->press
.x
+ i
, p_board
->press
.y
+ j
);
643 for( i
= -1; i
<= 1; i
++ )
644 for( j
= -1; j
<= 1; j
++ ) {
645 p_board
->box
[col
+ i
][row
+ j
].IsPressed
= FALSE
;
646 PressBox( p_board
, col
+ i
, row
+ j
);
649 p_board
->press
.x
= col
;
650 p_board
->press
.y
= row
;
654 static void CompleteBox( BOARD
*p_board
, unsigned col
, unsigned row
)
658 if( p_board
->box
[col
][row
].FlagType
!= COMPLETE
&&
659 p_board
->box
[col
][row
].FlagType
!= FLAG
&&
660 col
> 0 && col
< p_board
->cols
+ 1 &&
661 row
> 0 && row
< p_board
->rows
+ 1 ) {
662 p_board
->box
[col
][row
].FlagType
= COMPLETE
;
664 if( p_board
->box
[col
][row
].IsMine
) {
665 p_board
->face_bmp
= DEAD_BMP
;
666 p_board
->status
= GAMEOVER
;
668 else if( p_board
->status
!= GAMEOVER
)
669 p_board
->boxes_left
--;
671 if( p_board
->box
[col
][row
].NumMines
== 0 )
673 for( i
= -1; i
<= 1; i
++ )
674 for( j
= -1; j
<= 1; j
++ )
675 CompleteBox( p_board
, col
+ i
, row
+ j
);
681 static void CompleteBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
683 unsigned numFlags
= 0;
686 if( p_board
->box
[col
][row
].FlagType
== COMPLETE
) {
687 for( i
= -1; i
<= 1; i
++ )
688 for( j
= -1; j
<= 1; j
++ ) {
689 if( p_board
->box
[col
+i
][row
+j
].FlagType
== FLAG
)
693 if( numFlags
== p_board
->box
[col
][row
].NumMines
) {
694 for( i
= -1; i
<= 1; i
++ )
695 for( j
= -1; j
<= 1; j
++ ) {
696 if( p_board
->box
[col
+i
][row
+j
].FlagType
!= FLAG
)
697 CompleteBox( p_board
, col
+i
, row
+j
);
704 static void TestMines( BOARD
*p_board
, POINT pt
, int msg
)
709 col
= (pt
.x
- p_board
->mines_rect
.left
) / MINE_WIDTH
+ 1;
710 row
= (pt
.y
- p_board
->mines_rect
.top
) / MINE_HEIGHT
+ 1;
714 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
) {
716 p_board
->press
.x
, p_board
->press
.y
);
717 p_board
->press
.x
= col
;
718 p_board
->press
.y
= row
;
719 PressBox( p_board
, col
, row
);
725 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
)
727 p_board
->press
.x
, p_board
->press
.y
);
728 p_board
->press
.x
= 0;
729 p_board
->press
.y
= 0;
730 if( p_board
->box
[col
][row
].FlagType
!= FLAG
731 && p_board
->status
!= PLAYING
)
733 p_board
->status
= PLAYING
;
734 PlaceMines( p_board
, col
, row
);
736 CompleteBox( p_board
, col
, row
);
740 PressBoxes( p_board
, col
, row
);
745 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
)
746 UnpressBoxes( p_board
,
747 p_board
->press
.x
, p_board
->press
.y
);
748 p_board
->press
.x
= 0;
749 p_board
->press
.y
= 0;
750 CompleteBoxes( p_board
, col
, row
);
754 AddFlag( p_board
, col
, row
);
757 WINE_TRACE("Unknown message type received in TestMines\n");
763 RedrawWindow( p_board
->hWnd
, NULL
, 0,
764 RDW_INVALIDATE
| RDW_UPDATENOW
);
769 static void TestFace( BOARD
*p_board
, POINT pt
, int msg
)
771 if( p_board
->status
== PLAYING
|| p_board
->status
== WAITING
) {
772 if( msg
== WM_LBUTTONDOWN
|| msg
== WM_MBUTTONDOWN
)
773 p_board
->face_bmp
= OOH_BMP
;
774 else p_board
->face_bmp
= SMILE_BMP
;
776 else if( p_board
->status
== GAMEOVER
)
777 p_board
->face_bmp
= DEAD_BMP
;
778 else if( p_board
->status
== WON
)
779 p_board
->face_bmp
= COOL_BMP
;
781 if( PtInRect( &p_board
->face_rect
, pt
) ) {
782 if( msg
== WM_LBUTTONDOWN
)
783 p_board
->face_bmp
= SPRESS_BMP
;
785 if( msg
== WM_LBUTTONUP
)
786 CreateBoard( p_board
);
789 RedrawWindow( p_board
->hWnd
, &p_board
->face_rect
, 0,
790 RDW_INVALIDATE
| RDW_UPDATENOW
);
794 static void TestBoard( HWND hWnd
, BOARD
*p_board
, int x
, int y
, int msg
)
802 if( PtInRect( &p_board
->mines_rect
, pt
) && p_board
->status
!= GAMEOVER
803 && p_board
->status
!= WON
)
804 TestMines( p_board
, pt
, msg
);
806 UnpressBoxes( p_board
,
809 p_board
->press
.x
= 0;
810 p_board
->press
.y
= 0;
813 if( p_board
->boxes_left
== 0 ) {
814 p_board
->status
= WON
;
816 if (p_board
->num_flags
< p_board
->mines
) {
817 for( row
= 1; row
<= p_board
->rows
; row
++ ) {
818 for( col
= 1; col
<= p_board
->cols
; col
++ ) {
819 if (p_board
->box
[col
][row
].IsMine
&& p_board
->box
[col
][row
].FlagType
!= FLAG
)
820 p_board
->box
[col
][row
].FlagType
= FLAG
;
824 p_board
->num_flags
= p_board
->mines
;
826 RedrawWindow( p_board
->hWnd
, NULL
, 0,
827 RDW_INVALIDATE
| RDW_UPDATENOW
);
830 if( p_board
->difficulty
!= CUSTOM
&&
831 p_board
->time
< p_board
->best_time
[p_board
->difficulty
] ) {
832 p_board
->best_time
[p_board
->difficulty
] = p_board
->time
;
834 DialogBoxParamW( p_board
->hInst
, MAKEINTRESOURCEW(DLG_CONGRATS
), hWnd
,
835 CongratsDlgProc
, (LPARAM
) p_board
);
836 DialogBoxParamW( p_board
->hInst
, MAKEINTRESOURCEW(DLG_TIMES
), hWnd
,
837 TimesDlgProc
, (LPARAM
) p_board
);
840 TestFace( p_board
, pt
, msg
);
844 static LRESULT WINAPI
MainProc( HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
853 board
.hInst
= ((LPCREATESTRUCTW
) lParam
)->hInstance
;
856 CreateBoard( &board
);
863 WINE_TRACE("WM_PAINT\n");
864 hdc
= BeginPaint( hWnd
, &ps
);
865 hMemDC
= CreateCompatibleDC( hdc
);
867 DrawBoard( hdc
, hMemDC
, &ps
, &board
);
870 EndPaint( hWnd
, &ps
);
876 WINE_TRACE("WM_MOVE\n");
877 board
.pos
.x
= (short)LOWORD(lParam
);
878 board
.pos
.y
= (short)HIWORD(lParam
);
883 DestroyBoard( &board
);
884 PostQuitMessage( 0 );
888 if( board
.status
== PLAYING
) {
890 RedrawWindow( hWnd
, &board
.timer_rect
, 0,
891 RDW_INVALIDATE
| RDW_UPDATENOW
);
896 WINE_TRACE("WM_LBUTTONDOWN\n");
897 if( wParam
& MK_RBUTTON
)
898 msg
= WM_MBUTTONDOWN
;
899 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
904 WINE_TRACE("WM_LBUTTONUP\n");
905 if( wParam
& MK_RBUTTON
)
907 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
912 WINE_TRACE("WM_RBUTTONDOWN\n");
913 if( wParam
& MK_LBUTTON
) {
916 msg
= WM_MBUTTONDOWN
;
918 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
922 WINE_TRACE("WM_RBUTTONUP\n");
923 if( wParam
& MK_LBUTTON
)
925 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
929 WINE_TRACE("WM_MBUTTONDOWN\n");
930 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
934 WINE_TRACE("WM_MBUTTONUP\n");
935 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
940 if( ( wParam
& MK_MBUTTON
) ||
941 ( ( wParam
& MK_LBUTTON
) && ( wParam
& MK_RBUTTON
) ) ) {
942 msg
= WM_MBUTTONDOWN
;
944 else if( wParam
& MK_LBUTTON
) {
945 msg
= WM_LBUTTONDOWN
;
951 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
957 switch(LOWORD(wParam
)) {
959 CreateBoard( &board
);
963 hMenu
= GetMenu( hWnd
);
964 board
.IsMarkQ
= !board
.IsMarkQ
;
966 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_CHECKED
);
968 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_UNCHECKED
);
972 SetDifficulty( &board
, BEGINNER
);
973 CreateBoard( &board
);
977 SetDifficulty( &board
, ADVANCED
);
978 CreateBoard( &board
);
982 SetDifficulty( &board
, EXPERT
);
983 CreateBoard( &board
);
987 SetDifficulty( &board
, CUSTOM
);
988 CreateBoard( &board
);
992 SendMessageW( hWnd
, WM_CLOSE
, 0, 0);
996 DialogBoxParamW( board
.hInst
, MAKEINTRESOURCEW(DLG_TIMES
), hWnd
,
997 TimesDlgProc
, (LPARAM
) &board
);
1002 WCHAR appname
[256], other
[256];
1003 LoadStringW( board
.hInst
, IDS_APPNAME
, appname
, sizeof(appname
)/sizeof(WCHAR
) );
1004 LoadStringW( board
.hInst
, IDS_ABOUT
, other
, sizeof(other
)/sizeof(WCHAR
) );
1005 ShellAboutW( hWnd
, appname
, other
,
1006 LoadImageA( board
.hInst
, "WINEMINE", IMAGE_ICON
, 48, 48, LR_SHARED
));
1010 WINE_TRACE("Unknown WM_COMMAND command message received\n");
1014 return DefWindowProcW( hWnd
, msg
, wParam
, lParam
);
1017 int WINAPI
wWinMain( HINSTANCE hInst
, HINSTANCE hPrevInst
, LPWSTR cmdline
, int cmdshow
)
1025 LoadStringW( hInst
, IDS_APPNAME
, appname
, sizeof(appname
)/sizeof(WCHAR
));
1027 wc
.cbSize
= sizeof(wc
);
1029 wc
.lpfnWndProc
= MainProc
;
1032 wc
.hInstance
= hInst
;
1033 wc
.hIcon
= LoadIconW( hInst
, MAKEINTRESOURCEW(IDI_WINEMINE
) );
1034 wc
.hCursor
= LoadCursorW( 0, (LPWSTR
)IDI_APPLICATION
);
1035 wc
.hbrBackground
= GetStockObject( BLACK_BRUSH
);
1036 wc
.lpszMenuName
= MAKEINTRESOURCEW(IDM_WINEMINE
);
1037 wc
.lpszClassName
= appname
;
1038 wc
.hIconSm
= LoadImageW( hInst
, MAKEINTRESOURCEW(IDI_WINEMINE
), IMAGE_ICON
,
1039 GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
), LR_SHARED
);
1041 if (!RegisterClassExW(&wc
)) ExitProcess(1);
1042 hWnd
= CreateWindowW( appname
, appname
,
1044 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
1045 0, 0, hInst
, NULL
);
1047 if (!hWnd
) ExitProcess(1);
1049 ShowWindow( hWnd
, cmdshow
);
1050 UpdateWindow( hWnd
);
1052 haccel
= LoadAcceleratorsW( hInst
, MAKEINTRESOURCEW(IDA_WINEMINE
) );
1053 SetTimer( hWnd
, ID_TIMER
, 1000, NULL
);
1055 while( GetMessageW(&msg
, 0, 0, 0) ) {
1056 if (!TranslateAcceleratorW( hWnd
, haccel
, &msg
))
1057 TranslateMessage( &msg
);
1059 DispatchMessageW( &msg
);