木材守卫 (Timber Guard)
战术思路分析:自动化命令
`for` 循环是 `while` 循环的升级版,专为**遍历列表**而设计。它会自动处理初始化、条件判断和索引步进。
核心:`for X in Y`
- `Y`:要遍历的列表 (`hero.findFriends()`)。
- `X`:列表中的当前元素 (`friend`)。
无需手动 `friendIndex += 1`!
for Unit in Friends: Command(Unit, ...)
Python 程序解法
动物园管理员 (Zoo Keeper)
战术思路分析:定点防御
英雄需要将 4 个士兵分别派往 4 个固定的防御点。
关键:`for j in range(len(friends))`
使用 `range(len(friends))` 产生索引 `j`,通过索引 `j` 同时访问 `friends[j]` 和 `points[j]`,实现士兵与地点的精确配对。
for j in range(len(F)):
Command(F[j], "move", P[j])
Python 程序解法
壮烈牺牲 (Glorious Sacrifice)
战术思路分析:诱饵移动
英雄需要将诱饵士兵派往远处的不同死亡地点,为英雄开路。
循环与配对
使用 `for j in range(len(friends))` 结合索引 `j`,将士兵 `friends[j]` 命令移动到对应的地点 `points[j]`。
for j in range: Command(F[j], "move", P[j])