
CodeCombat代码全记录(Python学习利器)--边地森林(第二章)代码13
发布日期:2021-05-07 10:58:10
浏览次数:20
分类:原创文章
本文共 4631 字,大约阅读时间需要 15 分钟。
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 secretBif secretC: hero.moveXY(20, 33)else: hero.moveXY(20, 15)hero.moveXY(26, 24)# 如果 secretA 和 secretB 中有一个为真,走上面。secretD = secretA or secretBif secretD: hero.moveXY(32, 33)else: hero.moveXY(32, 15)hero.moveXY(38, 24)# 如果 secretB 不是真的,走上面。secretE = secretB is not True#secretE = not secretBif 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 Truewhile 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 secretChero.moveXY(19, 26)hero.say(tam)# 如果 A 或 B 为真,并且 C 为真,对 Zsofi 说 "TRUE" 。否则,说 "FALSE"。zso = (secretA or secretB) and secretChero.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 secretCif 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 secretCif 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 secretEif 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)
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2025年03月19日 02时47分25秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
android解决:使用多线程和Handler同步更新UI
2021-05-08
vue自定义封装Loading组件
2021-05-08
解决移动端项目中苹果ios和安卓android手机点击输入框网页页面自动放大缩小
2021-05-08
Element UI 中动态路由的分析及实现
2021-05-08
使用springMVC配置视图管理器后找不到指定的页面
2021-05-08
关于js中对于Promise的深入理解
2021-05-08
杭电 2007 平方和与立方和(输入数据的大小顺序并不能默认)
2021-05-08
十大排序算法之三:插入排序(Python)
2021-05-08
利用Python实现循环队列
2021-05-08
利用递归实现二叉树的前中后序遍历(Python)
2021-05-08
Python刷题输入输出
2021-05-08
冒泡排序又来啦(C/C++版本)
2021-05-08
python负数存储
2021-05-08
求二维数组中最大值的位置
2021-05-08
python中sort和sorted的区别
2021-05-08
vue中echart数据动态切换,一看就懂
2021-05-08
Python3.6爬虫记录
2021-05-08
搞清楚Spring Cloud架构原理的这4个点,轻松应对面试
2019-03-05
1月份2月份GitHub上最热门的23个Java开源项目
2019-03-05
maven安装
2019-03-05