home *** CD-ROM | disk | FTP | other *** search
/ MORE WinGames / WINGAMES.iso / chess / chess.c < prev    next >
C/C++ Source or Header  |  1991-06-16  |  27KB  |  815 lines

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