【网易官方】极客战记(codecombat)攻略-森林-逻辑圈logical-circle

以更复杂的逻辑跟随 Lukacs 进入森林深处

简介

Lukacs 带你到另一个有更多宝藏的小树林! 他只需要你回答几个他的谜语......

使用 AND 和 OR 将布尔值链连接在一起以执行更复杂的逻辑。

默认代码

# 移动到巫师的旁边,获得他的密码
hero.moveXY(20, 24)
secretA = hero.findNearestFriend().getSecretA()
secretB = hero.findNearestFriend().getSecretB()
secretC = hero.findNearestFriend().getSecretC()
# 如果所有三个值都是真的,则采用高路径。
# 否则,采取低路径。保存第四值。
secretD = secretA and secretB and secretC
if secretD:
    hero.moveXY(30, 33)
else:
    hero.moveXY(30, 15)
# 如果三个值中的任何一个都为真,则采取左侧路径。
# 否则,向右走。保存第五值。
# 如果所有五个值都为真,则采取高路径。
# 否则,走低路径。

概览

像 "逻辑之路" 那样,你会用上 布尔逻辑 (boolean logic) 决定你的道路,不过这次,你正在寻找最大的宝箱的路上。

首先你要按照秘密值的真假决定走上方的路还是下方的路。然后是左右,上下,以此类推。

布尔操作符 ,比如 与 (AND)、或(OR) 可以同时处理两个或以上的值。AND 在所有值都为真时返回真:

# Python 里 'and' 是小写。'True' 和 'False' 首字母大写。
a = True and True and True  # a = True
b = True and False and True  # b = False
c = False and True and False  # c = False
d = False and False and False  # d = False
e = True and True and True and True and False and True  # e = False

如果任意一个值为真,或 (OR) 返回真:

a = True or True or True  # a = True
b = True or False or True  # b = True
c = False or True or False  # c = True
d = False or False or False  # d = False
c = False or False or False or False or True or False  # e = True

使用变量记录要求的值,然后把他们连起来,铺成通往 Lukacs 的宝藏的路!

逻辑圈 解法

# 移动到巫师的旁边,获得他的密码
hero.moveXY(20, 24)
secretA = hero.findNearestFriend().getSecretA()
secretB = hero.findNearestFriend().getSecretB()
secretC = hero.findNearestFriend().getSecretC()
# 如果所有三个值都是真的,则采用高路径。
# 否则,采取低路径。保存第四值。
secretD = secretA and secretB and secretC
if secretD:
    hero.moveXY(30, 33)
else:
    hero.moveXY(30, 15)
# 如果三个值中的任何一个都为真,则采取左侧路径。
# 否则,向右走。保存第五值。
secretE = secretA or secretB or secretC
if secretE:
    hero.moveXY(20, 24)
else:
    hero.moveXY(40, 24)
# 如果所有五个值都为真,则采取高路径。
# 否则,走低路径。
secretF = secretA and secretB and secretC and secretD and secretE
if secretF:
    hero.moveXY(30, 33)
else:
    hero.moveXY(30, 15)
本攻略发于极客战记官方教学栏目,原文地址为:

猜你喜欢

转载自www.cnblogs.com/codecombat/p/12422405.html