今天有空,写了一个测试程序,通过此程序,发现并修正了几个错误:
1、[修正]后缀自增、自减计算错误
2、[修正]浮点数求余错误,更改为:所有浮点数转为整数再求余,结果为整数。
3、[增加]内置函数 mod 浮点数求模
4、[增加]内置函数 StrFormat 字符串格式化
连连看检查两点之间是否可连接的核心算法(GScript 源码)
protected bool CheckMatch(int x1, int y1, int x2, int y2)//检查两点之间是否可到达,重要的逻辑函数
{
ArrayClear(this.arTraceMatch);
if(this.SimpleMatch(x1, y1, x2, y2) || this.LineMatch(x1, y1, x2, y2))
{
this.arTraceMatch[]=["x":x1*BMP_WIDTH+DRAW_LEFT+BMP_WIDTH/2,"y":y1*BMP_HEIGHT+DRAW_TOP+BMP_HEIGHT/2];
this.arTraceMatch[]=["x":x2*BMP_WIDTH+DRAW_LEFT+BMP_WIDTH/2,"y":y2*BMP_HEIGHT+DRAW_TOP+BMP_HEIGHT/2];
return true;
}
else
{
int cornerX,cornerY;
if(this.OneCornerMatch(x1, y1, x2, y2,cornerX,cornerY))
{
this.arTraceMatch[]=["x":x1*BMP_WIDTH+DRAW_LEFT+BMP_WIDTH/2,"y":y1*BMP_HEIGHT+DRAW_TOP+BMP_HEIGHT/2];
this.arTraceMatch[]=["x":cornerX*BMP_WIDTH+DRAW_LEFT+BMP_WIDTH/2,"y":cornerY*BMP_HEIGHT+DRAW_TOP+BMP_HEIGHT/2];
this.arTraceMatch[]=["x":x2*BMP_WIDTH+DRAW_LEFT+BMP_WIDTH/2,"y":y2*BMP_HEIGHT+DRAW_TOP+BMP_HEIGHT/2];
return true;
}
else
{
int cornerX2,cornerY2;
if(this.TwoCornerMatch(x1, y1, x2, y2,cornerX,cornerY,cornerX2,cornerY2))
{
this.arTraceMatch[]=["x":x1*BMP_WIDTH+DRAW_LEFT+BMP_WIDTH/2,"y":y1*BMP_HEIGHT+DRAW_TOP+BMP_HEIGHT/2];
this.arTraceMatch[]=["x":cornerX*BMP_WIDTH+DRAW_LEFT+BMP_WIDTH/2,"y":cornerY*BMP_HEIGHT+DRAW_TOP+BMP_HEIGHT/2];
this.arTraceMatch[]=["x":cornerX2*BMP_WIDTH+DRAW_LEFT+BMP_WIDTH/2,"y":cornerY2*BMP_HEIGHT+DRAW_TOP+BMP_HEIGHT/2];
this.arTraceMatch[]=["x":x2*BMP_WIDTH+DRAW_LEFT+BMP_WIDTH/2,"y":y2*BMP_HEIGHT+DRAW_TOP+BMP_HEIGHT/2];
return true;
}
}
}
return false;
}
protected bool SimpleMatch(int x1, int y1, int x2, int y2)
{
if((x1==x2))
return (Abs(y2-y1)==1);
else if((y1==y2))
return (Abs(x2-x1)==1);
return false;
}
protected bool LineMatch(int x1, int y1, int x2, int y2)
{
if (! ( x1==x2 || y1 == y2 ) )
return false ;
if( x1==x2)
{
if( y1 > y2 )
{
int nItem=(y1-1)* VIRTUAL_LINE+x1;
for(int i=y1-1 ; i>y2 ; --i)
{
if((this.Map[nItem]&0x3)!=ITEM_DEATH)
return false;
nItem-=VIRTUAL_LINE;
}
}
else
{
int nItem=(y2-1)* VIRTUAL_LINE+x2;
for(int i=y2-1 ; i>y1 ; --i)
{
if((this.Map[nItem]&0x3)!=ITEM_DEATH)
return false;
nItem-=VIRTUAL_LINE;
}
}
}
else
{
if( x1>x2)
{
int nItem=y1* VIRTUAL_LINE+(x1-1);
for(int i = x1 - 1 ; i>x2 ; --i )
{
if((this.Map[nItem--]&0x3)!=ITEM_DEATH)
return false;
}
}
else
{
int nItem=y2* VIRTUAL_LINE+(x2-1);
for(int i =x2 -1 ; i>x1 ; --i )
{
if((this.Map[nItem--]&0x3)!=ITEM_DEATH)
return false;
}
}
}
return true ;
}
protected bool OneCornerMatch(int x1, int y1, int x2, int y2,int& cornerX,int& cornerY)
{
int nItem=y2* VIRTUAL_LINE+x1;
if(((this.Map[nItem]&0x3)==ITEM_DEATH) && this.LineMatch(x1,y1,x1,y2) && this.LineMatch(x2,y2,x1,y2))
{
cornerX=x1;
cornerY=y2;
return true ;
}
int nItem=y1* VIRTUAL_LINE+x2;
if(((this.Map[nItem]&0x3)==ITEM_DEATH) && this.LineMatch(x1,y1,x2,y1) && this.LineMatch(x2,y2,x2,y1))
{
cornerX=x2;
cornerY=y1;
return true ;
}
return false ;
}
protected bool TwoCornerMatch( int x1, int y1, int x2, int y2,int& cornerX,int& cornerY,int& cornerX2,int& cornerY2)
{
int nItem=y1* VIRTUAL_LINE+(x1 -1);
for( int i=x1 -1 ; i>=0 ; --i )
{
if((this.Map[nItem--]&0x3)!=ITEM_DEATH)
break;
else if( this.OneCornerMatch( i,y1 , x2, y2,cornerX2,cornerY2) )
{
cornerX=i;
cornerY=y1;
return true ;
}
}
int nItem=y1* VIRTUAL_LINE+(x1 +1);
for(int i = x1 +1 ; i<VIRTUAL_LINE ; ++i )
{
if((this.Map[nItem++]&0x3)!=ITEM_DEATH)
break;
else if( this.OneCornerMatch(i,y1 , x2, y2 ,cornerX2,cornerY2) )
{
cornerX=i;
cornerY=y1;
return true ;
}
}
int nItem=(y1-1)* VIRTUAL_LINE+x1;
for( int i = y1 - 1 ; i>=0 ; --i )
{
if((this.Map[nItem]&0x3)!=ITEM_DEATH)
break;
else if( this.OneCornerMatch( x1,i ,x2, y2,cornerX2,cornerY2) )
{
cornerX=x1;
cornerY=i;
return true ;
}
nItem-=VIRTUAL_LINE;
}
int nItem=(y1+1)* VIRTUAL_LINE+x1;
for( i = y1 + 1 ; i< VIRTUAL_ROW ; ++i )
{
if((this.Map[nItem]&0x3)!=ITEM_DEATH)
break;
else if( this.OneCornerMatch( x1,i ,x2, y2,cornerX2,cornerY2) )
{
cornerX=x1;
cornerY=i;
return true ;
}
nItem+=VIRTUAL_LINE;
}
return false ;
}
最后上几张Demo图片