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

总体来讲,第四关给的钱还是挺多的!!!

Highlanders 苏格兰高地部队士兵

# You must defeat the ogres
# But they are using black magic!
# Only the highlander soldiers are immune.
# Find highlanders, their names always contain "mac"

highlanderName = "mac"

# This function should search for a string inside of a word:
def wordInString(string, word):
    lenString = len(string)
    lenWord = len(word)
    # Step through indexes (i) from 0 to (lenString - lenWord)
    index = lenString - lenWord
        # For each of them step through indexes (j) of the word length
    for i in range(index + 1):
            # If [i + j]th letter of the string is not equal [j]th letter of world, then break loop
        for j in range(lenWord):
            # if this is the last letter of the word (j == lenWord - 1), then return True.
            letterIndex = i + j
    # If loops are ended then the word is not inside the string. Return False.
            if string[letterIndex] != word[j]:
                break
            if j == lenWord -1:
                return True
    
    #return True # ∆ Remove this when the function is written.

# Look at your soldiers and choose highlanders only
soldiers = hero.findFriends()
for soldier in soldiers:
    if wordInString(soldier.id, highlanderName):
        hero.say(soldier.id + " be ready.")
        
# Time to attack!
hero.say("ATTACK!!!")

Count Emptiness空虚数

# Solve the riddler puzzle and find the treasure.
# Count the whitespace on both sides of a riddle.

# This function moves the hero N steps right.
def moveNSteps(n):
    hero.moveXY(hero.pos.x + 8 * n, hero.pos.y)

# Read the riddle.
riddle = hero.findNearestEnemy().riddle
# Trim whitespace from both sides and store in a variable
trimmed = riddle.trim()
# Find the difference between the `riddle` and `trimmed` lengths:
r = len(riddle)
t = len(trimmed)
# Use the result and moveNSteps function to move to the spot:
moveNSteps(r-t)
# Say something there!
hero.say("I will go!!")
hero.say(r)
hero.say(t)

The Hunt Begins 雪人食人族

# Senick is trying to find the elusive Burleous Majoris!
# But he doesn't know how big a Burleous Majoris would be...
# Find the average size of this burl population to use as a baseline!

# This function returns average size of all the burls in an array.
def averageSize(burls):
    sum = sumSize(burls)
    # Remember the average is the sum of the parts divided by the amount!
    return sum / burls.length

# This function should return the sum of all the burls sizes.
def sumSize(burls):
    # Implement the sum function using the burls 'size':
    size = 0
    for burl in burls:
        size += burl.size
    return size

while True:
    # Find the average size of the burls by calling the 'averageSize' function.
    burls = hero.findEnemies()
    #burls = hero.findByType("burls")
    # Say the average size of the seen burls!
    hero.say(averageSize(burls))

Grim Determination 严峻的决心

这关注意需要换个好点的眼镜,因为敌人会出现发导弹的这货,而且这货会buff小怪,给他们加属性。所以你需要有这个方法的眼镜来收拾他。

可以过关版(仅完成第一个要求):

# Your goal is to protect Reynaldo

# Find the paladin with the lowest health.
def lowestHealthPaladin():
    lowestHealth = 99999
    lowestFriend = None
    friends = hero.findFriends()
    for friend in friends:
        if friend.type != "paladin":
            continue
        if friend.health < lowestHealth and friend.health < friend.maxHealth:
            lowestHealth = friend.health
            lowestFriend = friend

    return lowestFriend

def commandPaladin(paladin):
    # Heal the paladin with the lowest health using lowestHealthPaladin()
    # You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    # Paladins can also shield: command(paladin, "shield")
    # And don't forget, they can attack, too!
    lowestFriend = lowestHealthPaladin()
    #paladin = hero.findNearest(hero.findByType("paladin"))
    if lowestFriend and paladin.canCast("heal"):
        hero.command(paladin, "cast", "heal", lowestFriend)
    else:
        missile = paladin.findNearest(hero.findEnemyMissiles())
        if missile:
            hero.command(paladin, "shield")
        else:
            enemy = paladin.findNearestEnemy()
            if enemy:
                hero.command(paladin, "attack", enemy)

def commandPeasant(friend):
    item = friend.findNearestItem()
    if item:
        hero.command(friend, "move", item.pos)

def commandGriffin(friend):
    enemy = friend.findNearestEnemy()
    if enemy:
        hero.command(friend, "attack", enemy)

def summonGriffin(friend):
    if hero.gold > hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")

def commandFriends():
    # Command your friends.
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "peasant":
            commandPeasant(friend)
            #pass
        elif friend.type == "griffin-rider":
            commandGriffin(friend)
            #pass
        elif friend.type == "paladin":
            commandPaladin(friend)

while True:
    commandFriends()
    # Summon griffin riders!
    summonGriffin()

++++++++++++++++++++++++++++++
完美版(当然方法可以自行定义,看你觉得哪个更好呢!!)

# Your goal is to protect Reynaldo

# Find the paladin with the lowest health.
def lowestHealthPaladin():
    lowestHealth = 99999
    lowestFriend = None
    friends = hero.findFriends()
    for friend in friends:
        if friend.type != "paladin":
            continue
        if friend.health < lowestHealth and friend.health < friend.maxHealth:
            lowestHealth = friend.health
            lowestFriend = friend

    return lowestFriend

def commandPaladin(paladin):
    # Heal the paladin with the lowest health using lowestHealthPaladin()
    # You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    # Paladins can also shield: command(paladin, "shield")
    # And don't forget, they can attack, too!
    lowestFriend = lowestHealthPaladin()
    #paladin = hero.findNearest(hero.findByType("paladin"))
    if lowestFriend and paladin.canCast("heal"):
        hero.command(paladin, "cast", "heal", lowestFriend)
    else:
        missile = paladin.findNearest(hero.findEnemyMissiles())
        if missile:
            hero.command(paladin, "shield")
        else:
            enemy = paladin.findNearestEnemy()
            if enemy:
                hero.command(paladin, "attack", enemy)

def commandPeasant(friend):
    item = friend.findNearestItem()
    if item:
        hero.command(friend, "move", item.pos)

def commandGriffin(friend):
    warlock = friend.findNearest(hero.findByType("warlock"))
    if warlock:
        hero.command(friend, "attack", warlock)
        enemy = friend.findNearestEnemy()
        if enemy:
            hero.command(friend, "attack", enemy)

def summonGriffin(friend):
    if hero.gold > hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")

def commandFriends():
    # Command your friends.
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "peasant":
            commandPeasant(friend)
            #pass
        elif friend.type == "griffin-rider":
            commandGriffin(friend)
            #pass
        elif friend.type == "paladin":
            commandPaladin(friend)

while True:
    commandFriends()
    # Summon griffin riders!
    summonGriffin()

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值