#include<afxwin>
#include"resource"
using namespace std;
class Cplate
{
int m_ndx;
int m_ndy;
int m_nLeft;
int m_nTop;
int m_nRight;
int m_nBottom;
public:
CPlate();
void ShowPlate(CDC *pDC);
void DrawConer(CDC *pDC,int row,int col,int type);
CPoint GetPosition(int col,int row);
};
CPlate::CPlate()
{
m_ndx =50;
m_ndy =45;
m_nLeft =198;
m_nTop =30;
m_nRight =m_nLeft+m_ndx*8;
m_nBottom=m_nTop+m_ndy*9;
}
void CPlate::ShowPlate(CDC *pDC) //显示棋盘
{ //画棋盘方格
CPen penRed2(PS_SOLLD,2,RGB(255,0,0));
CPen *pOldPen=pDC->SelectObject(&penRed2);
for(int i=0;i<10;i++)
{ pDC->MoveTo(m_nLeft,m_nTop+i*m_ndy);
pDC->LineTo(m_nRight,m_nTop+i*m_ndy);
}
for(i=0;i<9;i++)
{
if(i==0!!i==8)
{
pDC->MoveTo(m_nLeft+i*m_ndx,m_nTop);
pDC->lineTo(m_nLeft+i*m_ndx,m_nBottom);
}
else
{
pDC->MoveTo(m_nLeft+i*m_ndx,m_nTop);
pDC->LineTo(m_nLeft+i*m_ndx,m_nTop+4*m_ndy);
pDC->MoveTo(m_nLeft+i*m_ndx,m_nTop+5*m_ndy);
pDC->LineTo(m_nLeft+i*m_ndx,m_nBottom);
}
}
//画棋盘斜线
pDC->SelectObject(pOldPen);
CPen penRed1(PS_SOLID,1,REG(255,0,0));
pOldPen=pDC->SelectObject(&penRed1);
pDC->MoveTo(m_nLeft+3*m_ndx,m_nTop);
pDC->LineTo(m_nLeft+5*m_ndx,m_nTop+2*m_ndy);
pDC->MoveTo(m_nLeft+3*m_ndx,m_nTop+2*m_ndy);
pDC->LineTo(m_nLeft+5*m_ndx,m_nTop);
pDC->MoveTo(m_nLeft+3*m_ndx,m_nBottom);
pDC->LineTo(m_nLeft+5*m_ndx,m_nBottom-2*m_ndy);
pDC->MoveTo(m_nLeft+3*m_ndx,m_nBottom-2*m_ndy);
pDC->LineTo(m_nLeft+5*m_ndx,m_nBottom);
pDC->SelectObject(pOldPen);
//画兵,炮位标记
DrawConer(pDC,2,1,0);
DrawConer(pDC,2,7,0);
DrawConer(pDC,3,0,1);
DrawConer(pDC,3,2,0);
DrawConer(pDC,3,4,0);
DrawConer(pDC,3,6,0);
DrawConer(pDC,3,8,2);
DrawConer(pDC,7,1,0);
DrawConer(pDC,7,7,0);
DrawConer(pDC,6,0,1);
DrawConer(pDC,6,2,0);
DrawConer(pDC,6,4,0);
DrawConer(pDC,6,6,0);
DrawConer(pDC,6,8,2);
}
void CPlate::DrawConer(CDC *pDC,int row,int col,int type)//绘制兵,炮位标志
{
CPen penRed1(PS_SOLID,1,REG(255,0,0));
CPen *pOldPen=pDC->SelectObject(&penRed1);
if(type==0!!type==1)
{
pDC->MoveTo(m_nLeft+col*m_ndx+3,m_nTop+row*m_ndy-10);
pDC->LineTo(m_nLeft+col*m_ndx+3,m_nTop+row*m_ndy-3);
pDC->LineTo(m_nLeft+col*m_ndx+10,m_nTop+row*m_ndy-3);
pDC->MoveTo(m_nLeft+col*m_ndx+3,m_nTop+row*m_ndy+10);
pDC->LineTo(m_nLeft+col*m_ndx+3,m_nTop+row*m_ndy+3);
pDC->LineTo(m_nLeft+col*m_ndx+10,m_nTop+row*m_ndy+3);
}
if(type==0!!type==2)
{
pDC->MoveTo(m_nLeft+col*m_ndx-3,m_nTop+row*m_ndy-10);
pDC->LineTo(m_nLeft+col*m_ndx-3,m_nTop+row*m_ndy-3);
pDC->LineTo(m_nLeft+col*m_ndx-10,m_nTop+row*m_ndy-3);
pDC->MoveTo(m_nLeft+col*m_ndx-3,m_nTop+row*m_ndy+10);
pDC->LineTo(m_nLeft+col*m_ndx-3,m_nTop+row*m_ndy+3);
pDC->LineTo(m_nLeft+col*m_ndx-10,m_nTop+row*m_ndy+3);
}
pDC->SelectObject(pOldPen);
}
CPoint CPlate::GetPosition(int col,int row)//取棋盘上交叉点的坐标
{
CPoint point;
point.x=m_nLeft+col*m_ndx;
point.y=m_nTop+row*m_ndy;
return point;
}
//棋子类
//定义棋子名称
#define BING 1
#define PAO 2
#define JU 3
#define MA 4
#define XIANG 5
#define SHI 6
#define JIANG 7
class CStone
{
BOOL m_bRed;
BOOL m_bSelected;
int m_nCol;
int m_nRow;
CRect m_rectStone;
BOOL m_bShow
CString m_sName;
int m_nR;
int m_nType;
public:
CStone(){}
CStone(BOOL red,int col,int row,LPCSTR name,int type,CPlate &plate);
void ShowStone(CDC *pDC);
void MoveTo(int col,int row,CPlate &plate);
CRect GetRect(){return m_rectStone;}
int GetType(){return m_nType;}
BOOL BeKilled(int col,int row)
{ return m_bShow &&m_nCol==col&&m_nRow==row;}
BOOL MouseOnStone(CPoint point)
{ return m_nrectStone.PtInRect(point)&&m_bShow;}
void KillIt(){m_bShow=FALSE;}
void SelectStone(){m_bSelected=!m_bSelected;}
};
//棋子类的成员函数
//棋子类的构造函数:初始化棋子
CStone::CStone(BOOL red,int col,int row,LPCSTR name,int type,CPlate &plate)
{
m_bShow=TRUE;
m_bSelected=FALSE;
m_bRed=red;
m_nCol=col;
m_nRow=row;
m_sName=name;
m_nType=type;
m_nR=23;
CPoint pos=plate.GetPosition(col,row);
m_rectStone=CRect(pos.x-mR,pos.y-m_nR,pos.x+m_nR,pos.y+m_nR);
}
void CStone::ShowStone(CDC *pDC)//显示棋子
{
if(m_bShow)//只有未被吃掉的棋子才显示
{//准备画棋子的画笔和画刷
CPen *pOldPen,penNormal(PS_SOLID,3,REG(120,120,120));
CBrush *pOldBrush,brushNormal,brushSelected;
brushNormal.CreateSolidBrush(RGB(255,255,0));
brushSelected.CreateSolidBrush(RGB(127,127,0));
pOldPen=pDC->SelectObject(&penNormal);
//被选中的棋子颜色不同
if(m_bSelected)
pOldBrush=pDC->SelectObject(&brushSelected);
else
pOldBrush=pDC->SelectObject(&brushNormal);
//显示棋子
CRect r(m_rectStone);
r.left++;
r.top++;
r.right --;
r.bottom--;
pDC->Ellipse(r);
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
//准备显示棋子名称的字体
CFont *pOldFont,fontStone;
fontStone.CreateFont(40,0,0,0,400,FALSE,FALSE,0,OEM_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,"楷体");
pOldFont=pDC->SelectObject(&fontStone);
int tx=m_rectStone.left+6;
int ty=m_rectStone.top+6;
pDc->SetBkMode(TRANSPARENT);
//棋子背景均为黄色,字分红,黑两色
pDC->SetTextColor(REG(m_bRed?255:0,0,0));
pDC->TextOut(tx,ty,m_sName);
pDC->SelectObject(pOldFont);
}
}
void CStone::MoveTo(int col,int row,CPlate &plate)
{
m_bSelected=FALSE;
m_nCol=col;
m_nRow=row;
CPoint pos=plate.GetPosition(col,row);
m_rectStone=CRect(pos.x-m_nR,pos.y-m_nR,pos.x+m_nR,pos.y+m_nR);
}
//文档类/
class CMyDoc : public CDocument
{protected://create from serialization only
DECLARE_DYNCREATE(CMyDoc)
public:
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(CMyDoc,CDocument)
BEGIN_MESSAGE_MAP(CMyDoc,CDocument)
END_MESSAGE_MAP()
//视图类
class CMyView : public CView
{
protected://create from serialization only
CMyView();
DECLARE_DYNCREATE(CMyView)
//Attributes
private:
CStone m_StoneList[32];
CPlate m_Plate;
BOOL m_bRedTurn;
BOOL m_bSelectOne;
int m_nWhichStone;
CRect m_rectInfo;
public:
CMyDoc *GetDocument();
//Opertion
public:
BOOL MoveTo(int col,int row);
void Go(Cpoint point);
void SelectStone(Cpoint point);
void MoveStone(Cpoint point);
int KillEnemy(int col,int row,BOOL red);
BOOL KillSelfStone(int col,int row,BOOL red);
void ShowInfo(CDC *pDC);
void InitGame();
//Overrides
//ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyView)
public:
virtual void OnDraw(CDC *pDC);//overrridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT &cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo *pInfo);
virtual void OnBenginPrinting(CDC *pDC,CPrintInfo *pInfo);
virtual void OnEndPrinting(CDC *pDC,CPrintInfo *pInfo);
//}}AFX_VIRTUAL
//Implementation
public:
virtual~CMyView();
#ifdef_DEBUG
virtual void AssertValid()const;
virtual void Dump(CDumpContext& dc)const;
#endif
protected:
//Generated message map functions
protected:
//{{AFX_MSG(CMyView)
afx_msg void OnLButtonDown(UINT nFlags,CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(CMyView,CView)
BEGIN_MESSAGE_MAP(CMyView,CView)
//{{AFX_MSG_MAP(CMyView)
ON_WN_LBUTTONDOWN()
//}}AFX_MSG_MAP
//Standard printing commands
ON_COMMAND(ID_FILE_PRINT,CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT,CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW,CView::OnFilePrint)
END_MESSAGE_MAP()
//视图类的成员函数
CMyView::CMyView()//初始化棋局
{
//TODO:add construction code here
InitGame();
}
void CMyView::OnDraw(CDC *pDC)
{
CMyDoc *pDoc=GetDocument();
ASSERT_VALID(pDoc);
//TODO:add draw code for native data here
m_Plate.ShowPlate(pDC);//显示棋盘
for(int i=0;i<32;i++)//显示所有棋子
m_StoneList[i].ShowStone(pDC);
ShowInfo(pDC);
}
void CMyView::InitGame()
{
m_bRedTurn=TRUE;
m_bSelectOne=FALSE;
m_rectInfo=CRect(315,480,515,550);
m_StoneList[0]=CStone(TRUE,0,0,"车",m_Plate);
m_StoneList[1]=CStone(TRUE,1,0,"马",m_Plate);
m_StoneList[2]=CStone(TRUE,2,0,"象",m_Plate);
m_StoneList[3]=CStone(TRUE,3,0,"士",m_Plate);
m_StoneList[4]=CStone(TRUE,4,0,"将",m_Plate);
m_StoneList[5]=CStone(TRUE,5,0,"士",m_Plate);
m_StoneList[6]=CStone(TRUE,6,0,"象",m_Plate);
m_StoneList[7]=CStone(TRUE,7,0,"马",m_Plate);
m_StoneList[8]=CStone(TRUE,8,0,"车",m_Plate);
m_StoneList[10]=CStone(TRUE,1,2,"炮",m_Plate);
m_StoneList[11]=CStone(TRUE,7,2,"炮",m_Plate);
m_StoneList[12]=CStone(TRUE,0,3,"兵",m_Plate);
m_StoneList[13]=CStone(TRUE,2,3,"兵",m_Plate);
m_StoneList[14]=CStone(TRUE,4,3,"兵",m_Plate);
m_StoneList[15]=CStone(TRUE,6,3,"兵",m_Plate);
m_StoneList[16]=CStone(TRUE,8,3,"兵",m_Plate);
m_StoneList[17]=CStone(FALSE,0,9,"车",m_Plate);
m_StoneList[18]=CStone(FALSE,1,9,"马",m_Plate);
m_StoneList[19]=CStone(FALSE,2,9,"相",m_Plate);
m_StoneList[20]=CStone(FALSE,3,9,"仕",m_Plate);
m_StoneList[21]=CStone(FALSE,4,9,"帅",m_Plate);
m_StoneList[22]=CStone(FALSE,5,9,"仕",m_Plate);
m_StoneList[23]=CStone(FALSE,6,9,"相",m_Plate);
m_StoneList[24]=CStone(FALSE,7,9,"马",m_Plate);
m_StoneList[25]=CStone(FALSE,8,9,"车",m_Plate);
m_StoneList[26]=CStone(FALSE,1,7,"炮",m_Plate);
m_StoneList[27]=CStone(FALSE,7,7,"炮",m_Plate);
m_StoneList[28]=CStone(FALSE,0,6,"卒",m_Plate);
m_StoneList[29]=CStone(FALSE,2,6,"卒",m_Plate);
m_StoneList[30]=CStone(FALSE,4,6,"卒",m_Plate);
m_StoneList[31]=CStone(FALSE,6,6,"卒",m_Plate);
m_StoneList[31]=CStone(FALSE,8,6,"卒",m_Plate);
}
void CMyView::ShowInfo(CDC *pDC)
{
CFont *pOldFont,fontInfo;
fontInfo.CreateFont(50,0,0,0,400,FALSE,FALSE
0,OEM_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,"楷体");
pOldFont=pDC->SelectObject(&fontInfo);
pDC->SetTextColor(RGB(m_bRedTurn?255:0,0,0));
pDC->TextOut(m_rectInfo.left.m_rectInfo.top,m_bRedTurn?"红方行棋":"黑方行棋");
pDC->SelectObject(pOldFont);
}
BOOL CMyView::KillSelfStone(int col,int row,BOOL red)
{
int from=red?0:16;
for(int i=from;i<from+16;i++)
if(m_StoneList[i].BeKilled(col,row))
return TRUE;
return FALSE;
}
int CMyView::KillEnmeny(int col,int row,BOOL red)
{
int from=red?16:0;
for(int i=from;i<from+16;i++)
if(m_StoneList[i].BeKilled(col,row))
return i;
return -i;
}
void CMyView::MoveStone(CPoint point)
{
if(m_StoneList[m_nWhichStone].MouseOnStone(point))
{
m_StoneList[m_nWhichStone].SelectStone();
m_bSelectOne=FALSE;
InvalidateRect(m_StoneList[m_nWhichStone].GetRect(),FALSE);
}
esle
Go(point);
}
void CMyView::SelectStone(CPoint point)
{
int nwhich=m_bRedTurn?0:16;
for(int i=nwhich;i<nwhich+16;i++)
if(m_StoneList[i].MouseOnStone(point))
{
m_StoneList[i].SelectStone();
m_bSelectOne=FALSE;
InvalidateRect(m_StoneList[i].GetRect(),FALSE);
m_nWhichStone=i;
m_bSelectOne=TRUE;
break;
}
}
void CMyView::Go(CPoint point)
{
for(int col=0;col<9;col++)
for(int row=0;row<10;row++)
{
CPoint p=m_Plate.GetPosition(col,row);
CRect r(p.x-23,p.y-23,p.x+23,p.y+23);
if(r.PtInRect(point))
{
if(KillSelfStone(col,row,m_bRedTurn))
MessageBox("您竟然想吃自己人?!");
else
m_bRedTurn=MoveTo(col,row);
}
}
}
BOOL CMyView::MoveTo(int col,int row)
{
InvalidateRect(m_StoneList[m_nWhichStone].GetRect());
m_StoneList[m_nWhichStone].MoveTo(col,row,m_Plate);
InvalidateRect(m_StoneList[m_nWhichStone].GetRect());
m_bSelectOne=FALSE;
int i=KillEnemy(col,row,m_bRedTurn);
if(i>=0)
{
m_StoneList[i].KillIt();
if(m_StoneList[i].GetType()==JIANG)
{
if(m_bRedTurn)
MessageBox(m_bRedTurn?"红方胜!":"黑方胜!");
InitGame();
Invalidate();
return TRUE;
}
}
InvalidateRect(m_rectInfo);
return !m_bRedTurn;
}
void CMyView::OnLButtonDown(UINT nFlags,CPoint point)
{
if(m_bSelectOne)
MoveStone(point);
else
SelectStone(point);
CView::OnLButtonDown(nFlags,point);
}