核心策略:
英雄不动手,
指挥弓箭手,
集火最强者!
食人魔 (Ogres) 正在和 雪人 (Yetis) 互殴。
如果我们乱打,或者只打最近的,可能会输掉。
集火 (Focus Fire): 所有人一起攻击同一个目标。
目标选择: 谁血最厚,就打谁!(Most Health)
遍历所有敌人,找出 health 最大的那个。
这个我们之前学过:最大值算法。
一旦找到目标 (toughest),
遍历所有朋友,命令他们攻击这个目标。
while(health > 0) 死磕!while (true) { auto enemies = hero.findEnemies(); // --- 第一步:找血量最多的敌人 --- auto toughest = nullptr; int mostHealth = 0; for(int i=0; i < enemies.size(); i++) { auto enemy = enemies[i]; // 擂台赛:谁血多谁赢 if (enemy.health > mostHealth) { mostHealth = enemy.health; toughest = enemy; } } // (循环继续...)
// (接上文...)
// --- 第二步:如果有这样的敌人,全员攻击 ---
if (toughest) {
auto friends = hero.findFriends();
for(int j=0; j < friends.size(); j++) {
auto friend = friends[j];
hero.command(friend, "attack", toughest);
}
}
} // while true 结束
为什么有两个循环?
第一个循环(i) 是为了看清楚局势。
第二个循环(j) 是为了做出行动。
先谋后动!
while(true) 主循环for(i) 遍历敌人找老大if(toughest) 确认有目标for(j) 遍历朋友下命令智慧 > 武力
英雄不需要自己动手,
也能赢得战争!