鱼C论坛

 找回密码
 立即注册
查看: 1542|回复: 1

[技术交流] 贪吃蛇最终型AI,填满格子

[复制链接]
发表于 2018-3-15 11:21:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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 前面加#号,这样就只会看到每次吃完
食物的时刻。结尾有瑕疵,会出现最后一个食物没吃,停在那里,但是还没找出原因。也有少数情况,
停在最后几个格子不动,进入死循环。待处理
以下~:
  1. import random
  2. import time

  3. class snake():
  4.    
  5.     def __init__(self,ite):
  6.         self.ite = ite
  7.         self.snk = ['o','o','O']
  8.         self.way = [[5,4],[5,5],[5,6]]
  9.         self.tail = [0,0]
  10.         self.ite[5][4],self.ite[5][5],self.ite[5][6]='o','o','O'
  11.             
  12.     def move(self,a,b):
  13.         if a == self.way[-1][0] and b == self.way[-1][1]:
  14.             return
  15.         self.way.append([a,b])
  16.         self.tail = self.way.pop(0)
  17.         self.ite[self.tail[0]][self.tail[1]] = ' '
  18.         for c,(i,j) in enumerate(self.way):
  19.             self.ite[i][j] = self.snk[c]
  20.         return self.ite

  21. class food():

  22.     def __init__(self,ite):
  23.         self.ite = ite

  24.     def prod(self,way):
  25.         sign = 0
  26.         for i in range(10):
  27.             for j in range(10):
  28.                 if [i,j] not in way:
  29.                     sign = 1
  30.                     break
  31.         if not sign:
  32.             return 1
  33.         while True:
  34.             a,b = random.randint(0,9),random.randint(0,9)
  35.             if [a,b] not in way:
  36.                 self.ite[a][b] = 'Q'
  37.                 break
  38.         return [a,b]
  39.    
  40. def show(ite):
  41.     #print(' ','--'*10)
  42.     for i in ite:
  43.         print('|',end='')
  44.         for j in i:
  45.             print(j,end=' ')
  46.         print('|')
  47.     #print(' ','--'*10)
  48.     print()   

  49. def find(c,d):
  50.     global visit
  51.     way_x = tuple(S.way)
  52.     tail_x = tuple(S.tail)
  53.     f = []
  54.     f_long = []
  55.     f_any = []
  56.     [[a,b],[sx,sy]] = visit
  57.     if a == c and b == d:
  58.         right[(a,b)] = [sx,sy]
  59.         return 1
  60.     for ste in step:
  61.         a1 = a + ste[0]
  62.         b1 = b + ste[1]
  63.         if 0<=a1<10 and 0<=b1<10 and [a1,b1] not in S.way[1:]:
  64.             f_any.append([[a1,b1],ste])
  65.             count = 0
  66.             way_x = tuple(S.way)
  67.             tail_x = tuple(S.tail)
  68.             snk_x = tuple(S.snk)
  69.             cot_food,cot_tail = -1,-1
  70.             cot_food = compare(a1,b1,c,d,count)
  71.             if cot_food>=0:
  72.                 a_t,b_t = S.tail[0],S.tail[1]
  73.                 cot_tail = tail_fl(c,d,-1,-1,a_t,b_t,count)
  74.             S.way = list(way_x)
  75.             S.tail = list(tail_x)
  76.             S.snk = list(snk_x)
  77.             recover(c,d)
  78.             if cot_food>=0 and cot_tail>=0:
  79.                 f.append([cot_food,[a1,b1],ste])

  80.     if len(f):
  81.         visit = [min(f)[1],min(f)[2]]
  82.     else:
  83.         for ste in step:
  84.             a1 = a + ste[0]
  85.             b1 = b + ste[1]
  86.             if 0<=a1<10 and 0<=b1<10 and [a1,b1] not in S.way[1:]:
  87.                 a_t,b_t = S.way[0][0],S.way[0][1]
  88.                 way_x = tuple(S.way)
  89.                 tail_x = tuple(S.tail)
  90.                 snk_x = tuple(S.snk)
  91.                 cot_tail = tail_fl(a1,b1,c,d,a_t,b_t,count)

  92.                 if cot_tail >=0:
  93.                     f_long.append([cot_tail,[a1,b1],ste])
  94.                 S.way = list(way_x)
  95.                 S.tail = list(tail_x)
  96.                 S.snk = list(snk_x)
  97.                 recover(c,d)
  98.         if len(f_long):
  99.             visit = [max(f_long)[1],max(f_long)[2]]
  100.         else:
  101.             visit = random.choice(f_any)        
  102.     temp=visit[0]
  103.     S.move(temp[0],temp[1])

  104.     if temp[0] == c and temp[1] == d:
  105.         S.snk.insert(0,'o')
  106.         S.way.insert(0,S.tail)
  107.         ite[S.tail[0]][S.tail[1]] = 'o'
  108.         return 0
  109.     show(ite)
  110.     time.sleep(0.5)

  111. def recover(c,d):
  112.     for i in range(10):
  113.         for j in range(10):
  114.             ite[i][j] = ' '
  115.     ite[c][d] = 'Q'
  116.             
  117. def tail_fl(a,b,c,d,a_t,b_t,count):
  118.     S.move(a,b)
  119.     if a == c and b == d:
  120.         S.snk.insert(0,'o')
  121.         S.way.insert(0,S.tail)
  122.         ite[S.tail[0]][S.tail[1]] = 'o'
  123.     if a == a_t and b == b_t:
  124.         return count
  125.     f = []
  126.     for ste in step:
  127.         a1 = a + ste[0]
  128.         b1 = b + ste[1]
  129.         if 0<=a1<10 and 0<=b1<10 and [a1,b1] not in S.way[1:]:
  130.             cot = abs(a1-a_t)+abs(b1-b_t)
  131.             f.append([cot,[a1,b1]])
  132.     if not len(f):
  133.         return -1
  134.     return tail_fl(min(f)[1][0],min(f)[1][1],c,d,a_t,b_t,count+1)   
  135.    
  136. def compare(a,b,c,d,count):
  137.    
  138.     S.move(a,b)
  139.     if a == c and b == d:
  140.         S.snk.insert(0,'o')
  141.         S.way.insert(0,S.tail)
  142.         ite[S.tail[0]][S.tail[1]] = 'o'
  143.         return count
  144.     f = []
  145.     for ste in step:
  146.         a1,b1 = a+ste[0],b+ste[1]
  147.         if 0<=a1<10 and 0<=b1<10 and [a1,b1] not in S.way[1:]:
  148.             cot  = abs(a1-c)+abs(b1-d)
  149.             f.append([cot,[a1,b1]])
  150.     if not len(f):
  151.         return -1
  152.     [a,b] = min(f)[1]
  153.     return compare(a,b,c,d,count+1)
  154.                
  155. ite = [[' ']*10 for i in range(10)]
  156. S = snake(ite)
  157. F = food(ite)
  158. step = [[1,0],[-1,0],[0,1],[0,-1]]
  159. a,b = F.prod(S.way)
  160. visit = [[5,6],[0,1]]
  161. right = {(5,6):[0,1]}

  162. while True:
  163.     if [a,b] == S.way[-1]:
  164.         pro = F.prod(S.way)
  165.         if pro != 1:
  166.             [a,b] = pro
  167.         else:
  168.             show(ite)
  169.             break
  170.     show(ite)
  171.     visit = [S.way[-1],right[tuple(S.way[-1])]]
  172.     while True:
  173.         if find(a,b):
  174.             break
  175.     #time.sleep(0.5)
复制代码

评分

参与人数 1鱼币 +2 收起 理由
膜法记者 + 2 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-3-15 12:13:44 | 显示全部楼层
本帖最后由 JAY饭 于 2018-3-15 13:28 编辑

已更新,没有问题了代码写的乱真痛苦,出了错都不知道哪儿错了,引以为戒
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 19:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表