⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i_goview.cpp

📁 一个简单的围棋游戏
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	pDoc->nCalculateBlack = 0;
	pDoc->nCalculateWhite = 0;
	for(int row=0;row<19;row++)
		for(int col=0;col<19;col++)
			if(nCenter==nWhite)
				pDoc->nCalculateWhite++;
			else if(nCenter==nBlack)
				pDoc->nCalculateBlack++;		
}

void CI_goView::recover_Black_Chain()
{
	CI_goDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	for(int row=0;row<19;row++)
		for(int col=0;col<19;col++)
		{
			if(nCenter==nDie&&nOldCenter==nBlack)
			{
				nCenter=nBlack;
				Set_Left_Up_Right_Down_Chain(row,col);
			}
		}
}

//===========================================================================//
//更改当前点和周围四个点的关系,即链标志
//===========================================================================//
void CI_goView::Set_Left_Up_Right_Down_Chain(int row, int col)
{
	CI_goDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if(nCenter==nDie)
	{
		if(row==0)
			nUpChain  = nDifferent;
		else
			nUpChain  = nFree;
		if(col==0)
			nLeftChain    = nDifferent;
		else
			nLeftChain    = nFree;
		if(row==18)
			nDownChain = nDifferent;
		else
			nDownChain = nFree;
		if(col==18)
			nRightChain  = nDifferent;
		else
			nRightChain  = nFree;
	}
	else
	{
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
	//判断左边的点,及改变连接链
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

		if(nCenter==nLeft)
			nLeftChain=nSame;     //点左边链,即左边的点和新点相同
		else if(nLeft==nDie)
			nLeftChain=nFree;     //左边空位
		else 
			nLeftChain=nDifferent;//左边点不同
		
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
	//判断右边的点,及改变连接链
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
		if(nCenter==nRight)
			nRightChain=nSame; 
		else if(nRight==nDie)
			nRightChain=nFree; 
		else 
			nRightChain=nDifferent;

	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
	//判断上边的点,及改变连接链
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
		if(nCenter==nUp)
			nUpChain=nSame;     //点上边边链,即上边的点和新点相同
		else if(nUp==0)
			nUpChain=nFree;     //上边边空位
		else 
			nUpChain=nDifferent;//上边点不同
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
	//判断下边的点,及改变连接链
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
		if(nCenter==nDown)
			nDownChain=nSame; 
		else if(nDown==0)
			nDownChain=nFree; 
		else 
			nDownChain=nDifferent;
	}
}
//===========================================================================//
//打劫:即来回吃为非法
//===========================================================================//
//新填的子被吃?Y-go;N-合法
	//同时导致对方的字被吃?Y-go; N-非法
		//前一次有没有与新填的字同色的子在该处被吃?Y-go;N-非法
			//则为合法走步!则恢复被吃掉的与新填子同色的被吃子,更改链标志
//如果为非法走步,则此次走棋无效,调用悔棋程序,并提示走步非法
//===========================================================================//
void CI_goView::Determine_Illegal_And_Dajie(int row, int col)
{
	CI_goDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	//新填子被吃
	if(nCenter==nDie)
	{
		//TRACE("\n新填子被吃!当前%d***前一%d前二****%d\n",nCenter,nBackCenter,nBackTwoTimesBeforeCenter);
		//若新填为黑子,且白子数减少?
		if(pDoc->nCount%2==1 && pDoc->nCalculateWhite<pDoc->nBackCalculateWhite)
		{	
		//	TRACE("新填为黑子,且白子减少!!当前%d***前一%d前二****%d\n",nCenter,nBackCenter,nBackTwoTimesBeforeCenter);
			//前一次在该位置无黑子被吃?
			if(nBackTwoTimesBeforeCenter==nBlack)
			{	
				//TRACE("前一次在该处有黑子被吃!!非法!!!当前%d***前一%d前二****%d\n",nCenter,nBackCenter,nBackTwoTimesBeforeCenter);
				pDoc->fIllegal=TRUE;	
			}
			else
			{	
				//TRACE("前一次在该处没有黑子被吃!!当前%d***前一%d前二****%d\n",nCenter,nBackCenter,nBackTwoTimesBeforeCenter);
				//合法走步,恢复被吃的黑子,更改与之有关的链标志
				pDoc->fIllegal=FALSE;
				recover_Black_Chain();
			}						
		}
		//若新填白子,且黑子数减少
		else if(pDoc->nCount%2==0 && pDoc->nCalculateBlack<pDoc->nBackCalculateBlack)
		{
			//TRACE("新填白子,且黑子减少!!当前%d***前一%d前二****%d\n",nCenter,nBackCenter,nBackTwoTimesBeforeCenter);
			//前一次该位置无白子被吃?
			if(nBackTwoTimesBeforeCenter==nWhite)
			{
				//TRACE("前一次在该处有白子被吃!!当前%d***前一%d前二****%d\n",nCenter,nBackCenter,nBackTwoTimesBeforeCenter);
				pDoc->fIllegal=TRUE;
			}
			else
			{
				//合法走步,恢复被吃的白子,更改与之有关的链标志
				//TRACE("前一次在该处没有白子被吃!!当前%d***前一%d前二****%d\n",nCenter,nBackCenter,nBackTwoTimesBeforeCenter);
				pDoc->fIllegal=FALSE;
				recover_White_Chain();
			}
		}
		else
		{
			//TRACE("自杀,非法 !!当前%d***前一%d前二****%d\n",nCenter,nBackCenter,nBackTwoTimesBeforeCenter);
			pDoc->fIllegal=TRUE;
		}
	}
	else
	{
		//TRACE("新填子没有被吃!!合法!!当前%d***前一%d前二****%d\n",nCenter,nBackCenter,nBackTwoTimesBeforeCenter);
		pDoc->fIllegal=FALSE;
	}

	if(pDoc->fIllegal==TRUE)
		regret();

}

