CodeCombat代码全记录(Python学习利器)--安息之云山峰(第四章)代码8
发布日期:2021-05-07 10:54:44 浏览次数:20 分类:精选文章

本文共 4200 字,大约阅读时间需要 14 分钟。

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.lengthyaks = hero.findEnemies()avgSize = averageSize(yaks)bestYak = NoneclosestDist = 9999for 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.idfriends = 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 >= minDistancewhile 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
上一篇:第三章 C#高级
下一篇:第一章 初识类和对象

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月14日 09时52分40秒