home *** CD-ROM | disk | FTP | other *** search
/ Dream 57 / Amiga_Dream_57.iso / Amiga / Jeux / Reflexion / Crafty-15.19.lha / crafty-15.19 / src / repeat.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  5KB  |  108 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. *   RepetitionCheck() is used to detect a draw by repetition.  it saves the    *
  11. *   current position in the repetition list each time it is called.  the list  *
  12. *   contains all positions encountered since the last irreversible move        *
  13. *   (capture or pawn push).                                                    *
  14. *                                                                              *
  15. *   RepetitionCheck() then scans the list to determine if this position has    *
  16. *   occurred before.  if so, the position will be treated as a draw by         *
  17. *   Search().                                                                  *
  18. *                                                                              *
  19. *   RepetitionCheck() also handles 50-move draws.  the position[] structure    *
  20. *   countains the count of moves since the last capture or pawn push.  when    *
  21. *   this value reaches 100 (plies, which is 50 moves) the score is set to      *
  22. *   DRAW.                                                                      *
  23. *                                                                              *
  24. ********************************************************************************
  25. */
  26. int RepetitionCheck(TREE *tree, int ply, int wtm)
  27. {
  28.   register int entries;
  29.   register BITBOARD *replist, *thispos;
  30. /*
  31.  ----------------------------------------------------------
  32. |                                                          |
  33. |   if the 50-move rule has been reached, then adjust the  |
  34. |   score to reflect the impending draw.                   |
  35. |                                                          |
  36.  ----------------------------------------------------------
  37. */
  38.   if (Rule50Moves(ply) > 99) return(1);
  39. /*
  40.  ----------------------------------------------------------
  41. |                                                          |
  42. |   check for trivial draws, like insufficient material.   |
  43. |                                                          |
  44.  ----------------------------------------------------------
  45. */
  46.   if (!(TotalWhitePawns+TotalBlackPawns) &&
  47.       TotalWhitePieces<5 && TotalBlackPieces<5) return(1);
  48. /*
  49.  ----------------------------------------------------------
  50. |                                                          |
  51. |   insert the board into the next slot in the repetition  |
  52. |   list.  then scan the list.  we look for the case where |
  53. |   the position has been seen one time before.            |
  54. |                                                          |
  55.  ----------------------------------------------------------
  56. */
  57.   entries=Rule50Moves(ply)>>1;
  58.   thispos=((wtm)?tree->rephead_w:tree->rephead_b)+((ply-1)>>1);
  59.   *thispos=HashKey;
  60.   for (replist=thispos-1;entries;replist--,entries--)
  61.     if(*thispos == *replist) return(1);
  62.   return(0);
  63. }
  64.  
  65. /* last modified 03/11/98 */
  66. /*
  67. ********************************************************************************
  68. *                                                                              *
  69. *   RepetitionDraw() is used to detect a draw by repetition.  this routine is  *
  70. *   only called from Main() and simply scans the complete list searching for   *
  71. *   exactly three repetitions (two additional repetitions of the current       *
  72. *   position.)                                                                 *
  73. *                                                                              *
  74. ********************************************************************************
  75. */
  76. int RepetitionDraw(TREE *tree, int wtm)
  77. {
  78.   register int reps;
  79.   BITBOARD *thispos;
  80. /*
  81.  ----------------------------------------------------------
  82. |                                                          |
  83. |   if the 50-move rule has been reached, then adjust the  |
  84. |   score to reflect the impending draw.                   |
  85. |                                                          |
  86.  ----------------------------------------------------------
  87. */
  88.   if (Rule50Moves(0) > 99) return(2);
  89. /*
  90.  ----------------------------------------------------------
  91. |                                                          |
  92. |   scan the repetition list to determine if this position |
  93. |   has occurred before.                                   |
  94. |                                                          |
  95.  ----------------------------------------------------------
  96. */
  97.   reps=0;
  98.   if (wtm) {
  99.     for (thispos=tree->replist_w;thispos<tree->rephead_w;thispos++)
  100.       if(HashKey == *thispos) reps++;
  101.   }
  102.   else {
  103.     for (thispos=tree->replist_b;thispos<tree->rephead_b;thispos++)
  104.       if(HashKey == *thispos) reps++;
  105.   }
  106.   return(reps == 3);
  107. }
  108.