void CI_goView::recover_White_Chain()
{
	CI_goDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	for(int row=0;row<19;row++)
		for(int col=0;col<19;col++)
		{
			if(nCenter==nDie&&(nOldCenter==nWhite||nOldCenter==nWhiteWait))
			{
				nCenter=nWhite;
				Set_Left_Up_Right_Down_Chain(row,col);
			}
		}

}

void CI_goView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// TODO: Add your specialized code here and/or call the base class
	CI_goDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	//设置定时器,时间间隔100us
	pDoc->m_timer = SetTimer(1,100,NULL);
	
	//初始化位图参数
	BITMAP	BMToRight;
	pDoc->m_BitmapToRight.GetBitmap(&BMToRight);
	pDoc->m_nBitmapToRightWidth  = BMToRight.bmWidth;
	pDoc->m_nBitmapToRightHeight = BMToRight.bmHeight;
	CRect rectClient;
	GetClientRect(&rectClient);
	pDoc->m_rectBitmapToRight = CRect(rectClient.left-pDoc->m_nBitmapToRightWidth,rectClient.top,rectClient.left,rectClient.top+pDoc->m_nBitmapToRightHeight);

	BITMAP	BMToLeft;
	pDoc->m_BitmapToLeft.GetBitmap(&BMToLeft);
	pDoc->m_nBitmapToLeftWidth  = BMToLeft.bmWidth;
	pDoc->m_nBitmapToLeftHeight = BMToLeft.bmHeight;
	pDoc->m_rectBitmapToLeft = CRect(rectClient.right,rectClient.bottom-pDoc->m_nBitmapToLeftHeight,rectClient.right+pDoc->m_nBitmapToLeftWidth,rectClient.bottom);

}

void CI_goView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CI_goDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	//更新原来区域
	CRect rectClient;
	GetClientRect(&rectClient);
	if(pDoc->m_fAlternate == FALSE)
	{
		if(pDoc->m_fClear==TRUE)
		{
			Invalidate();
			pDoc->m_fClear=FALSE;
		}
		if(pDoc->m_rectBitmapToLeft.bottom != rectClient.bottom)
		{	
			Invalidate();
			pDoc->m_rectBitmapToLeft = CRect(rectClient.right,rectClient.bottom-pDoc->m_nBitmapToLeftHeight,rectClient.right+pDoc->m_nBitmapToLeftWidth,rectClient.bottom);
		}
		InvalidateRect(pDoc->m_rectBitmapToRight);
		if(pDoc->m_rectBitmapToRight.left < rectClient.right)
		{
			pDoc->m_rectBitmapToRight.right += 5;
			pDoc->m_rectBitmapToRight.left += 5;
		}
		else 
		{	
			pDoc->m_fAlternate = TRUE;
			pDoc->m_rectBitmapToRight.left = rectClient.left-pDoc->m_nBitmapToRightWidth;
			pDoc->m_rectBitmapToRight.right = rectClient.left;
		}
		InvalidateRect(pDoc->m_rectBitmapToRight);
	}
	else
	{
		InvalidateRect(pDoc->m_rectBitmapToLeft);
		if(pDoc->m_rectBitmapToLeft.right > rectClient.left)
		{
			pDoc->m_rectBitmapToLeft.right -= 3;
			pDoc->m_rectBitmapToLeft.left -= 3;
		}
		else 
		{
			pDoc->m_fAlternate = FALSE;
			pDoc->m_rectBitmapToLeft.left = rectClient.right;
			pDoc->m_rectBitmapToLeft.right = rectClient.right+pDoc->m_nBitmapToLeftWidth;
		}
		InvalidateRect(pDoc->m_rectBitmapToLeft);
	}

	CView::OnTimer(nIDEvent);
}

BOOL CI_goView::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	//窗口关闭前清除计时器
	CI_goDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	KillTimer(pDoc->m_timer);

	return CView::DestroyWindow();
}

//为了在不同的显示器上显示整个棋盘而设置的
void CI_goView::OnIs15() 
{
	// TODO: Add your command handler code here
	CI_goDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->nDistance = 23;
	pDoc->nRadium      = (int)(pDoc->nDistance/10.0);
	pDoc->r			 = (int)(pDoc->nDistance*2/5.0);
	for(int i=0;i<19;i++)
		for(int j=0;j<19;j++)
			pDoc->m_rectEllipse[i][j]=CRect(pDoc->pointBegin.x+j*pDoc->nDistance-pDoc->r,pDoc->pointBegin.y+i*pDoc->nDistance-pDoc->r,pDoc->pointBegin.x+j*pDoc->nDistance+pDoc->r,pDoc->pointBegin.y+i*pDoc->nDistance+pDoc->r);
	Invalidate();
	
}

void CI_goView::OnIs17() 
{
	// TODO: Add your command handler code here
	CI_goDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->nDistance = 30;
	pDoc->nRadium      = (int)(pDoc->nDistance/10.0);
	pDoc->r			 = (int)(pDoc->nDistance*2/5.0);
	for(int i=0;i<19;i++)
		for(int j=0;j<19;j++)
			pDoc->m_rectEllipse[i][j]=CRect(pDoc->pointBegin.x+j*pDoc->nDistance-pDoc->r,pDoc->pointBegin.y+i*pDoc->nDistance-pDoc->r,pDoc->pointBegin.x+j*pDoc->nDistance+pDoc->r,pDoc->pointBegin.y+i*pDoc->nDistance+pDoc->r);
	Invalidate();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -