zoukankan      html  css  js  c++  java
  • 「网易官方」极客战记(codecombat)攻略-山峰-木材守卫-timber-guard

    (点击图片进入关卡)

    使用循环命令的部队。

    简介

    一个 for 循环类似于一个 while 循环。有更多的设置,但它可以用来完成同样的事情。

    默认代码

    while True:
        # 收集金子

     

        # 如果你有足够的金币,召唤一个士兵。

     

        # 使用 for 循环来命令每个士兵。
        # for 循环有两个部分『for X in Y』
        # Y 是被循环的数组结构
        # Y 中的每个元素都会执行,X 会被设置称当前循环的个体
        for friend in hero.findFriends():
            if friend.type == "soldier":
                enemy = friend.findNearestEnemy()
                # 如果这有一个敌人,命令她攻击。
                # 否则的话,移动她到地图的右边。

    概览

    这个关卡介绍了 for 循环。根据你所使用的语言,它们可以是完全不同的,所以请查看 S 级默认代码中的注释以获得帮助。

    记住,你命令你的士兵 command(soldier, "attack", enemy) 或者 command(soldier, "move",pos) .

    你的英雄应该留下来收集硬币。如果你试着亲自和食人魔作战,你可能会吸引食人魔的注意!

    木材守卫解法

    while True:
        # 收集金子
        coin = hero.findNearest(hero.findItems())
            if coin:
            hero.move(coin.pos)

     

        # 如果你有足够的金币,召唤一个士兵。
            if hero.gold >= hero.costOf("soldier"):
            hero.summon("soldier")

     

        # 使用 for 循环来命令每个士兵。
        # for 循环有两个部分『for X in Y』
        # Y 是被循环的数组结构
        # Y 中的每个元素都会执行,X 会被设置称当前循环的个体
        for friend in hero.findFriends():
            if friend.type == "soldier":
                enemy = friend.findNearestEnemy()
                # 如果这有一个敌人,命令她攻击。
                # 否则的话,移动她到地图的右边。
                if enemy:
                    hero.command(friend, "attack", enemy)
                else:
                    rightPos = {"x": 83, "y": 45}
                    hero.command(friend, "move", rightPos)
     
    本攻略发于极客战记官方教学栏目,原文地址为:
  • 相关阅读:
    leetcode--Longest Valid Parentheses
    leetcode--Sum Root to Leaf Numbers
    leetcode--Max Points on a Line
    leetcode--Substring with Concatenation of All Words
    leetcode--Restore IP Addresses
    leetcode--4Sum
    leetcode--3Sum
    leetcode--Simplify Path
    leetcode--Text Justification
    leetcode--Multiply Strings
  • 原文地址:https://www.cnblogs.com/codecombat/p/13563379.html
Copyright © 2011-2022 走看看