Code Combat - The Two Flowers

Words
238
Reading
2 min
Listen
Play
6y

这一关有点意思,不仅仅要自己写三个不同的函数来调用。更重要的是我卡在了一个bug上,用了好久才解决问题。

问题是hero.findFriends() 函数不仅仅能找到自己召唤的战士,还能找到农民。。。所以如果不用if来排除农民的话,那么程序就总是出错。或者直接不调用commandSoldiers函数,导致召唤了士兵也不能攻击。

找bug真是一个痛苦又快乐的过程,如果能找到的话。。。

image.png

这一关胜利还奖励一个花环戒指。。。
image.png

最后老规矩,源代码:

# If the peasant is damaged, the flowers will shrink!

def summonSoldiers():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")

# Define the function: commandSoldiers
def commandSoldiers():
    for soldier in hero.findFriends():
        if soldier.type == "soldier":
            #hero.say(soldier)
            enemy = soldier.findNearestEnemy()
            if enemy:
                #hero.say(enemy)
                hero.command(soldier, "attack", enemy)


# Define the function: pickUpNearestCoin
def pickUpNearestCoin():
    items = hero.findItems()
    nearestCoin = hero.findNearest(items)
    if nearestCoin:
        hero.move(nearestCoin.pos)


peasant = hero.findByType("peasant")[0]

while True:
    summonSoldiers()
    commandSoldiers()
    pickUpNearestCoin()
Code Combat - The Two Flowers | Ecency