此处内容已经被作者隐藏,请输入验证码查看内容请关注本站微信公众号,回复“验证码”,获取验证码。在微信里搜索“义龙教体之窗”或者“YL18208698906”或者微信扫描右侧二维码都可以关注本站微信公众号。 # 依次计算场上所有宝石到企鹅的距离,将target_gem设置为距离更小的宝石名称
def target_choice(context):
min_distance = 99
color = ["red_gem", "purple_gem", "pink_gem", "blue_gem","yellow_gem"]
for i in range(0, 5):
path_gem = api.check.path(start=(context.me.row, context.me.col), end=(context.items[color[i]][0].row, context.items[color[i]][0].col))
gem_distance = len(path_gem) - 1
if gem_distance < min_distance:
min_distance = gem_distance
target_gem = color[i]
return target_gem
def update(context):
m_row = context.me.row
m_col = context.me.col
gem_row = context.items[target_choice(context)][0].row
gem_col = context.items[target_choice(context)][0].col
#计算当前企鹅到出口的有效路径距离
exit_row = context.exit.row
exit_col = context.exit.col
path_exit = api.check.path(start=(m_row, m_col), end=(exit_row, exit_col))
exit_distance = len(path_exit)
m_energy = context.me.energy
b = 5 #本关卡中,宝箱期望收益为5。b大于5,则为风险偏好策略;b小于5,则为风险厌恶策略
#计算当前企鹅至宝箱的有效路径距离
path_box = api.check.path(start=(context.me.row, context.me.col), end=(context.items["box"][0].row, context.items["box"][0].col))
box_distance = len(path_box) - 1
#如果当前企鹅体力小于等于出口距离,就走向出口
if m_energy <= exit_distance:
t_row = path_exit[1][0]
t_col = path_exit[1][1]
#否则如果企鹅至宝箱的距离小于等于开宝箱阀值距离b,就走向宝箱
elif box_distance <= b :
t_row = path_box[1][0]
t_col = path_box[1][1]
#否则,就走向最近的宝石
else:
path_gem = api.check.path(start=(m_row, m_col), end=(gem_row, gem_col))
t_row = path_gem[1][0]
t_col = path_gem[1][1]
#开始向目标移动
if t_row > m_row:
return "D"
elif t_row < m_row:
return "U"
elif t_col > m_col:
return "R"
elif t_col < m_col:
return "L"
打赏微信扫一扫,打赏作者吧~