home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 7 Games / 07-Games.zip / 32PMCHES.ZIP / BOOK.C < prev    next >
Text File  |  1991-04-30  |  10KB  |  355 lines

  1. //
  2. //  Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  3. //  Copyright (c) 1988, 1989, 1990  John Stanback
  4. //
  5. //  Project:    OS/2 PM Port of GNU CHESS 3.1 (PmChess)
  6. //
  7. //  Version:    1.02  1990-12-17
  8. //
  9. //   Module:    Book Move Load Logic (Book.c)
  10. //
  11. //   Porter:    Ported to Windows 3.0 by Darly Baker
  12. //
  13. //   Porter:    Ported to OS/2 1.2+ by Kent Cedola
  14. //
  15. //   Update:    Fixes and enhancements by Benny N. Ormson
  16. //
  17. //   System:    OS2 1.2 using Microsoft C 6.0
  18. //
  19. //  Remarks:    Since OS/2 doesn't have all the restrictions of Windows, I
  20. //              restored most of this code from the MS-DOS version of
  21. //              GNU Chess.
  22. //
  23. //  License:
  24. //
  25. //    CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  26. //    WARRANTY.  No author or distributor accepts responsibility to anyone for
  27. //    the consequences of using it or for whether it serves any particular
  28. //    purpose or works at all, unless he says so in writing.  Refer to the
  29. //    CHESS General Public License for full details.
  30. //
  31. //    Everyone is granted permission to copy, modify and redistribute CHESS,
  32. //    but only under the conditions described in the CHESS General Public
  33. //    License.  A copy of this license is supposed to have been given to you
  34. //    along with CHESS so you can know your rights and responsibilities.  It
  35. //    should be in a file named COPYING.  Among other things, the copyright
  36. //    notice and this notice must be preserved on all copies.
  37. //
  38. #define INCL_DOS
  39. #define INCL_PM
  40. #include <os2.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <memory.h>
  45. #include "PmChess.h"
  46. #include "GNUChess.h"
  47. #include "Defs.h"
  48. #include "Resource.h"
  49.  
  50.  
  51. void
  52. GetOpenings (HWND hWnd)
  53. /*
  54.    Read in the Opening Book file and parse the algebraic notation for a move
  55.    into an unsigned integer format indicating the from and to square. Create
  56.    a linked list of opening lines of play, with entry->next pointing to the
  57.    next line and entry->move pointing to a chunk of memory containing the
  58.    moves. More Opening lines of up to 256 half moves may be added to
  59.    pmchess.boo.
  60. */
  61.  
  62. {
  63.   FILE *fd;
  64.   int c, i, j, side;
  65.   /* char buffr[2048]; */
  66.   struct BookEntry *entry;
  67.   unsigned short mv, *mp, tmp[100];
  68.   static struct BookEntry *BookOrg = NULL;       /* Ormson 12/13/90  */
  69.  
  70.    if(BookOrg) {                                 /* Ormson 12/13/90  */
  71.       Book = BookOrg;                            /* Ormson 12/13/90  */
  72.       return;                                    /* Ormson 12/13/90  */
  73.    }                                             /* Ormson 12/13/90  */
  74.  
  75.   if ((fd = fopen ("pmchess.boo", "r")) != NULL) /* Ormson 12/13/90  */
  76.     {
  77.       /* setvbuf(fd,buffr,_IOFBF,2048); */
  78.       Book = NULL;
  79.       i = 0;
  80.       side = white;
  81.       while ((c = parse (fd, &mv, side)) >= 0)
  82.    if (c == 1)
  83.      {
  84.        tmp[++i] = mv;
  85.        side = otherside[side];
  86.      }
  87.    else if (c == 0 && i > 0)
  88.      {
  89.        entry = (struct BookEntry *) malloc (sizeof (struct BookEntry));
  90.        mp = (unsigned short *) malloc ((i + 1) * sizeof (unsigned short));
  91.        entry->mv = mp;
  92.        entry->next = Book;
  93.        Book = entry;
  94.        for (j = 1; j <= i; j++)
  95.          *(mp++) = tmp[j];
  96.        *mp = 0;
  97.        i = 0;
  98.        side = white;
  99.      }
  100.       fclose (fd);
  101.       BookOrg = Book;                   /* Ormson 12/13/90           */
  102.     }
  103.   else
  104.     {
  105.     SMessageBox(hWnd, IDS_OBNF, IDS_CHESS);
  106.     }
  107. }
  108.  
  109. int
  110. parse (fd, mv, side)
  111.      FILE *fd;
  112.      short unsigned int *mv;
  113.      short int side;
  114. {
  115.   int c, i, r1, r2, c1, c2;
  116.   char s[100];
  117.   while ((c = getc (fd)) == ' ') ;
  118.   i = 0;
  119.   s[0] = (CHAR)c;
  120.   while (c != ' ' && c != '\n' && c != EOF)
  121.     s[++i] = (CHAR)(c = getc (fd));
  122.   s[++i] = '\0';
  123.   if (c == EOF)
  124.     return (-1);
  125.   if (s[0] == '!' || s[0] == ';' || i < 3)
  126.     {
  127.       while (c != '\n' && c != EOF)
  128.    c = getc (fd);
  129.       return (0);
  130.     }
  131.   if (s[4] == 'o')
  132.     if (side == black)
  133.       *mv = 0x3C3A;
  134.     else
  135.       *mv = 0x0402;
  136.   else if (s[0] == 'o')
  137.     if (side == black)
  138.       *mv = 0x3C3E;
  139.     else
  140.       *mv = 0x0406;
  141.   else
  142.     {
  143.       c1 = s[0] - 'a';
  144.       r1 = s[1] - '1';
  145.       c2 = s[2] - 'a';
  146.       r2 = s[3] - '1';
  147.       *mv = (locn (r1, c1) << 8) + locn (r2, c2);
  148.     }
  149.   return (1);
  150. }
  151.  
  152. void
  153. OpeningBook (unsigned short *hint)
  154.  
  155. /*
  156.   Go thru each of the opening lines of play and check for a match with the
  157.   current game listing. If a match occurs, generate a random number. If this
  158.   number is the largest generated so far then the next move in this line
  159.   becomes the current "candidate". After all lines are checked, the
  160.   candidate move is put at the top of the Tree[] array and will be played by
  161.   the program. Note that the program does not handle book transpositions.
  162. */
  163.  
  164. {
  165.   short j, pnt;
  166.   unsigned short m, *mp;
  167.   unsigned r, r0;
  168.   struct BookEntry *p;
  169.  
  170.   srand ((unsigned int) time0);
  171.   r0 = m = 0;
  172.   p = Book;
  173.   while (p != NULL) {
  174.       mp = p->mv;
  175.       for (j = 1; j <= GameCnt; j++)    /* Ormson 12/13/90           */
  176.          if (GameList[j].gmove != *(mp++))
  177.             break;
  178.       if (j > GameCnt)
  179.          if ((r = rand ()) > r0) {
  180.             r0 = r;
  181.             m = *mp;
  182.             *hint = *(++mp);
  183.          }
  184.       p = p->next;
  185.   }
  186.  
  187.   for (pnt = TrPnt[1]; pnt < TrPnt[2]; pnt++)
  188.     if (((Tree[pnt].f << 8) | Tree[pnt].t) == (SHORT)m)
  189.       Tree[pnt].score = 0;
  190.   pick (TrPnt[1], TrPnt[2] - 1);
  191.   if (Tree[TrPnt[1]].score < 0)
  192.     Book = NULL;
  193. }
  194.  
  195.  
  196.  
  197. #ifdef oldjunk
  198.  
  199. static unsigned int book_used = 0;
  200. static char FAR * xBook;
  201. GLOBALHANDLE hBook = 0;
  202.  
  203. void FreeBook (void)
  204. {
  205.    GlobalUnlock ( hBook );
  206.    GlobalFree ( hBook);
  207.    hBook = 0;
  208.    book_used = 0;
  209. }
  210.  
  211. static void FAR *Book_alloc ( unsigned int size )
  212. {
  213.     char FAR * temp;
  214.     if ( book_used > 32*1024 ) return (void FAR *)0;
  215.     temp = xBook+book_used;
  216.     book_used += size;
  217.     return temp;
  218. }
  219.  
  220.  
  221. void
  222. GetOpenings (HWND hWnd)
  223.  
  224. /*
  225.    Read in the Opening Book file and parse the algebraic notation for a move
  226.    into an unsigned integer format indicating the from and to square. Create
  227.    a linked list of opening lines of play, with entry->next pointing to the
  228.    next line and entry->move pointing to a chunk of memory containing the
  229.    moves. More Opening lines of up to 256 half moves may be added to
  230.    pmchess.boo.
  231. */
  232. #ifndef BOOK
  233. #define BOOK "/usr/games/lib/gnuchess.book"
  234. #endif /* BOOK */
  235. {
  236.   FILE *fd;
  237.   int c, i, j, side;
  238.  
  239.   /* char buffr[2048]; */
  240.   struct BookEntry FAR  *entry;
  241.   unsigned short mv, FAR  *mp, tmp[100];
  242.   char lpFile[_MAX_FNAME+_MAX_EXT+_MAX_DRIVE+_MAX_DIR+1];
  243.   char sFname[_MAX_FNAME], sExt[_MAX_EXT], sDrive[_MAX_DRIVE], sDir[_MAX_DIR];
  244.  
  245.   GetModuleFileName ( GetWindowWord(hWnd, GWW_HINSTANCE),
  246.                       lpFile, sizeof(lpFile) );
  247.  
  248.   _splitpath ( lpFile, sDrive, sDir, sFname, sExt);
  249.   _makepath  ( lpFile, sDrive, sDir, "pmchess", "boo"); /* Ormson    */
  250.  
  251.   fd = fopen (lpFile, "r");
  252.  
  253. /*  if ((fd = fopen (BOOK, "r")) == NULL)
  254.     fd = fopen ("pmchess.boo", "r"); */ /* Ormson 12/13/90           */
  255.   if (fd != NULL)
  256.     {
  257.       hBook = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  258.                             (long) (32*1024 * sizeof (char)) );
  259.  
  260.       if( hBook == NULL )
  261.         {
  262.            Book = NULL;
  263.            SMessageBox (hWnd, IDS_OBAE, IDS_CHESS);
  264.            return;
  265.         }
  266.       xBook = (unsigned char FAR *) GlobalLock (hBook);
  267.  
  268.  
  269.       /* setvbuf(fd,buffr,_IOFBF,2048); */
  270.       Book = NULL;
  271.       i = 0;
  272.       side = white;
  273.       while ((c = parse (fd, &mv, side)) >= 0)
  274.         if (c == 1)
  275.           {
  276.             tmp[++i] = mv;
  277.             side = otherside[side];
  278.           }
  279.         else if (c == 0 && i > 0)
  280.           {
  281.             entry = (struct BookEntry FAR *) Book_alloc ( sizeof (struct BookEntry));
  282.             mp = (unsigned short FAR *) Book_alloc ( (i + 1) * sizeof (unsigned short));
  283.             if ( (entry == 0 ) || (mp == 0) )
  284.               {
  285.                 Book = NULL;
  286.                 SMessageBox (hWnd, IDS_OBAE, IDS_CHESS);
  287.                 GlobalUnlock ( hBook );
  288.                 GlobalFree ( hBook);
  289.                 return;
  290.               }
  291.             entry->mv = mp;
  292.             entry->next = Book;
  293.             Book = entry;
  294.             for (j = 1; j <= i; j++)
  295.               *(mp++) = tmp[j];
  296.             *mp = 0;
  297.             i = 0;
  298.             side = white;
  299.           }
  300.       fclose (fd);
  301.     }
  302.   else
  303.     SMessageBox (hWnd, IDS_OBNF, IDS_CHESS);
  304. }
  305.  
  306. void
  307. OpeningBook (unsigned short *hint)
  308.  
  309. /*
  310.   Go thru each of the opening lines of play and check for a match with the
  311.   current game listing. If a match occurs, generate a random number. If this
  312.   number is the largest generated so far then the next move in this line
  313.   becomes the current "candidate". After all lines are checked, the
  314.   candidate move is put at the top of the Tree[] array and will be played by
  315.   the program. Note that the program does not handle book transpositions.
  316. */
  317.  
  318. {
  319.   short j, pnt;
  320.   unsigned short m, FAR *mp;
  321.   unsigned r, r0;
  322.   struct BookEntry FAR *p;
  323.  
  324.   srand ((unsigned int) time ((long *) 0));
  325.   r0 = m = 0;
  326.   p = Book;
  327.   while (p != NULL)
  328.     {
  329.       mp = p->mv;
  330.       for (j = 1; j <= GameCnt; j++)
  331.         if (GameList[j].gmove != *(mp++))
  332.           break;
  333.       if (j > GameCnt)
  334.         if ((r = urand ()) > r0)
  335.           {
  336.             r0 = r;
  337.             m = *mp;
  338.             *hint = *(++mp);
  339.           }
  340.       p = p->next;
  341.     }
  342.  
  343.   for (pnt = TrPnt[1]; pnt < TrPnt[2]; pnt++)
  344.     if (((Tree[pnt].f << 8) | Tree[pnt].t) == m)
  345.       Tree[pnt].score = 0;
  346.   pick (TrPnt[1], TrPnt[2] - 1);
  347.   if (Tree[TrPnt[1]].score < 0) {
  348.    FreeBook ();
  349.    Book = NULL;
  350.   }
  351. }
  352.  
  353. #endif
  354. 
  355.