「网易官方」极客战记(codecombat)攻略-山峰-钢爪间隙-steelclaw-gap

(点击图片进入关卡)

在爪子的缝隙中被敌人包围,其余的部队保卫着一个临时的栅栏。

简介

使用模运算符( % )向每个保护点发送两个单位。

你需要召唤 8 个单位并移动到 4 点,所以 unitIndex%4 需要 defendIndex 。

默认代码

# 这个关卡介绍了%操作符,也称为模操作符。
# a % b返回除以b的余数
# 当索引可能大于长度时,这可以用于环绕数组的开头。
defendPoints = [{"x": 35, "y": 63},{"x": 61, "y": 63},{"x": 32, "y": 26},{"x":64, "y": 26}]
summonTypes =["soldier","soldier","soldier","soldier","archer","archer","archer","archer"]
# 你用360金开始建造一个士兵和弓箭手的混合体。
# Self.Bug是你曾经建造的一个部队数组。
# 在这里,我们使用"len(self.built) % len(summonTypes)"来环绕召唤类型数组。
def summonTroops():
    type = summonTypes[len(hero.built) % len(summonTypes)]
    if hero.gold >= hero.costOf(type):
        hero.summon(type)
def commandTroops():
    friends = hero.findFriends()
    for i in range(len(friends)):
        friend = friends[i]
        # 根据friendIndex使用%来环绕防卫点

 

        # 命令你的手下捍卫防卫点

 

while True:
    summonTroops()
    commandTroops()

概览

%运算符被称为模运算符。

a%b 给你 a / b 的余数(作为一个整数)。 所以 12%5 == 2 。

这可以用来环绕数组,例如:

使用数组: summonTypes = ["soldier","archer","peasant","paladin"]

用: type = summonTypes [i%summonTypes.length] 0 % 4 == 0 因此 type == "soldier"

1 % 4 == 1` 因此 `type == "archer"
2 % 4 == 2` 因此 `type == "peasant"
3 % 4 == 3` 因此 `type == "paladin"
4 % 4 == 0` 因此 `type == "soldier"
5 % 4 == 1` 因此 `type == "archer"

等等...

钢爪间隙解法

# 这个关卡介绍了%操作符,也称为模操作符。
# a % b返回除以b的余数
# 当索引可能大于长度时,这可以用于环绕数组的开头。
defendPoints = [{"x": 35, "y": 63},{"x": 61, "y": 63},{"x": 32, "y": 26},{"x":64, "y": 26}]
summonTypes =["soldier","soldier","soldier","soldier","archer","archer","archer","archer"]
# 你用360金开始建造一个士兵和弓箭手的混合体。
# Self.Bug是你曾经建造的一个部队数组。
# 在这里,我们使用"len(self.built) % len(summonTypes)"来环绕召唤类型数组。
def summonTroops():
    type = summonTypes[len(hero.built) % len(summonTypes)]
    if hero.gold >= hero.costOf(type):
        hero.summon(type)
def commandTroops():
    friends = hero.findFriends()
    for i in range(len(friends)):
        friend = friends[i]
        # 根据friendIndex使用%来环绕防卫点
        point = defendPoints[i % len(defendPoints)]
        # 命令你的手下捍卫防卫点
        hero.command(friend, "defend", point)
while True:
    summonTroops()
    commandTroops()
 
本攻略发于极客战记官方教学栏目,原文地址为:
原文地址:https://www.cnblogs.com/codecombat/p/13588186.html