home *** CD-ROM | disk | FTP | other *** search
/ Dream 44 / Amiga_Dream_44.iso / RiscPc / jeux / ArcBoard004.arc / !GNUChessX / src / misc / postprint_ < prev    next >
Text File  |  1995-07-02  |  5KB  |  226 lines

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