?? chessman.cpp
字號:
#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);
/////下面是說假如之間有一棋子,并且目標(biāo)位置是對方的棋子
//////或者是之間沒有棋子并且目標(biāo)位置是空的
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);
////之間有一個棋子并且目標(biāo)位置是對方的棋子 吃掉或者之間沒有棋子并且目標(biāo)位置是空的
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);
///之間有一個棋子,并且目標(biāo)位置是對方的棋子,之間沒有棋子,目標(biāo)位置也沒有棋子
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);
///之間只有一個棋子并且目標(biāo)位置是對方的棋子,之間沒有棋子并且目標(biāo)位置是空的
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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -