home *** CD-ROM | disk | FTP | other *** search
/ MORE WinGames / WINGAMES.iso / chess / initiali.c < prev    next >
C/C++ Source or Header  |  1991-06-16  |  14KB  |  481 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 NOWINOFFSETS
  35. #define NOCOMM
  36. #define NOKANJI
  37.  
  38. #include <windows.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <time.h>
  42.  
  43. #include "gnuchess.h"
  44. #include "defs.h"
  45.  
  46. extern HWND hStats;
  47.  
  48. /*extern short distdata[64][64], taxidata[64][64];*/
  49. extern short far *distdata, far *taxidata;
  50.  
  51. extern FILE *hashfile;
  52. extern short stage, stage2, Developed[2];
  53. extern short ChkFlag[maxdepth], CptrFlag[maxdepth], PawnThreat[maxdepth];
  54. extern short Pscore[maxdepth], Tscore[maxdepth];
  55. extern short rehash;
  56.  
  57. /* extern struct hashval hashcode[2][7][64]; */
  58. extern struct hashval far *hashcode;
  59. extern unsigned char far * history;
  60.  
  61. void
  62. Initialize_dist (void)
  63. {
  64.   register short a, b, d, di;
  65.  
  66.   for (a = 0; a < 64; a++)
  67.     for (b = 0; b < 64; b++)
  68.       {
  69.         d = abs (column (a) - column (b));
  70.         di = abs (row (a) - row (b));
  71. /*
  72.         taxidata[a][b] = d + di;
  73.         distdata[a][b] = (d > di ? d : di);
  74. */
  75.         *(taxidata+a*64+b) = d + di;
  76.         *(distdata+a*64+b) = (d > di ? d : di);
  77.  
  78.       }
  79. }
  80.  
  81. static short _based(_segname("_CODE")) Stboard[64] =
  82. {rook, knight, bishop, queen, king, bishop, knight, rook,
  83.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  84.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  85.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  86.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  87.  rook, knight, bishop, queen, king, bishop, knight, rook};
  88.  
  89. static short _based(_segname("_CODE")) Stcolor[64] =
  90. {white, white, white, white, white, white, white, white,
  91.  white, white, white, white, white, white, white, white,
  92.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  93.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  94.  black, black, black, black, black, black, black, black,
  95.  black, black, black, black, black, black, black, black};
  96.  
  97. extern short board[64], color[64];
  98.  
  99. /*extern unsigned char nextpos[8][64][64];*/
  100. /*extern unsigned char nextdir[8][64][64];*/
  101. extern unsigned char far * nextpos;
  102. extern unsigned char far * nextdir;
  103. /*
  104.   ptype is used to separate white and black pawns, like this;
  105.   ptyp = ptype[side][piece]
  106.   piece can be used directly in nextpos/nextdir when generating moves
  107.   for pieces that are not black pawns.
  108. */
  109. static short _based(_segname("_CODE")) ptype[2][8] =
  110. {
  111.   no_piece, pawn, knight, bishop, rook, queen, king, no_piece,
  112.   no_piece, bpawn, knight, bishop, rook, queen, king, no_piece};
  113.  
  114. static short _based(_segname("_CODE")) direc[8][8] =
  115. {
  116.   0, 0, 0, 0, 0, 0, 0, 0,
  117.   10, 9, 11, 0, 0, 0, 0, 0,
  118.   8, -8, 12, -12, 19, -19, 21, -21,
  119.   9, 11, -9, -11, 0, 0, 0, 0,
  120.   1, 10, -1, -10, 0, 0, 0, 0,
  121.   1, 10, -1, -10, 9, 11, -9, -11,
  122.   1, 10, -1, -10, 9, 11, -9, -11,
  123.   -10, -9, -11, 0, 0, 0, 0, 0};
  124.  
  125. static short _based(_segname("_CODE")) max_steps[8] =
  126. {0, 2, 1, 7, 7, 7, 1, 2};
  127.  
  128. static short _based(_segname("_CODE")) nunmap[120] =
  129. {
  130.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  131.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  132.   -1, 0, 1, 2, 3, 4, 5, 6, 7, -1,
  133.   -1, 8, 9, 10, 11, 12, 13, 14, 15, -1,
  134.   -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
  135.   -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
  136.   -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
  137.   -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
  138.   -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
  139.   -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
  140.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  141.   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  142.  
  143.  
  144. void
  145. Initialize_moves (void)
  146.  
  147. /*
  148.   This procedure pre-calculates all moves for every piece from every square.
  149.   This data is stored in nextpos/nextdir and used later in the move generation
  150.   routines.
  151. */
  152.  
  153. {
  154.   short ptyp, po, p0, d, di, s, delta;
  155.   unsigned char far *ppos, far *pdir;
  156.   short dest[8][8];
  157.   short steps[8];
  158.   short sorted[8];
  159.  
  160.   for (ptyp = 0; ptyp < 8; ptyp++)
  161.     for (po = 0; po < 64; po++)
  162.       for (p0 = 0; p0 < 64; p0++)
  163.         {
  164.           *(nextpos+ptyp*64*64+po*64+p0) = (unsigned char) po;
  165.           *(nextdir+ptyp*64*64+po*64+p0) = (unsigned char) po;
  166.         }
  167.   for (ptyp = 1; ptyp < 8; ptyp++)
  168.     for (po = 21; po < 99; po++)
  169.       if (nunmap[po] >= 0)
  170.         {
  171.           ppos = nextpos+ptyp*64*64+nunmap[po]*64;
  172.           pdir = nextdir+ptyp*64*64+nunmap[po]*64;
  173.           /* dest is a function of direction and steps */
  174.           for (d = 0; d < 8; d++)
  175.             {
  176.               dest[d][0] = nunmap[po];
  177.               delta = direc[ptyp][d];
  178.               if (delta != 0)
  179.                 {
  180.                   p0 = po;
  181.                   for (s = 0; s < max_steps[ptyp]; s++)
  182.                     {
  183.                       p0 = p0 + delta;
  184.                       /*
  185.                         break if (off board) or
  186.                         (pawns only move two steps from home square)
  187.                       */
  188.                       if (nunmap[p0] < 0 || (ptyp == pawn || ptyp == bpawn)
  189.                           && s > 0 && (d > 0 || Stboard[nunmap[po]] != pawn))
  190.                         break;
  191.                       else
  192.                         dest[d][s] = nunmap[p0];
  193.                     }
  194.                 }
  195.               else
  196.                 s = 0;
  197.  
  198.               /*
  199.                 sort dest in number of steps order
  200.                 currently no sort is done due to compability with
  201.                 the move generation order in old gnu chess
  202.               */
  203.               steps[d] = s;
  204.               for (di = d; s > 0 && di > 0; di--)
  205.                 if (steps[sorted[di - 1]] == 0) /* should be: < s */
  206.                   sorted[di] = sorted[di - 1];
  207.                 else
  208.                   break;
  209.               sorted[di] = d;
  210.             }
  211.  
  212.           /*
  213.             update nextpos/nextdir,
  214.             pawns have two threads (capture and no capture)
  215.           */
  216.           p0 = nunmap[po];
  217.           if (ptyp == pawn || ptyp == bpawn)
  218.             {
  219.               for (s = 0; s < steps[0]; s++)
  220.                 {
  221.                   ppos[p0] = (unsigned char) dest[0][s];
  222.                   p0 = dest[0][s];
  223.                 }
  224.               p0 = nunmap[po];
  225.               for (d = 1; d < 3; d++)
  226.                 {
  227.                   pdir[p0] = (unsigned char) dest[d][0];
  228.                   p0 = dest[d][0];
  229.                 }
  230.             }
  231.           else
  232.             {
  233.               pdir[p0] = (unsigned char) dest[sorted[0]][0];
  234.               for (d = 0; d < 8; d++)
  235.                 for (s = 0; s < steps[sorted[d]]; s++)
  236.                   {
  237.                     ppos[p0] = (unsigned char) dest[sorted[d]][s];
  238.                     p0 = dest[sorted[d]][s];
  239.                     if (d < 7)
  240.                       pdir[p0] = (unsigned char) dest[sorted[d + 1]][0];
  241.                     /* else is already initialized */
  242.                   }
  243.             }
  244.         }
  245. }
  246.  
  247. GLOBALHANDLE hGameList, hTree, hHistory, hTTable, hHashCode, hdistdata;
  248. GLOBALHANDLE htaxidata, hnextdir, hnextpos;
  249.  
  250. /* hmm.... shouldn`t main be moved to the interface routines */
  251. int
  252. init_main (HWND hWnd)
  253. {
  254.   short int ahead = true, hash = true;
  255.  
  256. /* Mods for MS WINDOWS includes moving statics to dynamic allocated */
  257.  
  258.    hHistory = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  259.                           (long) (8192 * sizeof (char) ) );
  260.  
  261.    if ( hHistory == NULL ) {
  262.       return 1;
  263.    }
  264.  
  265.    history = (unsigned char far *) GlobalLock (hHistory);
  266.    
  267.    hnextdir = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  268.                           (long) (64*64*8 * sizeof (char) ) );
  269.  
  270.    if ( hnextdir == NULL ) {
  271.       return 1;
  272.    }
  273.  
  274.    nextdir = (unsigned char far *) GlobalLock (hnextdir);
  275.    
  276.    hnextpos = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  277.                           (long) (64*64*8 * sizeof (char) ) );
  278.  
  279.    if ( hnextpos == NULL ) {
  280.       return 1;
  281.    }
  282.  
  283.    nextpos = (unsigned char far *) GlobalLock (hnextpos);
  284.  
  285.    hdistdata = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  286.                           (long) (64*64 * sizeof (short) ) );
  287.  
  288.    if ( hdistdata == NULL ) {
  289.       return 1;
  290.    }
  291.  
  292.    distdata = (short far *) GlobalLock (hdistdata);
  293.  
  294.    htaxidata = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  295.                           (long) (64*64 * sizeof (short) ) );
  296.  
  297.    if ( htaxidata == NULL ) {
  298.       return 1;
  299.    }
  300.  
  301.    taxidata = (unsigned short far *) GlobalLock (htaxidata);
  302.  
  303.    hHashCode = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  304.                           (long) (2*7*64 * sizeof (struct hashval) ) );
  305.  
  306.    if ( hHashCode == NULL ) {
  307.       return 1;
  308.    }
  309.  
  310.    hashcode = (struct hashval far *) GlobalLock (hHashCode);
  311.  
  312.    hTree = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  313.                           (long) (2000 * sizeof (struct leaf)) );
  314.  
  315.    if ( hTree == NULL ) {
  316.       return 1;
  317.    }
  318.  
  319.    Tree = (struct leaf far *) GlobalLock ( hTree);
  320.  
  321.    hGameList = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  322.                           (long) (512 * sizeof (struct GameRec)) );
  323.  
  324.    if ( hGameList == NULL ) {
  325.       return 1;
  326.    }
  327.  
  328.    GameList = (struct GameRec far *) GlobalLock (hGameList);
  329.  
  330.    hTTable = GlobalAlloc ( GMEM_MOVEABLE | GMEM_ZEROINIT,
  331.                           (long) (2*ttblsz * sizeof (struct hashentry)) );
  332.  
  333.    if ( hTTable == NULL ) {
  334.       return 1;
  335.    }
  336.  
  337.    ttable = (struct hashentry far *) GlobalLock (hTTable);
  338.  
  339.   Level = 0;
  340.   TCflag = false;
  341.   OperatorTime = 0;
  342.   Initialize ();
  343.   Initialize_dist ();
  344.   Initialize_moves ();
  345.   NewGame (hWnd);
  346.   GetOpenings ( hWnd);
  347.  
  348.   flag.easy = ahead;
  349.   flag.hash = hash;
  350.   hashfile = NULL;
  351.   return (0);
  352. }
  353.  
  354.  
  355. void FreeGlobals (void)
  356. {
  357.    if ( hHistory ) {
  358.       GlobalUnlock (hHistory);
  359.       GlobalFree   (hHistory);
  360.    }
  361.  
  362.    if ( hnextdir ) {
  363.       GlobalUnlock (hnextdir);
  364.       GlobalFree   (hnextdir);
  365.    }
  366.  
  367.    if ( hGameList ) {
  368.       GlobalUnlock (hGameList);
  369.       GlobalFree   (hGameList);
  370.    }
  371.  
  372.    if ( hTTable ) {
  373.       GlobalUnlock (hTTable);
  374.       GlobalFree   (hTTable);
  375.    }
  376.  
  377.    if ( hTree ) {
  378.       GlobalUnlock (hTree);
  379.       GlobalFree   (hTree);
  380.    }
  381.  
  382.    if ( hHashCode ) {
  383.       GlobalUnlock (hHashCode);
  384.       GlobalFree   (hHashCode);
  385.    }
  386.  
  387.    if ( htaxidata ) {
  388.       GlobalUnlock (htaxidata);
  389.       GlobalFree   (htaxidata);
  390.    }
  391.  
  392.    if ( hdistdata ) {
  393.       GlobalUnlock (hdistdata);
  394.       GlobalFree   (hdistdata);
  395.    }
  396.  
  397.    if ( hnextpos ) {
  398.       GlobalUnlock (hnextpos);
  399.       GlobalFree   (hnextpos);
  400.    }
  401.  
  402. }
  403.  
  404.  
  405. void
  406. NewGame (HWND hWnd)
  407.  
  408. /*
  409.   Reset the board and other variables to start a new game.
  410. */
  411.  
  412. {
  413.   short l, c, p;
  414.  
  415.   stage = stage2 = -1;          /* the game is not yet started */
  416.  
  417.   if ( flag.post ) {
  418.       SendMessage ( hStats, WM_SYSCOMMAND, SC_CLOSE, 0 );
  419.       flag.post = false;
  420.   }
  421.  
  422.   flag.mate = flag.quit = flag.reverse = flag.bothsides = false;
  423.   flag.force = false;
  424.   flag.hash = flag.easy = flag.beep = flag.rcptr = true;
  425.   NodeCnt = et0 = epsquare = 0;
  426.   dither = 0;
  427.   Awindow = 90;
  428.   Bwindow = 90;
  429.   xwndw = 90;
  430.   MaxSearchDepth = 29;
  431.   contempt = 0;
  432.   GameCnt = 0;
  433.   Game50 = 1;
  434.   hint = 0x0C14;
  435.   ZeroRPT ();
  436.   Developed[white] = Developed[black] = false;
  437.   castld[white] = castld[black] = false;
  438.   PawnThreat[0] = CptrFlag[0] = false;
  439.   Pscore[0] = 12000;
  440.   Tscore[0] = 12000;
  441.   opponent = white;
  442.   computer = black;
  443.   for (l = 0; l < 2000; l++)
  444. /*    Tree[l].f = Tree[l].t = 0;*/
  445.     (Tree+l)->f = (Tree+l)->t = 0;
  446. #if ttblsz
  447.   rehash = 6;
  448.   ZeroTTable ();
  449.   srand ((unsigned int) 1);
  450.   for (c = white; c <= black; c++)
  451.     for (p = pawn; p <= king; p++)
  452.       for (l = 0; l < 64; l++)
  453.         {
  454.           (hashcode+c*7*64+p*64+l)->key = (((unsigned long) urand ()));
  455.           (hashcode+c*7*64+p*64+l)->key += (((unsigned long) urand ()) << 16);
  456.           (hashcode+c*7*64+p*64+l)->bd = (((unsigned long) urand ()));
  457.           (hashcode+c*7*64+p*64+l)->bd += (((unsigned long) urand ()) << 16);
  458.         }
  459. #endif /* ttblsz */
  460.   for (l = 0; l < 64; l++)
  461.     {
  462.       board[l] = Stboard[l];
  463.       color[l] = Stcolor[l];
  464.       Mvboard[l] = 0;
  465.     }
  466.   if (TCflag)
  467.     SetTimeControl ();
  468.   else if (Level == 0) {
  469.     OperatorTime = 1;
  470.     TCmoves = 60;
  471.     TCminutes = 5;
  472.     TCflag = (TCmoves > 1);
  473.     SetTimeControl ();
  474.   }
  475.   InitializeStats ();
  476.   time0 = time ((long *) 0);
  477.   ElapsedTime (1);
  478.   UpdateDisplay (hWnd, 0, 0, 1, 0);
  479. }
  480.  
  481.