home *** CD-ROM | disk | FTP | other *** search
/ MORE WinGames / WINGAMES.iso / cbrk / cbreak.c next >
Text File  |  1991-05-14  |  12KB  |  369 lines

  1. /*
  2.     Program :   Code Breaker Game   ( cbreak.* )
  3.     Purpose :   Fun and pleasure, as well as a learning
  4.                 experiment in Windows programming.
  5.                 
  6.     Author :    Ken Fogel
  7.                 Omnibus Systems
  8.                 8108 Norfolk Road
  9.                 Cote St-Luc, Qubec
  10.                 Canada  H4X 1A3
  11.                 (514) 487-1565
  12.                 CIS: 74646,2157
  13.                 
  14.     Co-Author : Charles W. Haden
  15.                 Shoebox Software
  16.                 699 Lantana Street, Apt. #54
  17.                 Camarillo, CA   93010
  18.                 USA
  19.                 CIS: 71760,3557
  20.     
  21.     Revision :  April 15, 1991  - Version 1         (KF)
  22.                 April 22, 1991  - Version 1.01      (KF)
  23.                 May    9, 1991  - Version 1.01.a    (CWH)
  24.                 
  25.     Notes :
  26.     
  27. */
  28. /* -------------------------------------------------------- */
  29.  
  30. #include <windows.h>
  31. #include <stdlib.h>
  32. #include <time.h>
  33. #include "cbreak.h"
  34.  
  35. /* -------------------------------------------------------- */
  36.  
  37. /* Function declarations */
  38. long FAR PASCAL WndProc( HWND hWnd, WORD message, WORD wParam, LONG lParam );
  39. BOOL FAR PASCAL AboutProc( HWND hDlg, WORD message, WORD wParam, LONG lParam );
  40. BOOL FAR PASCAL HelpProc( HWND hDlg, WORD message, WORD wParam, LONG lParam );
  41. void vInitialize();
  42. void vMakeGoal();
  43. BOOL bCheckFill();
  44. BOOL bCheckGuess();
  45. BOOL bMakeGameButtons( HWND hWnd );
  46.  
  47. /* -------------------------------------------------------- */
  48.  
  49. HANDLE  hInst;
  50. HWND    hGameGrid[ROWMAX][COLMAX];
  51. int     guess[ROWMAX][COLMAX], guess_allowed;
  52. int     goal[COLMAX];
  53. char    clues[ROWMAX][COLMAX];
  54. int     current_row;
  55. HPEN    hSolidPen;
  56. char    str[80];
  57.  
  58. /* -------------------------------------------------------- */
  59.  
  60. int PASCAL
  61. WinMain( HANDLE hInstance, HANDLE hPrevInstance,
  62.          LPSTR lpCmdLine, int nCmdShow ){
  63.  
  64.     MSG         msg;
  65.     WNDCLASS    wc;
  66.     HWND        hWnd;
  67.     
  68.     if( !hPrevInstance ){
  69.         wc.style = CS_HREDRAW | CS_VREDRAW;
  70.         wc.lpfnWndProc = WndProc;
  71.         wc.cbClsExtra = 0;
  72.         wc.cbWndExtra = 0;
  73.         wc.hInstance = hInstance;
  74.         wc.hIcon = LoadIcon( hInstance, "CBrkIcon" );
  75.         wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  76.         wc.hbrBackground = GetStockObject( WHITE_BRUSH );
  77.         wc.lpszMenuName = "CBrkMenu";
  78.         wc.lpszClassName = "CBrkClass";
  79.  
  80.         RegisterClass( &wc );
  81.     }
  82.  
  83.     guess_allowed = TRUE;
  84.     hInst = hInstance;
  85.     hWnd = CreateWindow( "CBrkClass", "Code Breaker 1.01.a",
  86.         WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  87.         CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
  88.  
  89.     if( !hWnd )
  90.         return FALSE;
  91.             
  92.     ShowWindow( hWnd, nCmdShow );
  93.     UpdateWindow( hWnd );
  94.  
  95.     while( GetMessage( &msg, NULL, 0, 0 )){
  96.         TranslateMessage( &msg );
  97.         DispatchMessage( &msg );
  98.     }
  99.     return msg.wParam;
  100. }
  101.  
  102. /* -------------------------------------------------------- */
  103. long FAR PASCAL
  104. WndProc( HWND hWnd, WORD message, WORD wParam, LONG lParam ){
  105.     FARPROC     lpProcAbout, lpProcHelp;
  106.     HDC         hDC;
  107.     PAINTSTRUCT ps;
  108.     int         col, a, b;
  109.  
  110.     switch( message ){
  111.      case WM_CREATE:
  112.         if( hSolidPen == NULL )
  113.             hSolidPen = CreatePen( PS_SOLID, 2, RGB( 0, 0, 0 ));
  114.  
  115.         if( !bMakeGameButtons( hWnd ))
  116.             MessageBox( hWnd, "Unable to create buttons.", NULL,
  117.                 MB_OK | MB_ICONHAND );
  118.  
  119.         vInitialize();
  120.         for( col = 0; col < COLMAX; col++ )
  121.             EnableWindow( hGameGrid[current_row][col], TRUE );
  122.         vMakeGoal();
  123.         break;
  124.  
  125.      case WM_COMMAND:
  126.         if(( wParam >= 0 ) && ( wParam < ( ROWMAX * COLMAX ))){
  127.             if( HIWORD( lParam ) == BN_CLICKED ){
  128.                 col = wParam % COLMAX;
  129.                 guess[current_row][col]++;
  130.                 if( guess[current_row][col] > 8 )
  131.                     guess[current_row][col] = 1;
  132.                 sprintf( str, "%d", guess[current_row][col] );
  133.                 SetWindowText( hGameGrid[current_row][col], str );
  134.             }
  135.         } else {
  136.             switch( wParam ){
  137.              case IDM_GUESS:
  138.                 if(( guess_allowed ) && ( bCheckFill() )){
  139.                     if( bCheckGuess() ){
  140.                     /* Player has won the game. */
  141.                         guess_allowed = FALSE;
  142.                         sprintf( str, "You guessed the correct answer of %d %d %d %d %d",
  143.                             goal[0], goal[1], goal[2], goal[3], goal[4] );
  144.                         MessageBox( hWnd, str, "Congratulations !!!",
  145.                             MB_OK | MB_ICONINFORMATION );
  146.                         for( col = 0; col < COLMAX; col++ ){
  147.                             EnableWindow( hGameGrid[current_row][col], FALSE );
  148.                         }
  149.                     } else {
  150.                     /* guess was incorrect, dispay clue. */
  151.                         hDC = GetDC( hWnd );
  152.                         sprintf( str, "%c %c %c %c %c", clues[current_row][0],
  153.                             clues[current_row][1], clues[current_row][2],
  154.                             clues[current_row][3], clues[current_row][4] );
  155.                         TextOut( hDC, 300, 300 - ( current_row * 28 ), str,
  156.                             strlen( str ));
  157.                         ReleaseDC( hWnd, hDC );
  158.                         current_row++;
  159.                         if( current_row >= 10 ){
  160.                         /* Player lost. */
  161.                             guess_allowed = FALSE;
  162.                             sprintf( str, "The code was %d %d %d %d %d.",
  163.                                 goal[0], goal[1], goal[2], goal[3], goal[4] );
  164.                             MessageBox( hWnd, str, "Sorry. Better luck next time.",
  165.                                 MB_OK | MB_ICONINFORMATION );
  166.                         } else {
  167.                             for( col = 0; col < COLMAX; col++ ){
  168.                                 EnableWindow( hGameGrid[current_row-1][col], FALSE );
  169.                                 EnableWindow( hGameGrid[current_row][col], TRUE );
  170.                             }
  171.                         }
  172.                     }
  173.                 }
  174.                 break;
  175.              case IDM_NEW:
  176.                 guess_allowed = TRUE;
  177.                 InvalidateRect( hWnd, NULL, TRUE );
  178.                 SendMessage( hWnd, WM_CREATE, NULL, NULL );
  179.                 break;
  180.              case IDM_EXIT:
  181.                 SendMessage( hWnd, WM_CLOSE, 0, 0L );
  182.                 break;
  183.              case IDM_HELP:
  184.                 lpProcHelp = MakeProcInstance( HelpProc, hInst );
  185.  
  186.                 DialogBox( hInst, "HelpBox", hWnd, lpProcHelp );
  187.  
  188.                 FreeProcInstance( lpProcHelp );
  189.                 break;
  190.              case IDM_ABOUT:
  191.                 lpProcAbout = MakeProcInstance( AboutProc, hInst );
  192.  
  193.                 DialogBox( hInst, "AboutBox", hWnd, lpProcAbout );
  194.  
  195.                 FreeProcInstance( lpProcAbout );
  196.                 break;
  197.              default:
  198.                 return( DefWindowProc( hWnd, message, wParam, lParam ));
  199.             }
  200.         }
  201.         break;
  202.      case WM_PAINT:
  203.         hDC = BeginPaint( hWnd, &ps );
  204.         for( a = 0; a < ROWMAX; a++ ){
  205.             sprintf( str, "%c %c %c %c %c", clues[a][0], clues[a][1],
  206.                 clues[a][2], clues[a][3], clues[a][4] );
  207.             TextOut( hDC, 300, 300 - ( a * 28 ), str, strlen( str ));
  208.         }
  209.         EndPaint( hWnd, &ps );
  210.         break;
  211.      case WM_DESTROY:
  212.         DeleteObject( hSolidPen );
  213.         PostQuitMessage( 0 );
  214.         break;
  215.      default:
  216.         return( DefWindowProc( hWnd, message, wParam, lParam ));
  217.     }
  218.     return 0;
  219. }
  220.  
  221. /* -------------------------------------------------------- */
  222. BOOL FAR PASCAL
  223. AboutProc( HWND hDlg, WORD message, WORD wParam, LONG lParam ){
  224.     switch( message ){
  225.      case WM_INITDIALOG:
  226.         return TRUE;
  227.      case WM_COMMAND:
  228.         if(( wParam == IDOK ) || ( wParam == IDCANCEL )){
  229.             EndDialog( hDlg, TRUE );
  230.             return TRUE;
  231.         }
  232.         break;
  233.     }
  234.     return FALSE;
  235. }
  236.  
  237. /* -------------------------------------------------------- */
  238. BOOL FAR PASCAL
  239. HelpProc( HWND hDlg, WORD message, WORD wParam, LONG lParam ){
  240.     switch( message ){
  241.      case WM_INITDIALOG:
  242.         return TRUE;
  243.      case WM_COMMAND:
  244.         if(( wParam == IDOK ) || ( wParam == IDCANCEL )){
  245.             EndDialog( hDlg, TRUE );
  246.             return TRUE;
  247.         }
  248.         break;
  249.     }
  250.     return FALSE;
  251. }
  252.  
  253. /* -------------------------------------------------------- */
  254. void
  255. vInitialize(){
  256.     int x, y;
  257.  
  258.     for( x = 0; x < ROWMAX; x++ ){
  259.         for( y = 0; y < COLMAX; y++ ){
  260.             guess[x][y] = 0;
  261.             clues[x][y] = ' ';
  262.         }
  263.     }
  264.     current_row = 0;
  265. }
  266.  
  267. /* -------------------------------------------------------- */
  268. void
  269. vMakeGoal(){
  270.     int     x;
  271.     time_t  t;
  272.  
  273.     srand( (unsigned)time( &t ));
  274.     for( x = 0; x < COLMAX; x++ ){
  275.         goal[x] = ( rand() % 8 ) + 1;
  276.     }
  277. }
  278.  
  279. /* -------------------------------------------------------- */
  280. BOOL
  281. bCheckFill(){
  282.     int x;
  283.  
  284.     for( x = 0; x < COLMAX; x++ ){
  285.         if( guess[current_row][x] == 0 )
  286.             return FALSE;
  287.     }
  288.     return TRUE;
  289. }
  290.  
  291. /* -------------------------------------------------------- */
  292. BOOL
  293. bCheckGuess(){
  294.     int x, y = 0, z = 0;
  295.     int tgoal[COLMAX], tguess[COLMAX];
  296.  
  297.     for( x = 0; x < COLMAX; x++ ){
  298.         if( guess[current_row][x] == goal[x] ){
  299.             clues[current_row][z] = '1';
  300.             tguess[x] = 0;
  301.             tgoal[x] = -1;
  302.             z++;
  303.         } else {
  304.             tguess[x] = guess[current_row][x];
  305.             tgoal[x] = goal[x];
  306.         }
  307.     }
  308.  
  309.     if( z >= COLMAX )
  310.         return TRUE;    /* Player guessed the right number. */
  311.  
  312.     for( x = 0; x < COLMAX; x++ ){
  313.         for( y = 0; y < COLMAX; y++ ){
  314.             if( tguess[x] == tgoal[y] ){
  315.                 clues[current_row][z] = '0';
  316.                 tguess[x] = 0;
  317.                 tgoal[y] = -1;
  318.                 z++;
  319.                 break;
  320.             }
  321.         }
  322.     }
  323.     return FALSE;
  324. }
  325.  
  326. /* -------------------------------------------------------- */
  327. BOOL
  328. bMakeGameButtons( HWND hWnd ){
  329.     int     x, y, Bnum = 0;
  330.     HWND    hGuessButton;
  331.  
  332.     if( current_row >= ROWMAX )
  333.         current_row--;
  334.  
  335.     for( x = 0; x < ROWMAX; x++ ){
  336.         for( y = 0; y < COLMAX; y++ ){
  337.             if( hGameGrid[x][y] != NULL ){
  338.                 if( x == current_row )
  339.                     EnableWindow( hGameGrid[current_row][y], FALSE );
  340.                 SetWindowText( hGameGrid[x][y], "?" );
  341.                 Bnum++;
  342.             } else
  343.                 hGameGrid[x][y] = CreateWindow( "button", "?",
  344.                     BS_PUSHBUTTON | WS_CHILD | WS_DISABLED,
  345.                     50 + ( y * 40 ), 300 - ( x * 28 ), 40, 25,
  346.                     hWnd, Bnum++, hInst, NULL );
  347.             if( !hGameGrid[x][y] )
  348.                 return FALSE;
  349.             ShowWindow( hGameGrid[x][y], SW_SHOW );
  350.             UpdateWindow( hGameGrid[x][y] );
  351.         }
  352.     }
  353.  
  354.     if( hGuessButton != NULL )
  355.         DestroyWindow( hGuessButton );
  356.     hGuessButton = CreateWindow( "button", "Did I get it right?",
  357.         BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE, 65, 330, 170, 30,
  358.         hWnd, IDM_GUESS, hInst, NULL );
  359.     if( !hGuessButton )
  360.         return FALSE;
  361.     ShowWindow( hGuessButton, SW_SHOW );
  362.     UpdateWindow( hGuessButton );
  363.  
  364.     return TRUE;
  365. }
  366.  
  367. /* -------------------------------------------------------- */
  368.  
  369.