附录3:可供两个人对弈的中国象棋程序 350- 附录3:可供两人对弈的中国象棋程序 / Sample10-4:中国象棋程序 #include <afxwin h /棋盘类 /棋盘格宽 int m ndy //棋盘格高 int m nLeft /棋盘位置 int int m bOttom late void ShowPlate(CDC **pDC) void DrawConer(CDC *pDC, int row, int col, int type) CPoint GetPosition(int col, int row) //棋盘类的成员函数 /构造函数:初始化 CPlate: CPlate o m nLeft m nTop rIght = m nLeft+m ndx *8: m nBot tom = m nToptm ndy 9: /显示棋盘 void CPlate: ShowPlate(CDC *pDC) /画棋盘方格 CPen penRed2(PS SOLID, 2, RGB(255, 0, 0)) CPen *pOldPen pDC->SelectObject(&penRed2) for(int i<10;i+)
附录 3:可供两个人对弈的中国象棋程序 - 350 - 附录 3:可供两人对弈的中国象棋程序 // Sample 10-4: 中国象棋程序 #include <afxwin.h> // 棋盘类 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_SOLID,2,RGB(255,0,0)); CPen *pOldPen = pDC->SelectObject(&penRed2); for(int i=0; i<10; i++)
附录3:可供两个人对弈的中国象棋程序 351 pDC->Move To(m nLeft, m nTop+ pDC->LineTo(m nIght, m nTop+i*m ndy) for(i if(i==0|i==8) pDC->Move To(m nLeft+i*m ndx, m nTop) pDC->Line To(m nLeft+i*km ndx, m nBot tom pDC->Move To(m nLeft+i=sm ndx, m nTop pDC->Line To(m nLeft+i=lm ndx, m nTop+4*m ndy) pDC->Move To(m nLeft+i*km ndx, m nTop+5*m ndy) pDC->Line To(m nLeft+i=*m ndx, m nBot tom) /画棋盘斜线 pDC->SelectObject(pOldPen) CPen penRed1(PS SOLID, 1, RGB(255, 0, 0)) pOldPen pDC->SelectObject(&penRed1) pDC->Move To(m nLeft+3*m ndx, m nTop) DC->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->Line To(m nLeft+5*m ndx, m nBottom-2*m ndy) pDC->MoveTo(m nLeft+3*m ndx, m nBottom-2*m ndy) DC->LineTo(m nLeft+5*m ndx, m bOttom) 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) (pDC,3,6,0)
附录 3:可供两个人对弈的中国象棋程序 - 351 - { 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,RGB(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);
附录3:可供两个人对弈的中国象棋程序 352 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) /绘制兵,炮位标志 void CPlate: DrawConer(CDC *pDC, int row, int col, int type CPen penRed1(PS SOLID, 1, RGB(255, 0, 0)) CPen *pOldPen pDC->SelectObject(&penRed1) if(type =0 II type = 1) pDC->Move To(m nLeft+col*m ndx+3, m nToptrow*m ndy-10 pDC->LineTo(m nLeft+col=*m ndx+ 3, m n Toptrowkm ndy-3) pDC->LineTo(m nLeft+col*km ndx+10, m n Toptrowsm ndy-3) pDC->Move To(m nLeft+col*m ndx+3, m nToptrow*km ndy+10) neTo(m nLeft+col=*m ndx+ 3, m nTop+ pDC->LineTo(m nLeft+col*m ndx+10, m nToptrowkm ndy+3) pDC->MoveTo(m nLeft+col*m ndx-3, m n Toptrowkm ndy-10) pDC->Line To(m nLeft+col*m ndx- 3, m n Toptrow*m ndy-3) pDC->LineTo(m nLeft+col=*m ndx-10, m n Toptrowakm ndy-3) pDC->Line To(m nLeft+col *km ndx-3, m nToptrow*m ndy+3) pDC->LineTo(m nLeft+col=m ndx-10, m n Toptrowkm ndy+3) pDC->SelectObject(pOldPen) /取棋盘上各交叉点的坐标 CPoint CPlate: GetPosition (int col, int row)
附录 3:可供两个人对弈的中国象棋程序 - 352 - 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,RGB(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;
附录3:可供两个人对弈的中国象棋程序 353- point x =m nLeft+col*m ndx point. y m nToptrowakm ndy /棋子类 //定义棋子名称 #define BING 1 #define PAO 2 efine JU #define #define XIANG 5 #define SHII 6 #define JIANG 7 class stone BOOL /是否红方 booL m sElected;//是否被选择 m /路 m nrow CRect m rectstone;∥/棋子包含矩形 BOOI /是否显示 CString m sName /棋子名称 m nR: /棋子半径 m nType /棋子类型 lbl STone o什} CStone (BOOL red, int col, int row, LPCSTR name, int type, CPlate &plate) void ShowStone(CDC *pDC) d Move To (int col, int row, CPlate &plate) CRect GetRecto freturn m rectStone: I GetType ireturn m nType: 1 BOOL BeKilled (int col, int row) freturn m bShow & m nCol==col & m nRow = row BOOLMouseOnStone(CPoint point) [return m rectStone. PtInRect(point)&& m bShow: I voidKillItoim bShow FALSE: I voidSelectStone o(m sElected =!m bSelected; 1
附录 3:可供两个人对弈的中国象棋程序 - 353 - 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;} BOOLMouseOnStone(CPoint point) {return m_rectStone.PtInRect(point) && m_bShow;} voidKillIt(){m_bShow = FALSE;} voidSelectStone(){m_bSelected = !m_bSelected;} };
附录3:可供两个人对弈的中国象棋程序 354 /棋子类的成员函数 /棋子类的构造函数:初始化棋子 CStone: CStone(BOoL red, int col, int row, LPCSTR name, int type, CPlate &plate) m show TRUE m sElected False: m nRow row m sName type 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) /显示棋子 id CStone: ShowStone(CDC *pDC) if( m show)//只有未被吃掉的棋子才显示 //准备画棋子的画笔和画刷 CPen * dPen, pen Normal (PS SOLID, 3, RGB(120, 120, 120)) CBrush *kpOldBrush, brushNormal, brushSelected brushNormal. CreateSolidBrush (RGB(255, 255, 0)) brushSelected. CreateSolidBrush(RGB(127, 127, 0)) pOldPen pDC->SelectOb ject(&pen Normal) //被选中的棋子颜色不同 if(m sElected) pOldBrush pDC->SelectOb ject(&brush Selected) else pOldBrush =pDC-SelectObject(&brushNormal) /显示棋子 CRect r(m rectStone) r. right pDC->Ellipse(r)
附录 3:可供两个人对弈的中国象棋程序 - 354 - // 棋子类的成员函数 // 棋子类的构造函数:初始化棋子 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-m_nR, 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, RGB(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);