地图:
1、体力不足去找终点,体力足则原地不动
import api
## 判断是否需要离场的函数
# 体力足返回False,体力不足返回True
def should_leave():
# 拿到我离终点的距离
e_row = api.get.exit(what="row")
e_col = api.get.exit(what="col")
path_len = api.check.path_len(row=e_row, col=e_col)
# 拿到我的当前体力
my_energy = api.get.my(what="energy")
# 判断是否需要离场
# 注意:这里是 <= 而不是 <
if my_energy <= path_len + 1:
return True # 体力不足离场
else:
return False #体力足够
## 体力不足去找终点,体力足原地不动
def update(context):
if should_leave(): # 上一环节定义的函数
# 体力不足,需要立即离开
direction = "D" # 这里替换成你的离场方向
#去往终点
#e_row = api.get.exit(what="row")
#e_col = api.get.exit(what="col")
#direction = api.check.next(end=(e_row, e_col))
else:
# 还有足够体力,在这里写你的其他逻辑
direction = "S"
print("第", context.round, "回合:", "向", direction, "方向移动")
return direction
知识点:
(1)我的当前体力:
api.get.my(what="energy")
(2)我离终点的距离:
api.check.path_len(row=e_row, col=e_col)
注意:
if my_energy <= path_len + 1
这里为什么要加 1 呢:因为当企鹅往远离终点的方向移动时,我们需要预留一个体力来走回终点。 你可以分别尝试有 +1 和没有 +1 的情况,看看会有什么不同。
2、尝试为企鹅编写程序,使它收集超过5颗紫宝石后,自动抵达出口。
import api
def should_leave():
# 拿到我离终点的距离
e_row = api.get.exit(what="row")
e_col = api.get.exit(what="col")
path_len = api.check.path_len(row=e_row, col=e_col)
# 拿到我的当前体力
my_energy = api.get.my(what="energy")
# 判断是否需要离场
# 思考一下为什么这里是 <= 而不是 < ?
if my_energy <= path_len + 1:
return True
else:
return False
def update(context):
if should_leave(): # 上一环节定义的函数
## 体力不足,需要立即离开
e_row = api.get.exit(what="row")
e_col = api.get.exit(what="col")
direction = api.check.next(end=(e_row, e_col))
else:
## 有足够体力,找紫宝石
items=context.items
purple_gems=items["purple_gem"]
purple_gem_row=purple_gems[0].row
purple_gem_col=purple_gems[0].col
direction = api.check.next(end=(purple_gem_row,purple_gem_col))
return direction
print("第", context.round, "回合:", "向", direction, "方向移动")
return direction