zoukankan      html  css  js  c++  java
  • 【网易官方】极客战记(codecombat)攻略-森林-春雷spring-thunder

    我爱暴风雨。大雨和闪电洗刷大地,展现闪亮的事物。

    简介

    收集宝石的工作在有雷暴雨的天气下很危险。 有些宝石和金币会引来闪电,你得提防!

    使用 与 (AND) 操作符决定物品是否安全。

    item = hero.findNearestItem()
    # 银币有一个为 2 的 value
    if item.type == "coin" and item.value == 2:
        hero.moveXY(item.pos.x, item.pos.y)

    A AND B 只在 A 和 B 都为真时为真。

    默认代码

    # 某些硬币和宝石吸引闪电。
    # 这个英雄应只收集银币和蓝宝石
    while True:
        item = hero.findNearestItem()
        # 一枚银币的价值为2。
        # 如果item.type等于“coin”,则收集
        # AND item.value 相等于2
        if item.type == "coin" and item.value == 2:
            hero.moveXY(item.pos.x, item.pos.y)
        # 一个蓝宝石价值10
        # 如果item.type等于“gem”,则收集
        # AND item.value等于10。

    概览

    这关介绍的是 布尔与(boolean and) 的概念。

    在两个布尔值之间加一个 and 会返回单个布尔值。 就像给 * 喂两个数然后吐出另一个数那样。(这里说的就是乘法啦)

    记得布尔型 (boolean) 是一字节 (bit) 的简单数据,不是 真(true) 就是 假(false) 。

    and 只在两个值都为 真 时才返回 真 ,否则出现了 假 ,结果就为 假 。

    # 布尔值加上 'and'
    hero.say(False and False) 英雄会说 'False'
    hero.say(False and True) # 英雄会说 'False'
    hero.say(True and False) # 英雄会说 'False'
    hero.say(True and True) # 英雄会说 'True'

    我们想起 < , > , <= , >= , == 返回的也是布尔值,所以来做点有用的事:

    item = hero.findNearestItem()
    # 读起来琅琅上口:
    if hero.distanceTo(item) < 15 and item.type == "potion":
        # 如果离物品的距离小于 15 ,并且,物品的类型是 potion
        hero.moveXY(item.pos.x, item.pos.y)

    春雷 解法

    # 某些硬币和宝石吸引闪电。
    # 这个英雄应只收集银币和蓝宝石
    while True:
        item = hero.findNearestItem()
        # 一枚银币的价值为2。
        # 如果item.type等于“coin”,则收集
        # AND item.value 相等于2
        if item.type == "coin" and item.value == 2:
            hero.moveXY(item.pos.x, item.pos.y)
        # 一个蓝宝石价值10
        # 如果item.type等于“gem”,则收集
        # AND item.value等于10。
        if item.type == "gem" and item.value == 10:
            hero.moveXY(item.pos.x, item.pos.y)
     
     
    本攻略发于极客战记官方教学栏目,原文地址为:
  • 相关阅读:
    Codeforces Round #271 (Div. 2) F. Ant colony 线段树
    poj 1744 tree 树分治
    HDU Shell Necklace CDQ分治+FFT
    BZOJ 1567: [JSOI2008]Blue Mary的战役地图 矩阵二维hash
    BZOJ 1042: [HAOI2008]硬币购物 容斥+背包
    HDU 6078 Wavel Sequence 树状数组优化DP
    Gym
    HDU 6058 Kanade's sum 二分,链表
    HDU 6061 RXD and functions NTT
    ZOJ 3233 Lucky Number 容斥原理
  • 原文地址:https://www.cnblogs.com/codecombat/p/12395342.html
Copyright © 2011-2022 走看看