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

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "chess.h"
  4. #include "data.h"
  5.  
  6. /* last modified 03/12/98 */
  7. /*
  8. ********************************************************************************
  9. *                                                                              *
  10. *   AttacksFrom() is used to produce a BITBOARD which is a map of all squares  *
  11. *   attacked from this <square>.  this procedure uses the rotated bitboard     *
  12. *   technique to compute the attack maps for sliding pieces.                   *
  13. *                                                                              *
  14. ********************************************************************************
  15. */
  16. BITBOARD AttacksFrom(TREE *tree, int square, int wtm) {
  17. /*
  18.  ----------------------------------------------------------
  19. |                                                          |
  20. |  determine the type of piece on <square>.  if it's not a |
  21. |  sliding piece, simply return the normal attack bitmap.  |
  22. |  otherwise, use the rotated bitboards to compute the     |
  23. |  attack bitmap..                                         |
  24. |                                                          |
  25.  ----------------------------------------------------------
  26. */
  27.   switch (abs(PieceOnSquare(square))) {
  28.   case pawn:
  29.     if (wtm) return(w_pawn_attacks[square]);
  30.     else return(b_pawn_attacks[square]);
  31.   case knight:
  32.     return(knight_attacks[square]);
  33.   case bishop:
  34.     return(AttacksBishop(square));
  35.   case rook:
  36.     return(AttacksRook(square));
  37.   case queen:
  38.     return(AttacksQueen(square));
  39.   case king:
  40.     return(king_attacks[square]);
  41.   default:
  42.     return(0);
  43.   }
  44. }
  45.  
  46. /* last modified 03/12/98 */
  47. /*
  48. ********************************************************************************
  49. *                                                                              *
  50. *   AttacksTo() is used to produce a BITBOARD which is a map of all squares    *
  51. *   that directly attack this <square>.  the non-sliding pieces are trivial    *
  52. *   to detect, but for sliding pieces, we use a rotated bitboard trick.  the   *
  53. *   idea is to compute the squares a queen would attack, if it was standing on *
  54. *   <square> and then look at the last square attacked in each direction to    *
  55. *   determine if it is a sliding piece that moves in the right direction.  to  *
  56. *   finish up, we simply need to Or() all these attackers together.            *
  57. *                                                                              *
  58. ********************************************************************************
  59. */
  60. BITBOARD AttacksTo(TREE *tree, int square) {
  61.   register BITBOARD attacks;
  62. /*
  63.  ----------------------------------------------------------
  64. |                                                          |
  65. |  start with the pawn attacks by checking in both         |
  66. |  directions to see if a pawn on <square> would attack    |
  67. |  a pawn.                                                 |
  68. |                                                          |
  69.  ----------------------------------------------------------
  70. */
  71.   attacks=And(w_pawn_attacks[square],BlackPawns);
  72.   attacks=Or(attacks,And(b_pawn_attacks[square],WhitePawns));
  73. /*
  74.  ----------------------------------------------------------
  75. |                                                          |
  76. |  now the knights.  same drill as above.                  |
  77. |                                                          |
  78.  ----------------------------------------------------------
  79. */
  80.   attacks=Or(attacks,And(knight_attacks[square],Or(BlackKnights,
  81.                                                    WhiteKnights)));
  82. /*
  83.  ----------------------------------------------------------
  84. |                                                          |
  85. |  now the bishops and queens.  we generate the diagonal   |
  86. |  attacks from <square> then see if the blocking piece    |
  87. |  is a bishop or queen for either side.  if so, we add    |
  88. |  in that attack.                                         |
  89. |                                                          |
  90.  ----------------------------------------------------------
  91. */
  92.   attacks=Or(attacks,And(AttacksBishop(square),BishopsQueens));
  93. /*
  94.  ----------------------------------------------------------
  95. |                                                          |
  96. |  now the rooks and queens.  just like bishops and        |
  97. |  queens, but along ranks and files.                      |
  98. |                                                          |
  99.  ----------------------------------------------------------
  100. */
  101.   attacks=Or(attacks,And(AttacksRook(square),RooksQueens));
  102. /*
  103.  ----------------------------------------------------------
  104. |                                                          |
  105. |  now the kings.  just like pawns and knights.            |
  106. |                                                          |
  107.  ----------------------------------------------------------
  108. */
  109.   attacks=Or(attacks,And(king_attacks[square],Or(BlackKing,
  110.                                                  WhiteKing)));
  111.  
  112.   return(attacks);
  113. }
  114.  
  115. /* last modified 03/12/98 */
  116. /*
  117. ********************************************************************************
  118. *                                                                              *
  119. *   Attacked() is used to determine if <square> is attacked by "wtm".  the     *
  120. *   algorithm is simple, and is based on the AttacksTo() algorithm, but,       *
  121. *   rather than returning a bitmap of squares attacking <square> it returns a  *
  122. *   "1" as soon as it finds anything that attacks <square>.                    *
  123. *                                                                              *
  124. ********************************************************************************
  125. */
  126.  
  127. int Attacked(TREE *tree, int square, int wtm) {
  128.   register BITBOARD attacks;
  129.  
  130.   if (wtm) {
  131. /*
  132.  ----------------------------------------------------------
  133. |                                                          |
  134. |  start with the white attacks by checking in both        |
  135. |  directions to see if a pawn on <square> would attack    |
  136. |  a pawn.  ditto for knights, bishops, rooks, queens and  |
  137. |  kings                                                   |
  138. |                                                          |
  139.  ----------------------------------------------------------
  140. */
  141.     attacks=And(b_pawn_attacks[square],WhitePawns);
  142.     if (attacks) return(1);
  143.     attacks=And(knight_attacks[square],WhiteKnights);
  144.     if(attacks) return(1);
  145.     attacks=And(And(AttacksBishop(square),BishopsQueens),WhitePieces);
  146.     if(attacks) return(1);
  147.     attacks=And(And(AttacksRook(square),RooksQueens),WhitePieces);
  148.     if(attacks) return(1);
  149.     attacks=And(king_attacks[square],WhiteKing);
  150.     if(attacks) return(1);
  151.     return(0);
  152.   }
  153.  
  154. /*
  155.  ----------------------------------------------------------
  156. |                                                          |
  157. |  start with the white attacks by checking in both        |
  158. |  directions to see if a pawn on <square> would attack    |
  159. |  a pawn.  ditto for knights, bishops, rooks, queens and  |
  160. |  kings                                                   |
  161. |                                                          |
  162.  ----------------------------------------------------------
  163. */
  164.   else {
  165.     attacks= And(w_pawn_attacks[square],BlackPawns);
  166.     if (attacks) return(1);
  167.     attacks=And(knight_attacks[square],BlackKnights);
  168.     if(attacks) return(1);
  169.     attacks=And(And(AttacksBishop(square),BishopsQueens),BlackPieces);
  170.     if (attacks) return(1);
  171.     attacks=And(And(AttacksRook(square),RooksQueens),BlackPieces);
  172.     if(attacks) return(1);
  173.     attacks=And(king_attacks[square],BlackKing);
  174.     if(attacks) return(1);
  175.     return(0);
  176.   }
  177. }
  178.