经过昨天到今天的奋斗,经过反复修改,没参考任何代码,核心已经完成。核心代码不过百行,原来好的设计,太重要了!!!!
//判断二个位置是否直线相连
bool CLinkGameLog::NoCorner(const CPoint & pS,const CPoint & pE)
  {
CPoint pitch,temp=pS;
CSize area=pE-pS;
if(area.cx==0 && area.cy==0)
return true;
int len=max(abs(area.cx),abs(area.cy));
area.cx/=len;area.cy/=len;
pitch.x=area.cx;pitch.y=area.cy;
temp+=pitch;
while (temp!=pE)
 {
if(m_iBoard[temp.y][temp.x]!=-1)
return false;
temp+=pitch;
}
return true;
}

//判断二个位置是否一个折点相连 若相连的话cLoc为折点
bool CLinkGameLog::OneCorner(const CPoint & pS,const CPoint & pE,CPoint & cLoc)
  {
CPoint pX,pY;
pX.x=pS.x;pX.y=pE.y;
pY.x=pE.x;pY.y=pS.y;
if(isEmpty(pX) && NoCorner(pS,pX) && NoCorner(pX,pE))
 {cLoc=pX;return true;}
if(isEmpty(pY) && NoCorner(pS,pY) && NoCorner(pY,pE))
 {cLoc=pY;return true;}
return false;
}

//判断二个位置是否二个折点相连 若相连的话cLoc1,cLoc2为折点
bool CLinkGameLog::TwoCorner(const CPoint & pS,const CPoint & pE,CPoint & cLoc1,CPoint & cLoc2,const CPoint & dirP)
  {
CPoint temp=pS;
temp+=dirP;
while (isValid(temp) && m_iBoard[temp.y][temp.x]==-1)
 {
if(OneCorner(temp,pE,cLoc2)==true)
 {
cLoc1=temp;
return true;
}
temp+=dirP;
}
return false;
}

//能否连接
bool CLinkGameLog::beCanBreak()
  {
CSize area;
area=m_nextLoc-m_preLoc;
//点同样的点
if (area.cx==0 && area.cy==0)
return false;

if (area.cx!=0 && area.cy!=0)
 {
if(OneCorner(m_preLoc,m_nextLoc,m_pcLoc1)==true)
 {
m_icNum=1;
return true;
}
if( TwoCorner(m_preLoc,m_nextLoc,m_pcLoc1,m_pcLoc2,CPoint(-1,0)) ||
TwoCorner(m_preLoc,m_nextLoc,m_pcLoc1,m_pcLoc2,CPoint( 1,0)) ||
TwoCorner(m_preLoc,m_nextLoc,m_pcLoc1,m_pcLoc2,CPoint(0,1 )) ||
TwoCorner(m_preLoc,m_nextLoc,m_pcLoc1,m_pcLoc2,CPoint(0,-1)) )
 {
m_icNum=2;
return true;
}
}
else
 {
if (NoCorner(m_preLoc,m_nextLoc)==true)
 {
m_icNum=0;
return true;
}
if (area.cx==0)
 {
if (TwoCorner(m_preLoc,m_nextLoc,m_pcLoc1,m_pcLoc2,CPoint(-1,0)) ||
TwoCorner(m_preLoc,m_nextLoc,m_pcLoc1,m_pcLoc2,CPoint( 1,0)) )
 {
m_icNum=2;
return true;
}
}
else
 {
if (TwoCorner(m_preLoc,m_nextLoc,m_pcLoc1,m_pcLoc2,CPoint(0, 1)) ||
TwoCorner(m_preLoc,m_nextLoc,m_pcLoc1,m_pcLoc2,CPoint(0,-1)) )
 {
m_icNum=2;
return true;
}
}
}
return false;
}

|