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

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