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

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "chess.h"
  4. #include "data.h"
  5.  
  6. /* last modified 03/11/98 */
  7. /*
  8. ********************************************************************************
  9. *                                                                              *
  10. *   UnMakeMove() is responsible for updating the position database whenever a  *
  11. *   move is retracted.  it is the exact inverse of MakeMove().                 *
  12. *                                                                              *
  13. ********************************************************************************
  14. */
  15. void UnMakeMove(TREE *tree, int ply, int move, int wtm)
  16. {
  17.   register int piece, from, to, captured, promote;
  18.   BITBOARD bit_move;
  19. /*
  20.  ----------------------------------------------------------
  21. |                                                          |
  22. |   first, take care of the hash key if there's a possible |
  23. |   enpassant pawn capture.                                |
  24. |                                                          |
  25.  ----------------------------------------------------------
  26. */
  27.   HashKey=tree->save_hash_key[ply];
  28.   PawnHashKey=tree->save_pawn_hash_key[ply];
  29. /*
  30.  ----------------------------------------------------------
  31. |                                                          |
  32. |   now do the piece-specific things by calling the        |
  33. |   appropriate routine.                                   |
  34. |                                                          |
  35.  ----------------------------------------------------------
  36. */
  37.   piece=Piece(move);
  38.   from=From(move);
  39.   to=To(move);
  40.   captured=Captured(move);
  41.   promote=Promote(move);
  42. UnMakePieceMove:
  43.   SetRL90(from,OccupiedRL90);
  44.   SetRL45(from,OccupiedRL45);
  45.   SetRR45(from,OccupiedRR45);
  46.   ClearRL90(to,OccupiedRL90);
  47.   ClearRL45(to,OccupiedRL45);
  48.   ClearRR45(to,OccupiedRR45);
  49.   bit_move=Or(set_mask[from],set_mask[to]);
  50.   PieceOnSquare(to)=0;
  51.   switch (piece) {
  52.  
  53. /*
  54. ********************************************************************************
  55. *                                                                              *
  56. *   unmake pawn moves.                                                         *
  57. *                                                                              *
  58. ********************************************************************************
  59. */
  60.   case pawn:
  61.     if (wtm) {
  62.       ClearSet(bit_move,WhitePawns);
  63.       ClearSet(bit_move,WhitePieces);
  64.       PieceOnSquare(from)=pawn;
  65.       if (captured == 1) {
  66.         if(EnPassant(ply) == to) {
  67.           TotalPieces++;
  68.           SetRL90(to-8,OccupiedRL90);
  69.           SetRL45(to-8,OccupiedRL45);
  70.           SetRR45(to-8,OccupiedRR45);
  71.           Set(to-8,BlackPawns);
  72.           Set(to-8,BlackPieces);
  73.           PieceOnSquare(to-8)=-pawn;
  74.           Material-=PAWN_VALUE;
  75.           TotalBlackPawns++;
  76.           captured=0;
  77.         }
  78.       }
  79. /*
  80.  --------------------------------------------------------------------
  81. |                                                                    |
  82. |  if this is a pawn promotion, remove the pawn from the counts      |
  83. |  then update the correct piece board to reflect the piece just     |
  84. |  created.                                                          |
  85. |                                                                    |
  86.  --------------------------------------------------------------------
  87. */
  88.       if (promote) {
  89.         TotalWhitePawns++;
  90.         Material+=PAWN_VALUE;
  91.         Clear(to,WhitePawns);
  92.         Clear(to,WhitePieces);
  93.         switch (promote) {
  94.         case knight:
  95.           Clear(to,WhiteKnights);
  96.           TotalWhitePieces-=knight_v;
  97.           WhiteMinors--;
  98.           Material-=KNIGHT_VALUE;
  99.           break;
  100.         case bishop:
  101.           Clear(to,WhiteBishops);
  102.           Clear(to,BishopsQueens);
  103.           TotalWhitePieces-=bishop_v;
  104.           WhiteMinors--;
  105.           Material-=BISHOP_VALUE;
  106.           break;
  107.         case rook:
  108.           Clear(to,WhiteRooks);
  109.           Clear(to,RooksQueens);
  110.           TotalWhitePieces-=rook_v;
  111.           WhiteMajors--;
  112.           Material-=ROOK_VALUE;
  113.           break;
  114.         case queen:
  115.           Clear(to,WhiteQueens);
  116.           Clear(to,BishopsQueens);
  117.           Clear(to,RooksQueens);
  118.           TotalWhitePieces-=queen_v;
  119.           WhiteMajors-=2;
  120.           Material-=QUEEN_VALUE;
  121.           break;
  122.         }
  123.       }
  124.     }
  125.     else {
  126.       ClearSet(bit_move,BlackPawns);
  127.       ClearSet(bit_move,BlackPieces);
  128.       PieceOnSquare(from)=-pawn;
  129.       if (captured == 1) {
  130.         if(EnPassant(ply) == to) {
  131.           TotalPieces++;
  132.           SetRL90(to+8,OccupiedRL90);
  133.           SetRL45(to+8,OccupiedRL45);
  134.           SetRR45(to+8,OccupiedRR45);
  135.           Set(to+8,WhitePawns);
  136.           Set(to+8,WhitePieces);
  137.           PieceOnSquare(to+8)=pawn;
  138.           Material+=PAWN_VALUE;
  139.           TotalWhitePawns++;
  140.           captured=0;
  141.         }
  142.       }
  143. /*
  144.  --------------------------------------------------------------------
  145. |                                                                    |
  146. |  if this is a pawn promotion, remove the pawn from the counts      |
  147. |  then update the correct piece board to reflect the piece just     |
  148. |  created.                                                          |
  149. |                                                                    |
  150.  --------------------------------------------------------------------
  151. */
  152.       if (promote) {
  153.         TotalBlackPawns++;
  154.         Material-=PAWN_VALUE;
  155.         Clear(to,BlackPawns);
  156.         Clear(to,BlackPieces);
  157.         switch (promote) {
  158.         case knight:
  159.           Clear(to,BlackKnights);
  160.           TotalBlackPieces-=knight_v;
  161.           BlackMinors--;
  162.           Material+=KNIGHT_VALUE;
  163.           break;
  164.         case bishop:
  165.           Clear(to,BlackBishops);
  166.           Clear(to,BishopsQueens);
  167.           TotalBlackPieces-=bishop_v;
  168.           BlackMinors--;
  169.           Material+=BISHOP_VALUE;
  170.           break;
  171.         case rook:
  172.           Clear(to,BlackRooks);
  173.           Clear(to,RooksQueens);
  174.           TotalBlackPieces-=rook_v;
  175.           BlackMajors--;
  176.           Material+=ROOK_VALUE;
  177.           break;
  178.         case queen:
  179.           Clear(to,BlackQueens);
  180.           Clear(to,BishopsQueens);
  181.           Clear(to,RooksQueens);
  182.           TotalBlackPieces-=queen_v;
  183.           BlackMajors-=2;
  184.           Material+=QUEEN_VALUE;
  185.           break;
  186.         }
  187.       }
  188.     }
  189.     break;
  190.  
  191. /*
  192. ********************************************************************************
  193. *                                                                              *
  194. *   unmake knight moves.                                                       *
  195. *                                                                              *
  196. ********************************************************************************
  197. */
  198.   case knight:
  199.     if (wtm) {
  200.       ClearSet(bit_move,WhiteKnights);
  201.       ClearSet(bit_move,WhitePieces);
  202.       PieceOnSquare(from)=knight;
  203.     }
  204.     else {
  205.       ClearSet(bit_move,BlackKnights);
  206.       ClearSet(bit_move,BlackPieces);
  207.       PieceOnSquare(from)=-knight;
  208.     }
  209.     break;
  210.  
  211. /*
  212. ********************************************************************************
  213. *                                                                              *
  214. *   unmake bishop moves.                                                       *
  215. *                                                                              *
  216. ********************************************************************************
  217. */
  218.   case bishop:
  219.     ClearSet(bit_move,BishopsQueens);
  220.     if (wtm) {
  221.       ClearSet(bit_move,WhiteBishops);
  222.       ClearSet(bit_move,WhitePieces);
  223.       PieceOnSquare(from)=bishop;
  224.     }
  225.     else {
  226.       ClearSet(bit_move,BlackBishops);
  227.       ClearSet(bit_move,BlackPieces);
  228.       PieceOnSquare(from)=-bishop;
  229.     }
  230.     break;
  231. /*
  232. ********************************************************************************
  233. *                                                                              *
  234. *   unmake rook moves.                                                         *
  235. *                                                                              *
  236. ********************************************************************************
  237. */
  238.   case rook:
  239.     ClearSet(bit_move,RooksQueens);
  240.     if (wtm) {
  241.       ClearSet(bit_move,WhiteRooks);
  242.       ClearSet(bit_move,WhitePieces);
  243.       PieceOnSquare(from)=rook;
  244.     }
  245.     else {
  246.       ClearSet(bit_move,BlackRooks);
  247.       ClearSet(bit_move,BlackPieces);
  248.       PieceOnSquare(from)=-rook;
  249.     }
  250.     break;
  251. /*
  252. ********************************************************************************
  253. *                                                                              *
  254. *   unmake queen moves.                                                        *
  255. *                                                                              *
  256. ********************************************************************************
  257. */
  258.   case queen:
  259.     ClearSet(bit_move,BishopsQueens);
  260.     ClearSet(bit_move,RooksQueens);
  261.     if (wtm) {
  262.       ClearSet(bit_move,WhiteQueens);
  263.       ClearSet(bit_move,WhitePieces);
  264.       PieceOnSquare(from)=queen;
  265.     }
  266.     else {
  267.       ClearSet(bit_move,BlackQueens);
  268.       ClearSet(bit_move,BlackPieces);
  269.       PieceOnSquare(from)=-queen;
  270.     }
  271.     break;
  272. /*
  273. ********************************************************************************
  274. *                                                                              *
  275. *   unmake king moves.                                                         *
  276. *                                                                              *
  277. ********************************************************************************
  278. */
  279.   case king:
  280.     if (wtm) {
  281.       ClearSet(bit_move,WhitePieces);
  282.       PieceOnSquare(from)=king;
  283.       WhiteKingSQ=from;
  284.       if (abs(to-from) == 2) {
  285.         if (to == 6) {
  286.           from=H1;
  287.           to=F1;
  288.           piece=rook;
  289.           goto UnMakePieceMove;
  290.         }
  291.         else {
  292.           from=A1;
  293.           to=D1;
  294.           piece=rook;
  295.           goto UnMakePieceMove;
  296.         }
  297.       }
  298.     }
  299.     else {
  300.       ClearSet(bit_move,BlackPieces);
  301.       PieceOnSquare(from)=-king;
  302.       BlackKingSQ=from;
  303.       if (abs(to-from) == 2) {
  304.         if (to == 62) {
  305.           from=H8;
  306.           to=F8;
  307.           piece=rook;
  308.           goto UnMakePieceMove;
  309.         }
  310.         else {
  311.           from=A8;
  312.           to=D8;
  313.           piece=rook;
  314.           goto UnMakePieceMove;
  315.         }
  316.       }
  317.     }
  318.     break;
  319.   }
  320. /*
  321. ********************************************************************************
  322. *                                                                              *
  323. *   now it is time to restore a piece that was captured.                       *
  324. *                                                                              *
  325. ********************************************************************************
  326. */
  327.   if(captured) {
  328.     TotalPieces++;
  329.     SetRL90(to,OccupiedRL90);
  330.     SetRL45(to,OccupiedRL45);
  331.     SetRR45(to,OccupiedRR45);
  332.     switch (captured) {
  333. /*
  334.  ----------------------------------------------------------
  335. |                                                          |
  336. |   restore a captured pawn.                               |
  337. |                                                          |
  338.  ----------------------------------------------------------
  339. */
  340.     case pawn: 
  341.       if (wtm) {
  342.         Set(to,BlackPawns);
  343.         Set(to,BlackPieces);
  344.         PieceOnSquare(to)=-pawn;
  345.         Material-=PAWN_VALUE;
  346.         TotalBlackPawns++;
  347.       }
  348.       else {
  349.         Set(to,WhitePawns);
  350.         Set(to,WhitePieces);
  351.         PieceOnSquare(to)=pawn;
  352.         Material+=PAWN_VALUE;
  353.         TotalWhitePawns++;
  354.       }
  355.     break;
  356. /*
  357.  ----------------------------------------------------------
  358. |                                                          |
  359. |   restore a captured knight.                             |
  360. |                                                          |
  361.  ----------------------------------------------------------
  362. */
  363.     case knight: 
  364.       if (wtm) {
  365.         Set(to,BlackKnights);
  366.         Set(to,BlackPieces);
  367.         PieceOnSquare(to)=-knight;
  368.         TotalBlackPieces+=knight_v;
  369.         BlackMinors++;
  370.         Material-=KNIGHT_VALUE;
  371.       }
  372.       else {
  373.         Set(to,WhiteKnights);
  374.         Set(to,WhitePieces);
  375.         PieceOnSquare(to)=knight;
  376.         TotalWhitePieces+=knight_v;
  377.         WhiteMinors++;
  378.         Material+=KNIGHT_VALUE;
  379.       }
  380.     break;
  381. /*
  382.  ----------------------------------------------------------
  383. |                                                          |
  384. |   restore a captured bishop.                             |
  385. |                                                          |
  386.  ----------------------------------------------------------
  387. */
  388.     case bishop: 
  389.       Set(to,BishopsQueens);
  390.       if (wtm) {
  391.         Set(to,BlackBishops);
  392.         Set(to,BlackPieces);
  393.         PieceOnSquare(to)=-bishop;
  394.         TotalBlackPieces+=bishop_v;
  395.         BlackMinors++;
  396.         Material-=BISHOP_VALUE;
  397.       }
  398.       else {
  399.         Set(to,WhiteBishops);
  400.         Set(to,WhitePieces);
  401.         PieceOnSquare(to)=bishop;
  402.         TotalWhitePieces+=bishop_v;
  403.         WhiteMinors++;
  404.         Material+=BISHOP_VALUE;
  405.       }
  406.     break;
  407. /*
  408.  ----------------------------------------------------------
  409. |                                                          |
  410. |   restore a captured rook.                               |
  411. |                                                          |
  412.  ----------------------------------------------------------
  413. */
  414.     case rook: 
  415.       Set(to,RooksQueens);
  416.       if (wtm) {
  417.         Set(to,BlackRooks);
  418.         Set(to,BlackPieces);
  419.         PieceOnSquare(to)=-rook;
  420.         TotalBlackPieces+=rook_v;
  421.         BlackMajors++;
  422.         Material-=ROOK_VALUE;
  423.       }
  424.       else {
  425.         Set(to,WhiteRooks);
  426.         Set(to,WhitePieces);
  427.         PieceOnSquare(to)=rook;
  428.         TotalWhitePieces+=rook_v;
  429.         WhiteMajors++;
  430.         Material+=ROOK_VALUE;
  431.       }
  432.     break;
  433. /*
  434.  ----------------------------------------------------------
  435. |                                                          |
  436. |   restore a captured queen.                              |
  437. |                                                          |
  438.  ----------------------------------------------------------
  439. */
  440.     case queen: 
  441.       Set(to,BishopsQueens);
  442.       Set(to,RooksQueens);
  443.       if (wtm) {
  444.         Set(to,BlackQueens);
  445.         Set(to,BlackPieces);
  446.         PieceOnSquare(to)=-queen;
  447.         TotalBlackPieces+=queen_v;
  448.         BlackMajors+=2;
  449.         Material-=QUEEN_VALUE;
  450.       }
  451.       else {
  452.         Set(to,WhiteQueens);
  453.         Set(to,WhitePieces);
  454.         PieceOnSquare(to)=queen;
  455.         TotalWhitePieces+=queen_v;
  456.         WhiteMajors+=2;
  457.         Material+=QUEEN_VALUE;
  458.       }
  459.       break;
  460. /*
  461.  ----------------------------------------------------------
  462. |                                                          |
  463. |   restore a captured king. [this is an error condition]  |
  464. |                                                          |
  465.  ----------------------------------------------------------
  466. */
  467.     case king: 
  468.       Print(128,"captured a king\n");
  469.       Print(128,"piece=%d,from=%d,to=%d,captured=%d\n",
  470.             piece,from,to,captured);
  471.       Print(128,"ply=%d\n",ply);
  472.       if (log_file) DisplayChessBoard(log_file,tree->pos);
  473.     }
  474.   }
  475. #if defined(DEBUG)
  476.   ValidatePosition(tree,ply,move,"UnMakeMove(2)");
  477. #endif
  478.   return;
  479. }
  480.