本文实例为大家分享了Android实现数独游戏机器人的具体代码,供大家参考,具体内容如下 针对该应用的数独机器人,填答案逻辑未完成,主要原因在于游戏响应触屏事件参数有待调整,简单的使
本文实例为大家分享了Android实现数独游戏机器人的具体代码,供大家参考,具体内容如下
针对该应用的数独机器人,填答案逻辑未完成,主要原因在于游戏响应触屏事件参数有待调整,简单的使用input tap,界面响应不正常。
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, maskmap
posinfo0={}
for x in xrange(0,9):
for y in xrange(0,9):
x0=(x/3)*(257-26)+(x%3)*(96-26)+26
y0=(y/3)*(432-179)+(y%3)*(255-179)+179
x1=x0+(88-26)
y1=y0+(240-179)
dig=img.crop((x0,y0,x1,y1))
throd=100
res=0
for 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]<throd:
break
if t!=dig.size[1]-1:break
for marg_right in xrange(dig.size[0]-1, dig.size[0]/2, -1):
for t in xrange(0,dig.size[1]):
if dig.getpixel((marg_right, t))[0]<throd:
break
if t!=dig.size[1]-1:break
for marg_top in xrange(0,dig.size[1]/2+2):
for t in xrange(0,dig.size[0]):
if dig.getpixel((t,marg_top))[0]<throd:
break
if t!=dig.size[0]-1:break
for marg_bottom in xrange(dig.size[1]-1, dig.size[1]/2, -1):
for t in xrange(0,dig.size[0]):
if dig.getpixel((t,marg_bottom))[0]<throd:
break
if t!=dig.size[0]-1:break
if marg_left>=marg_right:
#print "(%d,%d):%d"%(x,y,res)
continue
mask=0
bitcnt=1
dig=dig.crop((marg_left,marg_top,marg_right,marg_bottom))
olddirect=0
direct=0
for digy in xrange(dig.size[1]/8,dig.size[1], dig.size[1]/8):
cnt=0
for digx in xrange(0,dig.size[0]-1):
if dig.getpixel((digx, digy))[0]>throd and dig.getpixel((digx+1, digy))[0]<=throd:
cnt+=1
mask+=cnt*bitcnt
bitcnt*=5
for digx in xrange(dig.size[0]/6,dig.size[0], dig.size[0]/6):
cnt=0
for digy in xrange(0,dig.size[1]-1):
if dig.getpixel((digx, digy))[0]>throd and dig.getpixel((digx, digy+1))[0]<=throd:
cnt+=1
mask+=cnt*bitcnt
bitcnt*=5
if 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 677
def 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 deepcopy
from 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=0
for 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=1
break
elif len(posb1[(pos[0],pos[1])])==0:
ret=2
break
posb=posb1
posinfo=posinfo1
return ret
def cleanother(mod,x,y,v,posb,posinfo):
posb1=posb.copy()
x0=x/3*3
y0=y/3*3
for 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=0
if posb1!=posb:
ret = checkpos(posb1,posinfo)
posb=posb1
return ret
def findmin(posb):
minv=9
minpos=(0,0)
for key,val in posb.items():
if minv>len(val):
minv=len(val)
minpos=key
return (minv,minpos)
def outputInfo(posinfo):
print "\n %s"%" ".join(str(v) for v in xrange(0,9))
print "-----------"*2
for 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 " ",
print
print "==========="*2
def addinfo(x,y,v,posb,posinfo):
if (x,y) in posinfo:return
#print "addinfo",x,y,v
if (x,y) in posb:
del posb[(x,y)]
posinfo[(x,y)]=v
clearallinfo(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: return
newposinfo=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)+26
y0=(i/3)*(432-179)+(i%3)*(255-179)+179
x1=x0+(88-26)/2
y1=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 " ",
print
def tryonestep(minpos, posb, posinfo, layer):
global tic
for 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!!!", layer
continue
if 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!!"
--结束END--
本文标题: android实现数独游戏机器人
本文链接: https://lsjlt.com/news/143765.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0