经过昨天到今天的奋斗,经过反复修改,没参考任何代码,核心已经完成。核心代码不过百行,原来好的设计,太重要了!!!!
//判断二个位置是否直线相连
bool CLinkGameLog::NoCorner(const CPoint & pS,const CPoint & pE)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
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)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(m_iBoard[temp.y][temp.x]!=-1)
return false;
temp+=pitch;
}
return true;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//判断二个位置是否一个折点相连 若相连的话cLoc为折点
bool CLinkGameLog::OneCorner(const CPoint & pS,const CPoint & pE,CPoint & cLoc)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
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))
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {cLoc=pX;return true;}
if(isEmpty(pY) && NoCorner(pS,pY) && NoCorner(pY,pE))
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {cLoc=pY;return true;}
return false;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//判断二个位置是否二个折点相连 若相连的话cLoc1,cLoc2为折点
bool CLinkGameLog::TwoCorner(const CPoint & pS,const CPoint & pE,CPoint & cLoc1,CPoint & cLoc2,const CPoint & dirP)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
CPoint temp=pS;
temp+=dirP;
while (isValid(temp) && m_iBoard[temp.y][temp.x]==-1)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(OneCorner(temp,pE,cLoc2)==true)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
cLoc1=temp;
return true;
}
temp+=dirP;
}
return false;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
//能否连接
bool CLinkGameLog::beCanBreak()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
CSize area;
area=m_nextLoc-m_preLoc;
//点同样的点
if (area.cx==0 && area.cy==0)
return false;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if (area.cx!=0 && area.cy!=0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(OneCorner(m_preLoc,m_nextLoc,m_pcLoc1)==true)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
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)) )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_icNum=2;
return true;
}
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (NoCorner(m_preLoc,m_nextLoc)==true)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_icNum=0;
return true;
}
if (area.cx==0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
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)) )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_icNum=2;
return true;
}
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
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)) )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
m_icNum=2;
return true;
}
}
}
return false;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
|