home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09963.iso / strategy / gchess.zip / CHESS.C < prev    next >
C/C++ Source or Header  |  1995-02-22  |  28KB  |  883 lines

  1.  
  2. /*
  3.   C source for GNU CHESS
  4.  
  5.   Revision: 1991-01-20
  6.  
  7.   Modified by Daryl Baker for use in MS WINDOWS environment
  8.  
  9.   Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  10.   Copyright (c) 1988, 1989, 1990  John Stanback
  11.  
  12.   This file is part of CHESS.
  13.  
  14.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  15.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  16.   the consequences of using it or for whether it serves any particular
  17.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  18.   General Public License for full details.
  19.  
  20.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  21.   only under the conditions described in the CHESS General Public License.
  22.   A copy of this license is supposed to have been given to you along with
  23.   CHESS so you can know your rights and responsibilities.  It should be in a
  24.   file named COPYING.  Among other things, the copyright notice and this
  25.   notice must be preserved on all copies.
  26. */
  27.  
  28. /*
  29. #define NOATOM                Minimize windows.h processing
  30. #define NOCLIPBOARD
  31. #define NOCREATESTRUCT
  32. #define NOFONT
  33. #define NOREGION
  34. #define NOSOUND
  35. #define NOWH
  36. #define NOCOMM
  37. #define NOKANJI
  38. */
  39.  
  40. #include <windows.h>          /* required for all Windows applications */
  41. #include <string.h>
  42. #include <time.h>
  43.  
  44. #include "gnuchess.h"
  45.  
  46. #include "defs.h"
  47. #include "chess.h"
  48. #include "saveopen.h"
  49. #include "color.h"
  50.  
  51. DWORD clrBackGround;       /* rgb structures for various colors */
  52. DWORD clrBlackSquare;
  53. DWORD clrWhiteSquare;
  54. DWORD clrBlackPiece;
  55. DWORD clrWhitePiece;
  56. DWORD clrText;
  57.  
  58. static HBRUSH hBrushBackGround;
  59.  
  60. short boarddraw[64];       /* Display copies of the board */
  61. short colordraw[64];       /* Needed because while computer is calculating*/
  62.                            /* moves it updates board and color thus you can*/
  63.                            /* not repaint the screen accuratly */
  64.  
  65. struct PIECEBITMAP pieces[7];
  66.  
  67. HANDLE hInst;              /* current instance */
  68. HANDLE hAccel;
  69.  
  70. char szAppName[] = "Chess";
  71.  
  72. extern char mvstr[4][6];
  73. extern GLOBALHANDLE hBook;
  74. int coords = 1;
  75.  
  76. static int FirstSq = -1;         /* Flag is a square is selected */
  77. static int GotFirst = FALSE;
  78. static int EditActive = FALSE;   /* Edit mode? */
  79. static int User_Move = TRUE;     /* User or computer's turn */
  80.  
  81. static HMENU hMainMenu;
  82.  
  83.  
  84. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine,
  85.                   int nCmdShow);
  86.  
  87. #ifdef WIN32
  88. static void MakeHelpPathName (char * szFileName);
  89. #else
  90. static void _near MakeHelpPathName (char * szFileName);
  91. #endif
  92.  
  93. #define EXE_NAME_MAX_SIZE  128
  94.  
  95.  
  96. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine,
  97.                   int nCmdShow)
  98. {
  99.     HWND hWnd;
  100.     MSG msg;
  101.     POINT pt;
  102.  
  103.     lpCmdLine ++;
  104.  
  105.     if (!hPrevInstance)
  106.        if (!ChessInit(hInstance)) return ((int)NULL);
  107.  
  108.     hInst = hInstance;            /* Saves the current instance         */
  109.  
  110.     QueryBoardSize (&pt);
  111.  
  112.     /* Create the mailn window.  It will be autosized in WM_CREATE message */
  113.     hWnd = CreateWindow(szAppName,    szAppName,
  114.           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  115.          CW_USEDEFAULT, CW_USEDEFAULT,
  116.          CW_USEDEFAULT, CW_USEDEFAULT,
  117.          NULL,    NULL,    hInstance,    NULL);
  118.  
  119.     if (!hWnd)    return ((int)NULL);
  120.  
  121.     ShowWindow(hWnd, nCmdShow);              /* Shows the window         */
  122.  
  123.    /* Initialize chess */
  124.    if (init_main ( hWnd ) ) {
  125.       SMessageBox (hWnd, IDS_INITERROR,IDS_CHESS);
  126.       FreeGlobals ();
  127.       return ((int)NULL);
  128.    }
  129.  
  130.    UpdateWindow(hWnd);                  /* Sends WM_PAINT message  */
  131.  
  132.    hAccel = LoadAccelerators (hInstance, szAppName);
  133.  
  134.    player = opponent;
  135.    ShowSidetoMove ();
  136.  
  137.    while (GetMessage(&msg, (HWND)NULL, (UINT)NULL, (UINT)NULL)) {
  138.       if ( !TranslateAccelerator (hWnd, hAccel, &msg) ) {
  139.         TranslateMessage(&msg);
  140.         DispatchMessage(&msg);
  141.       }
  142.     }
  143.  
  144.     return (msg.wParam);       /* Returns the value from PostQuitMessage */
  145. }
  146.  
  147.  
  148. long FAR PASCAL ChessWndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  149. {
  150.  
  151.    FARPROC lpProcAbout;
  152.    HMENU hMenu;
  153.    PAINTSTRUCT ps;
  154.    HDC hDC;
  155.    TEXTMETRIC tm;
  156.    int i;
  157.    POINT point;
  158.    OFSTRUCT pof;
  159.    char FileName[256], str[80];
  160.    WORD Status;
  161.    HFILE hFile;
  162.  
  163.    switch (message) {
  164.        case WM_CREATE: {                /* message: window being created */
  165.          int xchar, ychar;
  166.  
  167.          GetStartupColors ( szAppName);
  168.  
  169.          hBrushBackGround = CreateSolidBrush ( clrBackGround );
  170.  
  171.          for ( i=pawn; i<pawn+6; i++ ) pieces[i].piece = LoadBitmap (hInst, MAKEINTRESOURCE(PAWNBASE+i));
  172.          for ( i=pawn; i<pawn+6; i++ ) pieces[i].mask = LoadBitmap (hInst, MAKEINTRESOURCE(PAWNBASE+6+i));
  173.          for ( i=pawn; i<pawn+6; i++ ) pieces[i].outline = LoadBitmap (hInst, MAKEINTRESOURCE(PAWNBASE+12+i));
  174.  
  175.  
  176.          hDC = GetDC (hWnd);
  177.          GetTextMetrics ( hDC, &tm);
  178.          xchar = tm.tmMaxCharWidth;
  179.          ychar = tm.tmHeight+tm.tmExternalLeading;
  180.  
  181.          /*Autosize main window */
  182.          QueryBoardSize (&point);            
  183.          SetWindowPos( hWnd, hWnd, 0,0,
  184.                  point.x+GetSystemMetrics(SM_CXFRAME)*2+50,
  185.                  point.y+GetSystemMetrics(SM_CYFRAME)*2+GetSystemMetrics(SM_CYMENU)+
  186.                  GetSystemMetrics(SM_CYCAPTION) + ychar,
  187.                  SWP_NOMOVE | SWP_NOZORDER);
  188.  
  189.          ReleaseDC ( hWnd, hDC);
  190.  
  191.          InitHitTest ();
  192.          Create_Children ( hWnd, hInst, xchar, ychar);
  193.          break;
  194.       }
  195.  
  196.      case MSG_DESTROY:
  197.          DestroyWindow ( hWnd );
  198.          break;
  199.  
  200.      case WM_DESTROY: {          /* message: window being destroyed */
  201.          char  szHelpFileName[EXE_NAME_MAX_SIZE+1];
  202.          MakeHelpPathName(szHelpFileName);
  203.          WinHelp(hWnd,szHelpFileName,HELP_QUIT,0L);
  204.  
  205.          DeleteObject (hBrushBackGround);
  206.          Hittest_Destructor ();
  207.          if ( hBook ) FreeBook();
  208.          FreeGlobals ();
  209.          SaveColors ( szAppName);
  210.          PostQuitMessage(0);
  211.           break;
  212.       }
  213.  
  214.       case WM_PAINT:
  215.  
  216.          if ( FirstSq != -1 ) {        /*Properly repaint hilighted square*/
  217.             POINT pt; RECT rect;
  218.             QuerySqOrigin ( FirstSq%8, FirstSq/8, &pt);
  219.             rect.left = pt.x; rect.right=pt.x+48;
  220.             rect.top = pt.y-48; rect.bottom = pt.y;
  221.             InvalidateRect (hWnd, &rect, FALSE);
  222.          }
  223.  
  224.          hDC = BeginPaint ( hWnd, &ps);
  225.          Draw_Board ( hDC, flag.reverse, clrBlackSquare, clrWhiteSquare );
  226.          if ( coords ) DrawCoords ( hDC, flag.reverse, clrBackGround, clrText);
  227.          DrawAllPieces ( hDC, flag.reverse, boarddraw, colordraw, clrBlackPiece, clrWhitePiece );
  228.          EndPaint ( hWnd, &ps);
  229.  
  230.          if ( FirstSq != -1 ) HiliteSquare ( hWnd, FirstSq);
  231.          break;
  232.  
  233.  
  234. #ifdef WIN32
  235.       case WM_CTLCOLORSTATIC:/*Enable setting colors for the message controls*/
  236.       {
  237.          POINT point;
  238.  
  239.      UnrealizeObject ( hBrushBackGround );
  240.      SetBkColor ( (HDC)wParam, clrBackGround);
  241.      SetBkMode ( (HDC)wParam, TRANSPARENT);
  242.      SetTextColor ( (HDC)wParam, clrText);
  243.      point.x = point.y = 0;
  244.      ClientToScreen ( hWnd, &point);
  245.      SetBrushOrgEx ((HDC)wParam, point.x, point.y, NULL);
  246.      return (DWORD) hBrushBackGround;
  247.        }
  248. #else
  249.        case WM_CTLCOLOR:     /*Enable setting colors for the message controls*/
  250.        {
  251.      POINT point;
  252.      if ( HIWORD(lParam) == CTLCOLOR_STATIC) {
  253.        UnrealizeObject ( hBrushBackGround );
  254.        SetBkColor ( wParam, clrBackGround);
  255.        SetBkMode ( wParam, TRANSPARENT);
  256.        SetTextColor ( wParam, clrText);
  257.        point.x = point.y = 0;
  258.        ClientToScreen ( hWnd, &point);
  259.        SetBrushOrg (wParam, point.x, point.y);
  260.        return (DWORD) hBrushBackGround;
  261.      } else
  262.        return DefWindowProc ( hWnd, message, wParam, lParam);
  263.        }
  264. #endif
  265.  
  266.       case WM_ERASEBKGND:
  267.       {
  268.          RECT rect;
  269.          UnrealizeObject ( hBrushBackGround);
  270.          GetClientRect ( hWnd, &rect);
  271.          FillRect ( (HDC)wParam, &rect, hBrushBackGround);
  272.          return 1;
  273.       }
  274.  
  275.       case WM_INITMENUPOPUP:
  276.          if ( !EditActive ) Init_Menus (hWnd, wParam, lParam);
  277.          break;
  278.  
  279.       case WM_LBUTTONDOWN:
  280.  
  281.          /* If computer is thinking on human's time stop it at the first
  282.             button click.  add test to ensure that "human" can't interupt
  283.             the computer from thinking through its turn */
  284.          if ( User_Move ) {
  285.             flag.timeout = true;
  286.             flag.bothsides = false;
  287.          }
  288.  
  289.          /* Don't continue unless reason to */
  290.          if ( !(EditActive || User_Move)) break;
  291.  
  292.  
  293. #ifdef WIN32
  294.      POINTSTOPOINT(point, MAKEPOINTS(lParam));
  295. #else
  296.          point = MAKEPOINT (lParam );
  297. #endif
  298.  
  299.       {
  300.          int Hit;
  301.  
  302.          Hit = HitTest (point.x, point.y );
  303.  
  304.          if ( Hit == -1 ){
  305.             if ( FirstSq != -1) {
  306.                UnHiliteSquare ( hWnd, FirstSq);
  307.                GotFirst = FALSE;
  308.                FirstSq = -1;
  309.             }
  310.             break;
  311.          }
  312.  
  313.          if ( GotFirst ) {
  314.             UnHiliteSquare( hWnd, FirstSq);
  315.             GotFirst = FALSE;
  316.  
  317.             if ( EditActive == TRUE) {
  318.                PostMessage ( hWnd, MSG_EDITBOARD,((FirstSq<<8)|Hit), 
  319.                 (LONG) NULL);
  320.             } else if ( User_Move == TRUE) {
  321.                PostMessage ( hWnd, MSG_USER_ENTERED_MOVE, ((FirstSq<<8)|Hit), 
  322.                 (LONG) NULL);
  323.             }
  324.             FirstSq = -1;
  325.          } else {
  326.             GotFirst = TRUE;
  327.             FirstSq = Hit;
  328.             HiliteSquare ( hWnd, Hit);
  329.          }
  330.       }
  331.          break;
  332.  
  333.       case MSG_EDITBOARD:
  334.       {
  335.          int Square, First;
  336.  
  337.          if ( flag.reverse ) {
  338.             First = 63 - ((wParam>>8) & 0xff);
  339.             Square  = 63 - (wParam & 0xff);
  340.          } else {
  341.             First = (wParam>>8) & 0xff;
  342.             Square  = wParam & 0xff;
  343.          }
  344.          
  345.          board[Square] = board[First];
  346.          color[Square] = color[First];
  347.  
  348.          board[First] = no_piece;
  349.          color[First] = neutral;
  350.  
  351.          UpdateDisplay (hWnd, First, Square, false, false);
  352.       }
  353.          break;
  354.  
  355.       case MSG_USER_MOVE:
  356.          if ( flag.bothsides && !flag.mate ) {
  357.             SelectMove ( hWnd, opponent, 1);
  358.             if ( flag.beep ) MessageBeep (0);
  359.             PostMessage ( hWnd, MSG_COMPUTER_MOVE, (WPARAM)NULL, (LPARAM)NULL);
  360.          } else if (!flag.mate) {
  361.             User_Move = TRUE;
  362.  
  363.             ft = 0;
  364.             player = opponent;
  365.             ShowSidetoMove ();
  366.          {
  367.             /* Set up to allow computer to think while user takes move*/
  368.             int tmp; unsigned short mv; char s[10];
  369.             if ( hint>0 && !flag.easy && Book == NULL) {
  370.                time0 = time ( NULL);
  371.                algbr ( hint>>8, hint&0xff, false);
  372.                lstrcpy ( s, mvstr[0]);
  373.                tmp = epsquare;
  374.                if ( VerifyMove (hWnd, s,1, &mv) ) {
  375.                   SelectMove ( hWnd, computer, 2);
  376.                   VerifyMove ( hWnd, mvstr[0], 2, &mv);
  377.                   if ( Sdepth>0 ) Sdepth --;
  378.                }
  379.                ft = time (NULL) - time0;
  380.                epsquare = tmp;
  381.             }
  382.             }
  383.          }
  384.          break;
  385.  
  386.       case MSG_USER_ENTERED_MOVE:
  387.       {
  388.          int temp; unsigned short mv; int Square,First; char str[10];
  389.          int algbr_flag;
  390.          User_Move = FALSE;
  391. /*         player = opponent;*/
  392.  
  393.          /* Fix coord's if user "reversed" board */
  394.          if ( flag.reverse ) {
  395.             First = 63 - ((wParam>>8) & 0xff);
  396.             Square  = 63 - (wParam & 0xff);
  397.          } else {
  398.             First = (wParam>>8) & 0xff;
  399.             Square  = wParam & 0xff;
  400.          }
  401.  
  402.          /* Logic to allow selection for pawn promotion */
  403.          if ( (board[First] == pawn) &&( (Square <8) || (Square>55)) ) {
  404.             algbr_flag = promote + PromoteDialog (hWnd, hInst);
  405.          } else algbr_flag = 0;
  406.          algbr ( First, Square, algbr_flag);
  407.  
  408.       /* Entry point for manual entry of move */
  409.       case MSG_MANUAL_ENTRY_POINT:
  410.  
  411.          strcpy ( str, mvstr[0] );
  412.          
  413.          temp = VerifyMove ( hWnd, str, 0, &mv);
  414.          if ( !temp) PostMessage ( hWnd, MSG_USER_MOVE, (WPARAM)NULL, 
  415.                   (LPARAM)NULL);
  416.          else {
  417.             ElapsedTime (1);
  418.             if ( flag.force ) {
  419.                computer = opponent;
  420.                opponent = otherside[computer];
  421.             }
  422.             if ( mv != hint) {
  423.                Sdepth = 0;
  424.                ft = 0;
  425.                PostMessage ( hWnd, MSG_COMPUTER_MOVE, (WPARAM)NULL, 
  426.                 (LPARAM)NULL);
  427.             } else {
  428.                PostMessage ( hWnd, MSG_COMPUTER_MOVE, (WPARAM)NULL, 
  429.                 (LPARAM)NULL);
  430.             }
  431.          }
  432.       }
  433.          break;
  434.  
  435.       case MSG_COMPUTER_MOVE:
  436.          if ( !(flag.quit || flag.mate || flag.force) ) {
  437.             SelectMove ( hWnd, computer, 1);
  438.             if ( flag.beep ) MessageBeep (0);
  439.          }
  440.          PostMessage ( hWnd, MSG_USER_MOVE, (WPARAM)NULL, 
  441.               (LPARAM)NULL);
  442.          break;
  443.  
  444.  
  445.       case WM_CHAR:
  446.          /* Allow Control C to abort bothsides (autoplay) mode */
  447.          /* And abort computer thinking */
  448.          if ( wParam == 3 ) {
  449.             flag.timeout = true;
  450.             flag.bothsides = false;
  451.          } else if ( wParam == VK_ESCAPE ) {
  452.             ShowWindow ( hWnd, SW_MINIMIZE);
  453.          }
  454.          break;
  455.  
  456.       case WM_KEYDOWN:
  457.          if ( User_Move && wParam == VK_F2 ) {   /* To invoke manual move entry */
  458.             char tmpmove[8];
  459.             flag.timeout = true;
  460.             flag.bothsides = false;
  461.             if ( GotFirst ) {
  462.                UnHiliteSquare( hWnd, FirstSq);
  463.                GotFirst = FALSE;
  464.                FirstSq = -1;
  465.             }
  466.             if ( DoManualMoveDlg (hInst, hWnd, tmpmove) ){
  467.                strcpy ( mvstr[0], tmpmove);
  468.                PostMessage ( hWnd, MSG_MANUAL_ENTRY_POINT, 0, 0);
  469.             }
  470.          }
  471.          break;
  472.  
  473.       case WM_SYSCOMMAND:
  474.          if (  wParam == SC_CLOSE ) {     /*Abort easy mode */
  475.             flag.timeout = true;
  476.             flag.bothsides = false;
  477.             PostMessage ( hWnd, MSG_DESTROY, 0, 0);
  478.          } else return (DefWindowProc(hWnd, message, wParam, lParam));
  479.          break;
  480.  
  481.       case WM_CLOSE:
  482.          flag.timeout = true;
  483.          flag.bothsides = false;
  484.          PostMessage ( hWnd, MSG_DESTROY, 0, 0);
  485.          break;
  486.          
  487.       /* Menu item processing */
  488.  
  489.       case WM_COMMAND:
  490.  
  491.          /* When we execute a command stop any look ahead */
  492.          /* Then call actual routine to process */
  493.          flag.timeout = true;
  494.          flag.bothsides = false;
  495.  
  496.          PostMessage ( hWnd, MSG_WM_COMMAND, wParam, lParam);
  497.          break;
  498.       
  499.  
  500.       case MSG_WM_COMMAND:
  501.          switch ( wParam ) {
  502.             case MSG_CHESS_QUIT:
  503. /*               DestroyWindow ( hWnd);*/
  504.                  PostMessage ( hWnd, MSG_DESTROY, 0, 0);
  505.                break;
  506.  
  507.             case MSG_CHESS_HINT:
  508.                GiveHint (hWnd);
  509.                break;
  510.  
  511.             case MSG_CHESS_LIST:
  512.                if ( !DoFileSaveDlg ( hInst, hWnd, "chess.lst", ".lst", &Status,
  513.                                FileName, &pof) ) break;
  514.                if ( Status == 1 ) {
  515.                   strcpy ( str, "Replace Existing ");
  516.                   strcat ( str, FileName);
  517.                   if ( MessageBox ( hWnd, str, szAppName, MB_YESNO | 
  518.                    MB_ICONQUESTION | MB_APPLMODAL) == IDNO)
  519.                      break;
  520.                   
  521.                } else OpenFile (FileName, &pof, OF_PARSE);
  522.  
  523.                ListGame ( hWnd, pof.szPathName );
  524.                break;
  525.  
  526.             case MSG_CHESS_SAVE:
  527.                if ( !DoFileSaveDlg ( hInst, hWnd, "chess.chs", ".chs", &Status,
  528.                                FileName, &pof) ) break;
  529.                if ( Status == 1 ) {
  530.                   strcpy ( str, "Replace Existing ");
  531.                   strcat ( str, FileName);
  532.                   if ( MessageBox ( hWnd, str, szAppName,
  533.                    MB_YESNO | MB_ICONQUESTION | 
  534.                    MB_APPLMODAL) == IDNO)
  535.                      break;
  536.                   
  537.                } else OpenFile (FileName, &pof, OF_PARSE);
  538.  
  539.                SaveGame ( hWnd, pof.szPathName );
  540.                break;
  541.  
  542.             case MSG_CHESS_GET:
  543.                if ( !DoFileOpenDlg ( hInst, hWnd, "*.chs", ".chs",0x0, FileName, &pof))
  544.                   break;
  545.  
  546.  
  547.                if ( (hFile=OpenFile(FileName, &pof, OF_READ|OF_REOPEN)) 
  548.            == (HFILE)-1) {
  549.                   strcpy ( str, "Cannot open file: ");
  550.                   strcat ( str, FileName);
  551.                   MessageBox (hWnd, str, szAppName,
  552.                               MB_OK | MB_APPLMODAL | MB_ICONQUESTION);
  553.                   break;
  554.                }
  555.                _lclose ( hFile );
  556.                GetGame ( hWnd, pof.szPathName );
  557.                break;
  558.  
  559.             case MSG_CHESS_NEW:
  560.                NewGame(hWnd);
  561.                if ( hBook ) FreeBook();
  562.                GetOpenings ( hWnd);
  563.                break;
  564.  
  565.             case MSG_CHESS_ABOUT:
  566.                  lpProcAbout = MakeProcInstance(About, hInst);
  567.                  DialogBox(hInst, MAKEINTRESOURCE(AboutBox),   hWnd,  lpProcAbout);
  568.                  FreeProcInstance(lpProcAbout);
  569.                break;
  570.  
  571.             case MSG_CHESS_EDIT:
  572.                EditActive = TRUE;
  573.                hMenu = CreateMenu ();
  574.                AppendMenu ( hMenu, MF_STRING, MSG_CHESS_EDITDONE,"&Done");
  575.                hMainMenu = GetMenu ( hWnd);
  576.                SetMenu ( hWnd, hMenu);
  577.                DrawMenuBar ( hWnd);
  578.                break;
  579.  
  580.             case MSG_CHESS_EDITDONE:
  581.                EditActive = FALSE;
  582.                hMenu = GetMenu ( hWnd );
  583.                SetMenu ( hWnd, hMainMenu );
  584.                DrawMenuBar ( hWnd );
  585.                DestroyMenu ( hMenu );
  586.                GameCnt = 0;
  587.                Game50 = 1;
  588.                ZeroRPT ();
  589.                Sdepth = 0;
  590.                InitializeStats ();
  591.                PostMessage ( hWnd, MSG_USER_MOVE, (WPARAM)NULL, 
  592.                 (LPARAM)NULL);
  593.                break;
  594.  
  595.             case MSG_CHESS_REVIEW:
  596.                ReviewDialog ( hWnd, hInst );
  597.                break;
  598.  
  599.             case MSG_CHESS_TEST:
  600.                TestDialog (hWnd, hInst);
  601.                break;
  602.         
  603.             case MSG_CHESS_HASH:
  604.                flag.hash = !flag.hash;
  605.                break;
  606.  
  607.             case MSG_CHESS_BEEP:
  608.                flag.beep = !flag.beep;
  609.                break;
  610.  
  611.             case MSG_CHESS_COORD:
  612.                coords = !coords;
  613.                UpdateDisplay(hWnd,0,0,1,0);
  614.                break;
  615.  
  616.             case MSG_CHESS_BOTH:
  617.                flag.bothsides = !flag.bothsides;
  618.                flag.easy = true;
  619.                Sdepth = 0;
  620.                PostMessage ( hWnd, MSG_USER_MOVE, (WPARAM)NULL, 
  621.                 (LPARAM)NULL);
  622.                break;
  623.  
  624.             case MSG_CHESS_BOOK:
  625.                if ( Book != NULL ) {
  626.                   FreeBook ();
  627.                   Book = NULL;
  628.                }
  629.                break;
  630.  
  631.             case MSG_CHESS_POST:
  632.                if ( flag.post ) {
  633.                   SendMessage ( hStats, WM_SYSCOMMAND, SC_CLOSE, 0 );
  634.                   flag.post = false;
  635.                } else {
  636.                   StatDialog ( hWnd, hInst);
  637.                   flag.post = TRUE;
  638.                }
  639.                break;
  640.  
  641.             case MSG_CHESS_AWIN: {
  642.                char str[40];
  643.  
  644. #ifdef WIN32
  645.                LoadString ( (HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE),
  646. #else
  647.                LoadString ( GetWindowWord(hWnd,GWW_HINSTANCE),
  648. #endif
  649.                             IDS_SETAWIN, str, sizeof ( str ) ),
  650.                Awindow = DoGetNumberDlg ( hInst, hWnd, str, Awindow);
  651.                break;
  652.             }
  653.  
  654.             case MSG_CHESS_BWIN: {
  655.                char str[40];
  656.  
  657. #ifdef WIN32
  658.                LoadString ( (HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE),
  659. #else
  660.                LoadString ( GetWindowWord(hWnd,GWW_HINSTANCE),
  661. #endif
  662.                             IDS_SETBWIN, str, sizeof ( str ) ),
  663.                Bwindow = DoGetNumberDlg ( hInst, hWnd, str, Bwindow);
  664.                break;
  665.             }
  666.  
  667.             case MSG_CHESS_CONTEMP: {
  668.                char str[40];
  669.  
  670. #ifdef WIN32
  671.                LoadString ( (HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE),
  672. #else
  673.                LoadString ( GetWindowWord(hWnd,GWW_HINSTANCE),
  674. #endif
  675.                             IDS_SETCONTEMPT, str, sizeof ( str ) ),
  676.                contempt = DoGetNumberDlg ( hInst, hWnd, str, contempt);
  677.                break;
  678.             }
  679.  
  680.             case MSG_CHESS_UNDO:
  681.                if ( GameCnt >0 ) {
  682.                   Undo(hWnd);
  683.                   player = opponent;
  684.                   ShowSidetoMove ();
  685.                }
  686.                break;
  687.  
  688.             case MSG_CHESS_REMOVE:
  689.                if ( GameCnt > 1) {
  690.                   Undo(hWnd); Undo (hWnd);
  691.                   ShowSidetoMove ();
  692.  
  693.                }
  694.                break;
  695.  
  696.             case MSG_CHESS_FORCE:
  697.                flag.force = !flag.force;
  698.                player = opponent;
  699.                ShowPlayers ();
  700.                break;
  701.  
  702.             case MSG_CHESS_RANDOM:
  703.                if ( dither==0 ) dither=6; else dither = 0;
  704.                break;
  705.  
  706.             case MSG_CHESS_EASY:
  707.                flag.easy = !flag.easy;
  708.                break;
  709.  
  710.             case MSG_CHESS_DEPTH: {
  711.                char str[40];
  712.  
  713.                LoadString ( (HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE),
  714.                             IDS_MAXSEARCH, str, sizeof ( str ) ),
  715.                MaxSearchDepth = DoGetNumberDlg ( hInst, hWnd, str, 
  716.                         MaxSearchDepth);
  717.                break;
  718.             }
  719.  
  720.             case MSG_CHESS_REVERSE:
  721.                flag.reverse = !flag.reverse;
  722.                ShowPlayers ();
  723.                UpdateDisplay(hWnd,0,0,1,0);
  724.                break;
  725.  
  726.             case MSG_CHESS_SWITCH:
  727.                computer = otherside[computer];
  728.                opponent = otherside[opponent];
  729.                flag.force = false;
  730.                Sdepth = 0;
  731.                ShowPlayers ();
  732.                PostMessage ( hWnd, MSG_COMPUTER_MOVE, (WPARAM)NULL, 
  733.                 (LPARAM)NULL);
  734.                break;
  735.  
  736.             case MSG_CHESS_BLACK:
  737.                computer = black;
  738.                opponent = white;
  739.                flag.force = false;
  740.                Sdepth = 0;
  741.                ShowPlayers ();
  742.                PostMessage ( hWnd, MSG_COMPUTER_MOVE, (WPARAM)NULL, 
  743.                 (LPARAM)NULL);
  744.                break;
  745.  
  746.             case MSG_CHESS_WHITE:
  747.                computer = white;
  748.                opponent = black;
  749.                flag.force = false;
  750.                Sdepth = 0;
  751.                ShowPlayers ();
  752.                PostMessage ( hWnd, MSG_COMPUTER_MOVE, (WPARAM)NULL, 
  753.                 (LPARAM)NULL);
  754.                break;
  755.  
  756.             case IDM_BACKGROUND:
  757.                if (ColorDialog ( hWnd, hInst, wParam) ) {
  758.                   InvalidateRect (hWnd, NULL, TRUE);
  759.                   DeleteObject (hBrushBackGround);
  760.                   hBrushBackGround = CreateSolidBrush ( clrBackGround );
  761.  
  762.                   /*Invalidate the text windows so they repaint */
  763.                   InvalidateRect (hComputerColor, NULL, TRUE);
  764.                   InvalidateRect (hComputerMove, NULL, TRUE);
  765.                   InvalidateRect (hWhosTurn, NULL, TRUE);
  766.                   InvalidateRect (hClockHuman, NULL, TRUE);
  767.                   InvalidateRect (hClockComputer, NULL, TRUE);
  768.                   InvalidateRect (hMsgComputer, NULL, TRUE);
  769.                   InvalidateRect (hMsgHuman, NULL, TRUE);
  770.                }
  771.                break;
  772.  
  773.             case IDM_BLACKSQUARE:
  774.                if (ColorDialog ( hWnd, hInst, wParam) ) {
  775.                   InvalidateRect (hWnd, NULL, TRUE);
  776.                }
  777.                break;
  778.  
  779.             case IDM_WHITESQUARE:
  780.                if (ColorDialog ( hWnd, hInst, wParam) ) {
  781.                   InvalidateRect (hWnd, NULL, TRUE);
  782.                }
  783.                break;
  784.  
  785.             case IDM_BLACKPIECE:
  786.                if (ColorDialog ( hWnd, hInst, wParam) ) {
  787.                   InvalidateRect (hWnd, NULL, TRUE);
  788.                }
  789.                break;
  790.  
  791.             case IDM_WHITEPIECE:
  792.                if (ColorDialog ( hWnd, hInst, wParam) ) {
  793.                   InvalidateRect (hWnd, NULL, TRUE);
  794.                }
  795.                break;
  796.  
  797.             case IDM_TEXT:
  798.                if ( ColorDialog (hWnd, hInst, wParam) ) {
  799.                   /*Invalidate the text windows so they repaint */
  800.                   InvalidateRect (hWnd, NULL, TRUE);
  801.                   InvalidateRect (hComputerColor, NULL, TRUE);
  802.                   InvalidateRect (hComputerMove, NULL, TRUE);
  803.                   InvalidateRect (hWhosTurn, NULL, TRUE);
  804.                   InvalidateRect (hClockHuman, NULL, TRUE);
  805.                   InvalidateRect (hClockComputer, NULL, TRUE);
  806.                   InvalidateRect (hMsgComputer, NULL, TRUE);
  807.                   InvalidateRect (hMsgHuman, NULL, TRUE);
  808.                }
  809.                break;
  810.  
  811.             case IDM_DEFAULT:
  812.                SetStandardColors ();
  813.                InvalidateRect (hWnd, NULL, TRUE);
  814.                DeleteObject (hBrushBackGround);
  815.                hBrushBackGround = CreateSolidBrush ( clrBackGround );
  816.  
  817.                /*Invalidate the text windows so they repaint */
  818.                InvalidateRect (hComputerColor, NULL, TRUE);
  819.                InvalidateRect (hComputerMove, NULL, TRUE);
  820.                InvalidateRect (hWhosTurn, NULL, TRUE);
  821.                InvalidateRect (hClockHuman, NULL, TRUE);
  822.                InvalidateRect (hClockComputer, NULL, TRUE);
  823.                InvalidateRect (hMsgComputer, NULL, TRUE);
  824.                InvalidateRect (hMsgHuman, NULL, TRUE);
  825.                break;
  826.  
  827.             case IDM_TIMECONTROL:
  828.                if ( TimeControlDialog (hWnd, hInst, wParam) ) {
  829.                   TCflag = (TCmoves>1);
  830.                   if (TCflag) {
  831.                   }
  832.                   SetTimeControl ();
  833.                }
  834.                break;
  835.  
  836.             case MSG_HELP_INDEX: {
  837.                char  szHelpFileName[EXE_NAME_MAX_SIZE+1];
  838.                MakeHelpPathName(szHelpFileName);
  839.                WinHelp(hWnd,szHelpFileName,HELP_INDEX,0L);
  840.                break;
  841.             }
  842.  
  843.             case MSG_HELP_HELP:           /*Help on Help*/
  844.                  WinHelp(hWnd,"WINHELP.HLP",HELP_INDEX,0L);
  845.                break;
  846.  
  847.          }
  848.          break;
  849.  
  850.       default:            /* Passes it on if unproccessed    */
  851.          return (DefWindowProc(hWnd, message, wParam, lParam));
  852.     }
  853.     return ((long)NULL);
  854. }
  855.  
  856. static void _NEAR MakeHelpPathName (char * szFileName)
  857. {
  858.    char *  pcFileName;
  859.    int     nFileNameLen;
  860.  
  861.    nFileNameLen = GetModuleFileName(hInst,szFileName,EXE_NAME_MAX_SIZE);
  862.    pcFileName = szFileName + nFileNameLen;
  863.  
  864.    while (pcFileName > szFileName) {
  865.        if (*pcFileName == '\\' || *pcFileName == ':') {
  866.            *(++pcFileName) = '\0';
  867.            break;
  868.        }
  869.    nFileNameLen--;
  870.    pcFileName--;
  871.    }
  872.  
  873.    if ((nFileNameLen+13) < EXE_NAME_MAX_SIZE) {
  874.        lstrcat(szFileName, "chess.hlp");
  875.    }
  876.  
  877.    else {
  878.        lstrcat(szFileName, "?");
  879.    }
  880.  
  881.    return;
  882. }
  883.