|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 JAY饭 于 2018-3-15 12:31 编辑
历时几天终于写出了完全填满格子的贪吃蛇AI, 因为是用 IDLE写的,没有导入任何模块,所以复制就可以用
但是,也是因为这样,界面及其简陋,打印起来速度也比较慢,蛇吃完整个格子要走很久,要跑两三万行!!!!
!!!!思路都是借鉴网络上的大神,没有他们的分析,我是怎么也写不出来的。
代码缺点很明显,写的乱七八糟,没有细致化和模块化,没办法,我这几天完全沉醉其中,没有心思去细致
化,所以肯定也没人会去研究,所以我说下网络大神的逻辑思路,这个是最最重要的!!!!
虚拟一条蛇去吃:
if 蛇能吃到食物 and 吃完食物能找到蛇尾:
return 真蛇去吃
elif 蛇能吃到食物 and 吃完食物不能找到蛇尾:
return 真蛇不去吃,但是跟着当前蛇尾走
elif 蛇不能吃到食物,蛇能找到当前自己的蛇尾(有路径可到达):
return 跟着蛇尾走
else: 吃不到食物,也找不到当前路径:
return 随意走一步,(没病走两步 )
如果嫌跑的时间太长,请将109行的 show(ite) 和110 行的time.sleep 前面加#号,这样就只会看到每次吃完
食物的时刻。结尾有瑕疵,会出现最后一个食物没吃,停在那里,但是还没找出原因。也有少数情况,
停在最后几个格子不动,进入死循环。待处理
以下~:
- import random
- import time
- class snake():
-
- def __init__(self,ite):
- self.ite = ite
- self.snk = ['o','o','O']
- self.way = [[5,4],[5,5],[5,6]]
- self.tail = [0,0]
- self.ite[5][4],self.ite[5][5],self.ite[5][6]='o','o','O'
-
- def move(self,a,b):
- if a == self.way[-1][0] and b == self.way[-1][1]:
- return
- self.way.append([a,b])
- self.tail = self.way.pop(0)
- self.ite[self.tail[0]][self.tail[1]] = ' '
- for c,(i,j) in enumerate(self.way):
- self.ite[i][j] = self.snk[c]
- return self.ite
- class food():
- def __init__(self,ite):
- self.ite = ite
- def prod(self,way):
- sign = 0
- for i in range(10):
- for j in range(10):
- if [i,j] not in way:
- sign = 1
- break
- if not sign:
- return 1
- while True:
- a,b = random.randint(0,9),random.randint(0,9)
- if [a,b] not in way:
- self.ite[a][b] = 'Q'
- break
- return [a,b]
-
- def show(ite):
- #print(' ','--'*10)
- for i in ite:
- print('|',end='')
- for j in i:
- print(j,end=' ')
- print('|')
- #print(' ','--'*10)
- print()
- def find(c,d):
- global visit
- way_x = tuple(S.way)
- tail_x = tuple(S.tail)
- f = []
- f_long = []
- f_any = []
- [[a,b],[sx,sy]] = visit
- if a == c and b == d:
- right[(a,b)] = [sx,sy]
- return 1
- for ste in step:
- a1 = a + ste[0]
- b1 = b + ste[1]
- if 0<=a1<10 and 0<=b1<10 and [a1,b1] not in S.way[1:]:
- f_any.append([[a1,b1],ste])
- count = 0
- way_x = tuple(S.way)
- tail_x = tuple(S.tail)
- snk_x = tuple(S.snk)
- cot_food,cot_tail = -1,-1
- cot_food = compare(a1,b1,c,d,count)
- if cot_food>=0:
- a_t,b_t = S.tail[0],S.tail[1]
- cot_tail = tail_fl(c,d,-1,-1,a_t,b_t,count)
- S.way = list(way_x)
- S.tail = list(tail_x)
- S.snk = list(snk_x)
- recover(c,d)
- if cot_food>=0 and cot_tail>=0:
- f.append([cot_food,[a1,b1],ste])
- if len(f):
- visit = [min(f)[1],min(f)[2]]
- else:
- for ste in step:
- a1 = a + ste[0]
- b1 = b + ste[1]
- if 0<=a1<10 and 0<=b1<10 and [a1,b1] not in S.way[1:]:
- a_t,b_t = S.way[0][0],S.way[0][1]
- way_x = tuple(S.way)
- tail_x = tuple(S.tail)
- snk_x = tuple(S.snk)
- cot_tail = tail_fl(a1,b1,c,d,a_t,b_t,count)
- if cot_tail >=0:
- f_long.append([cot_tail,[a1,b1],ste])
- S.way = list(way_x)
- S.tail = list(tail_x)
- S.snk = list(snk_x)
- recover(c,d)
- if len(f_long):
- visit = [max(f_long)[1],max(f_long)[2]]
- else:
- visit = random.choice(f_any)
- temp=visit[0]
- S.move(temp[0],temp[1])
- if temp[0] == c and temp[1] == d:
- S.snk.insert(0,'o')
- S.way.insert(0,S.tail)
- ite[S.tail[0]][S.tail[1]] = 'o'
- return 0
- show(ite)
- time.sleep(0.5)
- def recover(c,d):
- for i in range(10):
- for j in range(10):
- ite[i][j] = ' '
- ite[c][d] = 'Q'
-
- def tail_fl(a,b,c,d,a_t,b_t,count):
- S.move(a,b)
- if a == c and b == d:
- S.snk.insert(0,'o')
- S.way.insert(0,S.tail)
- ite[S.tail[0]][S.tail[1]] = 'o'
- if a == a_t and b == b_t:
- return count
- f = []
- for ste in step:
- a1 = a + ste[0]
- b1 = b + ste[1]
- if 0<=a1<10 and 0<=b1<10 and [a1,b1] not in S.way[1:]:
- cot = abs(a1-a_t)+abs(b1-b_t)
- f.append([cot,[a1,b1]])
- if not len(f):
- return -1
- return tail_fl(min(f)[1][0],min(f)[1][1],c,d,a_t,b_t,count+1)
-
- def compare(a,b,c,d,count):
-
- S.move(a,b)
- if a == c and b == d:
- S.snk.insert(0,'o')
- S.way.insert(0,S.tail)
- ite[S.tail[0]][S.tail[1]] = 'o'
- return count
- f = []
- for ste in step:
- a1,b1 = a+ste[0],b+ste[1]
- if 0<=a1<10 and 0<=b1<10 and [a1,b1] not in S.way[1:]:
- cot = abs(a1-c)+abs(b1-d)
- f.append([cot,[a1,b1]])
- if not len(f):
- return -1
- [a,b] = min(f)[1]
- return compare(a,b,c,d,count+1)
-
- ite = [[' ']*10 for i in range(10)]
- S = snake(ite)
- F = food(ite)
- step = [[1,0],[-1,0],[0,1],[0,-1]]
- a,b = F.prod(S.way)
- visit = [[5,6],[0,1]]
- right = {(5,6):[0,1]}
- while True:
- if [a,b] == S.way[-1]:
- pro = F.prod(S.way)
- if pro != 1:
- [a,b] = pro
- else:
- show(ite)
- break
- show(ite)
- visit = [S.way[-1],right[tuple(S.way[-1])]]
- while True:
- if find(a,b):
- break
- #time.sleep(0.5)
复制代码
|
评分
-
查看全部评分
|