Ticket #145: worm.c.patch

File worm.c.patch, 11.3 kB (added by draqo, 2 years ago)
  • gnugo/engine/worm.c

    RCS file: /sources/gnugo/gnugo/engine/worm.c,v
    retrieving revision 1.72
    diff -u -r1.72 worm.c
     
    106106  gg_assert(stackp == 0); 
    107107 
    108108  /* Count liberties of different orders and initialize cutstone fields. */ 
    109   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
     109  scan_board(pos, 
    110110    if (IS_STONE(board[pos]) && is_worm_origin(pos, pos)) { 
    111       int lib1, lib2, lib3, lib4; 
     111      int lib1; 
     112          int lib2; 
     113          int lib3; 
     114          int lib4; 
    112115       
    113116      ping_cave(pos, &lib1, &lib2, &lib3, &lib4); 
    114117      ASSERT1(worm[pos].liberties == lib1, pos); 
     
    119122      worm[pos].cutstone2 = 0; 
    120123      propagate_worm(pos); 
    121124    } 
    122   } 
     125  ) 
    123126   
    124127  gg_assert(stackp == 0); 
    125128 
     
    172175   * possible we also need the old concept for correct handling of lunches. 
    173176   */ 
    174177 
    175   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
     178  scan_board(pos, 
    176179    int w1 = NO_MOVE; 
    177180    int w2 = NO_MOVE; 
    178181    int k; 
     
    187190    /* Try to find two adjacent worms (w1) and (w2)  
    188191     * of opposite colour from (pos). 
    189192     */ 
    190     for (pos2 = BOARDMIN; pos2 < BOARDMAX; pos2++) { 
     193    scan_board(pos2, 
    191194      /* Work only with the opposite color from (pos). */ 
    192195      if (board[pos2] != OTHER_COLOR(board[pos]))  
    193196        continue; 
     
    207210        else if (!is_same_worm(pos2, w1)) 
    208211          w2 = worm[pos2].origin; 
    209212      } 
    210     } 
     213        ) 
    211214     
    212215    /*  
    213216     *  We now verify the definition of cutting stones. We have 
     
    225228      DEBUG(DEBUG_WORMS, "Worm at %1m has w1 %1m and w2 %1m, cutstone %d\n", 
    226229            pos, w1, w2, worm[pos].cutstone); 
    227230    } 
    228   } 
     231  ) 
    229232   
    230233  gg_assert(stackp == 0); 
    231234   
    232235  /* Set the genus of all worms. */ 
    233   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
     236  scan_board(pos, 
    234237    if (IS_STONE(board[pos]) && is_worm_origin(pos, pos)) { 
    235238      worm[pos].genus = genus(pos); 
    236239      propagate_worm(pos); 
    237240    } 
    238   } 
     241  ) 
    239242  gg_assert(stackp == 0); 
    240243 
    241244  /* Now we try to improve the values of worm.attack and worm.defend. 
     
    251254    memset(moves_to_try, 0, sizeof(moves_to_try)); 
    252255 
    253256    /* Find which colors to try at what points. */ 
    254     for (str = BOARDMIN; str < BOARDMAX; str++) { 
     257    scan_board(str, 
    255258      if (IS_STONE(board[str]) && is_worm_origin(str, str)) { 
    256259        color = board[str]; 
    257260        moves_to_try[worm[str].defense_points[0]] |= color; 
    258261        moves_to_try[worm[str].attack_points[0]] |= OTHER_COLOR(color); 
    259262      } 
    260     } 
     263        ) 
    261264 
    262265    /* Loop over the board and over the colors and try the moves found 
    263266     * in the previous loop. 
    264267     */ 
    265     for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
    266       if (!ON_BOARD(pos)) 
    267         continue; 
     268    scan_board(pos, 
    268269 
    269270      for (color = WHITE; color <= BLACK; color++) { 
    270271        if (!(moves_to_try[pos] & color)) 
     
    286287        /* Now we try to find a group which is saved or attacked as well 
    287288         * by this move. 
    288289         */ 
    289         for (str = BOARDMIN; str < BOARDMAX; str++) { 
     290        scan_board(str, 
    290291          if (!IS_STONE(board[str]) 
    291292              || !is_worm_origin(str, str)) 
    292293            continue; 
     
    376377              } 
    377378            } 
    378379          } 
    379         } 
     380        ) 
    380381        decrease_depth_values(); 
    381382        popgo(); 
    382383      } 
    383     } 
     384        ) 
    384385  } 
    385386   
    386387  gg_assert(stackp == 0); 
     
    403404   */ 
    404405 
    405406  /* First look for vertical neighbors. */ 
    406   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
     407  scan_board(pos, 
    407408    if (IS_STONE(board[pos]) 
    408409        && IS_STONE(board[SOUTH(pos)]) 
    409410        && !is_same_worm(pos, SOUTH(pos))) { 
     
    421422        } 
    422423      } 
    423424    } 
    424   } 
     425  ) 
    425426   
    426427  /* Then look for horizontal neighbors. */ 
    427   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
     428  scan_board(pos, 
    428429    if (IS_STONE(board[pos]) 
    429430        && IS_STONE(board[EAST(pos)]) 
    430431        && !is_same_worm(pos, EAST(pos))) { 
     
    442443        } 
    443444      } 
    444445    } 
    445   } 
     446  ) 
    446447   
    447448  gg_assert(stackp == 0); 
    448449   
    449450  /* Find adjacent worms that can be easily captured, aka lunches. */ 
    450451 
    451   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
     452  scan_board(pos, 
    452453    int lunch; 
    453454     
    454455    if (!IS_STONE(board[pos]) || !is_worm_origin(pos, pos)) 
     
    464465      worm[pos].lunch = NO_MOVE; 
    465466     
    466467    propagate_worm(pos); 
    467   } 
     468  ) 
    468469   
    469470  if (!disable_threat_computation) 
    470471    find_worm_threats(); 
     
    492493   * opponent's eye space. 
    493494   */ 
    494495 
    495   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
     496  scan_board(pos, 
    496497    if (IS_STONE(board[pos]) 
    497498        && worm[pos].origin == pos 
    498499        && worm[pos].genus == 0 
     
    507508        propagate_worm(pos); 
    508509      } 
    509510    } 
    510   } 
     511  ) 
    511512} 
    512513 
    513514 
     
    526527  memset(worm, 0 , sizeof(worm)); 
    527528 
    528529  /* Initialize the worm data for each worm. */ 
    529   for (pos = BOARDMIN; pos < BOARDMAX; pos++) 
    530     if (ON_BOARD(pos)) 
    531       worm[pos].origin = NO_MOVE; 
     530  scan_board(pos, 
     531    worm[pos].origin = NO_MOVE; 
     532  ) 
    532533   
    533   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
    534     if (!ON_BOARD(pos) || worm[pos].origin != NO_MOVE) 
     534  scan_board(pos, 
     535    if (worm[pos].origin != NO_MOVE) 
    535536      continue; 
    536537    worm[pos].color = board[pos]; 
    537538    worm[pos].origin = pos; 
     
    544545      worm[pos].size = countstones(pos); 
    545546      propagate_worm(pos); 
    546547    } 
    547   } 
     548  ) 
    548549} 
    549550 
    550551 
     
    596597  int r; 
    597598     
    598599  /* Initialize arrays. */ 
    599   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
    600     if (!ON_BOARD(pos)) 
    601       continue; 
     600  scan_board(pos, 
    602601 
    603602    for (k = 0; k < 2*(board_size-1); k++) 
    604603      worms[pos][k] = NO_MOVE; 
     
    612611    } 
    613612    else 
    614613      distance[pos] = -1; 
    615   } 
     614  ) 
    616615   
    617616  dist = 0; 
    618617  found_one = 1; 
    619618  while (found_one && dist <= max_distance) { 
    620619    found_one = 0; 
    621620    dist++; 
    622     for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
    623       if (!ON_BOARD(pos) || distance[pos] != -1) 
     621    scan_board(pos, 
     622      if (distance[pos] != -1) 
    624623        continue; /* already claimed */ 
    625624 
    626625      for (r = 0; r < 4; r++) { 
     
    644643          } 
    645644        } 
    646645      } 
    647     } 
     646        ) 
    648647  } 
    649648 
    650649  /* Compute the effective sizes but only when all worms are considered. */ 
    651650  if (color == (BLACK | WHITE)) { 
    652651    /* Distribute (fractional) contributions to the worms. */ 
    653     for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
    654       if (!ON_BOARD(pos)) 
    655         continue; 
     652    scan_board(pos, 
    656653       
    657654      for (k = 0; k < nworms[pos]; k++) { 
    658655        int w = worms[pos][k]; 
     
    661658        else 
    662659          worm[w].effective_size += 1.0; 
    663660      } 
    664     } 
     661        ) 
    665662     
    666663    /* Propagate the effective size values all over the worms. */ 
    667     for (pos = BOARDMIN; pos < BOARDMAX; pos++) 
     664    scan_board(pos, 
    668665      if (IS_STONE(board[pos]) && is_worm_origin(pos, pos)) 
    669         propagate_worm(pos); 
     666            propagate_worm(pos); 
     667        ) 
    670668  } 
    671669 
    672670  /* Fill in the appropriate close_*_worms (cw) and 
    673671   * number_close_*_worms (ncw) arrays. 
    674672   */ 
    675   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
    676     if (!ON_BOARD(pos)) 
    677       continue; 
    678  
     673  scan_board(pos, 
    679674    if (nworms[pos] > MAX_CLOSE_WORMS) 
    680675      ncw[pos] = 0; 
    681676    else 
     
    683678 
    684679    for (k = 0; k < ncw[pos]; k++) 
    685680      cw[pos][k] = worms[pos][k]; 
    686   } 
     681  ) 
    687682} 
    688683 
    689684/* Identify worms which are unconditionally uncapturable in the 
     
    702697  for (color = WHITE; color <= BLACK; color++) { 
    703698    unconditional_life(unconditional_territory, color); 
    704699 
    705     for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
    706       if (!ON_BOARD(pos) || !unconditional_territory[pos]) 
     700    scan_board(pos, 
     701      if (!unconditional_territory[pos]) 
    707702        continue; 
    708703         
    709704      if (board[pos] == color) { 
     
    719714      } 
    720715      else 
    721716        worm[pos].unconditional_status = DEAD; 
    722     } 
     717        ) 
    723718  } 
    724719  gg_assert(stackp == 0); 
    725720} 
     
    742737  int other; 
    743738 
    744739   /* 1. Start with finding attack points. */ 
    745   for (str = BOARDMIN; str < BOARDMAX; str++) { 
     740  scan_board(str, 
    746741    if (!IS_STONE(board[str]) || !is_worm_origin(str, str)) 
    747742      continue; 
    748743 
     
    762757            str, attack_point); 
    763758      change_attack(str, attack_point, acode); 
    764759    } 
    765   } 
     760  ) 
    766761  gg_assert(stackp == 0); 
    767762   
    768763  /* 2. Use pattern matching to find a few more attacks. */ 
     
    770765  gg_assert(stackp == 0); 
    771766   
    772767  /* 3. Now find defense moves. */ 
    773   for (str = BOARDMIN; str < BOARDMAX; str++) { 
     768  scan_board(str, 
    774769    if (!IS_STONE(board[str]) || !is_worm_origin(str, str)) 
    775770      continue; 
    776771 
     
    801796          } 
    802797      } 
    803798    } 
    804   } 
     799  ) 
    805800  gg_assert(stackp == 0); 
    806801 
    807802  /* 4. Use pattern matching to find a few more defense moves. */ 
     
    814809   *    matching and by trying whether each attack or defense point 
    815810   *    attacks or defends other strings. 
    816811   */ 
    817   for (str = BOARDMIN; str < BOARDMAX; str++) { 
     812  scan_board(str, 
    818813    color = board[str]; 
    819814    if (!IS_STONE(color) || !is_worm_origin(str, str)) 
    820815      continue; 
     
    860855          } 
    861856      } 
    862857    } 
    863   } 
     858  ) 
    864859  gg_assert(stackp == 0); 
    865860} 
    866861 
     
    880875  int l; 
    881876  int color; 
    882877   
    883   for (str = BOARDMIN; str < BOARDMAX; str++) { 
     878  scan_board(str, 
    884879    color = board[str]; 
    885880    if (!IS_STONE(color) || !is_worm_origin(str, str)) 
    886881      continue; 
     
    969964       
    970965      /* FIXME: Try other moves also (patterns?). */ 
    971966    } 
    972   } 
     967  ) 
    973968} 
    974969 
    975970 
     
    990985  ASSERT1(stackp == 0, str); 
    991986 
    992987  *lunch = NO_MOVE; 
    993   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
     988  scan_board(pos, 
    994989    if (board[pos] != OTHER_COLOR(board[str])) 
    995990      continue; 
    996991    for (k = 0; k < 8; k++) { 
     
    10131008        break; 
    10141009      } 
    10151010    } 
    1016   } 
     1011  ) 
    10171012   
    10181013  if (*lunch != NO_MOVE) 
    10191014    return 1; 
     
    12211216  ASSERT_ON_BOARD1(str); 
    12221217  ASSERT1(IS_STONE(worm[str].color), str); 
    12231218 
    1224   for (pos = BOARDMIN; pos < BOARDMAX; pos++) 
     1219  scan_board(pos, 
    12251220    if (board[pos] == board[str] && is_same_worm(pos, str) 
    12261221        && pos != str) 
    12271222      worm[pos] = worm[str]; 
     1223  ) 
    12281224} 
    12291225 
    12301226 
     
    12371233  int pos; 
    12381234  int k; 
    12391235   
    1240   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
    1241     if (!ON_BOARD(pos) || board[pos] == EMPTY) 
     1236  scan_board(pos, 
     1237    if (board[pos] == EMPTY) 
    12421238      continue; 
    12431239 
    12441240    if (!is_worm_origin(pos, pos)) 
     
    12661262                                  worm[pos].defense_threat_codes[k]); 
    12671263      } 
    12681264    } 
    1269   } 
     1265  ) 
    12701266} 
    12711267 
    12721268 
     
    13231319   * opposite color, or one stone and the edge. 
    13241320   */ 
    13251321 
    1326   for (pos = BOARDMIN; pos < BOARDMAX; pos++) 
    1327     if (ON_BOARD(pos) 
    1328         && mse[pos] 
     1322  scan_board(pos, 
     1323    if (mse[pos] 
    13291324        && (((      !ON_BOARD(SOUTH(pos)) || board[SOUTH(pos)] == other) 
    13301325             && (   !ON_BOARD(NORTH(pos)) || board[NORTH(pos)] == other)) 
    13311326            || ((   !ON_BOARD(WEST(pos))  || board[WEST(pos)]  == other) 
    13321327                && (!ON_BOARD(EAST(pos))  || board[EAST(pos)]  == other)))) 
    13331328      mse[pos] = 0; 
     1329  ) 
    13341330   
    13351331  *lib2 = 0; 
    13361332  memset(mrc, 0, sizeof(mrc)); 
     
    14081404  int gen = -1; 
    14091405 
    14101406  memset(mg, 0, sizeof(mg)); 
    1411   for (pos = BOARDMIN; pos < BOARDMAX; pos++) { 
    1412     if (ON_BOARD(pos) 
    1413         && !mg[pos] 
     1407  scan_board(pos, 
     1408    if (!mg[pos] 
    14141409        && (board[pos] == EMPTY || !is_same_worm(pos, str))) { 
    14151410      markcomponent(str, pos, mg); 
    14161411      gen++; 
    14171412    } 
    1418   } 
     1413  ) 
    14191414 
    14201415  return gen; 
    14211416} 
     
    17071702get_lively_stones(int color, signed char safe_stones[BOARDMAX]) 
    17081703{ 
    17091704  int ii; 
    1710   memset(safe_stones, 0, BOARDMAX * sizeof(*safe_stones)); 
    1711   for (ii = BOARDMIN; ii < BOARDMAX; ii++) 
    1712     ASSERT1(safe_stones[ii] == 0, ii); 
    1713   for (ii = BOARDMIN; ii < BOARDMAX; ii++) 
     1705  memset(safe_stones, 0, BOARDMAX * sizeof(signed char)); 
     1706   
     1707  scan_board(ii, 
    17141708    if (IS_STONE(board[ii]) 
    17151709        && worm[ii].origin == ii) { 
    17161710      if (worm[ii].attack_codes[0] == 0 
    17171711          || (board[ii] == color 
    17181712              && worm[ii].defense_codes[0] != 0)) 
    17191713        mark_string(ii, safe_stones, 1); 
    1720     } 
     1714        } 
     1715  ) 
    17211716} 
    17221717 
    17231718