(點擊圖片進入關卡)
找出最佳的兵種組合, 擊退食人大隊
簡介
開放式防御水平!
依靠你的技能生存 30 秒並收集 300 金幣。
你能把它變成 60 秒嗎?
默認代碼
# 練習用取模從數組中循環取值
# 在數組array中編排好兵種組合
summonTypes = []
def summonTroops():
# 用%取模來循環預設的征兵方案 len(self.built)
#type = summonTypes[???]
hero.say("I should summon troops!")
概覽
首先,按照您要召喚的單位類型填寫 “summonTypes” 數組。
然后,使用 summonTroops 函數完成
len(hero.built) % len(summonTypes)
遍歷 summonTypes 數組。
您還需要創建一個 collectCoins 函數和一個 commandTroops 函數。
提示:在 commandTroops 中,您會想要跳過任何擁有 type =="palisade" 的朋友!
聯合做戰解法
# 練習用取模從數組中循環取值
# 在數組array中編排好兵種組合
summonTypes = []
def summonTroops():
# 用%取模來循環預設的征兵方案 len(self.built)
type = summonTypes[len(hero.built) % len(summonTypes)]
if(hero.costOf(type) <= hero.gold):
hero.summon(type)
def gatherCoins():
items = hero.findItems()
item = hero.findNearest(items)
if item:
hero.move(item.pos)
def commandTroops():
friends = hero.findFriends()
for i in range(len(friends)):
friend = friends[i]
enemy = friend.findNearestEnemy()
if enemy:
hero.command(friend, "attack", enemy)
while True:
summonTroops()
gatherCoins()
commandTroops()