android实现数独游戏机器人

本文实例为大家分享了android实现数独游戏机器人的具体代码,供大家参考,具体内容如下
【android实现数独游戏机器人】针对该应用的数独机器人android实现数独游戏机器人
文章图片
,填答案逻辑未完成,主要原因在于游戏响应触屏事件参数有待调整,简单的使用input tap,界面响应不正常。
android实现数独游戏机器人
文章图片

import sys posinfo0={(0,0):8,(2,1):3,(3,1):6,(1,2):7,(4,2):9,(6,2):2,(1,3):5,(5,3):7,(4,4):4,(5,4):5,(6,4):7,(3,5):1,(7,5):3,(2,6):1,(7,6):6,(8,6):8,(2,7):8,(3,7):5,(7,7):1,(1,8):9,(6,8):4,}maskmap={1535553906:5,2441400:7,305175776:1,2746585781:6,1577050781:9,2756722681:8,1586816406:9,2756351406:6,2993066431:2,2758694531:3,1535569531:5,2756738306:8,1528222656:9,1525882656:6,2942285181:2,1535944531:5,1232906556:4,2709863281:3,1525882031:6,2707910181:8,1535960156:5,1477441406:9,2944238306:2,1525878901:1,2707910156:3,1477050781:6,2758691406:3,3002832056:2,2705941436:8,1242672181:4,1535647656:5,2705957686:8,1479394531:9,1487191406:3,2707894556:8,502832031:2,2705941431:8,}def ocr(img):global posinfo0, maskmapposinfo0={}for x in xrange(0,9):for y in xrange(0,9):x0=(x/3)*(257-26)+(x%3)*(96-26)+26y0=(y/3)*(432-179)+(y%3)*(255-179)+179x1=x0+(88-26)y1=y0+(240-179)dig=img.crop((x0,y0,x1,y1))throd=100res=0for marg_left in xrange(0,dig.size[0]/2+2):for t in xrange(0,dig.size[1]):if dig.getpixel((marg_left, t))[0]=marg_right:#print "(%d,%d):%d"%(x,y,res)continue mask=0bitcnt=1dig=dig.crop((marg_left,marg_top,marg_right,marg_bottom))olddirect=0direct=0for digy in xrange(dig.size[1]/8,dig.size[1], dig.size[1]/8):cnt=0for digx in xrange(0,dig.size[0]-1):if dig.getpixel((digx, digy))[0]>throd and dig.getpixel((digx+1, digy))[0]<=throd:cnt+=1mask+=cnt*bitcntbitcnt*=5for digx in xrange(dig.size[0]/6,dig.size[0], dig.size[0]/6):cnt=0for digy in xrange(0,dig.size[1]-1):if dig.getpixel((digx, digy))[0]>throd and dig.getpixel((digx, digy+1))[0]<=throd:cnt+=1mask+=cnt*bitcntbitcnt*=5if mask not in maskmap:dig.save("tmp/%d_%d_%d.png"%(mask,x,y))val=raw_input("%d:"%mask)maskmap[mask]=int(val)if mask in maskmap:#print "(%d,%d):%d"%(x,y,maskmap[mask])posinfo0[(x,y)]=maskmap[mask]#484 677def scan():os.system("adb shell screencap /mnt/sdcard/shudu.png")os.system("adb pull /mnt/sdcard/shudu.png")img=Image.open("shudu.png")ocr(img) from copy import deepcopyfrom time import clock posb0={} def init(posb, posinfo):for i in xrange(0,9):for j in xrange(0,9):if (i,j) not in posinfo:posb[(i,j)]=[n for n in xrange(1,10)]for pos in posinfo:cleanother(0,pos[0],pos[1],posinfo[pos],posb,posinfo) def checkpos(posb, posinfo): posb1=posb.copy()posinfo1=posinfo.copy()ret=0for pos in posb:if len(posb1[(pos[0],pos[1])])==1:addinfo(pos[0],pos[1],posb1[(pos[0],pos[1])][0],posb1,posinfo1)ret=1breakelif len(posb1[(pos[0],pos[1])])==0:ret=2breakposb=posb1posinfo=posinfo1return ret def cleanother(mod,x,y,v,posb,posinfo):posb1=posb.copy()x0=x/3*3y0=y/3*3for ii in xrange(0,3):for jj in xrange(0,3):if (x0+ii,y0+jj) in posb: if v in posb1[(x0+ii,y0+jj)]:posb1[(x0+ii,y0+jj)].remove(v)for ii in xrange(0,9):if (ii,y) in posb: if v in posb1[(ii,y)]:posb1[(ii,y)].remove(v)for jj in xrange(0,9):if (x,jj) in posb: if v in posb1[(x,jj)]:posb1[(x,jj)].remove(v)ret=0if posb1!=posb:ret = checkpos(posb1,posinfo)posb=posb1return ret def findmin(posb):minv=9minpos=(0,0)for key,val in posb.items():if minv>len(val):minv=len(val)minpos=keyreturn (minv,minpos) def outputInfo(posinfo):print "\n%s"%" ".join(str(v) for v in xrange(0,9))print "-----------"*2for i in xrange(0,9):print "%d|"%i,for j in xrange(0,9):if (j,i) in posinfo: print posinfo[(j,i)],else:print " ",printprint "==========="*2def addinfo(x,y,v,posb,posinfo):if (x,y) in posinfo:return #print "addinfo",x,y,vif (x,y) in posb: del posb[(x,y)]posinfo[(x,y)]=vclearallinfo(posb,posinfo)#outputInfo(posinfo)def clearallinfo(newposb, newposinfo):newposinfo2=newposinfo.copy()for pos in newposinfo:cleanother(0,pos[0],pos[1],newposinfo[pos],newposb,newposinfo2)if newposinfo2==newposinfo: returnnewposinfo=newposinfo2#print "newposinfo"#outputInfo(newposinfo)clearallinfo(newposb, newposinfo)def clickscreen(pos0, lastpos):for i in xrange(0,9):print "%d|"%i,for j in xrange(0,9):if (j,i) in lastpos and (j,i) not in pos0:print lastpos[(j,i)], x0=(j/3)*(257-26)+(j%3)*(96-26)+26y0=(i/3)*(432-179)+(i%3)*(255-179)+179x1=x0+(88-26)/2y1=y0+(240-179)/2#os.system("adb shell input tap %d %d"%(x1,y1))#raw_input("press...")#time.sleep(0.1)#os.system("adb shell input tap %d %d"%(lastpos[(j,i)]*80-40,980)) #time.sleep(0.1)#raw_input("press...")else:print " ",printdef tryonestep(minpos, posb, posinfo, layer):global ticfor posbv in posb[minpos]:newposb=deepcopy(posb)newposinfo=posinfo.copy()addinfo(minpos[0],minpos[1],posbv,newposb,newposinfo)minv2,minpos2=findmin(newposb)if minv2==0:#print "impossible!!!", layercontinueif len(newposb)==0:print "Got it!!!"outputInfo(newposinfo)clickscreen(posinfo0, newposinfo)exit(0)tryonestep(minpos2, newposb, newposinfo, layer+1)scan()init(posb0, posinfo0) minv,minpos=findmin(posb0)print "start===>"outputInfo(posinfo0)newposb=posb0.copy()newposinfo=posinfo0.copy()tryonestep(minpos, newposb, newposinfo, 0)print "No answer!!"

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读