Advertisement
kartikkukreja

Heuristic Function for Reversi/Othello

Mar 30th, 2013
1,209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.11 KB | None | 0 0
  1. /*
  2.  * canmove(), isLegalMove() and num_valid_moves() are helper functions to
  3.  * count the number of valid moves for a given player in a given
  4.  * board configuration
  5.  */
  6.  
  7. bool canmove(char self, char opp, char *str)  {
  8.     if (str[0] != opp) return false;
  9.     for (int ctr = 1; ctr < 8; ctr++) {
  10.         if (str[ctr] == '-') return false;
  11.         if (str[ctr] == self) return true;
  12.     }
  13.     return false;
  14. }
  15.  
  16. bool isLegalMove(char self, char opp, char grid[8][8], int startx, int starty)   {
  17.     if (grid[startx][starty] != '-') return false;
  18.     char str[10];
  19.     int x, y, dx, dy, ctr;
  20.     for (dy = -1; dy <= 1; dy++)
  21.         for (dx = -1; dx <= 1; dx++)    {
  22.     // keep going if both velocities are zero
  23.             if (!dy && !dx) continue;
  24.             str[0] = '\0';
  25.             for (ctr = 1; ctr < 8; ctr++)   {
  26.                 x = startx + ctr*dx;
  27.                 y = starty + ctr*dy;
  28.                 if (x >= 0 && y >= 0 && x<8 && y<8) str[ctr-1] = grid[x][y];
  29.                 else str[ctr-1] = 0;
  30.             }
  31.             if (canmove(self, opp, str)) return true;
  32.         }
  33.     return false;
  34. }
  35.  
  36. int num_valid_moves(char self, char opp, char grid[8][8])   {
  37.     int count = 0, i, j;
  38.     for(i=0; i<8; i++)
  39.         for(j=0; j<8; j++)
  40.             if(isLegalMove(self, opp, grid, i, j)) count++;
  41.     return count;
  42. }
  43.  
  44. /*
  45.  * Assuming my_color stores your color and opp_color stores opponent's color
  46.  * '-' indicates an empty square on the board
  47.  * 'b' indicates a black tile and 'w' indicates a white tile on the board
  48.  */
  49. double dynamic_heuristic_evaluation_function(char grid[8][8])  {
  50.     int my_tiles = 0, opp_tiles = 0, i, j, k, my_front_tiles = 0, opp_front_tiles = 0, x, y;
  51.     double p = 0, c = 0, l = 0, m = 0, f = 0, d = 0;
  52.  
  53.     int X1[] = {-1, -1, 0, 1, 1, 1, 0, -1};
  54.     int Y1[] = {0, 1, 1, 1, 0, -1, -1, -1};
  55.     int V[8][8];
  56.  
  57.     V[0] = {20, -3, 11, 8, 8, 11, -3, 20};
  58.         V[1] = {-3, -7, -4, 1, 1, -4, -7, -3};
  59.         V[2] = {11, -4, 2, 2, 2, 2, -4, 11};
  60.         V[3] = {8, 1, 2, -3, -3, 2, 1, 8};
  61.         V[4] = {8, 1, 2, -3, -3, 2, 1, 8};
  62.         V[5] = {11, -4, 2, 2, 2, 2, -4, 11};
  63.         V[6] = {-3, -7, -4, 1, 1, -4, -7, -3};
  64.         V[7] = {20, -3, 11, 8, 8, 11, -3, 20};
  65.  
  66. // Piece difference, frontier disks and disk squares
  67.     for(i=0; i<8; i++)
  68.         for(j=0; j<8; j++)  {
  69.             if(grid[i][j] == my_color)  {
  70.                 d += V[i][j];
  71.                 my_tiles++;
  72.             } else if(grid[i][j] == opp_color)  {
  73.                 d -= V[i][j];
  74.                 opp_tiles++;
  75.             }
  76.             if(grid[i][j] != '-')   {
  77.                 for(k=0; k<8; k++)  {
  78.                     x = i + X1[k]; y = j + Y1[k];
  79.                     if(x >= 0 && x < bound_x && y >= 0 && y < bound_y && grid[x][y] == '-') {
  80.                         if(grid[i][j] == my_color)  my_front_tiles++;
  81.                         else opp_front_tiles++;
  82.                         break;
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.     if(my_tiles > opp_tiles)
  88.         p = (100.0 * my_tiles)/(my_tiles + opp_tiles);
  89.     else if(my_tiles < opp_tiles)
  90.         p = -(100.0 * opp_tiles)/(my_tiles + opp_tiles);
  91.     else p = 0;
  92.  
  93.     if(my_front_tiles > opp_front_tiles)
  94.         f = -(100.0 * my_front_tiles)/(my_front_tiles + opp_front_tiles);
  95.     else if(my_front_tiles < opp_front_tiles)
  96.         f = (100.0 * opp_front_tiles)/(my_front_tiles + opp_front_tiles);
  97.     else f = 0;
  98.  
  99. // Corner occupancy
  100.     my_tiles = opp_tiles = 0;
  101.     if(grid[0][0] == my_color) my_tiles++;
  102.     else if(grid[0][0] == opp_color) opp_tiles++;
  103.     if(grid[0][7] == my_color) my_tiles++;
  104.     else if(grid[0][7] == opp_color) opp_tiles++;
  105.     if(grid[7][0] == my_color) my_tiles++;
  106.     else if(grid[7][0] == opp_color) opp_tiles++;
  107.     if(grid[7][7] == my_color) my_tiles++;
  108.     else if(grid[7][7] == opp_color) opp_tiles++;
  109.     c = 25 * (my_tiles - opp_tiles);
  110.  
  111. // Corner closeness
  112.     my_tiles = opp_tiles = 0;
  113.     if(grid[0][0] == '-')   {
  114.         if(grid[0][1] == my_color) my_tiles++;
  115.         else if(grid[0][1] == opp_color) opp_tiles++;
  116.         if(grid[1][1] == my_color) my_tiles++;
  117.         else if(grid[1][1] == opp_color) opp_tiles++;
  118.         if(grid[1][0] == my_color) my_tiles++;
  119.         else if(grid[1][0] == opp_color) opp_tiles++;
  120.     }
  121.     if(grid[0][7] == '-')   {
  122.         if(grid[0][6] == my_color) my_tiles++;
  123.         else if(grid[0][6] == opp_color) opp_tiles++;
  124.         if(grid[1][6] == my_color) my_tiles++;
  125.         else if(grid[1][6] == opp_color) opp_tiles++;
  126.         if(grid[1][7] == my_color) my_tiles++;
  127.         else if(grid[1][7] == opp_color) opp_tiles++;
  128.     }
  129.     if(grid[7][0] == '-')   {
  130.         if(grid[7][1] == my_color) my_tiles++;
  131.         else if(grid[7][1] == opp_color) opp_tiles++;
  132.         if(grid[6][1] == my_color) my_tiles++;
  133.         else if(grid[6][1] == opp_color) opp_tiles++;
  134.         if(grid[6][0] == my_color) my_tiles++;
  135.         else if(grid[6][0] == opp_color) opp_tiles++;
  136.     }
  137.     if(grid[7][7] == '-')   {
  138.         if(grid[6][7] == my_color) my_tiles++;
  139.         else if(grid[6][7] == opp_color) opp_tiles++;
  140.         if(grid[6][6] == my_color) my_tiles++;
  141.         else if(grid[6][6] == opp_color) opp_tiles++;
  142.         if(grid[7][6] == my_color) my_tiles++;
  143.         else if(grid[7][6] == opp_color) opp_tiles++;
  144.     }
  145.     l = -12.5 * (my_tiles - opp_tiles);
  146.  
  147. // Mobility
  148.     my_tiles = num_valid_moves(my_color, opp_color, grid);
  149.     opp_tiles = num_valid_moves(opp_color, my_color, grid);
  150.     if(my_tiles > opp_tiles)
  151.         m = (100.0 * my_tiles)/(my_tiles + opp_tiles);
  152.     else if(my_tiles < opp_tiles)
  153.         m = -(100.0 * opp_tiles)/(my_tiles + opp_tiles);
  154.     else m = 0;
  155.  
  156. // final weighted score
  157.     double score = (10 * p) + (801.724 * c) + (382.026 * l) + (78.922 * m) + (74.396 * f) + (10 * d);
  158.     return score;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement