home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnuch40.zip / gnuchess-4.0.pl79 / src / game.c < prev    next >
C/C++ Source or Header  |  1998-09-28  |  10KB  |  424 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. CHAR mvstr[5][7];
  28. #ifdef MSDOS
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #define RWA_ACC "r+b"
  32. #define WA_ACC "w+b"
  33. #else
  34. #define RWA_ACC "r+"
  35. #define WA_ACC "w+"
  36. #include <sys/param.h>
  37. #include <sys/types.h>
  38. #endif /* MSDOS */
  39. FILE *fd;
  40.  
  41. #define truescore 0x0001
  42. #define lowerbound 0x0002
  43. #define upperbound 0x0004
  44. #define kingcastle 0x0008
  45. #define queencastle 0x0010
  46. const SHORT otherside[3] =
  47. {black, white, neutral};
  48. const SHORT Stboard[64] =
  49. {rook, knight, bishop, queen, king, bishop, knight, rook,
  50.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  51.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  54.  rook, knight, bishop, queen, king, bishop, knight, rook};
  55. const SHORT Stcolor[64] =
  56. {white, white, white, white, white, white, white, white,
  57.  white, white, white, white, white, white, white, white,
  58.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  59.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  60.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  61.  neutral, neutral, neutral, neutral, neutral, neutral, neutral, neutral,
  62.  black, black, black, black, black, black, black, black,
  63.  black, black, black, black, black, black, black, black};
  64.  
  65. struct GameRec GameList[512];
  66. long i, j;
  67. int nr;
  68. SHORT ep;
  69. int r, c;
  70. CHAR line[128];
  71. CHAR *l;
  72. SHORT board[64];
  73. SHORT color[64];
  74. SHORT GameCnt;
  75. int from, to;
  76. CHAR *InPtr;
  77.  
  78. void
  79. skip ()
  80. {
  81.   while (*InPtr != ' ')
  82.     InPtr++;
  83.   while (*InPtr == ' ')
  84.     InPtr++;
  85. }
  86. void
  87. skipb ()
  88. {
  89.   while (*InPtr == ' ')
  90.     InPtr++;
  91. }
  92. int
  93. parser (CHAR *f, int side, UTSHORT *flags)
  94. {
  95.   int c1, r1, c2, r2;
  96.  
  97.   *flags = 0;
  98.  
  99.   if (f[4] == 'o')
  100.     if (side == black)
  101.       return 0x3C3A;
  102.     else
  103.       return 0x0402;
  104.   else if (f[0] == 'o')
  105.     if (side == black)
  106.       return 0x3C3E;
  107.     else
  108.       return 0x0406;
  109.   else
  110.     {
  111.       c1 = f[0] - 'a';
  112.       r1 = f[1] - '1';
  113.       c2 = f[2] - 'a';
  114.       r2 = f[3] - '1';
  115.       if (f[4] != ' ')
  116.     {
  117.       /* promotion */
  118.       for (i = 0; i < 7; i++)
  119.         if (f[4] == Qxx[i])
  120.           {
  121.         *flags = (UTSHORT)i | promote;
  122.         break;
  123.           }
  124.     }
  125.       return (locn (r1, c1) << 8) | locn (r2, c2);
  126.     }
  127.   return (0);
  128. }
  129.  
  130. void
  131. algbr (SHORT f, SHORT t, SHORT flag)
  132.  
  133.  
  134. /*
  135.  * Generate move strings in different formats.
  136.  */
  137.  
  138. {
  139.   int m3p;
  140.  
  141.   if (f != t)
  142.     {
  143.       /* algebraic notation */
  144.       mvstr[0][0] = Cxx[column (f)];
  145.       mvstr[0][1] = Rxx[row (f)];
  146.       mvstr[0][2] = Cxx[column (t)];
  147.       mvstr[0][3] = Rxx[row (t)];
  148.       mvstr[0][4] = mvstr[3][0] = '\0';
  149.       if (((mvstr[1][0] = Pxx[board[f]]) == 'P') || (flag & promote))
  150.     {
  151.       if (mvstr[0][0] == mvstr[0][2])    /* pawn did not eat */
  152.         {
  153.           mvstr[2][0] = mvstr[1][0] = mvstr[0][2];    /* to column */
  154.           mvstr[2][1] = mvstr[1][1] = mvstr[0][3];    /* to row */
  155.           m3p = 2;
  156.         }
  157.       else
  158.         /* pawn ate */
  159.         {
  160.           mvstr[2][0] = mvstr[1][0] = mvstr[0][0];    /* column */
  161.           mvstr[2][1] = mvstr[1][1] = mvstr[0][2];    /* to column */
  162.           mvstr[2][2] = mvstr[0][3];
  163.           m3p = 3;        /* to row */
  164.         }
  165.       if (flag & promote)
  166.         {
  167.           mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = Qxx[flag & pmask];
  168.           mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[0][5] = '\0';
  169. #ifdef CHESSTOOL
  170.           mvstr[3][0] = mvstr[0][0];    /* Allow e7e8 for chesstool */
  171.           mvstr[3][1] = mvstr[0][1];
  172.           mvstr[3][2] = mvstr[0][2];
  173.           mvstr[3][3] = mvstr[0][3];
  174.           mvstr[3][4] = '\0';
  175. #endif
  176.         }
  177.       mvstr[2][m3p] = mvstr[1][2] = '\0';
  178.     }
  179.       else
  180.     /* not a pawn */
  181.     {
  182.       mvstr[2][0] = mvstr[1][0];
  183.       mvstr[2][1] = mvstr[0][1];
  184.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  185.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  186.       mvstr[2][4] = mvstr[1][3] = '\0';
  187.       strcpy (mvstr[3], mvstr[2]);
  188.       mvstr[3][1] = mvstr[0][0];
  189.       if (flag & cstlmask)
  190.         {
  191.           if (t > f)
  192.         {
  193.           strcpy (mvstr[1], "o-o");
  194.           strcpy (mvstr[2], "O-O");
  195.         }
  196.           else
  197.         {
  198.           strcpy (mvstr[1], "o-o-o");
  199.           strcpy (mvstr[2], "O-O-O");
  200.         }
  201.         }
  202.     }
  203.     }
  204.   else
  205.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  206. }
  207.  
  208. void
  209. GetGame ()
  210. {
  211.   CHAR fb[256];
  212.   UTSHORT flags;
  213.  
  214.   fgets (fb, 256, fd);
  215.   fgets (fb, 256, fd);
  216.   while (fgets (fb, 256, fd))
  217.     {
  218.       struct GameRec *g;
  219.       int side = white;
  220.  
  221.       side = otherside[side];
  222.       if (fb[0] == '\n')
  223.     return;
  224.       ++GameCnt;
  225.       InPtr = fb;
  226.       skipb ();
  227.       g = &GameList[GameCnt];
  228.       g->gmove = parser (InPtr, side, &flags);
  229.       skip ();
  230.       g->score = atoi (InPtr);
  231.       skip ();
  232.       g->depth = atoi (InPtr);
  233.       skip ();
  234.       g->nodes = atol (InPtr);
  235.       skip ();
  236.       g->time = atol (InPtr);
  237.       g->flags = flags;
  238.       skip ();
  239.       ++GameCnt;
  240.       g = &GameList[GameCnt];
  241.       g->gmove = parser (InPtr, side, &flags);
  242.       skip ();
  243.       g->score = atoi (InPtr);
  244.       skip ();
  245.       g->depth = atoi (InPtr);
  246.       skip ();
  247.       g->nodes = atol (InPtr);
  248.       skip ();
  249.       g->time = atol (InPtr);
  250.       g->flags = flags;
  251.  
  252.     }
  253. }
  254. SHORT xside, side;
  255. void
  256. getboard (int mvno)
  257.  
  258. {
  259.   register SHORT f, t;
  260.   SHORT rf, rt;
  261.   UTSHORT mv;
  262.  
  263.   /* now update the board and hash values */
  264.  
  265.   /*
  266.    * should really check the moves as we do this, but???
  267.    */
  268.   mv = GameList[mvno].gmove;
  269.   f = mv >> 8 & 0x7F;
  270.   t = mv & 0xFF;
  271.   /* can only capture other side */
  272.   if (board[t] != no_piece)
  273.     {
  274.       if (color[t] != xside)
  275.     {
  276.       algbr (f, t, 0);
  277.       printf ("Illegal move - %d %s \n", mvno, mvstr[0]);
  278.     }
  279.     }
  280.   /* there must be a piece to move */
  281.   if (board[f] == no_piece || color[f] != side)
  282.     {
  283.       algbr (f, t, 0);
  284.       printf ("Illegal move + %d %s \n", mvno, mvstr[0]);
  285.     }
  286.   /* is it EnPassant */
  287.   if (board[f] == pawn && board[t] == no_piece)
  288.     {
  289.       if ((row (f) == 4 && row (t) == 3) || (row (f) == 5 && row (t) == 6))
  290.     {
  291.       if ((column (t) == column (f) + 1)
  292.           || (column (t) == column (f) - 1))
  293.         {
  294.           ep = t + ((t > f) ? -8 : 8);
  295.           if (board[ep] == pawn && color[ep] == xside)
  296.         {
  297.           board[ep] = no_piece;
  298.           color[ep] = neutral;
  299.         }
  300.         }
  301.     }
  302.     }
  303.   board[t] = board[f];
  304.   color[t] = color[f];
  305.   color[f] = neutral;
  306.   board[f] = no_piece;
  307.   /* castle moves */
  308.   if ((board[t] == king) & ((mv == BLACKCASTLE) || (mv == WHITECASTLE) || (mv == LONGBLACKCASTLE) || (mv == LONGWHITECASTLE)))
  309.     {
  310.  
  311.       if (t > f)
  312.     {
  313.       rf = f + 3;
  314.       rt = t - 1;
  315.     }
  316.       else
  317.     {
  318.       rf = f - 4;
  319.       rt = t + 1;
  320.     }
  321.       board[rt] = rook;
  322.       color[rt] = side;
  323.       board[rf] = no_piece;
  324.       color[rf] = neutral;
  325.     }
  326.   else if (GameList[i].flags & promote)
  327.  
  328.     board[t] = GameList[i].flags & pmask;
  329.   xside = side;
  330.   side = otherside[side];
  331. }
  332.  
  333. void
  334. main (int argc, CHAR **argv)
  335. {
  336.   int from, to;
  337.   int f = 0;
  338.   UTSHORT mv;
  339.   int start, end;
  340.  
  341.   if (argc > 4 || argc < 2)
  342.     {
  343.       printf ("Usage: game file [start [end] ] \n");
  344.       exit (1);
  345.     }
  346.   start = end = 0;
  347.   if (argc > 2)
  348.     start = (atoi (argv[2]) * 2) - 1;
  349.   if (argc == 4)
  350.     end = (atoi (argv[3]) * 2) - 1;
  351.   side = white;
  352.   xside = black;
  353.   for (i = 0; i < 64; i++)
  354.     {
  355.       board[i] = Stboard[i];
  356.       color[i] = Stcolor[i];
  357.     }
  358.   i = 1;
  359.   if ((fd = fopen (argv[1], RWA_ACC)) == NULL)
  360.     exit (1);
  361.   printf ("/V 11 72 mul def /L 60 def\n");
  362.   GetGame ();
  363.   if (!start || start < 1 || start > GameCnt)
  364.     start = 1;
  365.   if (!end || end > GameCnt || end < 1)
  366.     end = GameCnt;
  367.   for (i = 1; i < end; i++)
  368.     {
  369.       getboard ((int)i);
  370.       if (i < start)
  371.     continue;
  372.       nr++;
  373.       if (nr == 19)
  374.     {
  375.       nr = 1;
  376.       printf ("showpage\n/V 11 72 mul def\n");
  377.       printf ("/L 60 def\n");
  378.       f = 0;
  379.     }
  380.       /* now process this entry */
  381.       strcpy (line, "C ('#[");
  382.       for (r = 0; r < 8; r++)
  383.     {
  384.       l = line + 6 + (7 - r) * 9;
  385.       for (c = 0; c < 8; c++)
  386.         {
  387.           if (color[r * 8 + c] == black)
  388.         *l++ = Qxx[board[r * 8 + c]];
  389.           else
  390.         *l++ = Pxx[board[r * 8 + c]];
  391.         }
  392.       *l++ = ';';
  393.     }
  394.       l--;
  395.       line[79] = '\0';
  396.       strcat (line, "]') show");
  397.       /* decode flags */
  398.       printf ("L V moveto\n");
  399.       mv = GameList[i].gmove;
  400.       from = mv >> 8 & 0x7F;
  401.       to = mv & 0x7F;
  402.       algbr (from, to, 0);
  403.       if (i % 2)
  404.     printf ("R (%d %s score %d time %d", (i + 1) / 2, mvstr[0], GameList[i].score, GameList[i].time);
  405.       else
  406.     printf ("R (%d  ... %s score %d time %d", (i + 1) / 2, mvstr[0], GameList[i].score, GameList[i].time);
  407.       printf (") show\n");
  408.       printf ("L  V 100 sub moveto\n");
  409.       printf ("%s\n", line);
  410.       f++;
  411.       if (f == 3)
  412.     {
  413.       printf ("/V V 120 sub def /L 60 def\n");
  414.       f = 0;
  415.     }
  416.       else
  417.     printf ("/L 160 L add def\n");
  418.     }
  419.  
  420.   if (nr)
  421.     printf ("showpage\n");
  422.   exit (0);
  423. }
  424.