/* 【自学去】网站收集 http://www.zixue7.com */
#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++)
{
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);
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;
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_rectStone.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-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);
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(RGB(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 CMyWnd: public CFrameWnd
{
CPlate m_Plate; // 棋盘
CStone m_StoneList[32]; // 棋子数组
BOOL m_bRedTurn; // 轮红方下
BOOL m_bSelectOne; // 已选择一棋子
int m_nWhichStone; // 被选择棋子的编号
CRect m_rectInfo; // 信息显示位置
public:
CMyWnd(){InitGame();}
void InitGame();
void ShowInfo(CDC *pDC);
BOOL KillSelfStone(int col, int row, BOOL red);
int KillEnemy(int col, int row, BOOL red);
void MoveStone(CPoint point);
void SelectStone(CPoint point);
void Go(CPoint);
BOOL MoveTo(int, int);
protected:
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
// 消息映射
BEGIN_MESSAGE_MAP(CMyWnd,CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
// 框架窗口类的成员函数
// 初始化棋局
void CMyWnd::InitGame()
{
m_bRedTurn = TRUE;
m_bSelectOne = FALSE;
m_rectInfo = CRect(315, 480, 515, 550);
m_StoneList[ 0] = CStone(TRUE, 0, 0, "车", 3, m_Plate);
m_StoneList[ 1] = CStone(TRUE, 1, 0, "马", 4, m_Plate);
m_StoneList[ 2] = CStone(TRUE, 2, 0, "象", 5, m_Plate);
m_StoneList[ 3] = CStone(TRUE, 3, 0, "士", 6, m_Plate);
m_StoneList[ 4] = CStone(TRUE, 4, 0, "将", 7, m_Plate);
m_StoneList[ 5] = CStone(TRUE, 5, 0, "士", 6, m_Plate);
m_StoneList[ 6] = CStone(TRUE, 6, 0, "象", 5, m_Plate);
m_StoneList[ 7] = CStone(TRUE, 7, 0, "马", 4, m_Plate);
m_StoneList[ 8] = CStone(TRUE, 8, 0, "车", 3, m_Plate);
m_StoneList[ 9] = CStone(TRUE, 1, 2, "炮", 2, m_Plate);
m_StoneList[10] = CStone(TRUE, 7, 2, "炮", 2, m_Plate);
m_StoneList[11] = CStone(TRUE, 0, 3, "兵", 1, m_Plate);
m_StoneList[12] = CStone(TRUE, 2, 3, "兵", 1, m_Plate);
m_StoneList[13] = CStone(TRUE, 4, 3, "兵", 1, m_Plate);
m_StoneList[14] = CStone(TRUE, 6, 3, "兵", 1, m_Plate);
m_StoneList[15] = CStone(TRUE, 8, 3, "兵", 1, m_Plate);
程序员小马软件开发定制
- 粉丝: 8586
- 资源: 2245
最新资源
- 数据结构--实验报告2.docx
- 本软件包是用于Windows下往云端上传代码的工具
- MySQL-server-5.6.22-1.linux_glibc2.5.x86_64.rpm
- 基于Vue开发的碳排放数据可视化+源码+项目文档(毕业设计&课程设计&项目开发)
- 20241125-国外立体仓库丹巴赫堆垛机博图PLC项目&电气图纸PDF
- 信息化在线教学平台-毕业设计,基于Python+Django+Vue+MySql开发,源码+数据库+毕业论文+视频演示
- 基于PyTorch和Transformers框架的双语和跨语言嵌入模型(含项目源码、使用教程).zip
- CUMT自然语言处理报告-Qwen2大模型实现命名实体识别
- 毕业设计,基于Python+Django+Vue+MySql开发的信息化在线教学平台,内含完整源代码,数据库,毕业论文,视频教程
- 通信原理-AMI编译码(归零码)实验报告
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