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

  1. /*
  2.   C source for GNU CHESS
  3.  
  4.   Revision: 1990-09-30
  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 
  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>
  38. #include <time.h>
  39. #include <stdlib.h>
  40. #include <stdio.h>
  41.  
  42. #include "gnuchess.h"
  43. #include "defs.h"
  44. #include "chess.h"
  45.  
  46. #define MAX_BOOK_SIZE (32*1024)
  47.  
  48. static unsigned int book_used = 0;
  49. static char far * xBook;
  50. GLOBALHANDLE hBook = 0;
  51.  
  52. void FreeBook (void)
  53. {
  54.    GlobalUnlock ( hBook );
  55.    GlobalFree ( hBook);
  56.    hBook = 0;
  57.    book_used = 0;
  58. }
  59.  
  60. static void far *Book_alloc ( unsigned int size )
  61. {
  62.     char far * temp;
  63.     if ( book_used+size >= MAX_BOOK_SIZE ) return (void far *)0;
  64.     temp = xBook+book_used;
  65.     book_used += size;
  66.     return temp;
  67. }
  68.  
  69.  
  70. void
  71. GetOpenings (HWND hWnd)
  72.      
  73. /*
  74.    Read in the Opening Book file and parse the algebraic notation for a move
  75.    into an unsigned integer format indicating the from and to square. Create
  76.    a linked list of opening lines of play, with entry->next pointing to the
  77.    next line and entry->move pointing to a chunk of memory containing the
  78.    moves. More Opening lines of up to 256 half moves may be added to
  79.    gnuchess.book.
  80. */
  81. #ifndef BOOK
  82. #define BOOK "/usr/games/lib/gnuchess.book"
  83. #endif /* BOOK */     
  84. {
  85.   FILE *fd;
  86.   int c, i, j, side;
  87.  
  88.   /* char buffr[2048]; */
  89.   struct BookEntry far  *entry;
  90.   unsigned short mv, far  *mp, tmp[100];
  91.   char lpFile[_MAX_FNAME+_MAX_EXT+_MAX_DRIVE+_MAX_DIR+1];
  92.   char sFname[_MAX_FNAME], sExt[_MAX_EXT], sDrive[_MAX_DRIVE], sDir[_MAX_DIR];
  93.  
  94.   GetModuleFileName ( GetWindowWord(hWnd, GWW_HINSTANCE),
  95.                       lpFile, sizeof(lpFile) );
  96.  
  97.   _splitpath ( lpFile, sDrive, sDir, sFname, sExt);
  98.   _makepath  ( lpFile, sDrive, sDir, "gnuchess", "boo");
  99.  
  100.   fd = fopen (lpFile, "r");
  101.  
  102. /*  if ((fd = fopen (BOOK, "r")) == NULL)
  103.     fd = fopen ("gnuchess.book", "r"); */
  104.   if (fd != NULL)
  105.     {
  106.       hBook = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  107.                             (long) (MAX_BOOK_SIZE * sizeof (char)) );
  108.  
  109.       if( hBook == NULL )
  110.         {
  111.            Book = NULL;
  112.            SMessageBox (hWnd, IDS_OBAE, IDS_CHESS);
  113.            return;
  114.         }
  115.       xBook = (unsigned char far *) GlobalLock (hBook);
  116.  
  117.       Book = NULL;
  118.       i = 0;
  119.       side = white;
  120.       while ((c = parse (fd, &mv, side)) >= 0)
  121.         if (c == 1)
  122.           {
  123.             tmp[++i] = mv;
  124.             side = otherside[side];
  125.           }
  126.         else if (c == 0 && i > 0)
  127.           {
  128.             entry = (struct BookEntry far *) Book_alloc ( sizeof (struct BookEntry));
  129.             mp = (unsigned short far *) Book_alloc ( (i + 1) * sizeof (unsigned short));
  130.             if ( (entry == 0 ) || (mp == 0) )
  131.               {
  132.                 Book = NULL;
  133.                 SMessageBox (hWnd, IDS_OBAE, IDS_CHESS);
  134.                 GlobalUnlock ( hBook );
  135.                 GlobalFree ( hBook);
  136.                 return;
  137.               }
  138.             entry->mv = mp;
  139.             entry->next = Book;
  140.             Book = entry;
  141.             for (j = 1; j <= i; j++)
  142.               *(mp++) = tmp[j];
  143.             *mp = 0;
  144.             i = 0;
  145.             side = white;
  146.           }
  147.  
  148.       fclose (fd);
  149.     }
  150.   else
  151.     SMessageBox (hWnd, IDS_OBNF, IDS_CHESS);
  152. }
  153.  
  154. void
  155. OpeningBook (unsigned short *hint)
  156.  
  157. /*
  158.   Go thru each of the opening lines of play and check for a match with the
  159.   current game listing. If a match occurs, generate a random number. If this
  160.   number is the largest generated so far then the next move in this line
  161.   becomes the current "candidate". After all lines are checked, the
  162.   candidate move is put at the top of the Tree[] array and will be played by
  163.   the program. Note that the program does not handle book transpositions.
  164. */
  165.  
  166. {
  167.   short j, pnt;
  168.   unsigned short m, far *mp;
  169.   unsigned r, r0;
  170.   struct BookEntry far *p;
  171.  
  172.   srand ((unsigned int) time ((long *) 0));
  173.   r0 = m = 0;
  174.   p = Book;
  175.   while (p != NULL)
  176.     {
  177.       mp = p->mv;
  178.       for (j = 1; j <= GameCnt; j++)
  179.         if (GameList[j].gmove != *(mp++))
  180.           break;
  181.       if (j > GameCnt)
  182.         if ((r = urand ()) > r0)
  183.           {
  184.             r0 = r;
  185.             m = *mp;
  186.             *hint = *(++mp);
  187.           }
  188.       p = p->next;
  189.     }
  190.  
  191.   for (pnt = TrPnt[1]; pnt < TrPnt[2]; pnt++)
  192.     if (((Tree[pnt].f << 8) | Tree[pnt].t) == m)
  193.       Tree[pnt].score = 0;
  194.   pick (TrPnt[1], TrPnt[2] - 1);
  195.   if (Tree[TrPnt[1]].score < 0) {
  196.    FreeBook ();
  197.    Book = NULL;
  198.   }
  199. }
  200.