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

📄 chessman.cpp

📁 一个用MFC做的象棋游戏,可以联网双人对下,
💻 CPP
📖 第 1 页 / 共 2 页
字号:

#include "stdafx.h"
#include "chessman.h"
#include "Array.h"
//加载DLL
#pragma comment(lib,"ArrayDll")
extern CMyArr theArr;
int CChessMan::m_stcChosenID=-1;
void CChessMan::Create(UINT roleID,UINT nID,POINT initPos,int eachX,int eachY,int chessPaperBeginX,int chessPaperBeginY)
{
	m_btMapRole.LoadBitmap(nID);
	m_btMapRole.GetObject(sizeof(BITMAP),&m_btBM);
	m_uRoleID=roleID;
	m_ptNowPos=initPos;
	m_nEachX=eachX;
	m_nEachY=eachY;
	m_nChessPaperBeginX=chessPaperBeginX;
	m_nChessPaperBeginY=chessPaperBeginY;
	DPTOLP();
}
void CChessMan::Create(UINT roleID,UINT nID,int iIndex,int jIndex,int eachX,int eachY,int chessPaperBeginX,int chessPaperBeginY)
{
	m_btMapRole.LoadBitmap(nID);
	m_btMapRole.GetObject(sizeof(BITMAP),&m_btBM);
	m_uRoleID=roleID;
	uCol=iIndex;
	uRow=jIndex;
	m_nEachX=eachX;
	m_nEachY=eachY;
	m_nChessPaperBeginX=chessPaperBeginX;
	m_nChessPaperBeginY=chessPaperBeginY;
	LPTODP();
}
void CChessMan::ResetPos(POINT newPos)
{
	m_ptNowPos=newPos;
	DPTOLP();
}
void CChessMan::ResetPos(int ni,int nj)
{
	uCol=ni;
	uRow=nj;
	LPTODP();
}
void CChessMan::DPTOLP()
{
	uCol=(m_ptNowPos.x-m_nChessPaperBeginX+m_nEachX/2)/m_nEachX;
	uRow=(m_ptNowPos.y-m_nChessPaperBeginY+m_nEachY/2)/m_nEachY;
}
void CChessMan::LPTODP()
{
	m_ptNowPos.x=m_nChessPaperBeginX-m_nEachX/2+uCol*m_nEachX;
	m_ptNowPos.y=m_nChessPaperBeginY-m_nEachY/2+uRow*m_nEachY;
}
void  CChessMan::Draw(CDC *pDC)
{
	
	CBitmap memBitmap;
		memBitmap.CreateCompatibleBitmap(pDC,m_btBM.bmWidth,m_btBM.bmHeight);
		CDC memDC;
		memDC.CreateCompatibleDC(pDC);
		memDC.SelectObject(&memBitmap);
		CBitmap *pOldBitmap=memDC.SelectObject(&m_btMapRole);
		//pDC->BitBlt(m_ptNowPos.x-m_btBM.bmWidth/2,m_ptNowPos.y-m_btBM.bmHeight/2,m_btBM.bmWidth,m_btBM.bmHeight,&memDC,0,0,SRCCOPY);
		pDC->BitBlt(m_ptNowPos.x,m_ptNowPos.y,m_btBM.bmWidth,m_btBM.bmHeight,&memDC,0,0,SRCCOPY);
		memDC.SelectObject(pOldBitmap);
		//memDC.DeleteDC();
	
}

