home *** CD-ROM | disk | FTP | other *** search
/ Dream 57 / Amiga_Dream_57.iso / Amiga / Jeux / Reflexion / Crafty-15.19.lha / crafty-15.19 / src / setboard.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  9KB  |  225 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 05/13/97 */
  8. /*
  9. ********************************************************************************
  10. *                                                                              *
  11. *   SetBoard() is used to set up the board in any position desired.  it uses   *
  12. *   a forsythe-like string of characters to describe the board position.       *
  13. *                                                                              *
  14. *   the standard piece codes p,n,b,r,q,k are used to denote the type of piece  *
  15. *   on a square, upper/lower case are used to indicate the side (program/opp.) *
  16. *   of the piece.                                                              *
  17. *                                                                              *
  18. *   the pieces are entered with the rank on the program's side of the board    *
  19. *   entered first, and the rank on the opponent's side entered last.  to enter *
  20. *   empty squares, use a number between 1 and 8 to indicate how many adjacent  *
  21. *   squares are empty.  use a / to terminate each rank after all of the pieces *
  22. *   for that rank have been entered.                                           *
  23. *                                                                              *
  24. *   the following input will setup the board position that given below:        *
  25. *                                                                              *
  26. *         K2R/PPP////q/5ppp/7k/ b                                              *
  27. *                                                                              *
  28. *   this assumes that k represents a white king and -q represents a black      *
  29. *   queen.                                                                     *
  30. *                                                                              *
  31. *                          k  *  *  r  *  *  *  *                              *
  32. *                          p  p  p  *  *  *  *  *                              *
  33. *                          *  *  *  *  *  *  *  *                              *
  34. *                          *  *  *  *  *  *  *  *                              *
  35. *                          *  *  *  *  *  *  *  *                              *
  36. *                         -q  *  *  *  *  *  *  *                              *
  37. *                          *  *  *  *  * -p -p -p                              *
  38. *                          *  *  *  *  *  *  * -k                              *
  39. *                                                                              *
  40. *   the field after the final "/" should be either b or w to indicate which    *
  41. *   side is "on move."  after this side-to-move field any of the following     *
  42. *   characters can appear to indicate the following:  KQ: white can castle     *
  43. *   kingside/queenside/both;  kq: same for black;  a1-h8: indicates the        *
  44. *   square occupied by a pawn that can be captured en passant.                 *
  45. *                                                                              *
  46. ********************************************************************************
  47. */
  48. void SetBoard(int nargs, char *args[], int special)
  49. {
  50.   int twtm, i, match, num, pos, square, tboard[64];
  51.   int bcastle, ep, wcastle;
  52.   char input[80];
  53.   char bdinfo[] = {'q','r','b','*','k','n','p','*','P','N','K','*','B','R',
  54.                    'Q','*', '1','2','3','4','5','6','7','8','/'};
  55.   char status[13]={'K','Q','k','q','a','b','c','d','e','f','g','h',' '};
  56.   int whichsq, firstsq[8]={56,48,40,32,24,16,8,0};
  57.   TREE *tree=local[0];
  58.  
  59.   if (special)
  60.     strcpy(input,initial_position);
  61.   else
  62.     strcpy(input,args[0]);
  63.   for (i=0;i<64;i++) tboard[i]=0;
  64. /*
  65.  ----------------------------------------------------------
  66. |                                                          |
  67. |   scan the input string searching for pieces, numbers    |
  68. |   [empty squares], slashes [end-of-rank] and a blank     |
  69. |   [end of board, start of castle status].                |
  70. |                                                          |
  71.  ----------------------------------------------------------
  72. */
  73.   whichsq=0;
  74.   square=firstsq[whichsq];
  75.   num=0;
  76.   for (pos=0;pos<(int) strlen(args[0]);pos++) {
  77.     for (match=0;match<25 && args[0][pos]!=bdinfo[match];match++);
  78.     if (match > 24) break;
  79. /*
  80.    "/" -> end of this rank.
  81. */
  82.     else if (match == 24) {
  83.       num=0;
  84.       square=firstsq[++whichsq];
  85.       if (whichsq > 7) break;
  86.     }
  87. /*
  88.    "1-8" -> empty squares.
  89. */
  90.     else if (match >= 16) {
  91.       num+=match-15;
  92.       square+=match-15;
  93.       if (num > 8) {
  94.         printf("more than 8 squares on one rank\n");
  95.         return;
  96.       }
  97.       continue;
  98.     }
  99. /*
  100.    piece codes.
  101. */
  102.     else {
  103.       if (++num > 8) {
  104.         printf("more than 8 squares on one rank\n");
  105.         return;
  106.       }
  107.       tboard[square++]=match-7;
  108.     }
  109.   }
  110. /*
  111.  ----------------------------------------------------------
  112. |                                                          |
  113. |   now extract (a) side to move [w/b], (b) castle status  |
  114. |   [KkQq for white/black king-side ok, white/black queen- |
  115. |   side ok], (c) enpassant target square.                 |
  116. |                                                          |
  117.  ----------------------------------------------------------
  118. */
  119.   twtm=0;
  120.   ep=0;
  121.   wcastle=0;
  122.   bcastle=0;
  123. /*
  124.  ----------------------------------------------------------
  125. |                                                          |
  126. |   side to move.                                          |
  127. |                                                          |
  128.  ----------------------------------------------------------
  129. */
  130.   if (args[1][0] == 'w') twtm=1;
  131.   else if (args[1][0] == 'b') twtm=0;
  132.   else printf("side to move is bad\n");
  133. /*
  134.  ----------------------------------------------------------
  135. |                                                          |
  136. |   castling/enpassant status.                             |
  137. |                                                          |
  138.  ----------------------------------------------------------
  139. */
  140.   if (nargs>2 && strlen(args[2])) {
  141.     if (args[2][0]>='a' && args[2][0]<='h' &&
  142.         args[2][1]>'0' && args[2][1]<'9') {
  143.       ep=(args[2][1]-'1')*8+args[2][0]-'a';
  144.       pos++;
  145.     }
  146.     else for (pos=0;pos<(int) strlen(args[2]);pos++) {
  147.       for (match=0;(match<13) && (args[2][pos]!=status[match]);match++);
  148.       if (match == 0) wcastle+=1;
  149.       else if (match == 1) wcastle+=2;
  150.       else if (match == 2) bcastle+=1;
  151.       else if (match == 3) bcastle+=2;
  152.       else if (args[2][0]!='-') printf("castling status is bad.\n");
  153.     }
  154.   }
  155.   if (nargs>3 && strlen(args[3])) {
  156.     if (args[3][0]>='a' && args[3][0]<='h' &&
  157.         args[3][1]>'0' && args[3][1]<'9') {
  158.       ep=(args[3][1]-'1')*8+args[3][0]-'a';
  159.       pos++;
  160.     }
  161.     else if (args[3][0]!='-') printf("enpassant status is bad.\n");
  162.   }
  163.   for (i=0;i<64;i++) PieceOnSquare(i)=tboard[i];
  164.   WhiteCastle(0)=wcastle;
  165.   BlackCastle(0)=bcastle;
  166.   EnPassant(0)=ep;
  167.   Rule50Moves(0)=0;
  168. /*
  169.  ----------------------------------------------------------
  170. |                                                          |
  171. |   now check the castling status and enpassant status to  |
  172. |   make sure that the board is in a state that matches    |
  173. |   these.                                                 |
  174. |                                                          |
  175.  ----------------------------------------------------------
  176. */
  177.   if (((WhiteCastle(0) & 2) && (PieceOnSquare(A1) != rook)) ||
  178.       ((WhiteCastle(0) & 1) && (PieceOnSquare(H1) != rook)) ||
  179.       ((BlackCastle(0) & 2) && (PieceOnSquare(A8) != -rook)) ||
  180.       ((BlackCastle(0) & 1) && (PieceOnSquare(H8) != -rook))) {
  181.     printf("ERROR-- castling status does not match board position\n");
  182.     InitializeChessBoard(&tree->position[0]);
  183.   }
  184.   if ((twtm && EnPassant(0) && (PieceOnSquare(EnPassant(0)+8) != -pawn) &&
  185.        (PieceOnSquare(EnPassant(0)-7) != pawn) &&
  186.        (PieceOnSquare(EnPassant(0)-9) != pawn)) ||
  187.       (ChangeSide(twtm) && EnPassant(0) && (PieceOnSquare(EnPassant(0)-8) != pawn) &&
  188.        (PieceOnSquare(EnPassant(0)+7) != -pawn) &&
  189.        (PieceOnSquare(EnPassant(0)+9) != -pawn))) {
  190.     EnPassant(0)=0;
  191.   }
  192.   SetChessBitBoards(&tree->position[0]);
  193.   if (log_file) DisplayChessBoard(log_file,tree->pos);
  194.   tree->rephead_b=tree->replist_b;
  195.   tree->rephead_w=tree->replist_w;
  196.   if (twtm) *tree->rephead_w++=HashKey;
  197.   else *tree->rephead_b++=HashKey;
  198.   tree->position[0].rule_50_moves=0;
  199.   if (!special) {
  200.     last_mate_score=0;
  201.     for (i=0;i<4096;i++) {
  202.       history_w[i]=0;
  203.       history_b[i]=0;
  204.     }
  205.     for (i=0;i<MAXPLY;i++) {
  206.       tree->killer_move1[i]=0;
  207.       tree->killer_move2[i]=0;
  208.     }
  209.     last_pv.path_iteration_depth=0;
  210.     last_pv.path_length=0;
  211.     tree->pv[0].path_iteration_depth=0;
  212.     tree->pv[0].path_length=0;
  213.     moves_out_of_book=0;
  214.     largest_positional_score=100;
  215.     opening=0;
  216.     middle_game=1;
  217.     end_game=0;
  218.     wtm=twtm;
  219.   }
  220.   if (Check(!wtm)) {
  221.     Print(4095,"ERROR side not on move is in check!\n");
  222.     InitializeChessBoard(&tree->position[0]);
  223.   }
  224. }
  225.