CodeCombat代码全记录(Python学习利器)--安息之云山峰(第四章)代码8

YAK HEIST

# Senick needs big bait for a big burl!
# Help Senick find an above average yak!
# Don't pick one too deep in the herd, or risk angering the group.

# This function should return the average size of all the yaks:
def averageSize(yaks):
    sum = 0
    # Go through each yak and add its size to the sum.
    for yak in yaks:
        sum += yak.size
    return sum / yaks.length

yaks = hero.findEnemies()
avgSize = averageSize(yaks)

bestYak = None
closestDist = 9999
for i in range(len(yaks)):
    yak = yaks[i]
    yakDistance = hero.distanceTo(yak)
    yakSize = yak.size
    # Check if the yak is:
    # The distance is closer than the current 'closestDist' AND
    # The size is bigger than the 'avgSize'.
    if yak and yakSize > avgSize and yakDistance < closestDist:
        # Update the 'bestYak' and 'closestDist'
        closestDist = yakDistance
        bestYak = yak
        #hero.say(bestYak)

# Say the 'bestYak':
hero.say(bestYak)

Slumbering Sample

# One last stop before the plan can be set into motion!
# It's up to you to help Senick get the average size of these yetis.

# Find all the Yetis using 'findByType':
yetis = hero.findByType("yeti")
# Implement the averageSize function from scratch:
def averageSize(yetis):
    sum = 0
    for yeti in yetis:
        sum += yeti.size
    return sum / yetis.length
# Say the average size of the yetis:
average = averageSize(yetis)
hero.say(average)

Antipodes

这里需要注意的是和镜像相互碰撞即可干掉镜像,而不是attack镜像

# The warlock used the "clone" spell and created evil antipodes of our archers.
# But even that evil spell has weakness.
# If your archer touches his antipode, then it will disappear.
# If an archer touches the wrong clone or attacks one of them, then the clones start to fight.
# We can find antipodes by their names - they are each other's reverse.

# This function check two units whether they are antipodes or not.
def areAntipodes(unit1, unit2):
    reversed1 = ""
    for i in range(len(unit1.id) - 1, -1, -1):
        reversed1 += unit1.id[i]
    return reversed1 == unit2.id

friends = hero.findFriends()
enemies = hero.findEnemies()

# Find antipodes for each of your archers.
# Iterate all friends.
for friend in friends:
    # For each of friends iterate all enemies.
    for enemy in enemies:
        # Check if the pair of the current friend and the enemy are antipode.
        if areAntipodes(friend,enemy):
            # If they are antipodes, command the friend move to the enemy.
            hero.command(friend, "move", enemy.pos)

# When all clones disappears, attack the warlock.
while True:
    for friend in friends:
        enemy = hero.findNearestEnemy()
        if enemy and enemy.type == 'warlock':
            hero.command(friend, "attack", enemy)
#hero.attack(enemy)

Circumference of Yaks

# Calculate the circumference of yak circles.

# The first yak circle.
yak1 = hero.findNearestEnemy()
# The distance to the yak is the radius.
radius1 = hero.distanceTo(yak1)
# The circumference is calculated the following way:
circumference1 = 2 * Math.PI * radius1
# Let's say the result.
hero.say(circumference1)

# Move to the next mark.
hero.moveXY(60, 34)
# Find an yak from the second circle.
yak2 = hero.findNearestEnemy()
# Find the radius of the second circle.
radius2 = hero.distanceTo(yak2)
# Calculate the circumference of the second circle:
circumference2 = 2 * Math.PI * radius2
# Say the result.
hero.say(circumference2)

Clumsy Circle

# Find the soldiers who break the circle.

# All soldiers should be on the circle with the radius:
circleRadius = 20

# The function checks if an unit is placed on the circle
# with the radius with the hero in the center.
def onCircle(unit, radius):
    distance = hero.distanceTo(unit)
    # We check the approximation.
    inaccuracy = 2
    minDistance = radius - inaccuracy
    maxDistance = radius + inaccuracy
    return distance <= maxDistance and distance >= minDistance

while True:
    soldiers = hero.findByType("soldier")
    for soldier in soldiers:
        #distance = hero.distanceTo(soldier)
        # Use onCircle function to find
        #onCircle(soldier, circleRadius)
        # if the soldier is not on the circle:
        # Then say their name (`id`) to get rid of that one:
        if not onCircle(soldier, circleRadius):
            hero.say(soldier)
        pass
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值