-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMm.c
1856 lines (1530 loc) · 52.5 KB
/
Mm.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*** MM.C - Master Mind Game
*
* (c) 1991, Benjamin W. Slivka
*
* MasterMind is a game (c) 1981 Pressman Toy Corporation,
* 200 Fifth Avenue, New York, NY 10010.
*
* The object of MasterMind is to guess a secret code consisting of
* a series of 4 colored pegs. Each guess results in feedback which
* should narrow down the possible code values. Using this feedback,
* the player will (with suitable application of logic) be able to
* deduce the computer's code.
*
* To begin, the computer builds a code of 4 colored pegs. For
* example: red, white, green, yellow. The color and order of the
* pegs make up the code.
*
* The player then builds a guess of 4 colored pegs, and asks the
* computer for feedback on the guess. The computer reports the number
* of pegs in the guess which match the code:
* (1) in both color and position, and
* (2) in color only
*
* Any pegs counted for (1) are not eligible to be counted for (2).
*
*
* Visual Layout of Game Window
* ============================
*
* +-+-----------------------------------------------+-+
* |=| Master Mind |V|
* +-+-----------------------------------------------+-+
* | Options Help |
* +---------------------------------------------------+
* | ^ |
* | cxBorder +-- cyBorder |
* | | V |
* | | +---------------------------+ ^ |
* | | | | | |
* |<+>| Well Area | +- cyWell |
* | | | | |
* | +---------------------------+ V |
* | ^ cxBorder |
* | +-cyWellToMove +cxMoveToResult | |
* | V | | |
* | +-------------+ | +-----+ | /----------\ | |
* | | ^ |<+>| | | | New Game |<+>|
* | | | | | | | \----------/ |
* | | | | | R | | |
* | | Move | | | e | | /----------\ |
* | | Area | | | s | |<+->| Guess | |
* | | | | | u | | \----------/ |
* | | | | | l | | |
* | | | | | t | cxWellToButton |
* | | | | | | |
* | | | | | A | +----------+ |
* | | | | | r | | | |
* | | cyMove | | e | | | |
* | | | | | a | | Fun | |
* | | | | | | | | |
* | | | | | | | Area | |
* | | | | | | | | |
* | |<- cxMove -->| | | | | |
* | | | | |<-+->| | | |
* | | V | | | | | | |
* | +-------------+ +--|--+ | | |
* | ^ | | | |
* | +--cyMoveToAnswer cxResult | | |
* | V | | |
* | +-------------+ \ | | |
* | | Answer Area | +- cyAnswer | | |
* | +-------------+ / +----------+ |
* | ^ |
* | +--cyBorder |
* | V |
* +---------------------------------------------------+
*
*
* Visual Layout of Peg Box
* ========================
* The "Peg Box" is the building block for the "Well Area" and the
* "Move Area". It has room for a "Peg" centered inside the box.
*
*
* <-- cxPegBox --->
* ^ +---------------+
* | | |
* | | cxPegOffset |
* | | | --
* c | | ----- | ^
* y | | / \ | |
* P | | / \ | |
* e | | | Peg | | +-- cyPeg
* g |<->| | | |
* B | \ / | |
* o | \ / | |
* x | ----- | V
* | ^ | --
* | | +-- cyPegOffset
* | | V |
* V +---------------+
* | |
* |<---+--->|
* |
*
* cxPeg
*
*
* Visual Layout of Pin Box
* ========================
* The "Pin Box" is the building block for the "Result Area".
* It has room for up to 4 Pins grouped around the center of the box,
* so that Pins in one pin box do not blend visually with the pins of
* an adjacent Pin Box.
*
*
* <---- cxPinBox --->
* ^ +-----------------+
* | | ^ |
* | | +- cyPinOffset
* | V |
* c | /-\ /-\ |
* y | \-/ \-/ |
* P | ^+cyPinSpace
* i | V | --
* n | /-\ /-\ | ^--cyPin
* B |<+->\-/ \-/ | V
* o | | <> | --
* x | | +cxPinSpace
* | | |
* | | cxPinOffset |
* | | |
* V +-----------------+
* | |
* |<+>|
* |
*
* cxPin
*
*
* Algorithmic Relationship Between Dimensions
* ===========================================
*
* nPeg = 4 = number of pegs in a guess
* nColor = 6 = number of different colored pegs
*
* cxPegBox = x width of a Peg Box
* cyPegBox = y width of a Peg Box
* cxPeg = x width of a Peg
* cyPeg = y width of a Peg
* cxPegOffset = x distance between edge of Peg Box and Peg
* cyPegOffset = y distance between edge of Peg Box and Peg
*
* cxMove = nPeg * cxPegBox = x width of Move Area
* cyMove = maxMove * cyPegBox = y height of Move Area
* cxResult = x width of Result Area
* cyResult = cyMove = y height of Result Area
* cxWell = cxPegBox = x width of Well Area
* cyWell = nColor * cyPegBox = y height of Well Area
* cyBorder = y width of top and bottom border
* cxBorder = x width of left and right border
* cxButton = width of widest button (computed at run-time)
*
* cxMoveToResult = cxWell - (cxMove + cxResult) = x width between
* Move and Result Areas
* cxWellToButton = x width between Well Area and Buttons
*
* cyWellToMove = y height between Well and Move Areas
*
* cxClient = 2*cxBorder + cxWell + cxWellToButton + cxButton
* = x width of client area
*
* cyClient = 2*cyBorder + cyWell + cyWellToMove + cyMove +
* cyMoveToAnswer + cyAnswer
* = y height of client area
*
*
* Notes:
*
* (1) The Well Area is positioned over the Move and Result Areas
* (2) The Move and Result Areas are centered under the Well Area
* (2) The Buttons are postioned to the right of the Result Area
*
* "Peg Box" - The bounding rectangle of a "Peg". This is used to
* to lay out the "Move Area" and "Well Area".
*
* "Well Area" - The area where one peg of each color is displayed. The
* user drags a peg from this "well" into the "Move Area", and
* over the "Active Row" to place a peg for a "Guess".
*
* "Move Area" - The area where the player builds Guesses.
*
* "Result Area" - The area where the results of a players guesses are
* displayed. A *black* "Pin" is displayed for each Peg in the
* Guess that matches a Peg in the computer's "Goal" in both
* color and position. A *white* Pin is displayed for each Peg
* in the Guess that matches a Peg in the computer's Goal in
* color only.
*
* "Fun Area" - The area where visual indication of game won/lost occurs.
*
* "Answer Area" - The area where the computer code is revealed.
*
*
* Performance Notes
* =================
* (1) We create a Memory DC to hold a bitmap that contains images of
* each peg, the peg hole, and all combinations of pin results.
* Then, when painting the board, we do a BitBlt(...,SRC_COPY,...)
* from the memory DC to the the screen DC!
*
* Since we cleverly made <cxPegBox,cyPegBox> be the same size as
* <cxPinBox,cyPinBox>, all we need is (conceptually) an array of
* images of this size in a single bitmap.
*
* We need to construct the following images in the bitmap:
*
* Count Description of Image(s)
* ------ --------------------------------------------------------
* nColor Colored Pegs
* 1 Peg Hole
* 15 All possible Result Pin patterns (see below)
*
* Result Pin Patterns
* -------------------
* The tricky thing here is to pack these images tightly into
* the bitmap, so we need a mapping from (cPos,cClr) to [0..14]:
*
* i Pos Clr
* --- --- ---
* 0 0 0
* 1 0 1
* 2 0 2
* 3 0 3
* 4 0 4
* 5 1 0
* 6 1 1
* 7 1 2
* 8 1 3
* 9 2 0
* 10 2 1
* 11 2 2
* 12 3 0
* 13 3 1
* 14 4 0
*
* The solution is a two-dimensional array, mpResultToLibrary,
* indexed by cPos and cClr. We build this array as we build
* the images in the image bitmap. For valid combinations, we
* store the index of the image in the Bitmap into the array.
* For invalid combinations, the array entry is set to -1.
*
* (2) A Performance vs. Size Tradeoff in the organization of the
* Image Library Bitmap.
*
* We can either construct this as a horizontal vector of images,
* or a vertical vector of images.
*
* A horizontal organization is likely to consume less memory, since
* the bitmap will (we presume) only waste space at the end of a
* row -- and there will only be *cxPegBox* rows. However, this
* is could lead to non-byte-aligned BitBlts, which are slower.
*
* A vertical organization, on the other hand, ensures that at least
* the source bitmap is aligned on a byte boundary. The cost is that
* we may waste some bytes at the end of each pixel row. Since we
* are using BitBlt for speed, we will continue to follow that maxim
* here.
*/
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mm.h"
/**************
*** Macros *****************************************************************
**************/
#define MIR(x) MAKEINTRESOURCE(x) // Make DialogBox lines shorter
#define AssertMsg(x) // BUGBUG - Enable this for DEBUG build
/*****************
*** Constants **************************************************************
*****************/
#define cbMaxString 100 // Length of longest string resource
#define nColor 6 // Number of colors
#define nPeg 4 // Number of pegs per move
#define maxMove 10 // Maximum number of moves
#define nPin 2 // Number of result pin colors
#define cxPegBox 24 // x width of a Peg Box
#define cyPegBox 24 // y height of a Peg Box
#define cxPeg 16 // x width of a Peg
#define cyPeg 16 // y height of a Peg
#define cxPegOffset ((cxPegBox-cxPeg)/2) // x offset of Peg in Peg Box
#define cyPegOffset ((cyPegBox-cyPeg)/2) // y offset of Peg in Peg Box
#define cxPegHole 8 // x width of a Peg Hole
#define cyPegHole 8 // y height of a Peg Hole
#define cxPegHoleOffset ((cxPegBox-cxPegHole)/2) // x offset of Peg Hole
#define cyPegHoleOffset ((cyPegBox-cyPegHole)/2) // y offset of Peg Hole
#define cxPinBox (cxPegBox) // x width of a Pin Box
#define cyPinBox (cyPegBox) // y width of a Pin Box
#define cxPin 6 // x width of a Pin
#define cyPin 6 // y width of a Pin
#define cxPinSpace (cxPin/2) // x distance between Pins in Pin Box
#define cyPinSpace (cyPin/2) // y distance between Pins in Pin Box
#define cxPinOffset ((cxPinBox-2*cxPin-cxPinSpace)/2) // x offset of Pin
#define cyPinOffset ((cyPinBox-2*cyPin-cyPinSpace)/2) // y offset of Pin
#define cxImageBoxOffset 1 // x border around image
#define cyImageBoxOffset 1 // y border around image
#define cxImageBox (cxPegBox - 2*cxImageBoxOffset)
#define cyImageBox (cyPegBox - 2*cyImageBoxOffset)
#define nLibraryImage (nColor+1+15) // Colors + Peg Hole + Result Pin Patterns
// See "Performance Notes (1)" above.
#define cxLibrary (cxImageBox) // Width of Image Library Bitmap
#define cyLibrary (cyImageBox*nLibraryImage) // Height of Image Library Bitmap
#define cxMove (nPeg*cxPegBox) // x width of Move Area
#define cyMove (maxMove*cyPegBox) // y height of Move Area
#define cyBorder 8 // y width of client border
#define cxBorder 8 // x width of client border
#define cxResult (cxPinBox) // x width of Result Area
#define cyResult (cyMove) // y height of Result Area
#define cxWell (nColor*cxPegBox) // x width of Well Area
#define cyWell (cyPegBox) // y height of Well Area
#define cxMoveToResult 8 // x width between Move and Result Area
#define cxWellToButton 8 // x width between Result Area & Buttons
#define cyWellToMove 8 // y height between Well and Move Area
#define cyMoveToAnswer 8 // y height between Move and Answer Area
#define xWell (cxBorder) // x left of Well Area
#define xMove (xWell + (cxWell - (cxMove+cxResult+cxMoveToResult))/2)
#define xResult (xMove+cxMove+cxMoveToResult) // x left of Result Area
#define xButton (xWell+cxWell+cxWellToButton) // x left of Buttons
#define yWell (cyBorder) // y top of Well Area
#define yMove (yWell+cyWell+cyWellToMove) // y top of Move Area
#define yResult (yMove) // y top of Result Area
#define yButton (cyBorder) // y top of Buttons
#define xAnswer (xMove)
#define yAnswer (yMove + cyMove + cyMoveToAnswer)
#define cxAnswer (cxMove)
#define cyAnswer (cyPegBox)
/*
* Rectangle to bound cursor while dragging a peg
*/
#define xCursorLeft xWell
#define yCursorTop yWell
#define xCursorRight (xWell+cxWell)
#define yCursorBottom (yMove+cyMove)
/************************
*** Type Definitions *******************************************************
************************/
typedef int PEG; /* peg */ // Peg value
#define PEG_BLANK (nColor) // PEG color of empty peg box
typedef PEG GUESS[nPeg]; /* guess */
typedef struct _MOVE { /* mv */
GUESS guess; // The guess
int cPosition; // Pegs that match both position and color
int cColor; // Pegs that match color but not position
} MOVE, *PMOVE;
typedef struct _GLOBAL { /* g */ // Global Variables
HWND hwnd; // Client window
int cxMain; // X width of main window
int cyMain; // Y width of main window
int cxClient; // X width of client area of window
int cyClient; // Y width of client area of window
int cxButton; // X width of widest button
int cyButton; // Y height of tallest button
int cyButtonSpace; // Y separation between buttons
int xFun; // X of Fun area
int yFun; // Y of Fun area
int cxFun; // X width of Fun area
int cyFun; // Y height of Fun area
GUESS guessCode; // The code
int iMove; // Current move index
PEG pegMove; // Peg value being dragged
MOVE amove[maxMove]; // The game history
HANDLE hInstance; // App instance handle
FARPROC lpfnAboutDlgProc; // About DlgProc instance function pointer
BOOL fGameOver; // TRUE => current game is over
BOOL fGameWon; // TRUE => player won current game
BOOL fGuessAllowed; // TRUE => guess button enabled
HDC hdcLibrary; // HDC for Image Library
HBITMAP hbmLibrary; // HBM for Image Library
HCURSOR hcurCurrent; // Current cursor
HCURSOR hcurDefault; // Default cursor
HCURSOR hcurOverWell; // Cursor when over Well, but not dragging
HCURSOR hcurDrag; // Cursor during Drag
HCURSOR hcurDragOver; // Cursor at Drag Over Move row in Move Area
HFONT hfntButton; // Button font
} GLOBAL,*PGLOBAL;
// Where is the Mouse?
typedef enum {WH_NOTOURS,WH_WELL, WH_MOVE} WHERE; /* wh */
// BUTTON - Information used to create a button in the client area.
typedef struct tagButtonDescription { /* btn */
int ids; // String ID in resource file
int idc; // Command ID
long style; // Button Style
HWND hwnd; // Button window handle
} BUTTON, *PBUTTON;
/*****************
*** Variables **************************************************************
*****************/
GLOBAL g; // Globals
DWORD clrPeg[nColor] = { // Maps PEG value to screen color
CLR_BLACK,
CLR_BLUE,
CLR_GREEN,
CLR_YELLOW,
CLR_RED,
CLR_WHITE,
};
// Brush indicies for painting pins
int aPinColor[nPin] = {
BLACK_BRUSH, // Match Position and Color
WHITE_BRUSH, // Match Color but not Position
};
// Client-Area button definitions
BUTTON abutton[] = {
/* string id control ID style hwnd */
/* --------- ------------ ------------- ---- */
{IDS_NEW_GAME, IDC_NEW_GAME, BS_PUSHBUTTON, NULL},
{IDS_GUESS, IDC_GUESS , BS_PUSHBUTTON, NULL},
};
#define nButton (sizeof(abutton)/sizeof(BUTTON))
// Button indicies
#define iButtonNewGame 0
#define iButtonGuess 1
// mpResultToImage - This array is used to find the index in the Image
// Library Bitmap of a particular Result. The first index is the
// cPosition, the second index is the cColor.
//
// CreateImageLibrary initializes this array.
int mpResultToLibrary[nPeg+1][nPeg+1];
/***************************
*** Function Prototypes ****************************************************
***************************/
int PASCAL WinMain(HANDLE hInstance,
HANDLE hPrevInstance,
LPSTR lpszCmdLine,
int nCmdShow);
BOOL FAR PASCAL AboutDlgProc(HWND hDlg,UINT msg,UINT wParam,LONG lParam);
long FAR PASCAL WndProc(HWND hDlg,UINT msg,UINT wParam,LONG lParam);
BOOL BeginMM(HANDLE hInstance,HANDLE hPrevInstance);
VOID CreateButtons(HWND hwnd);
VOID CreateImageLibrary(HDC hdcDisplay);
VOID DestroyButtons(VOID);
VOID DoCommand(HWND hwnd,UINT wParam,LONG lParam);
BOOL DoMouse(HWND hwnd,UINT msg,UINT wParam,LONG lParam);
VOID EndMM(VOID);
VOID EraseForNewGame(HWND hwnd);
VOID FastSetCursor(HCURSOR hcur);
BOOL MouseInArea(int xM,int yM,int x,int y,int cx,int cy);
VOID NewGame(VOID);
BOOL QueryResignGame(HWND hwnd);
VOID PaintAnswer(HWND hwnd);
VOID PaintAnswerSub(HDC hdc);
VOID PaintBoard(HDC hdc);
VOID PaintHolesForPegs(HWND hwnd);
VOID PaintLibrary(HDC hdc, int x, int y, int iLibrary);
VOID PaintPeg(HWND hwnd, int ix, int iy);
VOID PaintPegSub(HWND hwnd, int ix, int iy);
VOID PaintResult(HWND hwnd);
VOID PaintResultSub(HDC hdc, int iMove);
VOID PickCode(VOID);
VOID PlayerLost(HWND hwnd);
VOID PlayerLostSub(HDC hdc);
VOID PlayerTextOut(HDC hdc, char *psz);
VOID PlayerWon(HWND hwnd);
VOID PlayerWonSub(HDC hdc);
VOID Randomize(VOID);
VOID ReleaseMouse(VOID);
VOID SetMouse(HWND hwnd);
BOOL TestGuess(VOID);
/***************
*** WinMain ****************************************************************
***************/
int PASCAL WinMain(HANDLE hInstance,
HANDLE hPrevInstance,
LPSTR lpszCmdLine,
int nCmdShow)
{
MSG msg;
if (!BeginMM(hInstance,hPrevInstance))
exit(1);
// Show window
ShowWindow(g.hwnd,nCmdShow);
UpdateWindow(g.hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
EndMM();
return msg.wParam;
}
/************************************************
*** Functions - Listed in Alphabetical Order *******************************
************************************************/
/*** AboutDlgProc - About Dialog Box Procedure
*
*/
BOOL FAR PASCAL AboutDlgProc(HWND hdlg,UINT msg,UINT wParam,LONG lParam)
{
switch (msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch (wParam)
{
case IDOK:
case IDCANCEL:
EndDialog(hdlg, 0);
return TRUE;
}
break;
}
return FALSE;
}
/*** BeginMM - Initialize MasterMind
*
*/
BOOL BeginMM(HANDLE hInstance,HANDLE hPrevInstance)
{
char ach[cbMaxString];
int cch;
int cchButton;
int cxChar;
int cyChar;
HDC hdc;
HFONT hfnt;
int i;
long lStyle;
static char szAppName [] = "MastMind";
TEXTMETRIC tm;
WNDCLASS wndclass;
// Randomize number generator
Randomize();
// Save hInstance
g.hInstance = hInstance;
// Load Cursors
g.hcurDefault = LoadCursor(NULL,IDC_ARROW);
g.hcurCurrent = g.hcurDefault; // Assume current is default
g.hcurOverWell = LoadCursor(g.hInstance,MIR(CUR_OVER_WELL));
g.hcurDrag = LoadCursor(g.hInstance,MIR(CUR_DRAG));
g.hcurDragOver = LoadCursor(g.hInstance,MIR(CUR_DRAG_OVER));
// Register window class, if necessary
if (!hPrevInstance)
{
wndclass.style = CS_DBLCLKS;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = g.hInstance;
wndclass.hIcon = LoadIcon(hInstance, szAppName);
wndclass.hCursor = NULL; // We want total control of cursor
wndclass.hbrBackground = COLOR_APPWORKSPACE+1;
wndclass.lpszMenuName = szAppName;
wndclass.lpszClassName = szAppName;
RegisterClass(&wndclass);
}
// Get system font metrics (to size buttons)
hdc = CreateDC("DISPLAY",NULL,NULL,NULL);
// Create Image Library DC and Bitmap
CreateImageLibrary(hdc);
// Get button font
g.hfntButton = GetStockObject(SYSTEM_FONT);
if (g.hfntButton == 0) {
// BUGBUG bens 01-Jul-1991 Create Font failed
}
// Get Font Metrics for sizing buttons
hfnt = SelectObject(hdc,g.hfntButton);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
// Done with Display DC
SelectObject(hdc,hfnt); // Restore original font
DeleteDC(hdc);
// Compute button dimensions
cchButton = 0;
for (i=0; i<nButton; i++) { // Get length of longest string
cch = LoadString(g.hInstance,abutton[i].ids,ach,sizeof(ach));
cchButton = max(cch,cchButton);
}
g.cxButton = (cchButton+4)*cxChar; // Give 2 char room on either side
g.cyButton = (7 * cyChar) / 4; // BUGBUG - Magic number from petzold ex.
g.cyButtonSpace = g.cyButton/2; // Button Spacing
// Compute size of client area and main window
g.cxClient = 2*cxBorder + cxWell + cxWellToButton + g.cxButton;
g.cyClient = 2*cyBorder + cyWell +
cyWellToMove + cyMove +
cyMoveToAnswer + cyAnswer;
g.cxMain = g.cxClient +
GetSystemMetrics(SM_CXBORDER);
g.cyMain = g.cyClient +
GetSystemMetrics(SM_CYBORDER) +
GetSystemMetrics(SM_CYCAPTION) +
GetSystemMetrics(SM_CYMENU);
// Compute Fun area dimensions
g.xFun = xButton;
g.yFun = yButton + 2*(g.cyButton+g.cyButtonSpace);
g.cxFun = g.cxButton;
g.cyFun = g.cyClient - (g.yFun+cyBorder);
// Create main window
LoadString(g.hInstance,IDS_APP_TITLE,ach,sizeof(ach));
lStyle = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
g.hwnd = CreateWindow(
szAppName, // Window class
ach, // Window Title
lStyle, // Window Style
CW_USEDEFAULT, // Left edge of window
CW_USEDEFAULT, // Top edge of window
g.cxMain, // Width of window
g.cyMain, // Height of window
NULL, // Parent window
NULL, // Control ID
hInstance, // App instance
NULL // lpCreateStruct
);
// Create dialog procedure instance(s)
g.lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc,g.hInstance);
return TRUE;
}
/*** CreateButtons - Create buttons in client area
*
*/
VOID CreateButtons(HWND hwnd)
{
char ach[cbMaxString];
HFONT hfnt;
int i;
long lStyle;
int x,y;
hfnt = GetStockObject(ANSI_VAR_FONT); // Font for button text
x = xButton;
y = yButton;
for (i=0; i<nButton; i++) {
LoadString(g.hInstance,abutton[i].ids,ach,sizeof(ach));
lStyle = WS_CHILD | abutton[i].style;
abutton[i].hwnd = CreateWindow(
"button", // Window class
ach, // Button text
lStyle, // Button style;
x, // Left edge of button
y, // Top edge of button
g.cxButton, // Width of button
g.cyButton, // Height of button
hwnd, // Parent window (gets BM_ messages)
abutton[i].idc, // Button Control ID
g.hInstance, // App instance
NULL); // lpCreateStruct
y += (g.cyButton + g.cyButtonSpace); // Advance button position
// Set button font
SendMessage(abutton[i].hwnd,WM_SETFONT,g.hfntButton,FALSE);
// Show button
ShowWindow(abutton[i].hwnd,SW_SHOWNORMAL);
}
}
/*** CreateImageLibrary - Create bitmap with all peg and pin images
*
* Entry
* hdc - Display DC
*/
VOID CreateImageLibrary(HDC hdcDisplay)
{
int acPins[2];
int cPinsPainted;
HPEN hpenSave;
HBRUSH hbrush;
HBRUSH hbrushLast;
HBRUSH hbrushSave;
HDC hdc;
int i;
int iClr;
int iLoop;
int iPinBox;
int iPos;
int x1,y1,x2,y2;
// Create Image Library Bitmap -- Same color depth as display!
g.hbmLibrary = CreateCompatibleBitmap(hdcDisplay,cxLibrary,cyLibrary);
// Create the memory DC
hdc = CreateCompatibleDC(hdcDisplay); // Image Library DC
g.hdcLibrary = hdc; // Save so we can free it at exit
SelectObject(hdc,g.hbmLibrary); // Select bitmap into DC
// Set desired pen/brush and save default pen/brush
hpenSave = SelectObject(hdc,GetStockObject(BLACK_PEN));
hbrushSave = SelectObject(hdc,GetStockObject(LTGRAY_BRUSH));
// Fill bitmap with background color (selected brush above!)
PatBlt(hdc,0,0,cxLibrary,cyLibrary,PATCOPY);
// Draw Color Pegs
x1 = cxPegOffset - cxImageBoxOffset;
x2 = x1+cxPeg;
y1 = cyPegOffset - cyImageBoxOffset;
for (i=0; i<nColor; i++) {
y2 = y1+cyPeg;
hbrush = CreateSolidBrush(clrPeg[i]); // Get brush for peg color
hbrushLast = SelectObject(hdc,hbrush);
if (i > 0) // Have a brush from last iteration
DeleteObject(hbrushLast); // Delete it!
Ellipse(hdc,x1,y1,x2,y2);
y1 += cyImageBox;
}
// Now, we have to delete the brush we created in the final iteration.
// So, we select in the brush we need for drawing the peg hole, and then
// we are able to delete that last brush we created!
hbrush = SelectObject(hdc,GetStockObject(LTGRAY_BRUSH));
DeleteObject(hbrush); // Delete brush from final iteration
//
// Draw Peg Hole
//
x1 = cxPegHoleOffset - cxImageBoxOffset;
x2 = x1 + cxPegHole;
y1 = nColor*cyImageBox + cyPegHoleOffset - cyImageBoxOffset;
y2 = y1 + cyPegHole;
Ellipse(hdc,x1,y1,x2,y2);
//
// Draw all valid combinations of Result Pin Patterns
//
iPinBox = nColor + 1; // Index of first pin box in Library
for (iPos=0; iPos<=nPeg; iPos++) {
for (iClr=0; iClr<=nPeg; iClr++) {
if ((iPos+iClr) > nPeg) { // Invalid combination
mpResultToLibrary[iPos][iClr] = -1; // No image index
continue; // Do next iteration of for loop!
}
mpResultToLibrary[iPos][iClr] = iPinBox; // Set image index
acPins[0] = iPos; // Set count of positions for painting
acPins[1] = iClr; // Set count of colors for painting
cPinsPainted = 0; // No pins painted in this box, yet
x1 = cxPinOffset - cxImageBoxOffset;
y1 = iPinBox*cyImageBox + cyPinOffset - cyImageBoxOffset;
// Paint black, then white pins
for (iLoop=0; iLoop<nPin; iLoop++) {
// Select color
SelectObject(hdc,GetStockObject(aPinColor[iLoop]));
// Paint pins of this color
for (i=0; i<acPins[iLoop]; i++) {
x2 = x1 + cxPin;
y2 = y1 + cyPin;
Ellipse(hdc,x1,y1,x2,y2); // Draw pin
x1 = x2 + cxPinSpace; // x for second pin
cPinsPainted++;
if (cPinsPainted == 2) { // Wrap to second row of pins
x1 = cxPinOffset - cxImageBoxOffset; // x of third pin
y1 = y2 + cyPinSpace; // y for third and fourth pins
}
}
}
iPinBox++; // Count Result Pin Pattern
}
}
//
// Set restore original pen/brush
//
SelectObject(hdc,hpenSave);
SelectObject(hdc,hbrushSave);
}
/*** DestroyButtons - Destroy buttons in client area
*
*/
VOID DestroyButtons()
{
int i;
for (i=0; i<nButton; i++) {
DestroyWindow(abutton[i].hwnd);
#ifdef DEBUG
abutton[i].hwnd = NULL;
#endif
}
}
/*** DoCommand - Handle COMMAND messages
*
*/
VOID DoCommand(HWND hwnd,UINT wParam,LONG lParam)
{
BOOL f;
switch (wParam) {
case IDM_ABOUT:
DialogBox(g.hInstance,MIR(IDD_ABOUT),hwnd,g.lpfnAboutDlgProc);
return;
case IDC_GUESS:
f = TestGuess();
PaintResult(hwnd); // Show result
// Disable Guess button
EnableWindow(abutton[iButtonGuess].hwnd,FALSE);
g.fGuessAllowed = FALSE;
if (f) { // Guess is correct
PlayerWon(hwnd); // Indicate a win
// Show answer
PaintAnswer(hwnd);
// Game won, disable play until New Game selected
g.fGameOver = TRUE;
g.fGameWon = TRUE;
}
else { // Guess is not correct
g.iMove++; // Advance to next row
if (g.iMove >= maxMove) { // Used up all guesses
// Reset move index to last row, to keep PaintBoard
// from crashing
g.iMove = maxMove - 1;
// Indicate the loss
PlayerLost(hwnd);
// Show answer
PaintAnswer(hwnd);
// Disable play until New Game selected
g.fGameOver = TRUE;
g.fGameWon = FALSE;
}
else { // Player still has more guess(s) to make
PaintHolesForPegs(hwnd); // Show where next moves go
}
}
return;
case IDC_NEW_GAME:
if ( (g.iMove > 0) && (!g.fGameOver) ) { // Is game in progress?
// Verify that user wants to resign current game
if (QueryResignGame(hwnd)) {
// Yes, user wants to resign.
// Indicate loss
PlayerLost(hwnd);
// Show answer
PaintAnswer(hwnd);
// Disable play until New Game selected
g.fGameOver = TRUE;
g.fGameWon = FALSE;
}
else // No, user does not want to resign
return; // Ignore command
}
else { // No game underway, do new game
NewGame(); // New game
EraseForNewGame(hwnd);
}
return;
}
}
/*** DoMouse - Handle mouse messages
*
* Entry