c++做的象棋源代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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);
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)
// 二○○○年七月
// 让我们继续看我的程序。
//这是课本上的例子。
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
//可供两人对弈的中国象棋程序
#include <afxwin.h>
// 棋盘类
class CPlate
{
int m_ndx; // 棋盘格宽
{
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->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);
brushSelected.CreateSolidBrush(RGB(127, 127, 0));
pOldPen = pDC->SelectObject(&penNormal);
// 被选中的棋子颜色不同
if(m_bSelected)
pOldBrush = pDC->SelectObject(&brushSelected);
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);
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);
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);
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;
else
pOldBrush = pDC->SelectObject(&brushNormal);
#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; // 是否红方
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;
}
// 棋子类
// 定义棋子名称
void SelectStone(){m_bSelected = !m_bSelected;}
};
// 棋子类的成员函数
// 棋子类的构造函数:初始化棋子
CStone::CStone(BOOL red, int col, int row, LPCSTR name, int type, CPlate &plate)
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->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));
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)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
// 嗨!我是王晓钰。
// 我的E-mail是:XiaoYu.wang@ 有问题可以发邮件或在网站上留言
// ~~~~~~~~~~~~~~~~~~~~~|
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);
{return m_bShow && m_nCol==col && m_nRow == row;}
BOOL MouseOnStone(CPoint point)
{return m_rectStone.PtInRect(point) && m_bShow;}
void KillIt(){m_bShow = FALSE;}
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) // 只有未被吃掉的棋子才显示
{
// 准备画棋子的画笔和画刷
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);
BOOL m_bSelected; // 是否被选择
int m_nCol; // 路
int m_nRow; // 行
CRect m_rectStone; // 棋子包含矩形
BOOL m_bShow; // 是否显示
CString m_sName; // 棋子名称
void MoveTo(int col, int row, CPlate &plate);
CRect GetRect(){return m_rectStone;}
int GetType(){return m_nType;}
BOOL BeKilled(int col, int row)
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);
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);
}
// 取棋盘上各交叉点的坐标
{
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);
CPen *pOldPen, penNormal(PS_SOLID, 3, RGB(120, 120, 120));
CBrush *pOldBrush, brushNormal, brushSelected;
brushNormal.CreateSolidBrush(RGB(255, 255, 0));
相关文档
最新文档