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 / quality.cpp < prev    next >
Encoding:
Text File  |  1996-04-11  |  4.9 KB  |  124 lines

  1. /* --------------------------------------------------------------------------
  2. returns the value of the board from 'computer_colors' perspective
  3. -------------------------------------------------------------------------- */
  4. long QualityOfBoard(BOARD b,int t)
  5. {
  6.     /* ----- constants (to be tweaked) ----- */
  7.     const int wKing            = 32 + 256;
  8.     const int wPiece           = 256;
  9.     const int wBlank           = 1;
  10.     const int wMoveTheory      = 32;
  11.     const int wCenterPos       = 4;
  12.     const int wDoubleCorner    = 2;
  13.     const int wHoldingKingRow  = 2;
  14.     const int wEdges           = -1;
  15.  
  16.     /* ----- variable factors ----- */
  17.     long q=50000; /* quality */
  18.  
  19.     #define co_kings  (pieces[computer_color | KING]       )
  20.     #define op_kings  (pieces[next(computer_color) | KING] )
  21.     #define co_pieces (pieces[computer_color]              )
  22.     #define op_pieces (pieces[next(computer_color)]        )
  23.     #define blanks    (pieces[0]                           )
  24.  
  25.     int pieces[7];
  26.     BOARD pb = b + 32;
  27.     int mtpis = 0; // movetheory pieces in system
  28.  
  29.     /* ----- number of pieces ----- */
  30.     memset(pieces,0,sizeof(pieces));
  31.     for (;;)
  32.         {
  33.         ++pieces[pb[0]];
  34.         --pb;
  35.         Assert(pb >= b);
  36.         ++pieces[pb[0]];
  37.         --pb;
  38.         Assert(pb >= b);
  39.         ++pieces[pb[0]];
  40.         --pb;
  41.         Assert(pb >= b);
  42.         ++pieces[pb[0]];
  43.         --pb;
  44.         if (pb <= b) break;
  45.         }
  46.     q += (co_kings  * wKing);    /* multiply piece count factor by weights */
  47.     q -= (op_kings  * wKing);
  48.     q += (co_pieces * wPiece);
  49.     q -= (op_pieces * wPiece);
  50.     q += (blanks    * wBlank);
  51.  
  52.     /* ----- move theory ----- */
  53.     if (co_pieces == op_pieces && 0==co_kings && 0==op_kings)
  54.         {
  55.         int base_of_system=0;
  56.         if (BLACK == next(t)) base_of_system=4;
  57.         for (;base_of_system < 32; base_of_system += 8)
  58.             {
  59.             if (b[1+base_of_system]) mtpis++;
  60.             if (b[2+base_of_system]) mtpis++;
  61.             if (b[3+base_of_system]) mtpis++;
  62.             if (b[4+base_of_system]) mtpis++;
  63.             }
  64.         mtpis &= 1;
  65.         q += wMoveTheory;
  66.         if (next(t) == computer_color)
  67.             {
  68.             if (!mtpis) /* opponent has the move */
  69.                 q -= (wMoveTheory * 2);
  70.             }
  71.         else
  72.             {
  73.             if (mtpis) /* opponent has the move */
  74.                 q -= (wMoveTheory * 2);
  75.             }
  76.         }
  77.  
  78.     /* ----- position of pieces ----- */
  79.     static const long weightTable[][3] = {         {0,0,0},
  80.         {0, wDoubleCorner, -wDoubleCorner},        {0,0,0}, {0,0,0}, {0,0,0},
  81.         {0, wDoubleCorner, -wDoubleCorner},        {0,0,0}, {0,0,0}, {0,0,0},
  82.         {0,0,0}, {0,0,0}, {0,0,0},                 {0, wEdges, -wEdges},
  83.         {0, wEdges, -wEdges},                      {0, wCenterPos, -wCenterPos},
  84.         {0, wCenterPos, -wCenterPos},              {0,0,0}, {0,0,0},
  85.         {0, wCenterPos, -wCenterPos},              {0, wCenterPos, -wCenterPos},
  86.         {0, wEdges, -wEdges},                      {0, wEdges, -wEdges},
  87.         {0,0,0}, {0,0,0}, {0,0,0},                 {0,0,0}, {0,0,0}, {0,0,0},
  88.         {0, wDoubleCorner, -wDoubleCorner},        {0,0,0}, {0,0,0}, {0,0,0},
  89.         {0, wDoubleCorner, -wDoubleCorner},        {0,0,0}, {0,0,0}, {0,0,0},
  90.         {0,0,0}, {0,0,0}, {0,0,0}                  };
  91.     long q_posfactor = 0;
  92.     q_posfactor += weightTable[  1][b[  1] & 3];
  93.     q_posfactor += weightTable[  5][b[  5] & 3];
  94.     q_posfactor += weightTable[ 12][b[ 12] & 3];
  95.     q_posfactor += weightTable[ 13][b[ 13] & 3];
  96.     q_posfactor += weightTable[ 14][b[ 14] & 3];
  97.     q_posfactor += weightTable[ 15][b[ 15] & 3];
  98.     q_posfactor += weightTable[ 18][b[ 18] & 3];
  99.     q_posfactor += weightTable[ 19][b[ 19] & 3];
  100.     q_posfactor += weightTable[ 20][b[ 20] & 3];
  101.     q_posfactor += weightTable[ 21][b[ 21] & 3];
  102.     q_posfactor += weightTable[ 28][b[ 28] & 3];
  103.     q_posfactor += weightTable[ 32][b[ 32] & 3];
  104.     if (BLACK == computer_color)
  105.         q_posfactor = -q_posfactor;
  106.     q += q_posfactor;
  107.  
  108.     /* ----- king row ----- */
  109.     if ((0 == (KING & b[1])) && 0 != b[1])   {if (b[1] & computer_color)  q+=wHoldingKingRow; else q-=wHoldingKingRow;}
  110.     if ((0 == (KING & b[3])) && 0 != b[3])   {if (b[3] & computer_color)  q+=wHoldingKingRow; else q-=wHoldingKingRow;}
  111.     if ((0 == (KING & b[30])) && 0 != b[30]) {if (b[30] & computer_color) q+=wHoldingKingRow; else q-=wHoldingKingRow;}
  112.     if ((0 == (KING & b[32])) && 0 != b[32]) {if (b[32] & computer_color) q+=wHoldingKingRow; else q-=wHoldingKingRow;}
  113.  
  114.     /* ----- return the results ----- */
  115.     pdebug(stddbg,"QualityOfBoard=%ld %s(%d)\n",q,__FILE__,__LINE__);
  116.     AssertSz(q > 0,"queue too small");
  117.     #ifndef HIGH_PERFORMANCE
  118.     if (0==rConfig.iGameType) return q;
  119.     if (1==rConfig.iGameType) return 100000-q;
  120.     AssertSz(0,"what kind of game are you playing anyway?");
  121.     #endif
  122.     return q;
  123. }
  124.