home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / pmics.zip / game.cc < prev    next >
C/C++ Source or Header  |  1994-11-23  |  8KB  |  323 lines

  1. /*
  2.     PMICS -- PM interface for playing chess on internet chess server
  3.     Copyright (C) 1994  Kevin Nomura
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     Author can be reached at email: chow@netcom.com
  20. */
  21. typedef int Boolean;
  22. #include "game.hh"
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26.  
  27. void AGame::initialize()
  28. {
  29.   enum Piece initial_configuration[8][8] = {
  30.     W_ROO,W_KNI,W_BIS,W_QUE,W_KIN,W_BIS,W_KNI,W_ROO,
  31.     W_PAW,W_PAW,W_PAW,W_PAW,W_PAW,W_PAW,W_PAW,W_PAW,
  32.     EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,
  33.     EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,
  34.     EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,
  35.     EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,
  36.     B_PAW,B_PAW,B_PAW,B_PAW,B_PAW,B_PAW,B_PAW,B_PAW,
  37.     B_ROO,B_KNI,B_BIS,B_QUE,B_KIN,B_BIS,B_KNI,B_ROO
  38.     };
  39.   memcpy((void *)pieceArray,
  40.      initial_configuration,sizeof(initial_configuration));
  41.   setMoveNumber(1);
  42.   setOnMove(WHITE);
  43. }
  44.  
  45.  
  46. AGame::AGame()
  47. {
  48.   fprintf(stderr,"AGame constructor\n");
  49.   initialize();
  50. }
  51.  
  52.  
  53. AGame::~AGame()
  54. {
  55.   fprintf(stderr,"AGame destructor\n");
  56. }
  57.  
  58.  
  59. AGame *AGame::current()
  60. {
  61.   static AGame *currentGame = NULL;
  62.   if (currentGame == NULL)
  63.     currentGame = new AGame;
  64.   return currentGame;
  65. }
  66.  
  67.  
  68. char *AGame::asString()
  69. {
  70.   static char pieceMn[] = " RKBQKPrkbqkp";
  71.   static char s[64*2 + 8 + 1], *p;
  72.  
  73.   p = s;
  74.   for (int file=0; file<8; file++) {
  75.     for (int rank=0; rank<8; rank++) {
  76.       *p++ = pieceMn[piece(file,rank)];
  77.       *p++ = ' ';
  78.     }
  79.     *p++ = '\n';
  80.   }
  81.   *p = 0;
  82.   return s;
  83. }
  84.  
  85.  
  86. AGame::Piece AGame::toPiece(char c)
  87. {
  88.   Piece    p;
  89.   switch(c) {
  90.   case 'R': p = AGame::W_ROO; break;
  91.   case 'N': p = AGame::W_KNI; break;
  92.   case 'B': p = AGame::W_BIS; break;
  93.   case 'Q': p = AGame::W_QUE; break;
  94.   case 'K': p = AGame::W_KIN; break;
  95.   case 'P': p = AGame::W_PAW; break;
  96.   case 'r': p = AGame::B_ROO; break;
  97.   case 'n': p = AGame::B_KNI; break;
  98.   case 'b': p = AGame::B_BIS; break;
  99.   case 'q': p = AGame::B_QUE; break;
  100.   case 'k': p = AGame::B_KIN; break;
  101.   case 'p': p = AGame::B_PAW; break;
  102.   default:  p = AGame::EMPTY; break;
  103.   }
  104.   return p;
  105. }
  106.  
  107.  
  108. #ifdef MOO
  109.  
  110.  
  111.  
  112.  
  113. void GAME::plot(HPS hps)
  114. {
  115.   BITMAPINFO2 pbmi;
  116.   BITMAPINFO2 pbmi64;
  117.   HBITMAP       hbm;
  118.   POINTL        ptl[4] = {0,0,64,64,0,0,64,64};
  119.   POINTL        ptl2[4] = {80,80};
  120.   POINTL        p;
  121.   int           rank, file;
  122.  
  123.   pbmi.cbFix    = 16;
  124.   pbmi.cx       = 40;
  125.   pbmi.cy       = 40;
  126.   pbmi.cPlanes  = 1;
  127.   pbmi.cBitCount        = 1;
  128.  
  129.   pbmi64.cbFix  = 16;
  130.   pbmi64.cx     = 64;
  131.   pbmi64.cy     = 64;
  132.   pbmi64.cPlanes        = 1;
  133.   pbmi64.cBitCount      = 1;
  134.  
  135. /*  GpiDrawBits(hps, bishop64, &pbmi64, 4, ptl, ROP_SRCCOPY, 0);
  136.  
  137.   hbm = GpiCreateBitmap(hps, (PBITMAPINFOHEADER2)&pbmi, CBM_INIT, (PBYTE)bishop40, &pbmi);
  138.   WinDrawBitmap(hps, hbm, NULL, ptl2, CLR_BLACK, CLR_WHITE, DBM_NORMAL);*/
  139.  
  140.   for (rank=0; rank<8; rank++)
  141.     for (file=0; file<8; file++)
  142.       plot_piece(hps,piece(file,rank),rank,file);
  143. }
  144.  
  145. void GAME::update(HPS hps)
  146. {
  147.   register int           rank, file;
  148.  
  149.   /* incremental update: opiece represents what is on the screen; piece */
  150.   /* is the actual state of the board.  make the screen look like piece */
  151.   /* and copy piece to opiece.  all updates (flip, statusproc, etc). are */
  152.   /* made to piece. */
  153.   for (rank=0; rank<8; rank++)
  154.     for (file=0; file<8; file++)
  155.       if (piece[rank][file] != opiece[rank][file] ||
  156.       oAtTop != atTop)
  157.         plot_piece(hps, piece[rank][file],rank,file);
  158.   oAtTop = atTop;
  159. }
  160.  
  161.  
  162. void GAME::orient(char colour)
  163. {
  164.   if (colour == ' ')
  165.     atTop = (atTop == 'B' ? 'W' : 'B');
  166.   else
  167.     atTop = colour;
  168. }
  169.                           /*************/
  170.                           /* BoardInit */
  171.                           /*************/
  172.  
  173.  
  174. void GAME::init()
  175. {
  176.   register int rank,file;
  177.   newboard();
  178.   for (rank=0; rank<8; rank++)
  179.     for (file=0; file<8; file++)
  180.       opiece[rank][file] = PI_EMPTY;  // forces plot update
  181.  
  182.   atTop = oAtTop = 'B';
  183.   strcpy(playerW, "White");
  184.   strcpy(playerB, "Black");
  185.   timeW = 3600;
  186.   timeB = 3600;
  187.   moveNum = 0;
  188.   clockRun = FALSE;
  189. }
  190.  
  191.  
  192.                           /*************/
  193.                           /* BoardMove */
  194.                           /*************/
  195.  
  196. void GAME::move(HPS hps,char rankFrom, char fileFrom, char rankTo, char fileTo)
  197. {
  198.   register char temp;   /* to handle error case of a1->a1, etc */
  199.   temp = piece[rankFrom][fileFrom];
  200.   piece[rankFrom][fileFrom] = PI_EMPTY;
  201.   piece[rankTo][fileTo] = temp;
  202.   plot_piece(hps, PI_EMPTY, rankFrom, fileFrom);
  203.   plot_piece(hps, piece[rankTo][fileTo], rankTo, fileTo);
  204.   addmove (MOVE(SQ(rankFrom, fileFrom), SQ(rankTo, fileTo)));
  205. }
  206.  
  207. void GAME::addmove(int move)      // in format: int = MOVE(fromSQ, toSQ)
  208. {
  209.   fprintf(logfile, "move: added %d at position %d\n",
  210.       move,
  211.       gameCntr);
  212.   move_history[gameCntr++] = move;
  213.   if (viewCntr == gameCntr-1)   viewCntr++;
  214. }
  215.  
  216. void GAME::addmove(char *move, boolean white_on_move)   // in format: "Na1-b1"
  217. {
  218.   int m;
  219.  
  220.   if (strcmp(move, "o-o") == 0)
  221.     m = (white_on_move) ? MOVE(SQ(0,4), SQ(0,6)) : MOVE(SQ(7,4), SQ(7,6));
  222.  
  223.   else if (strcmp(move, "o-o-o") == 0)
  224.     m = (white_on_move) ? MOVE(SQ(0,4), SQ(0,2)) : MOVE(SQ(7,4), SQ(7,2));
  225.  
  226.   else
  227.     m = MOVE(SQ(move[2]-'1', move[1]-'a'), SQ(move[5]-'1', move[4]-'a'));
  228.  
  229.   move_history[gameCntr++] = m;
  230.   if (viewCntr == gameCntr-1)   viewCntr++;
  231.   fprintf(logfile, "move: added %s %d (%d %d %d %d) at position %d\n",
  232.       move, m,
  233.       RANK(FROMSQ(m)), FILE(FROMSQ(m)), RANK(TOSQ(m)), FILE(TOSQ(m)),
  234.       gameCntr-1);
  235. }
  236.  
  237. boolean GAME::advance_move_index(int by)
  238. {
  239.   if (by > 0)
  240.     {
  241.       if (viewCntr >= gameCntr)
  242.     return FALSE;        // no-op
  243.       viewCntr++;
  244.       return TRUE;            // did something
  245.     }
  246.  
  247.   else 
  248.     {
  249.       if (viewCntr == 0)
  250.     return FALSE;        // no-op
  251.       viewCntr--;
  252.       return TRUE;
  253.     }
  254. }
  255.  
  256. void GAME::forward()
  257. {
  258.   fprintf(logfile,"forward: viewCntr = %d\n", viewCntr);
  259.   if (advance_move_index(1))
  260.     apply(move_history[viewCntr]);
  261. }
  262.  
  263. void GAME::backward()
  264. {
  265.   register int i, j;
  266.  
  267.   fprintf(logfile,"backward: viewCntr = %d\n", viewCntr);
  268.   if (IsInitialPosition())
  269.     return;
  270.   i = viewCntr;
  271.   j = gameCntr;
  272.   newboard();
  273.   viewCntr = i;
  274.   gameCntr = j;
  275.   for (i=0; i<viewCntr; i++)
  276.     apply(move_history[i]);
  277.   advance_move_index(-1);
  278. }
  279.  
  280.  
  281. void GAME::apply(int move)        // apply specified move to piece[]
  282. {   // no validity checking
  283.   register int r1 = RANK(FROMSQ(move));
  284.   register int f1 = FILE(FROMSQ(move));
  285.   register int r2 = RANK(TOSQ(move));
  286.   register int f2 = FILE(TOSQ(move));
  287.  
  288.   if (r1==r2 && f1==4 && f2==6 && 
  289.       (piece[r1][f1]==PI_W_KIN || piece[r1][f1]==PI_B_KIN))
  290.     {
  291.       piece[r1][5] = piece[r1][7];
  292.       piece[r1][7] = PI_EMPTY;
  293.     }
  294.   else if (r1==r2 && f1==4 && f2==2 && 
  295.        (piece[r1][f1]==PI_B_KIN || piece[r1][f1]==PI_B_KIN))
  296.  
  297.     {
  298.       piece[r1][3] = piece[r1][0];
  299.       piece[r1][0] = PI_EMPTY;
  300.     }
  301.  
  302.   piece[r2][f2] = piece[r1][f1];
  303.   piece[r1][f1] = PI_EMPTY;
  304. }
  305.  
  306.                           /*********/
  307.                           /* TIMEL */
  308.                           /*********/
  309.  
  310. static VOID timel(timel_parm *tp)
  311. {
  312.   WinInitialize(0);
  313.   fprintf(logfile,"timel: ms=%d hwnd=%x\n", tp->ms, tp->hwnd);
  314.   while (1)
  315.     {
  316.       DosSleep(tp->ms);
  317.       WinPostMsg(tp->hwnd, IDM_TIMEL, NULL, NULL);
  318.     }
  319. }
  320.  
  321.  
  322. #endif
  323.