void CChessMan::InvalidateMyRect(CDC *pDC)
{

	CRect rect(m_ptNowPos.x-5,m_ptNowPos.y-5,m_ptNowPos.x+m_btBM.bmWidth+5,m_ptNowPos.y+m_btBM.bmHeight+5);
	CPen * pOldPen=pDC->SelectObject(&CPen(PS_NULL,0,RGB(255,255,255)));
	pDC->Rectangle(&rect);
	pDC->SelectObject(pOldPen);
    //InvalidateRect(hWnd,&rect,true);//先无效

}
void CChessMan::InvalidateMyRect(CDC *pDC,UINT uMyRow,UINT uMyCol)
{
	
	UINT uOldRow,uOldCol;
		uOldRow=uRow;
		uOldCol=uCol;
		ResetPos(uMyCol,uMyRow);
	    CRect rect(m_ptNowPos.x-5,m_ptNowPos.y-5,m_ptNowPos.x+m_btBM.bmWidth+5,m_ptNowPos.y+m_btBM.bmHeight+5);
		CPen * pOldPen=pDC->SelectObject(&CPen(PS_NULL,0,RGB(255,255,255)));
	    pDC->Rectangle(&rect);
		pDC->SelectObject(pOldPen);
		//InvalidateRect(hWnd,&rect,true);//先无效
		ResetPos(uOldCol,uOldRow);
	

}
/*
void CChessMan::GetRect(CRect &rect)
{
    CRect MyRect(m_ptNowPos.x-5,m_ptNowPos.y-5,m_ptNowPos.x+m_btBM.bmWidth+5,m_ptNowPos.y+m_btBM.bmHeight+5);
	rect=MyRect;
}*/

