home *** CD-ROM | disk | FTP | other *** search
/ Dream 57 / Amiga_Dream_57.iso / Amiga / Jeux / Reflexion / Crafty-15.19.lha / crafty-15.19 / src / edit.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  6KB  |  146 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "chess.h"
  5. #include "data.h"
  6.  
  7. /* last modified 04/16/97 */
  8. /*
  9. ********************************************************************************
  10. *                                                                              *
  11. *   Edit() is used to edit (alter) the current board position.  it clears the  *
  12. *   board and then allows the operator to enter a position using one of four   *
  13. *                                                                              *
  14. *   white sets the color of pieces added to white.  this color will stay in    *
  15. *   effect until specifically changed.                                         *
  16. *                                                                              *
  17. *   black sets the color of pieces added to black.  this color will stay in    *
  18. *   effect until specifically changed.                                         *
  19. *                                                                              *
  20. *   # clears the chessboard completely.                                        *
  21. *                                                                              *
  22. *   c changes (toggles) the color of pieces being placed on the board.         *
  23. *                                                                              *
  24. *   end (or . for ICS/Xboard) terminates Edit().                               *
  25. *                                                                              *
  26. *   pieces are placed on the board by three character "commands" of the form   *
  27. *   [piece][square] where piece is a member of the normal set of pieces        *
  28. *   {P,N,B,R,Q,K} and [square] is algebraic square notation (a1-h8).  ex: Ke8  *
  29. *   puts a king (of the current "color") on square e8.                         *
  30. *                                                                              *
  31. ********************************************************************************
  32. */
  33. void Edit(void)
  34. {
  35.   int athome=1, i, piece, readstat, square, tfile, trank, wtm=1;
  36.   char pieces[]={'x','X','P','p','N','n','K','k','*','*',
  37.                  'B','b','R','r','Q','q','\0'};
  38.   TREE *tree=local[0];
  39. /*
  40.  ----------------------------------------------------------
  41. |                                                          |
  42. |   next, process the commands to set the board[n] form    |
  43. |   of the chess position.                                 |
  44. |                                                          |
  45.  ----------------------------------------------------------
  46. */
  47.   while (1) {
  48.     if ((input_stream == stdin) && !xboard) {
  49.       if (wtm) printf("edit(white): ");
  50.       else printf("edit(black): ");
  51.     }
  52.     fflush(stdout);
  53.     readstat=Read(1,buffer);
  54.     if (readstat < 0) return;
  55.     nargs=ReadParse(buffer,args,"     ;");
  56.     if (xboard) Print(128,"edit.command:%s\n",args[0]);
  57.     
  58.     if (!strcmp(args[0],"white")) wtm=1;
  59.     else if (!strcmp(args[0],"black")) wtm=0;
  60.     if (!strcmp(args[0],"#"))
  61.       for (i=0;i<64;i++) PieceOnSquare(i)=0;
  62.     else if (!strcmp(args[0],"c")) wtm=ChangeSide(wtm);
  63.     else if (!strcmp(args[0],"end") || (!strcmp(args[0],"."))) break;
  64.     else if (!strcmp(args[0],"d")) DisplayChessBoard(stdout,tree->pos);
  65.     else if (strlen(args[0]) == 3) {
  66.       if (strchr(pieces,args[0][0])) {
  67.         piece=(strchr(pieces,args[0][0])-pieces) >> 1;
  68.         tfile=args[0][1]-'a';
  69.         trank=args[0][2]-'1';
  70.         square=(trank<<3)+tfile;
  71.         if ((square < 0) || (square > 63))
  72.           printf("unrecognized square %s\n",args[0]);
  73.         if (wtm) PieceOnSquare(square)=piece;
  74.         else PieceOnSquare(square)=-piece;
  75.       }
  76.     }
  77.     else if(strlen(args[0]) == 2) {
  78.       piece=pawn;
  79.       tfile=args[0][0]-'a';
  80.       trank=args[0][1]-'1';
  81.       square=(trank<<3)+tfile;
  82.       if ((square < 0) || (square > 63))
  83.         printf("unrecognized square %s\n",args[0]);
  84.       if (wtm) PieceOnSquare(square)=piece;
  85.       else PieceOnSquare(square)=-piece;
  86.     }
  87.     else printf("unrecognized piece %s\n",args[0]);
  88.   }
  89.  
  90. /*
  91.  ----------------------------------------------------------
  92. |                                                          |
  93. |   now, if a king is on its original square, check the    |
  94. |   rooks to see if they are and set the castle status     |
  95. |   accordingly.  note that this checks for pieces on the  |
  96. |   original rank, but not their original squares (ICS     |
  97. |   "wild" games) and doesn't set castling if true.        |
  98. |                                                          |
  99.  ----------------------------------------------------------
  100. */
  101.   WhiteCastle(0)=0;
  102.   BlackCastle(0)=0;
  103.   EnPassant(0)=0;
  104.   for (i=0;i<16;i++)
  105.     if (PieceOnSquare(i)==0 || PieceOnSquare(i+48)==0) athome=0;
  106.   if (!athome ||
  107.       (PieceOnSquare(A1)==rook    && PieceOnSquare(B1)==knight &&
  108.        PieceOnSquare(C1)==bishop  && PieceOnSquare(D1)==queen &&
  109.        PieceOnSquare(E1)==king    && PieceOnSquare(F1)==bishop &&
  110.        PieceOnSquare(G1)==knight  && PieceOnSquare(H1)==rook &&
  111.        PieceOnSquare(A8)==-rook   && PieceOnSquare(B8)==-knight &&
  112.        PieceOnSquare(C8)==-bishop && PieceOnSquare(D8)==-queen &&
  113.        PieceOnSquare(E8)==-king   && PieceOnSquare(F8)==-bishop &&
  114.        PieceOnSquare(G8)==-knight && PieceOnSquare(H8)==-rook)) {
  115.     if (PieceOnSquare(E1) == king) {
  116.       if (PieceOnSquare(A1) == rook) WhiteCastle(0)=WhiteCastle(0)|2;
  117.       if (PieceOnSquare(H1) == rook) WhiteCastle(0)=WhiteCastle(0)|1;
  118.     }
  119.     if (PieceOnSquare(E8) == -king) {
  120.       if (PieceOnSquare(A8) == -rook) BlackCastle(0)=BlackCastle(0)|2;
  121.       if (PieceOnSquare(H8) == -rook) BlackCastle(0)=BlackCastle(0)|1;
  122.     }
  123.   }
  124. /*
  125.  ----------------------------------------------------------
  126. |                                                          |
  127. |   basic board is now set.  now it's time to set the bit  |
  128. |   board representation correctly.                        |
  129. |                                                          |
  130.  ----------------------------------------------------------
  131. */
  132.   SetChessBitBoards(&tree->position[0]);
  133.   if (log_file) DisplayChessBoard(log_file,tree->pos);
  134.   wtm=1;
  135.   move_number=1;
  136.   tree->rephead_b=tree->replist_b;
  137.   tree->rephead_w=tree->replist_w;
  138.   *tree->rephead_w++=HashKey;
  139.   tree->position[0].rule_50_moves=0;
  140.   moves_out_of_book=0;
  141.   largest_positional_score=100;
  142.   opening=0;
  143.   middle_game=1;
  144.   end_game=0;
  145. }
  146.