home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / mapi / checkers.frm / engine / valid.cpp < prev   
Encoding:
Text File  |  1996-04-11  |  5.6 KB  |  166 lines

  1. /* --------------------------------------------------------------------------
  2. b - board on which to make the move (will be changed on return)
  3. t - RED, BLACK
  4.  
  5. returns 0 if invalid move
  6.         2 if continuable
  7. else    1 
  8. -------------------------------------------------------------------------- */
  9. int MoveValid(BOARD b, int start, int dest, int t)
  10. {
  11.     int j; /* the piece we are jumping over */
  12.     int restore_depth;
  13.     int must_jump=0;
  14.  
  15.     AssertSz(start > 0 && start <= 32, "ducks");
  16.     AssertSz(dest > 0 && dest <= 32, "geese");
  17.     AssertSz(t==RED || t==BLACK, "The Tetonkaha Wildabeast");
  18.     AssertSz(b[start], "it means buffalo");
  19.     
  20.     /* destination empty */
  21.     if (0 != b[dest]) return 0;
  22.     pdebug(stddbgmin,"MoveValid destination empty %s(%d)",__FILE__,__LINE__);
  23.  
  24.     /* if must jump rule is on */
  25.     if (rConfig.iMustJump)
  26.         {
  27.         int besttype, bestnumber;
  28.         long bestquality;
  29.         long c=-1; long p=-1; int fBreak=0;
  30.         int i=0;
  31.         
  32.         restore_depth = depth_maximum;
  33.         depth_maximum = 1;
  34.         for (i=0;i<SQRS_MAX;i++)
  35.             {
  36.             if (b[i] & t)
  37.                 {
  38.                 must_jump += IterateJumps(b,i,t,0,
  39.                             &besttype, &bestnumber, &bestquality,
  40.                             c,p,&fBreak);
  41.                 }
  42.             AssertSz(fBreak==0,"no way samual jones");
  43.             }
  44.         depth_maximum = restore_depth;
  45.         }
  46.  
  47.     if (t == RED || (b[start] & KING))
  48.         {
  49.         pdebug(stddbgmin,"MoveValid considering RED moves %s(%d)",__FILE__,__LINE__);
  50.         if (red_lut[start][0] == dest) goto slide;
  51.         if (red_lut[start][2] == dest) goto slide;
  52.         if (red_jump_lut[start][1] == dest && (b[red_jump_lut[start][0]]|KING) == (next(t)|KING)) {j=red_jump_lut[start][0]; goto jump;}
  53.         if (red_jump_lut[start][3] == dest && (b[red_jump_lut[start][2]]|KING) == (next(t)|KING)) {j=red_jump_lut[start][2]; goto jump;}
  54.         }
  55.     if (t == BLACK || (b[start] & KING))
  56.         {
  57.         pdebug(stddbgmin,"MoveValid considering BLACK moves %s(%d)",__FILE__,__LINE__);
  58.         if (black_lut[start][0] == dest) goto slide;
  59.         if (black_lut[start][2] == dest) goto slide;
  60.         if (black_jump_lut[start][1] == dest && (b[black_jump_lut[start][0]]|KING) == (next(t)|KING)) {j=black_jump_lut[start][0]; goto jump;}
  61.         if (black_jump_lut[start][3] == dest && (b[black_jump_lut[start][2]]|KING) == (next(t)|KING)) {j=black_jump_lut[start][2]; goto jump;}
  62.         }
  63.  
  64.     /* illegal position */
  65.     pdebug(stddbgmin,"MoveValid illegal position %s(%d)",__FILE__,__LINE__);
  66.     return 0;
  67.  
  68. slide:
  69.     pdebug(stddbgmin,"MoveValid found legal sliding move %s(%d)",__FILE__,__LINE__);
  70.     if (must_jump)
  71.         {
  72.         pdebug(stddbgmin,"slide attempt when jumping move is possible %s(%d)",__FILE__,__LINE__);
  73.         return 0;
  74.         }
  75.     b[dest] = b[start];
  76.     b[start] = 0;
  77.  
  78.     if (RED & b[1]) b[1] |= KING;
  79.     if (RED & b[2]) b[2] |= KING;
  80.     if (RED & b[3]) b[3] |= KING;
  81.     if (RED & b[4]) b[4] |= KING;
  82.     if (BLACK & b[32]) b[32] |= KING;
  83.     if (BLACK & b[31]) b[31] |= KING;
  84.     if (BLACK & b[30]) b[30] |= KING;
  85.     if (BLACK & b[29]) b[29] |= KING;
  86.  
  87.     return 1;
  88.  
  89. jump:
  90.     pdebug(stddbgmin,"MoveValid found legal jump %s(%d)",__FILE__,__LINE__);
  91.     b[dest] = b[start];
  92.     b[start] = 0;
  93.     b[j] = 0;
  94.  
  95.     if (t == BLACK || (b[dest] & KING))
  96.         {
  97.         if (b[black_jump_lut[dest][1]] == 0 && (b[black_jump_lut[dest][0]]|KING) == (next(t)|KING)) {return 2;}
  98.         if (b[black_jump_lut[dest][3]] == 0 && (b[black_jump_lut[dest][2]]|KING) == (next(t)|KING)) {return 2;}
  99.         }
  100.  
  101.     if (t == RED || (b[dest] & KING))
  102.         {
  103.         if (b[red_jump_lut[dest][1]] == 0 && (b[red_jump_lut[dest][0]]|KING) == (next(t)|KING)) {return 2;}
  104.         if (b[red_jump_lut[dest][3]] == 0 && (b[red_jump_lut[dest][2]]|KING) == (next(t)|KING)) {return 2;}
  105.         }
  106.  
  107.     pdebug(stddbgmin,"MoveValid found jump is NOT continuable %s(%d)",__FILE__,__LINE__);
  108.     return 1;
  109. }
  110.  
  111. /* --------------------------------------------------------------------------
  112. GameOver returns
  113.  
  114. GAME_{WON}|{PLAYABLE}|{DRAWN}
  115. --------------------------------------------------------------------------- */
  116. int GameOver(BOARD b, int t)
  117. {
  118.     int restore_depth;
  119.     int besttype, bestnumber;
  120.     long bestquality;
  121.     long c=-1; long p=-1; int fBreak=0;
  122.     int i=0;                             // Piece I'm on.
  123.     int move_is_possible=0;
  124.  
  125.     restore_depth = depth_maximum;
  126.     depth_maximum = 1;
  127.     for (i=0;i<SQRS_MAX;i++)
  128.         {
  129.         if (b[i] & t)
  130.             {
  131.             move_is_possible += IterateJumps(b,i,t,0,
  132.                         &besttype, &bestnumber, &bestquality,
  133.                         c,p,&fBreak);
  134.             move_is_possible += IterateMoves(b,i,t,0,
  135.                         &besttype, &bestnumber, &bestquality,
  136.                         c,p,&fBreak);
  137.             if (move_is_possible)
  138.                 break;
  139.             }
  140.             AssertSz(fBreak==0,"no way sammy jones");
  141.         }
  142.     depth_maximum = restore_depth;
  143.  
  144.     /* ----- make sure this isn't a drawn game ----- */
  145.     if (move_is_possible)
  146.     {
  147.         int i=rConfig.iMaxMoves;
  148.         int t=0;
  149.         SQUARE b[SQRS_MAX];
  150.  
  151.         if (CMoves.GetFirstBoard(b,&t))
  152.             while (CMoves.GetNextBoard(b,&t))
  153.                 i--;
  154.         AssertSz(i > -5, "vania lorelle");
  155.         if (i<0) 
  156.             return GAME_DRAWN;
  157.     }
  158.  
  159.     if (move_is_possible)
  160.         return(GAME_PLAYABLE);
  161.     else
  162.         return(GAME_WON);    
  163.  
  164. }
  165.  
  166.