home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuch40.zip / gnuchess-4.0.pl79 / src / postprint.c < prev    next >
C/C++ Source or Header  |  1998-09-28  |  5KB  |  229 lines

  1. /*
  2.  * postprint.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1985-1996 Stuart Cracraft, John Stanback,
  5.  *                         Daryl Baker, Conor McCarthy,
  6.  *                         Mike McGann, Chua Kong Sian
  7.  * Copyright (c) 1985-1996 Free Software Foundation
  8.  *
  9.  * This file is part of GNU CHESS.
  10.  *
  11.  * GNU Chess is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2, or (at your option)
  14.  * any later version.
  15.  *
  16.  * GNU Chess is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with GNU Chess; see the file COPYING.  If not, write to
  23.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  */
  25. #include <stdio.h>
  26. #include "gnuchess.h"
  27. #if !defined HASHFILE
  28. #ifdef MSDOS
  29. #define HASHFILE "gnuchess.has"
  30. #else
  31. #define HASHFILE "/usr/local/lib/gnuchess.hash"
  32. #endif
  33. #endif
  34. #ifdef MSDOS
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #define RWA_ACC "r+b"
  38. #define WA_ACC "w+b"
  39. #else
  40. #define RWA_ACC "r+"
  41. #define WA_ACC "w+"
  42. #include <sys/param.h>
  43. #include <sys/types.h>
  44. #endif /* MSDOS */
  45. FILE *hashfile;
  46.  
  47. long i, j;
  48. int nr[MAXDEPTH+1];
  49. int r, c;
  50. CHAR line[128];
  51. CHAR *l;
  52. short t;
  53. int cc1, cc2;
  54. CHAR mvstr[5][7];
  55. SHORT board[64];
  56. struct fileentry
  57.      {   
  58.        UCHAR bd[32];
  59.        UCHAR f, t, flags, depth, sh, sl;
  60.      } n;  
  61.  
  62.  
  63. unsigned long hashkey, hashbd;
  64. /* hashtable flags */
  65. #define truescore 0x0001
  66. #define lowerbound 0x0002
  67. #define upperbound 0x0004
  68. #define kingcastle 0x0008
  69. #define queencastle 0x0010
  70. #define evalflag 0x0020
  71.  
  72.  
  73. void
  74. algbr (SHORT f, SHORT t, SHORT flag)
  75.  
  76. /*
  77.  * Generate move strings in different formats.
  78.  */
  79.  
  80. {
  81.   int m3p;
  82.  
  83.   if (f != t)
  84.     {
  85.       /* algebraic notation */
  86.       mvstr[0][0] = Cxx[column (f)];
  87.       mvstr[0][1] = Rxx[row (f)];
  88.       mvstr[0][2] = Cxx[column (t)];
  89.       mvstr[0][3] = Rxx[row (t)];
  90.       mvstr[0][4] = mvstr[3][0] = '\0';
  91.       if (((mvstr[1][0] = Pxx[board[f]]) == 'P') || (flag & promote))
  92.     {
  93.       if (mvstr[0][0] == mvstr[0][2])
  94.         {            /* pawn did not eat */
  95.           mvstr[2][0] = mvstr[1][0] = mvstr[0][2];    /* to column */
  96.           mvstr[2][1] = mvstr[1][1] = mvstr[0][3];    /* to row */
  97.           m3p = 2;
  98.         }
  99.       else
  100.         /* pawn ate */
  101.         {
  102.           mvstr[2][0] = mvstr[1][0] = mvstr[0][0];    /* from column */
  103.           mvstr[2][1] = mvstr[1][1] = mvstr[0][2];    /* to column */
  104.           mvstr[2][2] = mvstr[0][3];
  105.           m3p = 3;        /* to row */
  106.         }
  107.       mvstr[2][m3p] = mvstr[1][2] = '\0';
  108.       if (flag & promote)
  109.         {
  110.           mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = Qxx[flag & pmask];
  111.           mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[0][5] = '\0';
  112.         }
  113.     }
  114.       else
  115.     /* not a pawn */
  116.     {
  117.       mvstr[2][0] = mvstr[1][0];
  118.       mvstr[2][1] = mvstr[0][1];
  119.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  120.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  121.       mvstr[2][4] = mvstr[1][3] = '\0';
  122.       strcpy (mvstr[3], mvstr[2]);
  123.       mvstr[3][1] = mvstr[0][0];
  124.       if (flag & cstlmask)
  125.         {
  126.           if (t > f)
  127.         {
  128.           strcpy (mvstr[1], "o-o");
  129.           strcpy (mvstr[2], "O-O");
  130.         }
  131.           else
  132.         {
  133.           strcpy (mvstr[1], "o-o-o");
  134.           strcpy (mvstr[2], "O-O-O");
  135.         }
  136.         }
  137.     }
  138.     }
  139.   else
  140.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  141. }
  142.  
  143. void
  144. main (int argc, CHAR **argv)
  145. {
  146.   int f = 0;
  147.   CHAR flbuf[10];
  148.   CHAR *fl;
  149.  
  150.   if ((hashfile = fopen (HASHFILE, RWA_ACC)) == NULL)
  151.     exit (1);
  152.   for (i = 0; i < MAXDEPTH; i++)
  153.     nr[i] = 0;
  154.   fseek (hashfile, 0L, SEEK_END);
  155.   i = ftell (hashfile) / sizeof (struct fileentry);
  156.   fseek (hashfile, 0L, SEEK_SET);
  157.   printf ("/V 11 72 mul def /L 60 def\n");
  158.   for (j = 0; j < i; j++)
  159.     {
  160.       fread (&n, sizeof (struct fileentry), 1, hashfile);
  161.       if (n.depth)
  162.     {
  163.       nr[0]++;
  164.       if (nr[0] == 19)
  165.         {
  166.           nr[0] = 1;
  167.           printf ("showpage\n/V 11 72 mul def\n");
  168.           printf ("/L 60 def\n");
  169.           f = 0;
  170.         }
  171.       /* now process this entry */
  172.       strcpy (line, "C ('#[");
  173.       for (r = 0; r < 8; r++)
  174.         {
  175.           l = line + 6 + (7 - r) * 9;
  176.           for (c = 0; c < 4; c++)
  177.         {
  178.           cc1 = (n.bd[r * 4 + c] >> 4) & 0xf;
  179.           cc2 = n.bd[r * 4 + c] & 0xf;
  180.           board[r * 8 + c * 2] = (int) cc1 & 0x7;
  181.           board[r * 8 + c * 2 + 1] = (int) cc2 & 0x7;
  182.           if (cc1 & 0x8)
  183.             *l++ = Qxx[cc1 & 0x7];
  184.           else
  185.             *l++ = Pxx[cc1 & 0x7];
  186.           if (cc2 & 0x8)
  187.             *l++ = Qxx[cc2 & 0x7];
  188.           else
  189.             *l++ = Pxx[cc2 & 0x7];
  190.         }
  191.           *l++ = ';';
  192.         }
  193.       l--;
  194.       line[79] = '\0';
  195.       strcat (line, "]') show");
  196.       algbr (n.f, n.t, 0);
  197.       t = (n.sh << 8) + n.sl;
  198.       /* decode flags */
  199.       fl = flbuf;
  200.       if (n.flags & kingcastle)
  201.         *fl++ = 'k';
  202.       if (n.flags & queencastle)
  203.         *fl++ = 'q';
  204.       if (n.flags & truescore)
  205.         *fl++ = 't';
  206.       if (n.flags & lowerbound)
  207.         *fl++ = 'l';
  208.       if (n.flags & upperbound)
  209.         *fl++ = 'u';
  210.       *fl = '\0';
  211.       printf ("L V moveto\n");
  212.       printf ("R (%s flags %s depth %d score %d", mvstr[0], flbuf, n.depth, t);
  213.       printf (") show\n");
  214.       printf ("L  V 100 sub moveto\n");
  215.       printf ("%s\n", line);
  216.       f++;
  217.       if (f == 3)
  218.         {
  219.           printf ("/V V 120 sub def /L 60 def\n");
  220.           f = 0;
  221.         }
  222.       else
  223.         printf ("/L 160 L add def\n");
  224.     }
  225.     }
  226.   if (nr[0])
  227.     printf ("showpage\n");
  228. }
  229.