码迷,mamicode.com
首页 > 其他好文 > 详细

日狗2048

时间:2017-02-17 00:20:05      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:ges   get   target   click   ted   while   for   .text   bsp   

安利一波链接:Kevin‘s 2048

mode 1为普通2048

mode 2为随机出现值2048(可能开局2048哦)

mode 3为神秘2048(有一种元素为1)

mode 4为负数2048

下面是蜜汁贪心刷分脚本。。

  1 import copy,os,random,time
  2 from collections import OrderedDict
  3 from selenium import webdriver
  4 from selenium.webdriver.common.keys import Keys
  5 
  6 def turn(n):
  7     if   n==1:
  8         return Keys.DOWN
  9     elif n==2:
 10         return Keys.LEFT
 11     elif n==3:
 12         return Keys.RIGHT
 13     else:
 14         return Keys.UP
 15 
 16 if __name__=="__main__":
 17     username=raw_input("Please input your name:")
 18     maxium,total,times=0,0.0,0
 19     while True:
 20         try:
 21             mode=input("Please input the mode of 2048:")
 22             break
 23         except:
 24             print "Unexpected format!"
 25     while True:
 26         cyz666=webdriver.Chrome()
 27         cyz666.get("http://kkeevviinnn.com/2048.html")
 28         while True:
 29             try:
 30                 cyz666.find_element_by_id("modebutton%d"%mode).click()
 31                 game=cyz666.find_element_by_xpath("/html/body")
 32                 break
 33             except:
 34                 print "There‘s no this mode!"
 35                 mode=input("Please input the mode of 2048:")
 36         while True:
 37             status=[[0 for i in xrange(4)] for j in xrange(4)]
 38             for i in xrange(4):
 39                 for j in xrange(4):
 40                     try:
 41                         status[i][j]=int(game.find_element_by_id("number-cell-%d-%d"%(i,j)).text)
 42                     except:
 43                         pass
 44             sum={0:0,1:0,2:0,3:0}
 45 
 46             # Move Up
 47             images,vis=copy.deepcopy(status),[[False for i in xrange(4)] for j in xrange(4)]
 48             for i in xrange(4):
 49                 for j in xrange(4):
 50                     if images[i][j]:
 51                         k=i-1
 52                         while (k>=0) and not images[k][j] and not vis[k][j]:
 53                             k-=1
 54                         if (k>=0) and ((images[i][j]==images[k][j]) or (images[i][j]==1)) and not vis[k][j]:
 55                             images[k][j]+=images[i][j]
 56                             images[i][j],vis[k][j]=0,True
 57                             sum[0]+=images[k][j]
 58             # Move Down
 59             images,vis=copy.deepcopy(status),[[False for i in xrange(4)] for j in xrange(4)]
 60             for i in xrange(4):
 61                 for j in xrange(4):
 62                     if images[i][j]:
 63                         k=i+1
 64                         while (k<4) and not images[k][j] and not vis[k][j]:
 65                             k+=1
 66                         if (k<4) and ((images[i][j]==images[k][j]) or (images[i][j]==1)) and not vis[k][j]:
 67                             images[k][j]+=images[i][j]
 68                             images[i][j],vis[k][j]=0,True
 69                             sum[1]+=images[k][j]
 70             # Move Left
 71             images,vis=copy.deepcopy(status),[[False for i in xrange(4)] for j in xrange(4)]
 72             for i in xrange(4):
 73                 for j in xrange(4):
 74                     if images[i][j]:
 75                         k=j-1
 76                         while (k>=0) and not images[i][k] and not vis[i][k]:
 77                             k-=1
 78                         if (k>=0) and ((images[i][j]==images[i][k]) or (images[i][j]==1)) and not vis[i][k]:
 79                             images[i][k]+=images[i][j]
 80                             images[i][j],vis[i][k]=0,True
 81                             sum[2]+=images[i][k]
 82             # Move Right
 83             images,vis=copy.deepcopy(status),[[False for i in xrange(4)] for j in xrange(4)]
 84             for i in xrange(4):
 85                 for j in xrange(4):
 86                     if images[i][j]:
 87                         k=j+1
 88                         while (k<4) and not images[i][k] and not vis[i][k]:
 89                             k+=1
 90                         if (k<4) and ((images[i][j]==images[i][k]) or (images[i][j]==1)) and not vis[i][k]:
 91                             images[i][k]+=images[i][j]
 92                             images[i][j],vis[i][k]=0,True
 93                             sum[3]+=images[i][k]
 94 
 95             dic=OrderedDict(sorted(sum.items(),key=lambda t:t[1],reverse=True))
 96             last,can=-1,[False,False,False,False]
 97             for odr,scr in dic.items():
 98                 if (~last) and (scr!=last):
 99                     break
100                 can[odr]=True
101                 last=scr
102             ans=random.randint(0,3)
103             while not can[ans]:
104                 ans=random.randint(0,3)
105             while True:
106                 try:
107                     game.send_keys(turn(ans))
108                     break
109                 except:
110                     pass
111             try:
112                 cyz666.find_element_by_id("field").send_keys(username)
113                 break
114             except:
115                 pass
116         scr=int(cyz666.find_element_by_id("score").text)
117         maxium=max(maxium,scr)
118         total+=scr
119         times+=1
120         print "------------------------------------"
121         print "You‘ve played 2048 for %d times."%times
122         print "This run you got the score:%d."%scr
123         print "Your average score:%.3f."%(total/times)
124         print "Your maxium score:%d."%maxium
125         print "------------------------------------"
126         print ‘‘
127         while True:
128             try:
129                 cyz666.find_element_by_id("btn1").click()
130                 break
131             except:
132                 pass
133         cyz666.close()

 

日狗2048

标签:ges   get   target   click   ted   while   for   .text   bsp   

原文地址:http://www.cnblogs.com/spactim/p/6407676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!