CodeCombat代码全记录(Python学习利器)--边地森林(第二章)代码13

Wonderglade(奇境)

# You need to collect several items.
# But, the burl wants the gems!
# Pick up all appearing items EXCEPT gems.

while True:
    item = hero.findNearestItem()
    if item:
        # If item.type isn't equal to "gem":
        if item.type != "gem":
            # Move to the item's position.
            hero.moveXY(item.pos.x, item.pos.y)

逻辑之路

注意:不是真的情况有多种写法,编写你认为可以的代码。

# 从巫师那得到两个秘密的真假值
hero.moveXY(14, 24)
secretA = hero.findNearestFriend().getSecretA()
secretB = hero.findNearestFriend().getSecretB()

# 如果 secretA 和 secretB 都为真,走上面的路;否则,走下面。
# 查看提示,学会写逻辑表达式。
secretC = secretA and secretB
if secretC:
    hero.moveXY(20, 33)
else:
    hero.moveXY(20, 15)
hero.moveXY(26, 24)

# 如果 secretA 和 secretB 中有一个为真,走上面。

secretD = secretA or secretB
if secretD:
    hero.moveXY(32, 33)
else:
    hero.moveXY(32, 15)
hero.moveXY(38, 24)

# 如果 secretB 不是真的,走上面。
secretE = secretB is not True
#secretE = not secretB
if secretE:
    hero.moveXY(44, 33)
else:
    hero.moveXY(44, 15)
hero.moveXY(50,24)

有用的对手

# 这片金币地中暗藏了致命的毒药。
# 兽人正在进攻,而苦力尝试偷你的金币!

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        # 只在敌人类型不是 "peon" 的时候攻击。
        if enemy.type != "peon":
            hero.attack(enemy)
    item = hero.findNearestItem()
    if item:
        # 只在物品的类型不是 "poison" 的时候收集。
        if item.type != "poison":
            hero.moveXY(item.pos.x, item.pos.y)
        pass

Burlbole Grove(树精树精)

# Don't attack the burls!
# Functions can return a value.
# When a function is called, it will be equal to the value the function returns.

def shouldAttack(target):
	# return False if no target
    if not target:
        return False
	# return False if target.type == "burl"
    if target.type == "burl":
        return False
    # Otherwise, return True
    return True

while True:
    enemy = hero.findNearestEnemy()
    # Here we use shouldAttack() to decide if we should attack!
    # heroShouldAttack will be assigned the same value that shouldAttack() returns!
    heroShouldAttack = shouldAttack(enemy)
    if heroShouldAttack:
    	hero.attack(enemy)

Cursed Wonderglade(被诅咒的梦境)

# Wonderglade has changed since our last visit.
# Ogres cursed it and we should defeat them.
# The burl still is collecting gems, so don't touch them.
# Also don't attack the burl.

while True:
    # Find the nearest item.
    # Collect it (if it exists) only if its type isn't "gem".
    item = hero.findNearestItem()
    if item and item.type != "gem":
        hero.moveXY(item.pos.x,item.pos.y)
    # Find the nearest enemy.
    # Attack it if it exists and its type isn't "burl".
    enemy = hero.findNearestEnemy()
    if enemy and enemy.type != "burl":
        hero.attack(enemy)

逻辑结论

# 移动到 Eszter 身边,从他那得到三个密码值
hero.moveXY(24, 16)
secretA = hero.findNearestFriend().getSecretA()
secretB = hero.findNearestFriend().getSecretB()
secretC = hero.findNearestFriend().getSecretC()

# 如果 A 和 B 都为真,或者 C 为真,对 Tamas 说 "TRUE"。否则,说 "FALSE"。
# 记得用上括号好好整理你的逻辑。
tam = (secretA and secretB) or secretC
hero.moveXY(19, 26)
hero.say(tam)

# 如果 A 或 B 为真,并且 C 为真,对 Zsofi 说 "TRUE" 。否则,说 "FALSE"。
zso = (secretA or secretB) and secretC
hero.moveXY(26, 36)
hero.say(zso)

# Say "TRUE" to Istvan if A OR C is true, AND if B OR C is true. Otherwise, say "FALSE."
ist = (secretA or secretC) and (secretB or secretC)
hero.moveXY(37, 34)
hero.say(ist)

# 如果 A 和 B都为真,或者 B 为真且 C 不为真,对 Csilla 说 "TRUE" 。否则,说 "FALSE"。
csi = (secretA and secretB) or (secretB and secretC)
hero.moveXY(40,22)
hero.say(csi)

逻辑圈

# 移动到巫师的旁边,获得他的密码
hero.moveXY(20, 24)
secretA = hero.findNearestFriend().getSecretA()
secretB = hero.findNearestFriend().getSecretB()
secretC = hero.findNearestFriend().getSecretC()

# If ALL three values are true, take the high path.
# Otherwise, take the low path. Save the 4th value.
secretD = secretA and secretB and secretC
if secretD:
    hero.moveXY(30, 33)
else:
    hero.moveXY(30, 15)

# If ANY of the three values are true, take the left path.
# Otherwise, go right. Save the 5th value.
secretE = secretA or secretB or secretC
if secretE:
    hero.moveXY(20, 24)
else:
    hero.moveXY(40, 24)
# If ALL five values are true, take the high path.
# Otherwise, take the low path.
secretF = secretA and secretB and secretC and secretD and secretE
if secretF:
    hero.moveXY(30, 33)
else:
    hero.moveXY(30, 15)

返回荆棘农场

函数中定义的参数不仅仅只有一个,也可以定义多个哦!

# 这个函数 “maybeBuildTrap” 定义了两个参数
def maybeBuildTrap(x, y):
    # 使用x和y作为移动的坐标。
    hero.moveXY(x, y)
    enemy = hero.findNearestEnemy()
    if enemy:
        pass
        # 使用 buildXY 在特定 x 和 y 处建造 "fire-trap".
        hero.buildXY("fire-trap", x, y)
while True:
    # 这会调用 maybeBuildTrap,并使用上方入口的坐标。
    maybeBuildTrap(43, 50)
    
    # 下面在左侧入口使用maybeBuildTrap!
    maybeBuildTrap(25, 33)
    # 在底部入口处使用“maybeBuildTrap” !
    maybeBuildTrap(43, 20)

你可能感兴趣的:(CodeCombat代码全记录(Python学习利器)--边地森林(第二章)代码13)