bool CChessMan::IfThisPosMe(POINT thisPos)
{
	int nOldI=uRow,nOldJ=uCol;
	ResetPos(thisPos);
	if(nOldI==uRow&&nOldJ==uCol)
	{
		ResetPos(nOldJ,nOldI);
		return true;
	}
	else
	{
		ResetPos(nOldJ,nOldI);
		return false;
	}
}
void CChessMan::ShowIAmHere(CDC *pDC)
{
	m_stcChosenID=m_uRoleID;
    CBrush *pOldBrush=(CBrush *)pDC->SelectStockObject(NULL_BRUSH);
	CPen pen;
	pen.CreatePen(PS_DOT,2,RGB(255,0,0));
	CPen *pOldPen=pDC->SelectObject(&pen);
	//CRect rect(m_ptNowPos.x-m_btBM.bmWidth/2-4,m_ptNowPos.y-m_btBM.bmHeight/2-4,m_ptNowPos.x+m_btBM.bmWidth/2+4,m_ptNowPos.y+m_btBM.bmHeight/2+4);
	CRect rect(m_ptNowPos.x-4,m_ptNowPos.y-4,m_ptNowPos.x+m_btBM.bmWidth+4,m_ptNowPos.y+m_btBM.bmHeight+4);
	pDC->Rectangle(&rect);
	pDC->SelectObject(pOldPen);
	pDC->SelectObject(pOldBrush);
}
bool CChessMan::NextStep(CChessMan *nextChessMan,int nNewRow,int nNewCol)
{
	CChessMan *chessManFront=nextChessMan;//查找前面有没有子
	if(m_uRoleID<=16)///表示是A方的
	{
		/*if(uRow<5)//表示小兵还没有过河
		{*/
		if(chessManFront)//不为空,有子
		{
			if(chessManFront->m_uRoleID<=16)//表示是自己家的子
			{
				return false;//表示这一步无效
			}
			else//是对方的子,哈哈吃掉
			{
				//chessMap->ResetStatePos(i,j,NULL);
				//chessMap->ResetStatePos(i+1,j,this);//后面一定要记得更新
				ResetPos(nNewCol,nNewRow);//更新棋子的位置信息
				//InvalidateRect(NULL);//重新绘制棋盘
				return true;
			}
		}
		else//表示前面是空的,那就直接向前走呀
		{
			ResetPos(nNewCol,nNewRow);
			//chessMap->ResetStatePos(i,j,NULL);
			//chessMap->ResetStatePos(i+1,j,this);
			//InvalidateRect(NULL);//重新绘制棋盘
			return true;
		}
		//}
		//else///小足子终于过河了哦
		//{

		//}
	}
	else if(m_uRoleID>=17)
	{
		if(chessManFront)//不为空,有子
		{
			if(chessManFront->m_uRoleID>=17)//表示是自己家的子
			{
				return false;//表示这一步无效
			}
			else//是对方的子,哈哈吃掉
			{
				//chessMap->ResetStatePos(i,j,NULL);
				//chessMap->ResetStatePos(i+1,j,this);//后面一定要记得更新
				ResetPos(nNewCol,nNewRow);//更新棋子的位置信息
				//InvalidateRect(NULL);//重新绘制棋盘
				return true;
			}
		}
		else//表示前面是空的,那就直接向前走呀
		{
			ResetPos(nNewCol,nNewRow);
			//chessMap->ResetStatePos(i,j,NULL);
			//chessMap->ResetStatePos(i+1,j,this);
			//InvalidateRect(NULL);//重新绘制棋盘
			return true;
		}
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
/////下面是兵
bool CSoldier::IfNextPosLegal(POINT nextPos)
{
	UINT uOldI=uRow,uOldJ=uCol;
	ResetPos(nextPos);
	if(m_uRoleID>16&&m_uRoleID<=32)//表示是A方的
	{
		if(uOldI<5&&uOldI>=3)//未过河
		{
			if (uRow==uOldI+1&&uCol==uOldJ)
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			else
			{
				ResetPos(uOldJ,uOldI);
				return false;
			}
		}
		else//过河啦
		{
			/////////////////
			///////////////下面分析小足子过河后走的三中情况
			if (uRow==uOldI&&uCol==uOldJ-1&&uCol>=0)//向左走
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			if(uRow==uOldI&&uCol==uOldJ+1&&uCol<=8)//向右走
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			if(uRow==uOldI+1&&uRow<=9&&uCol==uOldJ)
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			ResetPos(uOldJ,uOldI);
			return false;
		}
	}
	else if(m_uRoleID<=16)///表示是B方的
	{
		if(uOldI<=6&&uOldI>=5)//未过河
		{
			if (uRow==uOldI-1&&uCol==uOldJ)
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			else
			{
				ResetPos(uOldJ,uOldI);
				return false;
			}
		}
		else//过河啦
		{
			/////////////////
			///////////////下面分析小足子过河后走的三中情况
			if (uRow==uOldI&&uCol==uOldJ-1&&uCol>=0)
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			if(uRow==uOldI&&uCol==uOldJ+1&&uCol<=8)
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			if(uRow==uOldI-1&&uRow>=0&&uCol==uOldJ)
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			ResetPos(uOldJ,uOldI);
			return false;
		}   
	}
   return false;
}
/*bool CSoldier::NextStep(POINT nextPos,CDC *pDC,CChessMap*chessMap)
{
	if(m_uRoleID<=16)///表示是A方的
	{
		if(i<5)//表示小兵还没有过河
		{
			CChessMan *chessManFront=chessMap->FindStatePos(i+1,j);//查找前面有没有子
			if(chessManFront)//不为空,有子
			{
				if(chessManFront->m_uRoleID<=16)//表示是自己家的子
				{
					return false;//表示这一步无效
				}
				else//是对方的子,哈哈吃掉
				{
					chessMap->ResetStatePos(i,j,NULL);
					chessMap->ResetStatePos(i+1,j,this);
					ResetPos(i+1,j);//更新棋子的位置信息
					//InvalidateRect(NULL);//重新绘制棋盘
					return true;
				}
			}
			else//表示前面是空的,那就直接向前走呀
			{
				ResetPos(i+1,j);
				chessMap->ResetStatePos(i,j,NULL);
				chessMap->ResetStatePos(i+1,j,this);
				//InvalidateRect(NULL);//重新绘制棋盘
				return true;
			}
		}
		else///小足子终于过河了哦
		{

		}
	}
}*/

/*
bool CSoldier::NextStep(CChessMan *nextChessMan,int nNewRow,int nNewCol)
{
	CChessMan *chessManFront=nextChessMan;//查找前面有没有子
	if(m_uRoleID<=16)///表示是A方的
	{
		/ *if(uRow<5)//表示小兵还没有过河
		{* /
			if(chessManFront)//不为空,有子
			{
				if(chessManFront->m_uRoleID<=16)//表示是自己家的子
				{
					return false;//表示这一步无效
				}
				else//是对方的子,哈哈吃掉
				{
					//chessMap->ResetStatePos(i,j,NULL);
					//chessMap->ResetStatePos(i+1,j,this);//后面一定要记得更新
					ResetPos(nNewCol,nNewRow);//更新棋子的位置信息
					//InvalidateRect(NULL);//重新绘制棋盘
					return true;
				}
			}
			else//表示前面是空的,那就直接向前走呀
			{
				ResetPos(nNewCol,nNewRow);
				//chessMap->ResetStatePos(i,j,NULL);
				//chessMap->ResetStatePos(i+1,j,this);
				//InvalidateRect(NULL);//重新绘制棋盘
				return true;
			}
		//}
		//else///小足子终于过河了哦
		//{
            
		//}
	}
	else if(m_uRoleID>=17)
	{
		if(chessManFront)//不为空,有子
		{
			if(chessManFront->m_uRoleID>=17)//表示是自己家的子
			{
				return false;//表示这一步无效
			}
			else//是对方的子,哈哈吃掉
			{
				//chessMap->ResetStatePos(i,j,NULL);
				//chessMap->ResetStatePos(i+1,j,this);//后面一定要记得更新
				ResetPos(nNewCol,nNewRow);//更新棋子的位置信息
				//InvalidateRect(NULL);//重新绘制棋盘
				return true;
			}
		}
		else//表示前面是空的,那就直接向前走呀
		{
			ResetPos(nNewCol,nNewRow);
			//chessMap->ResetStatePos(i,j,NULL);
			//chessMap->ResetStatePos(i+1,j,this);
			//InvalidateRect(NULL);//重新绘制棋盘
			return true;
		}
	}
	return false;
}*/

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
///////////下面是马

bool CHorse::IfNextPosLegal(POINT nextPos)
{
	UINT uOldI=uRow,uOldJ=uCol;
	ResetPos(nextPos);
	/*
	if (m_uRoleID<=16)////表示是A方的
		{*/
	
     if (uRow==uOldI+1&&uRow<=9&&uCol==uOldJ+2&&uCol<=8)
     {
		 if (!theArr.Find(uOldI,uOldJ+1))
		 {
			 ResetPos(uOldJ,uOldI);
			 return true;
		 }
		 ResetPos(uOldJ,uOldI);
		 return false;
     }
	 else if (uRow==uOldI-1&&uRow>=0&&uCol==uOldJ+2&&uCol<=8)
	 {
		 if (!theArr.Find(uOldI,uOldJ+1))
		 {
			 ResetPos(uOldJ,uOldI);
			 return true;
		 }
		 ResetPos(uOldJ,uOldI);
		 return false;
	 }
	 else  if (uRow==uOldI-2&&uRow>=0&&uCol==uOldJ+1&&uCol<=8)
	 {
		 if (!theArr.Find(uOldI-1,uOldJ))
		 {
			 ResetPos(uOldJ,uOldI);
			 return true;
		 }
		 ResetPos(uOldJ,uOldI);
		 return false;
	 }
	 else if (uRow==uOldI-2&&uRow>=0&&uCol==uOldJ-1&&uOldJ>=0)
	 {
		 if (!theArr.Find(uOldI-1,uOldJ))
		 {
			 ResetPos(uOldJ,uOldI);
			 return true;
		 }
		 ResetPos(uOldJ,uOldI);
		 return false;
	 }
	 else if (uRow==uOldI-1&&uRow>=0&&uCol==uOldJ-2&&uCol>=0)
	 {
		 if (!theArr.Find(uOldI,uOldJ-1))
		 {
			 ResetPos(uOldJ,uOldI);
			 return true;
		 }
		 ResetPos(uOldJ,uOldI);
		 return false;
	 }
	 else if (uRow==uOldI+1&&uRow<=9&&uCol==uOldJ-2&&uCol>=0)
	 {
		 if (!theArr.Find(uOldI,uOldJ-1))
		 {
			 ResetPos(uOldJ,uOldI);
			 return true;
		 }
		 ResetPos(uOldJ,uOldI);
		 return false;
	 }
	 else if (uRow==uOldI+2&&uRow<=9&&uCol==uOldJ-1&&uCol>=0)
	 {
		 if (!theArr.Find(uOldI+1,uOldJ))
		 {
			 ResetPos(uOldJ,uOldI);
			 return true;
		 }
		 ResetPos(uOldJ,uOldI);
		 return false;
	 }
	 else if (uRow==uOldI+2&&uRow<=9&&uCol==uOldJ+1&&uCol<=8)
	 {
		 if (!theArr.Find(uOldI+1,uOldJ))
		 {
			 ResetPos(uOldJ,uOldI);
			 return true;
		 }
		 ResetPos(uOldJ,uOldI);
		 return false;
	 }
	 ResetPos(uOldJ,uOldI);
	 return false;      
	//}
	return false;
}
/*
bool CHorse::NextStep(CChessMan*nextChessMan,int nNewRow,int nNewCol)
{
	return false;
}*/
///////////////////////////////////////////////////
////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
////////下面是炮
bool CCannon::IfNextPosLegal(POINT nextPos)
{
	UINT uOldI=uRow,uOldJ=uCol;
	ResetPos(nextPos);
	if (m_uRoleID<=16)
	{
		if (uRow+1<=uOldI&&uRow>=0&&uCol==uOldJ)///表示炮向上走
		{
			if (uRow==uOldI-1&&!theArr.Find(uRow,uCol))//表示向上走一格,并且这个格子上没有子
			{

				ResetPos(uOldJ,uOldI);
				return true;
			}
			int nBetween=theArr.FindBetween(uCol,uRow,uOldI,0);
			/////下面是说假如之间有一棋子,并且目标位置是对方的棋子
			//////或者是之间没有棋子并且目标位置是空的
			if ((1==nBetween&&theArr.Find(uRow,uCol)==2)||(0==nBetween&&theArr.Find(uRow,uCol)==0))
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			else
			{
				ResetPos(uOldJ,uOldI);
				return false;
			}

		}
		else if (uRow-1>=uOldI&&uRow<=9&&uCol==uOldJ)////向下走子
		{
			if (uRow==uOldI+1&&!theArr.Find(uRow,uCol))//刚好向下走一格,并且格子上是没有棋子的
			{

				ResetPos(uOldJ,uOldI);
				return true;
			}
			int nBetween=theArr.FindBetween(uCol,uOldI,uRow,0);
			////之间有一个棋子并且目标位置是对方的棋子 吃掉或者之间没有棋子并且目标位置是空的
			if ((1==nBetween&&theArr.Find(uRow,uCol)==2)||(0==nBetween&&theArr.Find(uRow,uCol)==0))
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			else
			{
				ResetPos(uOldJ,uOldI);
				return false;
			}

		}
		else if (uCol+1<=uOldJ&&uCol>=0&&uRow==uOldI)//向左走子
		{
			if (uCol==uOldJ-1&&!theArr.Find(uRow,uCol))//向左走一格,并且这个格子上没有棋子
			{

				ResetPos(uOldJ,uOldI);
				return true;
			}
			int nBetween=theArr.FindBetween(uRow,uCol,uOldJ,1);
			///之间有一个棋子,并且目标位置是对方的棋子,之间没有棋子,目标位置也没有棋子
			if ((1==nBetween&&theArr.Find(uRow,uCol)==2)||(0==nBetween&&theArr.Find(uRow,uCol)==0))
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			else
			{
				ResetPos(uOldJ,uOldI);
				return false;
			}
		}
		else if (uCol-1>=uOldJ&&uCol<=8&&uRow==uOldI)//向右走子
		{
			if (uCol==uOldJ+1&&!theArr.Find(uRow,uCol))//向右一格并且这个格子上每偶棋子
			{

				ResetPos(uOldJ,uOldI);
				return true;
			}
			int nBetween=theArr.FindBetween(uRow,uOldJ,uCol,1);
			///之间只有一个棋子并且目标位置是对方的棋子,之间没有棋子并且目标位置是空的
			if ((1==nBetween&&theArr.Find(uRow,uCol)==2)||(0==nBetween&&theArr.Find(uRow,uCol)==0))
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			else
			{
				ResetPos(uOldJ,uOldI);
				return false;
			}
		}
	}
	else if (m_uRoleID>=17&&m_uRoleID<=32)//是B方的棋子
	{
		if (uRow+1<=uOldI&&uRow>=0&&uCol==uOldJ)
		{
			if (uRow==uOldI-1&&!theArr.Find(uRow,uCol))
			{

				ResetPos(uOldJ,uOldI);
				return true;
			}
			int nBetween=theArr.FindBetween(uCol,uRow,uOldI,0);
			if ((1==nBetween&&theArr.Find(uRow,uCol)==1)||(0==nBetween&&theArr.Find(uRow,uCol)==0))
			{
				ResetPos(uOldJ,uOldI);
				return true;
			}
			else
			{
				ResetPos(uOldJ,uOldI);
				return false;
			}

		}

⌨️ 快捷键说明

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