/* **AI的入口,得到最终最优解 */ void get_move(int who, int deep) { int bottom, top, i, move_weight, best_weight; if (!empties()) return; if ((empties() < 11) && deep) deep = 13; /*快结束时加大搜索力度*/ bottom = moves_next; /*起点*/ get_all(who); /*得到当前所有可能步骤*/ top = moves_next; /*终点*/ if (bottom == top) { unget_all(); return; } /*初始化各值*/ best_x = moves_x[bottom]; best_y = moves_y[bottom]; best_weight = weigh_move(best_x, best_y, who, deep, 1); /* **初始化结束 **开始测试各值 */ for (i = bottom + 1; i < top; ++i) { move_weight = weigh_move(moves_x[i], moves_y[i], who, deep, 1); if (move_weight > best_weight || ((move_weight == best_weight) && rand() % 2)) /*如果找到更优解*/ { best_x = moves_x[i]; best_y = moves_y[i]; best_weight = move_weight; } } unget_all(); }
/* **AI的核心,得到当前走法的权重 */ int weigh_move(int x, int y, int who, int max_depth, int add) { int bottom, top, i, move_weight = 0, depth_weight = 0, temp; bottom = flips_next; make_move(x, y, who); top = flips_next; for (i = bottom; i < top; ++i) move_weight += (add ? 1 : (-1)) * weight[flips_x[i]][flips_y[i]]; if (max_depth && empties()) { who = OPPOSITE(who); add = !add; bottom = moves_next; get_all(who); top = moves_next; for(i = bottom; i < top; ++i) { temp = weigh_move(moves_x[i], moves_y[i], who, max_depth - 1, add); if(temp < depth_weight) depth_weight = temp; } unget_all(); } undo_move(); return move_weight + depth_weight; }
bool Foam::timeClusterList::purgeEmpties() { if (!this->size()) return false; label empties(0); for (label i = 0; i < this->size(); i++) { if (!this->operator[](i).times().size()) { empties++; continue; } if (empties) { this->operator[](i - empties) = this->operator[](i); } } if (empties) { this->setSize(this->size() - empties); } if (!this->size()) return false; return true; }