以下是脚本程序
------------------------
--Read config----------------------------------------------------------
config = string.load("llk.ini","rb");
configs =table.loadList(config,"\n");
--end----------------------------------------------------------------------
--以下为系统参数设置的读取-----------------------------------
imgwidth=configs["IMG_WIDTH"]-0;
imgheight=configs["IMG_HEIGHT"]-0; --图片高
imgcolsize=configs["IMG_COLSIZE"]-0;
imgrowsize=configs["IMG_ROWSIZE"]-0; --19*11幅图片
offsetWidth=configs["OFFSET_WIDTH"]-0+configs["CROP_OFFSET_WIDTH"]-0;
offsetHeight=configs["OFFSET_HEIGHT"]-0;
diff=configs["NORMAL_DIFF"]-0;
diff1=configs["BLANK_DIFF"]-0;
diff2=configs["SAME_DIFF"]-0;
delay1=configs["PIC1_CLICK_DELAY"]-0;
delay2=configs["PIC2_CLICK_DELAY"]-0;
blankurl=configs["BLANK_IMAGE"];
--end------------------------------------------------------------
--全局变量定义-------------------------------------------
winleft=0;
wintop=0;
winright=0;
winbottom=0;
blankimg=nil;
fullimg=nil;
tab={};
imgs={};
hwnd=nil; --游戏窗口
--end-----------------------------------------------------------
--加载空白图片--------------------------------------------
function loadBlank(url)
local blank=image.new();
blank:load(url);
return blank;
end;
--end--------------------------------------------------------------
--本方法用来寻找是否可以相连------------------------
function findsame()
local same={};
local row=table.maxn(tab);
local col=table.maxn(tab[1]);
local visit={};
for i=1,row,1 do
for j=1,col,1 do
if(tab[j]>0) then
local index=tab[j];
if(visit[index]==nil) then
visit[index]={};
end;
table.insert(visit[index], {i,j});
end;
end;
end;
return visit;
end;
--end-----------------------------------------------------------
--在判断图片相同后的处理动作----------------------
function dosame(same)
local size=table.maxn(same);
local count=countLeft();
while(nStop() and count>0) do
for i=1,size,1 do
local items=same;
local isize=table.maxn(items);
for j=1,isize,1 do
if(items[j]~=nil) then
for k=j+1,isize,1 do
if(items[j]~=nil and items[k]~=nil) then
local result=tryconnect(items[j],items[k]);
if(result~=nil) then
if(connect(items[j],items[k])) then
items[j]=nil;
items[k]=nil;
count=count-1;
end;
--showpoint(result[1],result[2],result[3],result[4]);
end;
end;
end;
end;
end;
end;
delay(10);
end;
end;
--尝试连接----------------------------------------------------
function connect(pos1,pos2)
local r1=pos1[1];
local r2=pos2[1];
local c1=pos1[2];
local c2=pos2[2];
local offsetwidth1=(c1-0.5)*imgwidth;
local offsetheight1=(r1-0.5)*imgheight;
local offsetwidth2=(c2-0.5)*imgwidth;
local offsetheight2=(r2-0.5)*imgheight;
mouse.click(winleft+offsetWidth+offsetwidth1,wintop+offsetHeight+offsetheight1,true);
delay(delay1);
mouse.click(winleft+offsetWidth+offsetwidth2,wintop+offsetHeight+offsetheight2,true);
delay(delay2);
local img1=getImage1(c1,r1);
local img2=getImage1(c2,r2);
if(img1:test(imgs[r1][c1])
end;
return true;
end;
--end--------------------------------------------------------------------
function aroundcheck(r1,c1,r2,c2)
if(r1==r2 and (c1-c2==1 or c1-c2==-1)) then
return true;
end;
if(c1==c2 and (r1-r2==1 or r1-r2==-1)) then
return true;
end;
return false;
end;
function tryconnect(pos1,pos2)
local r1=pos1[1];
local r2=pos2[1];
local c1=pos1[2];
local c2=pos2[2];
--循同行往前移动寻找路线
for i=c1-1,1,-1 do
if(i<1) then
break;
end;
if(i==c2 and r1==r2) then
logicremove(r1,c1,r2,c2);
return {r1,c1,r2,c2};
end;
if(tab[r1]>0) then
break;
end;
local suc1=coltravel(i,r1,r2);
local suc2=rowtravel(r2,c2,i);
if(suc1 and suc2) then
logicremove(r1,c1,r2,c2);
return {r1,c1,r2,c2};
end;
end;
--循同行往后移动寻找路线
for i=c1+1,imgcolsize,1 do
if(i>imgcolsize) then
break;
end;
if(i==c2 and r1==r2) then
logicremove(r1,c1,r2,c2);
return {r1,c1,r2,c2};
end;
if(tab[r1]>0) then
break;
end;
local suc1=coltravel(i,r1,r2);
local suc2=rowtravel(r2,c2,i);
if(suc1 and suc2) then
logicremove(r1,c1,r2,c2);
return {r1,c1,r2,c2};
end;
end;
----循同列往上移动寻找路线
for j=r1-1,1,1 do
if(j<1) then
break;
end;
if(j==r2 and c1==c2) then
logicremove(r1,c1,r2,c2);
return {r1,c1,r2,c2};
end;
if(tab[j][c1]>0) then
break;
end;
local suc1=coltravel(c2,r2,j);
local suc2=rowtravel(j,c1,c2);
if(suc1 and suc2) then
logicremove(r1,c1,r2,c2);
return {r1,c1,r2,c2};
end;
end;
--循同列往下移动寻找路线
for j=r1+1,imgrowsize,1 do
if(j>imgrowsize) then
break;
end;
if(j==r2 and c1==c2) then
logicremove(r1,c1,r2,c2);
return {r1,c1,r2,c2};
end;
if(tab[j][c1]>0) then
break;
end;
local suc1=coltravel(c2,r2,j);
local suc2=rowtravel(j,c1,c2);
if(suc1 and suc2) then
logicremove(r1,c1,r2,c2);
return {r1,c1,r2,c2};
end;
end;
end;
function logicremove(r1,c1,r2,c2)
tab[r1][c1]=0;
tab[r2][c2]=0;
end;
function coltravel(c,r1,r2)
local step=1;
if(r1==r2) then
return true;
elseif(r1>r2 ) then
step=-1;
else
step=1;
end;
for i=r1+step,r2,step do
if(tab[c]>0) then
return false;
end;
end;
return true;
end;
function rowtravel(r,c1,c2)
local step=1;
if(c1==c2) then
return true;
elseif(c1>c2 ) then
step=-1;
else
step=1;
end;
for i=c1+step,c2,step do
if(tab[r]>0) then
return false;
end;
end;
return true;
end;
function printresult(result)
local size=table.maxn(result);
for i=1,size,1 do
showpoint(result[1],result[2],result[3],result[4]);
end;
end;
function showpoint(x1,y1,x2,y2)
win.messageBox("["..x1..","..y1.."] ["..x2..","..y2.."]");
end;
function getImage(fullimg,x,y)
local ileft=offsetWidth+(x-1)*imgwidth+3;
local itop=offsetHeight+(y-1)*imgheight+4;
local iright=offsetWidth+x*imgwidth-4;
local ibottom=offsetHeight+y*imgheight-5;
local tmp=image.new(fullimg);
tmp:Crop(ileft,itop,iright,ibottom);
return tmp;
end;
function getImage1(x,y)
local ileft=offsetWidth+(x-1)*imgwidth+3;
local itop=offsetHeight+(y-1)*imgheight+4;
local iright=offsetWidth+x*imgwidth-4;
local ibottom=offsetHeight+y*imgheight-5;
local tmp=image.new();
image.capture(tmp,hwnd,ileft,itop,imgwidth-7,imgheight-9);
return tmp;
end;
function countLeft()
local count=0;
for i=1,imgrowsize,1 do
for j=1,imgcolsize,1 do
if(tab[j]>0) then
count=count+1;
end;
end;
end;
return count;
end;
------------------------------------
--辨识图片是否已经存在---------
sample={};
imgIdentify=function(tmpimg)
if(blankfound==false) then
if(tmpimg:test(blankimg)
blankfound=true;
return 0;
end;
end;
if(blankfound==true) then
if(tmpimg:test(blankimg)
end;
end;
local count=table.maxn(sample);
for i=1,count,1 do
if(sample:test(tmpimg)
end;
end
sample[count+1]=tmpimg;
return count+1;
end;
------------------------------------
--显示图片对应的数字情况----
imageShow=function()
end;
------------------------------
--获取窗口的整幅画面------------------------------------------------------------
function getFullImage()
local winname="KYODAI~1.EXE";
hwnd=win.fromFile(winname,"","连连看");
winleft,wintop,winright,winbottom=win.getRect(hwnd);
local img =image.new();
image.capture(img,hwnd,0,0,0,0);
return img;
end;
--end----------------------------------------------------------------------------------
--图片转换为数字标识-----------------------------------------------------------
function image2num()
for i=1,imgrowsize,1 do
tab={};
imgs={};
for j=1,imgcolsize,1 do
--tmp:paint(ileft,itop,29,33);
--tmp:save("d:\\"..i..j..".png");
local tmp=getImage(fullimg,j,i);
tab[j]=imgIdentify(tmp);
imgs[j]=tmp;
end;
end;
end;
--end----------------------------------------------------------------------------------
function finish()
image.del(fullimg);
fullimg=image.new();
image.capture(fullimg,hwnd,0,0,0,0);
local count=0;
for i=1,imgrowsize,1 do
for j=1,imgcolsize,1 do
local img=getImage(fullimg,j,i);
if(img:test(blankimg)>diff1) then
count=count+1;
end;
end;
end;
if(count>0) then
image2num();
local visit=findsame();
end;
end;
function start()
blankimg=loadBlank(blankurl);
--blankimg:paint(100,100,40,40);
blankfound=false;
fullimg=getFullImage();
image2num();
local visit=findsame();
dosame(visit);
--finish();
collectgarbage();
end;
--启动程序-------------------------------
start();
--end--------------------------------------