马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
上一节已经做出了个可以自动找食物的贪食蛇,但是这条“笨蛇”既贪心又不够智能,所以往往自己把自己“嫩”死。
后面两节就要让这条蛇聪明一点,并且不要这么“贪心”,俗话讲“心急吃不了热豆腐”嘛。
由于贪食蛇的最优路径决策是依据board的最优路径决策表来选择的,所以记录每一步的最优路径决策表就至关重要了。
在上次的代码上加上以下代码,记录每一步的决策表。
f = open('snake.log','w')
init()
board_reset()
show()
board_refresh()
while not isdead():
time.sleep(0.3)
choose_shortest()
move()
eatfood()
if len(food) == 0:
putfood()
board_reset()
show()
board_refresh()
for i in range(len(board)):
f.write(str(board[i])+'\n')
f.write('='*50 + '\n')
f.close()
print('Game Over!')
这样,我们就能翻看每次贪食蛇是怎么死的了。
来看其中某一次的决策表:
==================================================
[9, 10, 9, 8, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 18]
[10, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[9, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[9, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[8, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[-1, -1, -1, 6, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[0, 0, -1, -1, -1, -1, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 19]
==================================================
[9, 10, 9, 8, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 18]
[10, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[9, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[9, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[8, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[-1, -1, -1, 6, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[-1, 0, -1, -1, -1, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 19]
==================================================
[9, 10, 9, 8, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 18]
[10, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[9, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
[8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[9, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
[8, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[-1, -1, -1, 6, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[-1, -1, -1, -1, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 19]
==================================================
其中-1代表蛇身,0代表此处空格与食物之间没有路径,除0和-1以外的数字代表每个空格到食物的最短路径。
但是由于0比其他数字要小,当蛇头附近有0存在时,笨蛇就会走进死胡同而自杀了。
知道了,问题点就很好办了,把0排除在最优选择范围以外。
修改半成品代码:
修改完的贪食蛇已经不会自杀了,看如下的决策表。
==================================================
[9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 26]
[10, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]
[9, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
[8, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
[7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
[6, 5, 6, 7, 8, 9, 10, 11, 12, 13, -1, -1, -1, -1, 20, 21, 22, 23, 24, 25]
[5, 4, 5, 6, 7, 8, 9, 10, 11, 12, -1, -1, -1, -1, 21, 22, 23, 24, 25, 26]
[4, 3, 4, 5, 6, 7, 8, 9, 10, -1, -1, -1, 24, 23, 22, 23, 24, 25, 26, 27]
[3, 2, -1, 6, 7, 8, 9, 10, 11, 10, -1, -1, 25, 24, 23, 24, 25, 26, 27, 28]
[2, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, 25, 26, 25, 24, 25, 26, 27, 28, 27]
==================================================
[9, 10, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 24]
[10, 9, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
[9, 8, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
[8, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
[7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
[6, 5, 4, 5, 6, 7, 8, 9, 10, 11, -1, -1, -1, -1, 18, 19, 20, 21, 22, 23]
[5, 4, 3, 4, 5, 6, 7, 8, 9, 10, -1, -1, -1, -1, 19, 20, 21, 22, 23, 24]
[4, 3, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, 22, 21, 20, 21, 22, 23, 24, 25]
[-1, -1, -1, 4, 5, 6, 7, 8, 9, 8, -1, -1, 23, 22, 21, 22, 23, 24, 25, 26]
[0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, 23, 24, 23, 22, 23, 24, 25, 26, 25]
==================================================
[9, 10, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 24]
[10, 9, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
[9, 8, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
[8, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
[7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
[6, 5, 4, 5, 6, 7, 8, 9, 10, 11, -1, -1, -1, -1, 18, 19, 20, 21, 22, 23]
[5, 4, 3, 4, 5, 6, 7, 8, 9, 10, -1, -1, -1, -1, 19, 20, 21, 22, 23, 24]
[-1, 3, 2, 3, 4, 5, 6, 7, 8, 9, 8, -1, 22, 21, 20, 21, 22, 23, 24, 25]
[-1, -1, -1, 4, 5, 6, 7, 8, 9, 8, -1, -1, 23, 22, 21, 22, 23, 24, 25, 26]
[0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, 23, 24, 23, 22, 23, 24, 25, 26, 25]
==================================================
[9, 10, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 24]
[10, 9, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
[9, 8, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]
[8, 7, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
[7, 6, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
[6, 5, 4, 5, 6, 7, 8, 9, 10, 11, -1, -1, -1, -1, 18, 19, 20, 21, 22, 23]
[5, 4, 3, 4, 5, 6, 7, 8, 9, 10, 9, -1, -1, -1, 19, 20, 21, 22, 23, 24]
[-1, -1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1, 22, 21, 20, 21, 22, 23, 24, 25]
[-1, -1, -1, 4, 5, 6, 7, 8, 9, 8, -1, -1, 23, 22, 21, 22, 23, 24, 25, 26]
[0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, 23, 24, 23, 22, 23, 24, 25, 26, 25]
==================================================
下面再来看一种更极端的情况,蛇头与食物之间没有路径,如下决策表:
==================================================
[0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, 11, 12, 11]
[0, 0, 0, 0, 0, -1, 13, 14, 13, 12, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12]
[0, 0, 0, 0, 0, -1, 14, 13, 12, 11, 10, 9, 8, -1, 6, 7, 8, 9, 10, 11]
[0, 0, 0, 0, 0, -1, 13, 12, 11, 10, 9, 8, 7, -1, 5, 6, 7, 8, 9, 10]
[0, 0, 0, 0, 0, -1, 12, 11, 10, 9, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9]
[0, 0, 0, 0, 0, -1, 11, 10, 9, 8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8]
[0, 0, 0, 0, 0, -1, 10, 9, 8, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
[0, 0, 0, 0, 0, -1, 11, 10, 9, 8, 7, 6, 5, -1, 3, 4, 5, 6, 7, 8]
[0, 0, 0, 0, 0, -1, 10, 11, 10, 9, 8, 7, 6, -1, 4, 5, 6, 7, 8, 9]
[0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, 5, 6, 7, 8, 9, 8]
==================================================
[0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, 11, 12, 11]
[0, 0, 0, 0, 0, -1, 13, 14, 13, 12, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12]
[0, 0, 0, 0, 0, -1, 14, 13, 12, 11, 10, 9, 8, -1, 6, 7, 8, 9, 10, 11]
[0, 0, 0, 0, 0, -1, 13, 12, 11, 10, 9, 8, 7, 6, 5, 6, 7, 8, 9, 10]
[0, 0, 0, 0, 0, -1, 12, 11, 10, 9, 8, 7, 6, 5, 4, 5, 6, 7, 8, 9]
[0, 0, 0, 0, 0, -1, 11, 10, 9, 8, 7, 6, 5, 4, 3, 4, 5, 6, 7, 8]
[0, 0, 0, 0, 0, -1, 12, 11, 10, 9, 8, 7, 6, -1, 2, 3, 4, 5, 6, 7]
[0, 0, 0, 0, 0, -1, 13, 12, 11, 10, 9, 8, 7, -1, 3, 4, 5, 6, 7, 8]
[0, 0, 0, 0, 0, -1, 12, 13, 12, 11, 10, 9, 8, -1, 4, 5, 6, 7, 8, 9]
[0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, 5, 6, 7, 8, 9, 8]
==================================================
[8, 9, 8, 9, 8, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, 0, 0, 0]
[9, 8, 7, 8, 9, -1, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0]
[8, 7, 6, 7, 8, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[7, 6, 5, 6, 7, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[6, 5, 4, 5, 6, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 4, 3, 4, 5, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[4, 3, 2, 3, 4, -1, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0]
[5, 4, 3, 4, 5, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0]
[6, 5, 4, 5, 6, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0]
[5, 6, 5, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0]
==================================================
[8, 9, 8, 9, 8, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, 0, 0, 0]
[9, 8, 7, 8, 9, -1, 0, 0, 0, 0, -1, -1, -1, 0, -1, -1, -1, 0, 0, 0]
[8, 7, 6, 7, 8, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[7, 6, 5, 6, 7, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[6, 5, 4, 5, 6, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 4, 3, 4, 5, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[4, 3, 2, 3, 4, -1, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0]
[5, 4, 3, 4, 5, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0]
[6, 5, 4, 5, 6, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0]
[5, 6, 5, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0]
==================================================
[8, 9, 8, 9, 8, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, 0, 0, 0]
[9, 8, 7, 8, 9, -1, 0, 0, 0, 0, -1, -1, -1, 0, 0, -1, -1, 0, 0, 0]
[8, 7, 6, 7, 8, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[7, 6, 5, 6, 7, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[6, 5, 4, 5, 6, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[5, 4, 3, 4, 5, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[4, 3, 2, 3, 4, -1, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, 0, 0]
[5, 4, 3, 4, 5, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0]
[6, 5, 4, 5, 6, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0]
[5, 6, 5, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0]
==================================================
[8, 9, 8, 9, 8, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, 0, 0, 0]
[9, 8, 7, 8, 9, -1, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0, -1, 0, 0, 0]
[8, 7, 6, 7, 8, -1, 0, 0, 0, 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6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0]
==================================================
可以看到蛇身把地图分成了两半,当食物出现在蛇身的另一边时,贪食蛇已经找不到方向了。
当遇到这种情况下,我们就需要引入一个新的决策机制,这个我们在下一节中讨论,敬请期待。 |