接下来,我们开门见山,先揭露代码!!!
import api
history = []
multi = 1.25
midfield_addition = 3
sidefield_addition = 1.5
midfield = [(1,7),(2,7),(7,1),(7,2),(7,12),(7,13),(12,7),(13,7),
(3,3),(3,4),(3,5),(3,6),(3,7),(3,8),(3,9),(3,10),(3,11),
(4,3),(4,4),(4,5),(4,9),(4,10),(4,11),
(5,3),(5,4),(5,5),(5,6),(5,7),(5,8),(5,9),(5,10),(5,11),
(6,3),(6,5),(6,9),(6,11),
(7,3),(7,5),(7,9),(7,11),
(8,3),(8,5),(8,9),(8,11),
(9,3),(9,4),(9,5),(9,6),(9,7),(9,8),(9,9),(9,10),(9,11),
(10,3),(10,4),(10,5),(10,9),(10,10),(10,11),
(11,3),(11,4),(11,5),(11,6),(11,7),(11,8),(11,9),(11,10),(11,11),
]
sidefield = [(1,4),(1,5),(1,6),(1,8),(1,9),(1,10),
(4,1),(5,1),(6,1),(8,1),(9,1),(10,1),
(4,13),(5,13),(6,13),(8,13),(9,13),(10,13),
(13,4),(13,5),(13,6),(13,8),(13,9),(13,10),
]
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 get_update_value(item,dis,item_row,item_col):
if item == "red_gem":
value = 6
value = value / (dis+1)
if closer(item_row,item_col) == False:
value = value / 100
return value
elif item == "yellow_gem":
value = 1
value = value / (dis+1)
if closer(item_row,item_col) == False:
value = value / 100
return value
def closer(item_row,item_col):
player = api.check.who_is_closer(row=item_row, col=item_col)
me_closer = player.id == api.get.my("id")
return me_closer
def update(context):
my_row = api.get.my('row')
my_col = api.get.my('col')
enemy_row = api.get.enemy('row')
enemy_col = api.get.enemy('col')
up_row = my_row - 1
up_col = my_col
down_row = my_row + 1
down_col = my_col
left_row = my_row
left_col = my_col - 1
right_row = my_row
right_col = my_col + 1
yellow0_row = api.get.item(name="yellow_gem", n=0, what="row")
yellow0_col = api.get.item(name="yellow_gem", n=0, what="col")
yellow1_row = api.get.item(name="yellow_gem", n=1, what="row")
yellow1_col = api.get.item(name="yellow_gem", n=1, what="col")
yellow2_row = api.get.item(name="yellow_gem", n=2, what="row")
yellow2_col = api.get.item(name="yellow_gem", n=2, what="col")
yellow3_row = api.get.item(name="yellow_gem", n=3, what="row")
yellow3_col = api.get.item(name="yellow_gem", n=3, what="col")
red0_row = api.get.item(name="red_gem", n=0, what="row")
red0_col = api.get.item(name="red_gem", n=0, what="col")
red1_row = api.get.item(name="red_gem", n=1, what="row")
red1_col = api.get.item(name="red_gem", n=1, what="col")
up_score = 0
down_score = 0
left_score = 0
right_score = 0
maze = context.maze
maze[yellow0_row][yellow0_col] = "yellow0_gem"
maze[yellow1_row][yellow1_col] = "yellow1_gem"
maze[yellow2_row][yellow2_col] = "yellow2_gem"
maze[yellow3_row][yellow3_col] = "yellow3_gem"
maze[red0_row][red0_col] = "red0_gem"
maze[red1_row][red1_col] = "red1_gem"
maze[my_row][my_col] = "me"
maze[enemy_row][enemy_col] = "enemy"
print("↑ 向上方向计算")
if maze[up_row][up_col] == "WALL":
up_score = 0
print("道路不可通行","\n")
else:
yellow0_path = api.check.path(start=(up_row, up_col), end=(yellow0_row, yellow0_col))
yellow1_path = api.check.path(start=(up_row, up_col), end=(yellow1_row, yellow1_col))
yellow2_path = api.check.path(start=(up_row, up_col), end=(yellow2_row, yellow2_col))
yellow3_path = api.check.path(start=(up_row, up_col), end=(yellow3_row, yellow3_col))
red0_path = api.check.path(start=(up_row, up_col), end=(red0_row, red0_col))
red1_path = api.check.path(start=(up_row, up_col), end=(red1_row, red1_col))
yellow0_score = get_update_value("yellow_gem",len(yellow0_path),yellow0_row,yellow0_col)
yellow1_score = get_update_value("yellow_gem",len(yellow1_path),yellow1_row,yellow1_col)
yellow2_score = get_update_value("yellow_gem",len(yellow2_path),yellow2_row,yellow2_col)
yellow3_score = get_update_value("yellow_gem",len(yellow3_path),yellow3_row,yellow3_col)
red0_score = get_update_value("red_gem",len(red0_path),red0_row,red0_col)
red1_score = get_update_value("red_gem",len(red1_path),red1_row,red1_col)
up1_area = []
if my_col == enemy_col:
if my_row > enemy_row:
for n in range(enemy_row+1,my_row):
up1_area.append(maze[n][my_col])
if "yellow0_gem" in up1_area:
yellow0_score = yellow0_score * multi
if "yellow1_gem" in up1_area:
yellow1_score = yellow1_score * multi
if "yellow2_gem" in up1_area:
yellow2_score = yellow2_score * multi
if "yellow3_gem" in up1_area:
yellow3_score = yellow3_score * multi
if "red0_gem" in up1_area:
red0_score = red0_score * multi
if "red1_gem" in up1_area:
red1_score = red1_score * multi
up2_area = []
if my_row < enemy_row:
if my_col < enemy_col:
for n in range(my_row,enemy_row+1):
for i in range(my_col,enemy_col+1):
up2_area.append(maze[n][i])
elif my_col > enemy_col:
for n in range(my_row,enemy_row+1):
for i in range(enemy_col,my_col+1):
up2_area.append(maze[n][i])
elif my_row > enemy_row:
if my_col < enemy_col:
for n in range(enemy_row,my_row+1):
for i in range(my_col,enemy_col+1):
up2_area.append(maze[n][i])
elif my_col > enemy_col:
for n in range(enemy_row,my_row+1):
for i in range(enemy_col,my_col+1):
up2_area.append(maze[n][i])
if "yellow0_gem" in up2_area:
yellow0_score = yellow0_score * multi
if "yellow1_gem" in up2_area:
yellow1_score = yellow1_score * multi
if "yellow2_gem" in up2_area:
yellow2_score = yellow2_score * multi
if "yellow3_gem" in up2_area:
yellow3_score = yellow3_score * multi
if "red0_gem" in up2_area:
red0_score = red0_score * multi
if "red1_gem" in up2_area:
red1_score = red1_score * multi
up3_area = []
for (row,col) in midfield:
up3_area.append(maze[row][col])
if "yellow0_gem" in up3_area:
yellow0_score = yellow0_score * midfield_addition
if "yellow1_gem" in up3_area:
yellow1_score = yellow1_score * midfield_addition
if "yellow2_gem" in up3_area:
yellow2_score = yellow2_score * midfield_addition
if "yellow3_gem" in up3_area:
yellow3_score = yellow3_score * midfield_addition
if "red0_gem" in up3_area:
red0_score = red0_score * midfield_addition
if "red1_gem" in up3_area:
red1_score = red1_score * midfield_addition
up_lis = [yellow0_score,yellow1_score,yellow2_score,yellow3_score,red0_score,red1_score]
best = max(up_lis)
up_lis.remove(best)
second_best = max(up_lis)
up_score = best + second_best
if context.round != 0:
if history[len(history)-1] == "D":
up_score = up_score * 0.95
print(best,second_best)
print("综合权重:", up_score,"\n")
print("↓ 向下方向计算")
if maze[down_row][down_col] == "WALL":
down_score = 0
print("道路不可通行","\n")
else:
yellow0_path = api.check.path(start=(down_row, down_col), end=(yellow0_row, yellow0_col))
yellow1_path = api.check.path(start=(down_row, down_col), end=(yellow1_row, yellow1_col))
yellow2_path = api.check.path(start=(down_row, down_col), end=(yellow2_row, yellow2_col))
yellow3_path = api.check.path(start=(down_row, down_col), end=(yellow3_row, yellow3_col))
red0_path = api.check.path(start=(down_row, down_col), end=(red0_row, red0_col))
red1_path = api.check.path(start=(down_row, down_col), end=(red1_row, red1_col))
yellow0_score = get_update_value("yellow_gem",len(yellow0_path),yellow0_row,yellow0_col)
yellow1_score = get_update_value("yellow_gem",len(yellow1_path),yellow1_row,yellow1_col)
yellow2_score = get_update_value("yellow_gem",len(yellow2_path),yellow2_row,yellow2_col)
yellow3_score = get_update_value("yellow_gem",len(yellow3_path),yellow3_row,yellow3_col)
red0_score = get_update_value("red_gem",len(red0_path),red0_row,red0_col)
red1_score = get_update_value("red_gem",len(red1_path),red1_row,red1_col)
down1_area = []
if my_col == enemy_col:
if my_row < enemy_row:
for n in range(my_row+1,enemy_row):
down1_area.append(maze[n][my_col])
if "yellow0_gem" in down1_area:
yellow0_score = yellow0_score * multi
if "yellow1_gem" in down1_area:
yellow1_score = yellow1_score * multi
if "yellow2_gem" in down1_area:
yellow2_score = yellow2_score * multi
if "yellow3_gem" in down1_area:
yellow3_score = yellow3_score * multi
if "red0_gem" in down1_area:
red0_score = red0_score * multi
if "red1_gem" in down1_area:
red1_score = red1_score * multi
down2_area = []
if my_row < enemy_row:
if my_col < enemy_col:
for n in range(my_row,enemy_row+1):
for i in range(my_col,enemy_col+1):
down2_area.append(maze[n][i])
elif my_col > enemy_col:
for n in range(my_row,enemy_row+1):
for i in range(enemy_col,my_col+1):
down2_area.append(maze[n][i])
elif my_row > enemy_row:
if my_col < enemy_col:
for n in range(enemy_row,my_row+1):
for i in range(my_col,enemy_col+1):
down2_area.append(maze[n][i])
elif my_col > enemy_col:
for n in range(enemy_row,my_row+1):
for i in range(enemy_col,my_col+1):
down2_area.append(maze[n][i])
if "yellow0_gem" in down2_area:
yellow0_score = yellow0_score * multi
if "yellow1_gem" in down2_area:
yellow1_score = yellow1_score * multi
if "yellow2_gem" in down2_area:
yellow2_score = yellow2_score * multi
if "yellow3_gem" in down2_area:
yellow3_score = yellow3_score * multi
if "red0_gem" in down2_area:
red0_score = red0_score * multi
if "red1_gem" in down2_area:
red1_score = red1_score * multi
down3_area = []
for (row,col) in midfield:
down3_area.append(maze[row][col])
if "yellow0_gem" in down3_area:
yellow0_score = yellow0_score * midfield_addition
if "yellow1_gem" in down3_area:
yellow1_score = yellow1_score * midfield_addition
if "yellow2_gem" in down3_area:
yellow2_score = yellow2_score * midfield_addition
if "yellow3_gem" in down3_area:
yellow3_score = yellow3_score * midfield_addition
if "red0_gem" in down3_area:
red0_score = red0_score * midfield_addition
if "red1_gem" in down3_area:
red1_score = red1_score * midfield_addition
down_lis = [yellow0_score,yellow1_score,yellow2_score,yellow3_score,red0_score,red1_score]
best = max(down_lis)
down_lis.remove(best)
second_best = max(down_lis)
down_score = best + second_best
if context.round != 0:
if history[len(history)-1] == "U":
down_score = down_score * 0.95
print(best,second_best)
print("综合权重:", down_score)
print("← 向左方向计算")
if maze[left_row][left_col] == "WALL":
left_score = 0
print("道路不可通行","\n")
else:
yellow0_path = api.check.path(start=(left_row, left_col), end=(yellow0_row, yellow0_col))
yellow1_path = api.check.path(start=(left_row, left_col), end=(yellow1_row, yellow1_col))
yellow2_path = api.check.path(start=(left_row, left_col), end=(yellow2_row, yellow2_col))
yellow3_path = api.check.path(start=(left_row, left_col), end=(yellow3_row, yellow3_col))
red0_path = api.check.path(start=(left_row, left_col), end=(red0_row, red0_col))
red1_path = api.check.path(start=(left_row, left_col), end=(red1_row, red1_col))
yellow0_score = get_update_value("yellow_gem",len(yellow0_path),yellow0_row,yellow0_col)
yellow1_score = get_update_value("yellow_gem",len(yellow1_path),yellow1_row,yellow1_col)
yellow2_score = get_update_value("yellow_gem",len(yellow2_path),yellow2_row,yellow2_col)
yellow3_score = get_update_value("yellow_gem",len(yellow3_path),yellow3_row,yellow3_col)
red0_score = get_update_value("red_gem",len(red0_path),red0_row,red0_col)
red1_score = get_update_value("red_gem",len(red1_path),red1_row,red1_col)
left1_area = []
if my_row == enemy_row:
if my_col > enemy_col:
for n in range(enemy_col+1,my_col):
left1_area.append(maze[my_row][n])
if "yellow0_gem" in left1_area:
yellow0_score = yellow0_score * multi
if "yellow1_gem" in left1_area:
yellow1_score = yellow1_score * multi
if "yellow2_gem" in left1_area:
yellow2_score = yellow2_score * multi
if "yellow3_gem" in left1_area:
yellow3_score = yellow3_score * multi
if "red0_gem" in left1_area:
red0_score = red0_score * multi
if "red1_gem" in left1_area:
red1_score = red1_score * multi
left2_area = []
if my_row < enemy_row:
if my_col < enemy_col:
for n in range(my_row,enemy_row+1):
for i in range(my_col,enemy_col+1):
left2_area.append(maze[n][i])
elif my_col > enemy_col:
for n in range(my_row,enemy_row+1):
for i in range(enemy_col,my_col+1):
left2_area.append(maze[n][i])
elif my_row > enemy_row:
if my_col < enemy_col:
for n in range(enemy_row,my_row+1):
for i in range(my_col,enemy_col+1):
left2_area.append(maze[n][i])
elif my_col > enemy_col:
for n in range(enemy_row,my_row+1):
for i in range(enemy_col,my_col+1):
left2_area.append(maze[n][i])
if "yellow0_gem" in left2_area:
yellow0_score = yellow0_score * multi
if "yellow1_gem" in left2_area:
yellow1_score = yellow1_score * multi
if "yellow2_gem" in left2_area:
yellow2_score = yellow2_score * multi
if "yellow3_gem" in left2_area:
yellow3_score = yellow3_score * multi
if "red0_gem" in left2_area:
red0_score = red0_score * multi
if "red1_gem" in left2_area:
red1_score = red1_score * multi
left3_area = []
for (row,col) in midfield:
left3_area.append(maze[row][col])
if "yellow0_gem" in left3_area:
yellow0_score = yellow0_score * midfield_addition
if "yellow1_gem" in left3_area:
yellow1_score = yellow1_score * midfield_addition
if "yellow2_gem" in left3_area:
yellow2_score = yellow2_score * midfield_addition
if "yellow3_gem" in left3_area:
yellow3_score = yellow3_score * midfield_addition
if "red0_gem" in left3_area:
red0_score = red0_score * midfield_addition
if "red1_gem" in left3_area:
red1_score = red1_score * midfield_addition
left_lis = [yellow0_score,yellow1_score,yellow2_score,yellow3_score,red0_score,red1_score]
best = max(left_lis)
left_lis.remove(best)
second_best = max(left_lis)
left_score = best + second_best
if context.round != 0:
if history[len(history)-1] == "R":
left_score = left_score * 0.95
print(best,second_best)
print("综合权重:", left_score)
print("→ 向右方向计算")
if maze[right_row][right_col] == "WALL":
right_score = 0
print("道路不可通行","\n")
else:
yellow0_path = api.check.path(start=(right_row, right_col), end=(yellow0_row, yellow0_col))
yellow1_path = api.check.path(start=(right_row, right_col), end=(yellow1_row, yellow1_col))
yellow2_path = api.check.path(start=(right_row, right_col), end=(yellow2_row, yellow2_col))
yellow3_path = api.check.path(start=(right_row, right_col), end=(yellow3_row, yellow3_col))
red0_path = api.check.path(start=(right_row, right_col), end=(red0_row, red0_col))
red1_path = api.check.path(start=(right_row, right_col), end=(red1_row, red1_col))
yellow0_score = get_update_value("yellow_gem",len(yellow0_path),yellow0_row,yellow0_col)
yellow1_score = get_update_value("yellow_gem",len(yellow1_path),yellow1_row,yellow1_col)
yellow2_score = get_update_value("yellow_gem",len(yellow2_path),yellow2_row,yellow2_col)
yellow3_score = get_update_value("yellow_gem",len(yellow3_path),yellow3_row,yellow3_col)
red0_score = get_update_value("red_gem",len(red0_path),red0_row,red0_col)
red1_score = get_update_value("red_gem",len(red1_path),red1_row,red1_col)
right1_area = []
if my_row == enemy_row:
if my_col < enemy_col:
for n in range(enemy_col+1,my_col):
right1_area.append(maze[my_row][n])
if "yellow0_gem" in right1_area:
yellow0_score = yellow0_score * multi
if "yellow1_gem" in right1_area:
yellow1_score = yellow1_score * multi
if "yellow2_gem" in right1_area:
yellow2_score = yellow2_score * multi
if "yellow3_gem" in right1_area:
yellow3_score = yellow3_score * multi
if "red0_gem" in right1_area:
red0_score = red0_score * multi
if "red1_gem" in right1_area:
red1_score = red1_score * multi
right2_area = []
if my_row < enemy_row:
if my_col < enemy_col:
for n in range(my_row,enemy_row+1):
for i in range(my_col,enemy_col+1):
right2_area.append(maze[n][i])
elif my_col > enemy_col:
for n in range(my_row,enemy_row+1):
for i in range(enemy_col,my_col+1):
right2_area.append(maze[n][i])
elif my_row > enemy_row:
if my_col < enemy_col:
for n in range(enemy_row,my_row+1):
for i in range(my_col,enemy_col+1):
right2_area.append(maze[n][i])
elif my_col > enemy_col:
for n in range(enemy_row,my_row+1):
for i in range(enemy_col,my_col+1):
right2_area.append(maze[n][i])
if "yellow0_gem" in right2_area:
yellow0_score = yellow0_score * multi
if "yellow1_gem" in right2_area:
yellow1_score = yellow1_score * multi
if "yellow2_gem" in right2_area:
yellow2_score = yellow2_score * multi
if "yellow3_gem" in right2_area:
yellow3_score = yellow3_score * multi
if "red0_gem" in right2_area:
red0_score = red0_score * multi
if "red1_gem" in right2_area:
red1_score = red1_score * multi
right3_area = []
for (row,col) in midfield:
right3_area.append(maze[row][col])
if "yellow0_gem" in right3_area:
yellow0_score = yellow0_score * midfield_addition
if "yellow1_gem" in right3_area:
yellow1_score = yellow1_score * midfield_addition
if "yellow2_gem" in right3_area:
yellow2_score = yellow2_score * midfield_addition
if "yellow3_gem" in right3_area:
yellow3_score = yellow3_score * midfield_addition
if "red0_gem" in right3_area:
red0_score = red0_score * midfield_addition
if "red1_gem" in right3_area:
red1_score = red1_score * midfield_addition
right_lis = [yellow0_score,yellow1_score,yellow2_score,yellow3_score,red0_score,red1_score]
best = max(right_lis)
right_lis.remove(best)
second_best = max(right_lis)
right_score = best + second_best
if context.round != 0:
if history[len(history)-1] == "L":
right_score = right_score * 0.95
print(best,second_best)
print("综合权重:", right_score)
if should_leave():
print("第", context.round, "回合,我要离开了")
e_row = api.get.exit("row")
e_col = api.get.exit("col")
return api.check.next((e_row, e_col))
else:
all_scores = [up_score,down_score,left_score,right_score]
max_score = max(all_scores)
if up_score == max_score:
print("第", context.round, "回合,我要向上走","\n")
history.append("U")
return "U"
elif down_score == max_score:
print("第", context.round, "回合,我要向下走","\n")
history.append("D")
return "D"
elif left_score == max_score:
print("第", context.round, "回合,我要向左走","\n")
history.append("L")
return "L"
elif right_score == max_score:
print("第", context.round, "回合,我要向右走","\n")
history.append("R")
return "R"
稍微有点长,希望不介意。。。
本策略须搭配其他策略相互进行,不可单独使用。
还是那句话:如果想借鉴使用,麻烦先点个赞再借鉴。如果因此导致失败,本人概不负责。
作者是个小学生,所以帖子里常常有不完善的地方。
如果有什么待改进的地方请在评论区提出建议,我会感激不尽。
好了,今天的帖子到此结束!!!