MFC制作象棋棋盘
mfc中国象棋课程设计

mfc中国象棋课程设计一、课程目标知识目标:1. 学生能理解中国象棋的基本规则,掌握棋盘布局、棋子走法及各类棋局的命名。
2. 学生能了解中国象棋的历史起源,理解其在中国传统文化中的地位。
3. 学生能掌握MFC(Microsoft Foundation Classes)编程环境下中国象棋游戏的开发基础。
技能目标:1. 学生能运用MFC编程技术,设计并实现一个具有基本功能的中国象棋游戏。
2. 学生通过编程实践,培养逻辑思维能力和问题解决能力。
3. 学生通过团队协作,提高沟通能力和项目协作能力。
情感态度价值观目标:1. 学生培养对中国传统文化的兴趣,弘扬和传承国粹。
2. 学生在编程过程中,树立正确的价值观,遵循编程道德规范。
3. 学生在团队协作中,学会尊重他人,培养团结互助的精神。
课程性质:本课程为信息技术与学科融合课程,结合中国象棋传统文化,培养学生编程兴趣和实践能力。
学生特点:六年级学生具备一定的计算机操作基础和逻辑思维能力,对中国象棋有一定了解。
教学要求:注重理论与实践相结合,以学生为主体,激发学生的学习兴趣和探究精神,培养其团队协作能力和创新意识。
通过本课程的学习,使学生能够将所学知识应用于实际项目中,达到学以致用的目的。
二、教学内容1. 中国象棋基础知识:- 棋盘布局与棋子走法- 常见棋局命名及战术分析- 中国象棋历史与文化背景2. MFC编程基础:- MFC概述与编程环境搭建- MFC常用类及其功能- MFC事件处理与消息映射机制3. 中国象棋游戏设计与实现:- 游戏界面设计- 棋子控制与走法逻辑实现- 棋局胜负判定算法- 游戏规则与提示功能设计4. 项目实践与团队协作:- 学生分组,明确项目任务与分工- 编程实践与功能模块实现- 团队沟通与协作,解决开发过程中遇到的问题- 项目总结与成果展示教学内容安排与进度:第一周:中国象棋基础知识学习,MFC编程环境搭建与基本概念介绍第二周:MFC常用类学习,设计游戏界面第三周:实现棋子控制与走法逻辑,学习棋局胜负判定算法第四周:完善游戏规则与提示功能,进行项目实践与团队协作第五周:项目总结与成果展示,交流与反馈教材关联章节:《信息技术》:《MFC编程与应用》相关章节;《中国象棋》相关章节。
MFC课程设计报告-一个简单的五子棋游戏

设计一个简单的五子棋游戏一、设计目标与容1.了解Windows编程的根底知识,掌握MFC应用程序的根本知识;2.根本掌握面向对象程序设计的根本思路和方法;3.掌握用VC++开发应用程序的的一般步骤和方法;4.能够利用所学的根本知识, 设计一个简单的五子棋游戏,具有以下功能:①数据构造的设计;五子棋棋盘的绘制。
②两人下棋时,两人下棋算法的设计。
③两人下棋时,判断任一方获胜的算法的设计。
二、设计要求1.用VC++进展编码,实现应用程序的功能。
注重编码质量,代码要有适当的注释;提交设计报告一份〔课程设计任务书、目录、主要的数据构造、设计的根本思路、设计的步骤及主要代码、心得体会、参考文献〕。
总体设计运行时效果如下:图3-1这个程序只能进展两个人之间的对弈,不能进展人机对弈,由于时间和个人能力的原因所以人机对弈的算法就没有写出。
同时程序中也存在着很多漏洞,但根本的功能都已经实现,还有待继续改进。
详细设计新建工程game_wzq选择单文档应用程序,在Step 4 of 6中先中Windows Sockets 复选框。
如以下图:图3-2资源编辑黑白位图Bitmap以表示棋盘上面的棋子:IDB_BLACKDB_WHITE黑白鼠标Cursor以替换当前鼠标:IDC_CURSOR1 黑棋子IDC_CURSOR2 白棋子黑白图标Icon以显示在状态栏供以提示IDI_BLACKIDI_WHITE菜单以供操作:开场:ID_START保存:ID_SAVE翻开:ID_OPEN如以下图所示:图3-3变量函数首先,为了实现状态栏的应用,我们必须更改它的变量:在MainFrm.h文件里面,把CStatusBar m_wndStatusBar 为public接着是在game_wzqView.h文件里面添加变量函数://两个鼠标HCURSOR hcursorwhite;HCURSOR hcursorblack;//棋盘数组int wzq[19][19];// colorwhite TRUE时白棋下,否那么黑棋下bool colorwhite;//棋子位图CBitmap m_bmblack;CBitmap m_bmwhite;//保存文件void Save();//检查是否完毕void over(CPoint point);//鼠标操作afx_msg void OnLButtonUp(UINT nFlags, CPoint point);//鼠标图形更换afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);//菜单的开场afx_msg void OnStart();//菜单的保存afx_msg void OnSave();//菜单的翻开afx_msg void OnOpen();具体实现1、由于我们的游戏的棋盘大小是一定的,不能改变大小的,是应该符合要求的。
在MFC框架下用OpenGL绘制金字塔及棋盘纹理

1应用程序功能要求使用多边形建模的方法构造一个金字塔(正四棱锥),并赋予黄金的黄铜材质使用Bezier曲面建模方法构建一块起伏的地板,并赋予绿色的塑料材质和类似国际象棋棋盘的纹理贴图且具有光照效果2建立工程运行VC++ 6.0,新建一个名为MyOpenGL的单文档MFC(Single document)项目,全部使用默认设置3设置环境参数通过菜单Project->Setting打开设置对话框,选择Link属性页,在Object/library modules 中输入opengl32.lib glu32.lib glaux.lib,注意中间以空格隔开,然后单击OK按钮,这是为了装载OpenGL编程所需的函数库。
也可以直接在CMyOpenGLView的头文件(即MyOpenGLView.h)中直接输入一下的编译参数:#pragma comment(lib,"opengl32.lib") //装载opengl32.lib#pragma comment(lib,"glu32.lib")#pragma comment(lib,"glaux.lib")上述设置与Setting对话框中直接设置的效果是一样的。
配置好链接库后,在CMyOpenGLView的头文件加入以下预处理命令:#include <gl/GL.H>#include <gl/GLU.H>#include <gl/GLAUX.H>之所以在MyOpenGLView.h中导入这些头文件,是因为整个图形编写过程都是在CMyOpenGLView中完成的。
完成上述配置后,就可以调用OpenGL的库函数了,否则会因为系统找不到OpenGL 的库函数,而出现一堆编译错误。
4设置窗口风格在CMyOpenGLView的PreCreateWindow()函数中,修改代码如下:BOOL CMyOpenGLView::PreCreateWindow(CREA TESTRUCT& cs){// TODO: Modify the Window class or styles here by modifying// the CREATESTRUCT cscs.style |=WS_CLIPSIBLINGS | WS_CLIPCHILDREN;//设置窗口风格,使得支持openglreturn CView::PreCreateWindow(cs);}5定义绘图上下文成员变量与Windows的DC(Device Context)类似,OpenGL使用了一个叫RC(Rendering Context)的上下文来进行绘图,所以必须为CMyOpenGLView添加一个HGLRC类型的成员变量m_hGLRC:HGLRC m_hGLRC; //OpenGL的绘图上下文(Rendering Context)6创建OpenGL绘图环境定义了m_hGLRC,接下来的工作便是在CMyOpenGLView类中的OnCreate()成员函数中创建它。
C#实现中国象棋【棋盘,棋子】

C#实现中国象棋【棋盘,棋⼦】本⽂是利⽤C# 实现中国象棋的棋盘绘制,以及初始化布局,并不实现中国象棋的对弈逻辑。
仅供学习参考使⽤。
思路:1. 绘制中国象棋棋盘,竖线九条,横线⼗条。
再中间绘制‘楚河’,‘汉界’ 。
2. 绘制棋⼦,然后将棋⼦布局在棋盘上即可。
涉及知识点:1. ⽤户控件:⽤于实现棋盘的绘制,重写 OnPaint(PaintEventArgs e) ⽅法。
2. Matrix:封装表⽰⼏何变换的 3x3 仿射矩阵。
本例中主要⽤于旋转绘制反⽅的‘汉界’。
3. GraphicsPath:表⽰⼀系列相互连接的直线和曲线。
本例中主要⽤于绘制圆形棋⼦。
效果图如下:(⼀)(⼆)核⼼代码棋盘核⼼代码如下:1protected override void OnPaint(PaintEventArgs e)2 {3base.OnPaint(e);45//初始化数组6 InitArrPieceInfo();78 Graphics g = e.Graphics;9int width = this.Width;10int height = this.Height;11int padding = this.Padding.All * 20;12int center = height / 2;//垂直中⼼位置13int s_width = (width - 2 * padding) / 8;//每⼀条横线的间距14int s_heigth = (height - 2 * padding) / 9;//每⼀条竖线的间距15int start_x = padding;//起始位置16int start_y = padding;//起始位置17 Pen pen = new Pen(Brushes.Black, 1.5f);18 Dictionary<string, string[]> dicNums = new Dictionary<string, string[]>();19 dicNums.Add("up", new string[9] { "1", "2", "3", "4", "5", "6", "7", "8", "9" });20 dicNums.Add("down", new string[9] { "九", "⼋", "七", "六", "五", "四", "三", "⼆", "⼀" });21 Font font = new Font("宋体", 12, FontStyle.Regular);22for (int i = 0; i < 9; i++)23 {24//竖线九条25 Point p0 = new Point(start_x + i * s_width, start_y);26 Point p1 = new Point(start_x + i * s_width, start_y + (s_heigth * 4));27 Point p2 = new Point(start_x + i * s_width, start_y + (s_heigth * 5));28 Point p3 = new Point(start_x + i * s_width, start_y + (s_heigth * 9));29 g.DrawLine(pen, p0, p1);30 g.DrawLine(pen, p2, p3);31//上下的⽂字32 Point p_up = new Point(start_x + i * s_width - 5, padding / 2);33 Point p_down = new Point(start_x + i * s_width - 5, start_y + (s_heigth * 9) + padding / 3);34 g.DrawString(dicNums["up"][i], font, Brushes.Black, p_up);35 g.DrawString(dicNums["down"][i], font, Brushes.Black, p_down);36//数组赋值37for (int j = 0; j < 10; j++)38 {39 Point absLocation = ArrPiece[i, j].AbsoluteLocation;40 absLocation.X = start_x + i * s_width;41 ArrPiece[i, j].AbsoluteLocation = absLocation;42 }44for (int i = 0; i < 10; i++)45 {46//横线⼗条47 Point p0 = new Point(start_x, start_y + i * s_heigth);48 Point p1 = new Point(start_x + s_width * 8, start_y + i * s_heigth);49 g.DrawLine(pen, p0, p1);50//数组赋值51for (int j = 0; j < 9; j++)52 {53 Point absLocation = ArrPiece[j, i].AbsoluteLocation;54 absLocation.Y = start_y + i * s_heigth;55 ArrPiece[j, i].AbsoluteLocation = absLocation;56 }57 }58//绘制九宫格59for (int i = 0; i < 2; i++)60 {61 Point p0 = new Point(start_x + (3 + i * 2) * s_width, start_y);62 Point p1 = new Point(start_x + (5 - i * 2) * s_width, start_y + (s_heigth * 2));63 Point p2 = new Point(start_x + (3 + i * 2) * s_width, start_y + (s_heigth * 7));64 Point p3 = new Point(start_x + (5 - i * 2) * s_width, start_y + (s_heigth * 9));65 g.DrawLine(pen, p0, p1);66 g.DrawLine(pen, p2, p3);67 }6869//兵和卒处有拐⾓,从左往右70for (int i = 0; i < 5; i++)71 {72int p_x = start_x + 2 * i * s_width;73int p_y = start_y + 3 * s_heigth;74 DrawCorner(g, pen, p_x, p_y);//兵75 p_y = start_y + 6 * s_heigth;76 DrawCorner(g, pen, p_x, p_y);//卒77 }78//炮处的拐⾓,从左往右79for (int i = 0; i < 2; i++)80 {81int p_x = start_x + (1 + 6 * i) * s_width;82int p_y = start_y + 2 * s_heigth;83 DrawCorner(g, pen, p_x, p_y);//炮84 p_y = start_y + 7 * s_heigth;85 DrawCorner(g, pen, p_x, p_y);//炮86 }87//绘制楚河汉界88 Point p_0 = new Point(2 * s_width, center - 25);89 Point p_1 = new Point(7 * s_width, center + 20);90 font = new Font("⽅正⾪⼆繁体", 30, FontStyle.Regular);91 g.DrawString("楚河", font, Brushes.Black, p_0);92 Matrix mtxSave = g.Transform;93 Matrix mtxRotate = g.Transform;94 mtxRotate.RotateAt(180, p_1);95 g.Transform = mtxRotate;96 g.DrawString("汉界", font, Brushes.Black, p_1);97 g.Transform = mtxSave;98//绘制外边框99 g.DrawRectangle(pen, 3, 3, width - 6, height - 6);100 g.DrawRectangle(pen, 5, 5, width - 10, height - 10);101 g.DrawRectangle(pen, 7, 7, width - 14, height - 14);102 }View Code棋⼦核⼼代码如下:1protected override void OnPaint(PaintEventArgs e)2 {3base.OnPaint(e);4 Graphics g = e.Graphics;5 GraphicsPath gPath = new GraphicsPath();6// Set a new rectangle to the same size as the button's ClientRectangle property.7 Rectangle rectangle = this.ClientRectangle;8 g.DrawEllipse(new Pen(this.FlatAppearance.BorderColor), rectangle);9 gPath.AddEllipse(rectangle);1011// Set the button's Region property to the newly created circle region.12this.Region = new Region(gPath);13 Rectangle inRect = new Rectangle(2, 2, this.Width - 4, this.Height - 3);14 g.FillEllipse(new SolidBrush(this.BackColor), rectangle);15 g.DrawEllipse(new Pen(Color.Black,2), inRect);1617 Font font = new Font("楷体", 25, FontStyle.Regular);18 g.DrawString(this.Text, font, new SolidBrush(this.ForeColor), 0,5);View Code 源码链接。
vc++6.0简易象棋制作

目录摘要 (1)关键字 (1)Abstract (1)Key words (1)1 概要设计 (1)1.1 设计分析 (1)1.1.1 课题背景 (1)1.1.2 主要功能 (2)1.1.3 软件信息 (2)1.2 软件流程图 (2)1.2.1 程序总体结构图 (2)1.2.2 键盘操作 (3)1.2.3 鼠标操作 (3)2 程序及说明 (4)2.1 背景色的设置 (4)2.2 大标题的制作 (6)2.3 棋盘生成 (7)2.4 走法生成 (9)2.5 棋子的生成 (11)2.6 走法生成 (14)2.6.1 車 (14)2.6.2 馬 (15)2.6.3 相(象) (17)2.6.4 仕(士) (17)2.6.5 帅(将) (17)2.6.6 炮(砲) (18)2.6.7 兵(卒) (19)2.7 鼠标键盘操作 (19)2.7.1 键盘操作 (19)2.7.2 鼠标操作 (23)2.8 消除闪烁 (24)2.9 初始状态恢复 (25)2.10 完成运行截图 (26)3 软件优缺点与运行维护 (26)3.1 优点 (26)3.2 缺点 (26)3.3 总结 (26)致谢 (26)参考文献 (27)中国象棋设计测控技术与仪器专业学生史彬指导教师陈梅摘要:中国象棋是我国历史悠久的智力对战游戏,随着计算机的普及,人们不再满足于手动的繁琐的木质棋子,而是希望有一个即开即用而且用完不用收拾收藏的象棋游戏,于是电子版的象棋游戏就应运而生了。
本设计采用Microsoft VC++6.0编程软件中的MFC编写了中国象棋小游戏,该程序包含背景色的设置,大标题的制作,棋盘生成,光标生成,棋子生成,走法生成,不仅可以鼠标操作,还可以用键盘操作,为操作提供了更多的选择性,本软件还进行了画面闪烁消除,视觉效果更加人性化。
操作简单,无需安装,即开即用,方便使用。
关键词:VC++6.0;MFC;消除闪烁;棋盘生成;走法生成The Design of Chinese ChessStudent majoring in Measuring and Control Technology and Instrumentations Shi BinTutor Chen MeiAbstract:Chinese chess has a long history as an intelligence against game in our country . With the popularity of computer, people are no longer satisfied with the tedious manual but hope to have a chess game with Open-and-Play without collection and story when finished.Then the chess game was born doorsteps. This design uses the Microsoft vc + + 6.0 programming in the software of MFC writing the little game of Chinese chess .This program includes the Settings of background color , headline making, chessboard generation, the cursor generation, the pieces to generate.It not only can use the mouse operation, but also can use the keyboard, providing operating more selective. The software also eliminates the screen flashing .So visual effect is more humane. The software has a lot of advantages ,such as simple operation, no installation, instant available, convenient using.Key words:VC++6.0;MFC;Eliminate flicker;Generation board;Moves generated引言象棋,又称中国象棋(英文现译作Chinese Chess)。
绘制国际象棋棋盘

目录一、设计任务,目的与要求 (1)1.设计内容: (1)2.设计目的: (1)3.设计要求: (1)二、概要设计 (1)1.设计流程: (1)三、运行结果及分析 (4)1.程序运行测试: (4)2.应用运行的结果: (5)四、源代码 (6)一、设计任务,目的与要求1.设计内容:在屏幕上绘制输出国际象棋棋盘,分别利用命令提示行和MFC制作输出。
2.设计目的:1)复习、巩固C++语言的基础知识,进一步加深对C++语言的理解和掌握;2)课程设计为将课本上的理论知识和实际有机的结合起来,锻炼分析解决实际问题的能力。
提高适应实际,实践编程的能力;3)加强学生的团队合作能力。
3.设计要求:1)对系统进行功能模块分析、控制模块分析正确,符合课题要求,实现相应功能;可以加以其他功能或修饰,使程序更加完善、合理;2)系统设计要实用,编程简练,可用,功能全面;3)说明书、流程图要清楚;4)记录设计情况(备查,也为编写设计说明书作好准备);5)要求采用模块化程序设计方法,要求上机调试通过和按设计报告格式;6)设计上交内容:设计报告一人一份(按格式书写),源程序文件。
二、概要设计1.设计流程:1)设计要求:(1)国际象棋棋盘是个正方形,由横纵各8格、颜色一深一浅交错排列的64个小方格组成,并且对奕时右下角为白色方格。
(2)能够写出相应的源程序代码,采用结构化、模块化程序设计方法,功能完善,界面要美观;(3)所设计的系统要求运行没有错误;(4)当程序运行时弹出一个界面,并显示棋盘;(5)最后经验收合格后,按要求写出课程设计报告。
2)运行环境:本设计使用的运行环境是Microsoft Visual C++ 6.0开发环境,所做的是基于MFC的打印国际象棋棋盘的应用程序。
3)总体设计:1.用命令提示行输出棋盘是横竖各8个方格排列而成的,将横定为排,纵定为列。
横向有8排编号为0到7,纵向亦有8列编号为0到7。
观察棋盘特点黑白相间可知排号与列号相加为偶数的是白色方块,反之为黑色方块。
MFC02_Visual Studio C++ MFC简单绘图程序

Visual studio C++ MFC 简单绘图程序-绘制国际象棋棋盘功能:由Button将绘制的棋盘显示于Picture Control中一、建立一个MFC功程序:1.启动Visual Studio C++;2.新建一个MFC对话框项目:New|Project|MFCAplication;中输入mfc_pic,然后点|OK;4.在Welcome to the MFC Application窗口:点next;5.在Application Type窗口:点dialoge base;然后点Finish;出现如图所示的窗口;6.删除窗口中的TODO…文本对象;二、向窗口添加一个Picture Control和一个Button共二个对象:1.修改Button的Caption属性为“绘制棋盘”,ID属性为IDC_BUTTON_DRAR;2.修改Picture Control的ID属性为IDC_PIC,建立关联变量名为picD;3.双击Button,添加如下代码:voidCmfcpicDlg::OnBnClickedButton1(){CRect r;intw,h,dxy,i,j;CWnd *pic = GetDlgItem(IDC_PIC); //取Picture的长宽信息GetDlgItem(IDC_PIC)->GetWindowRect(&r);w=r.Width();h=r.Height();dxy=(w<h?w:h)/8; //棋盘由8*8个方格组成CDC *pdc = pic->GetDC();pdc->Rectangle(r);CBrushmyBrush;pdc->Rectangle(0,0,8*dxy,8*dxy); //绘制外框线myBrush.CreateSolidBrush(RGB(0,0,0));for(i=0;i<8;i++){for(j=i%2;j<8;j+=2){r.left=i*dxy;r.top=j*dxy;r.right=(i+1)*dxy;r.bottom=(j+1)*dxy;pdc->FillRect(r,&myBrush); //绘制黑方格}}}4.为使图形美观,初始化图形大小为8的倍数,只需在BOOL Cmfc_picDlg::OnInitDialog()函数的定义体内“// TODO: Add extra initialization here”处加入如下代码即可;GetDlgItem(IDC_PIC)->MoveWindow(25,25,240,240,true);三、运行程序:按F5运行程序,得到如图所示的结果。
中国象棋源代码C#

//绘制棋盘上半部分的竖线 g.DrawLine(thinPen, new Point(_leftTop.X + _columnWidth * (col - 1), _leftTop.Y), new Point(_leftTop.X + _columnWidth * (col - 1), _leftTop.Y + 4 * _rowHeight)); //绘制棋盘下半部分的竖线 g.DrawLine(thinPen, new Point(_leftTop.X + _columnWidth * (col - 1), _leftTop.Y + 5 * _rowHeight), new Point(_leftTop.X + _columnWidth * (col - 1), _leftTop.Y + 9 * _rowHeight)); } //绘制链接楚河汉界左右两端的两条短竖线 g.DrawLine(thinPen, new Point(_leftTop.X, _leftTop.Y), new Point(_leftTop.X, _leftTop.Y + 9 * _rowHeight)); g.DrawLine(thinPen, new Point(_leftTop.X + 8 * _columnWidth, _leftTop.Y), new Point(_leftTop.X + 8 * _columnWidth, _leftTop.Y + 9 * _rowHeight)); //绘制上方大本营的交叉线 g.DrawLine(thinPen, new Point(_leftTop.X + 3 * _columnWidth, _leftTop.Y), new Point(_leftTop.X + 5 * _columnWidth, _leftTop.Y + 2 * _rowHeight)); g.DrawLine(thinPen, new Point(_leftTop.X + 5 * _columnWidth, _leftTop.Y), new Point(_leftTop.X + 3 * _columnWidth, _leftTop.Y + 2 * _columnWidth)); //绘制下方大本营交叉线 g.DrawLine(thinPen, new Point(_leftTop.X + 3 * _columnWidth, _leftTop.Y + 7 * _rowHeight), new Point(_leftTop.X + 5 * _columnWidth, _leftTop.Y + 9 * _rowHeight)); g.DrawLine(thinPen, new Point(_leftTop.X + 5 * _columnWidth, _leftTop.Y + 7 * _rowHeight), new Point(_leftTop.X + 3 * _columnWidth, _leftTop.Y + 9 * _rowHeight)); //书写“楚河”“汉界” Font font1 = new Font("隶书", (float)(_rowHeight * 0.8), FontStyle.Regular, GraphicsUnit.Pixel); SolidBrush fontBrush = new SolidBrush(Color.Black); g.DrawString("楚河", font1, fontBrush, new Point(_leftTop.X + _columnWidth, (int)(_leftTop.Y + 4.1 * _rowHeight))); g.DrawString("汉界", font1, fontBrush, new Point(_leftTop.X + 5 * _columnWidth, (int)(_leftTop.Y + 4.1 * _rowHeight))); //书写行数字编号 Font font2 = new Font("黑体", (float)(_rowHeight * 0.6), FontStyle.Regular, GraphicsUnit.Pixel);
精!!!C语言实现国际棋盘

C语言实现国际象棋盘的输出,不用画图头文件,不用Tutor 2.0,不用输出扩展的ASCI码,总之,一般的编译器都可以#include<windows.h>#include<stdio.h>void ConPrint(char*CharBuffer,int len);void ConPrintAt(int x,int y,char*CharBuffer,i nt len);void gotoXY(int x,int y);void ClearConsole(void);void ClearConsoleToColors(int ForgC,int BackC); void SetColorAndBackground(int ForgC,int BackC );void SetColor(int ForgC);void HideTheCursor(void);void ShowTheCursor(void);int main(int argc,char*argv[]){int i=0,j=0;HideTheCursor();ClearConsoleToColors(15,2);for(i=0;i<8;i++)for(j=0;j<8;j++){if(i%2==0){if(j%2==0){SetColorAndBackground(15,15); ConPrintAt(i,j,"\n",1);}else{SetColorAndBackground(1,16);ConPrintAt(i,j,"\n",1);} //红色为重点}else{if(j%2!=0){SetColorAndBackground(15,15); ConPrintAt(i,j,"\n",1);}else{SetColorAndBackground(1,16);ConPrintAt(i,j,"\n",1);}}}SetColorAndBackground(7,1);return0;}void ClearConsoleToColors(int ForgC,int BackC) {WORD wColor=((BackC&0x0F)<<4)+(ForgC&0x0 F);HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDL E);COORD coord={0,0};DWORD count;CONSOLE_SCREEN_BUFFER_INFO csbi;SetConsoleTextAttribute(hStdOut,wColor);if(GetConsoleScreenBufferInfo(hStdOut,&csbi )){FillConsoleOutputCharacter(hStdOut,(TCHAR) 32,csbi.dwSize.X*csbi.dwSize.Y,coord,&count );FillConsoleOutputAttribute(hStdOut,csbi.wA ttributes,csbi.dwSize.X*csbi.dwSize.Y,coord ,&count);SetConsoleCursorPosition(hStdOut,coord); }}void ClearConsole(){HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDL E);COORD coord={0,0};DWORD count;CONSOLE_SCREEN_BUFFER_INFO csbi;if(GetConsoleScreenBufferInfo(hStdOut,&csbi )){FillConsoleOutputCharacter(hStdOut,(TCHAR) 32,csbi.dwSize.X*csbi.dwSize.Y,coord,&count );FillConsoleOutputAttribute(hStdOut,csbi.wA ttributes,csbi.dwSize.X*csbi.dwSize.Y,coord ,&count);SetConsoleCursorPosition(hStdOut,coord); }}void gotoXY(int x,int y){COORD coord={x,y}; SetConsoleCursorPosition(GetStdHandle(STD_OU TPUT_HANDLE),coord);}void SetColor(int ForgC)WORD wColor;HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDL E);CONSOLE_SCREEN_BUFFER_INFO csbi;if(GetConsoleScreenBufferInfo(hStdOut,&csbi )){wColor=(csbi.wAttributes&0xF0)+(ForgC&0 x0F);SetConsoleTextAttribute(hStdOut,wColor);}}void SetColorAndBackground(int ForgC,int BackC) {WORD wColor=((BackC&0x0F)<<4)+(ForgC&0x0 F);;SetConsoleTextAttribute(GetStdHandle(STD_OUT PUT_HANDLE),wColor);}void ConPrint(char*CharBuffer,int len)DWORD count;WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE) ,CharBuffer,len,&count,NULL);}void ConPrintAt(int x,int y,char*CharBuffer,i nt len){DWORD count;COORD coord={x,y};HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDL E);SetConsoleCursorPosition(hStdOut,coord); WriteConsole(hStdOut,CharBuffer,len,&count, NULL);}void HideTheCursor(){CONSOLE_CURSOR_INFO cciCursor;HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDL E);if(GetConsoleCursorInfo(hStdOut,&cciCursor)) {cciCursor.bVisible=FALSE;SetConsoleCursorInfo(hStdOut,&cciCursor); }}void ShowTheCursor(){CONSOLE_CURSOR_INFO cciCursor;HANDLE hStdOut=GetStdHandle(STD_OUTPUT_HANDL E);if(GetConsoleCursorInfo(hStdOut,&cciCursor)) {cciCursor.bVisible=TRUE;SetConsoleCursorInfo(hStdOut,&cciCursor); }}。
C++ mfc五子棋

GetObject:
函数功能:该函数得到指定图形对象的信息,根据图形对象, 函数把填满的或结构,或表项(用于逻辑调色板)数目放入一个指定的缓冲区。 函数原型:int GetObject(HGDIOBJ hgdiobj, int cbBuffer, LPVOID lpvObject);
境(DC)。通过GetDc()获取的HDC直接与相关设备沟通,而本函数创建的DC, 则是与内存中的一个表面相关联。 函数原型:HDC CreateCompatibleDC(HDC hdc);
AddString:调用此成员函数添加一个字符串到列表框中。 EndDialog:函数功能:该函数清除一个模态对话框,并使系统中止对对话框的任 何处理。 函数原型:BOOL EndDialog(HWND hDlg,int nResult);
PlaySound简介
BOOL PlaySound(LPCSTR pszSound, HMODULE hmod,DWORD fdwSound); 参数pszSound是指定了要播放声音的字符串,该参数可以是WAVE文件的名字, 或是WAV资源的名字,或是内存中声音数据的指针,或是在系统注册表WIN.INI
{PlaySound((LPCTSTR)IDR_WAVE1, AfxGetInstanceHandle(), SND_RESOURCE | SND_ASYNC);}
常用函数说明
LoadBitmap:
函数功能:该函数从模块的可执行文件中加载指定的位图资源。 该函数已经被函数LoadImage替代。 函数原型:HBITMAP LoadBitmap(HINSTANCE hInstance, LPCTSTR lpBitmapName);
c++实现中国象棋(有行棋规则)

c++实现中国象棋(有行棋规则)// 读者需注意/* 用vc6.0时可能编译有错误,这时要设置vc编译器。
首先在菜单栏找到工程--设置--常规--Microsoft基础类,把“不使用MFC”改为使用MFC作为静态链接库*/#include#define IDM_MENU0 0#define IDM_MENU1 1#define IDM_MENU2 2#define IDM_MENU3 3#define IDM_ITEM0 10#define IDM_ITEM1 11#define IDM_ITEM2 12#define IDM_ITEM3 13#define IDM_ITEM4 14#define IDM_ITEM5 15#define IDM_ITEM6 16#define BING 1#define PAO 2#define JU 3#define MA 4#define XIANG 5#define SHI 6#define JIANG 7class CPlate{int m_ndx; // 棋盘格宽int m_ndy; // 棋盘格高int m_nLeft; // 棋盘位置int m_nT op;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 = 60;m_ndy = 55;m_nLeft = 198;m_nTop = 100;m_nRight = m_nLeft+m_ndx*8;m_nBottom = m_nTop+m_ndy*9;}// 显示棋盘void CPlate::ShowPlate(CDC *pDC){CBrush *pOldBrush, brushNormal, brushSelected;brushSelected.CreateSolidBrush(RGB(255,228,181));pOldBrush = pDC->SelectObject(&brushSelected);pDC->Rectangle(m_nLeft,m_nTop,m_nLeft+m_ndx*8,m_nT o p+m_ndy*9);pDC->SetBkMode(TRANSPARENT);CFont *pOldFont, fontInfo;fontInfo.CreateFont(40, 0, 0, 0, 40, FALSE, FALSE,0, OEM_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,DEFAULT_PITCH, "宋体");pOldFont = pDC->SelectObject(&fontInfo);pDC->SetT extColor(RGB(0,0,0));pDC->TextOut(220, 330,"漢 ");pDC->TextOut(260, 330,"界 ");pDC->TextOut(583, 330,"楚 ");pDC->TextOut(623, 330,"河 ");pDC->SelectObject(pOldFont);CPen penRed2(PS_SOLID,2,RGB(0,0,0));CPen *pOldPen = pDC->SelectObject(&penRed2);for(int i=0; i<10; i++){pDC->MoveTo(m_nLeft, m_nT op+i*m_ndy);pDC->LineT o(m_nRight, m_nTop+i*m_ndy);}pDC->MoveTo(m_nLeft, m_nT op-2);pDC->LineT o(m_nRight, m_nTop-2);pDC->MoveTo(m_nLeft, m_nT op+9*m_ndy+2);pDC->LineT o(m_nRight, m_nTop+9*m_ndy+2);for(i=0; i<9; i++){if(i==0 || i==8){pDC->MoveTo(m_nLeft+i*m_ndx, m_nTop);pDC->LineT o(m_nLeft+i*m_ndx, m_nBottom);pDC->MoveTo(m_nLeft+i*m_ndx-2, m_nTop);pDC->LineT o(m_nLeft+i*m_ndx-2, m_nBottom);}else{pDC->MoveTo(m_nLeft+i*m_ndx, m_nTop);pDC->LineT o(m_nLeft+i*m_ndx, m_nTop+4*m_ndy); pDC->MoveTo(m_nLeft+i*m_ndx, m_nTop+5*m_ndy); pDC->LineT o(m_nLeft+i*m_ndx, m_nBottom);}}// 画棋盘斜线pDC->SelectObject(pOldPen);CPen penRed1(PS_SOLID,1,RGB(0,0,0));pOldPen = pDC->SelectObject(&penRed1);pDC->MoveTo(m_nLeft+3*m_ndx, m_nT op);pDC->LineT o(m_nLeft+5*m_ndx,m_nTop+2*m_ndy);pDC->MoveTo(m_nLeft+3*m_ndx, m_nT op+2*m_ndy); pDC->LineT o(m_nLeft+5*m_ndx, m_nTop);pDC->MoveTo(m_nLeft+3*m_ndx, m_nBottom);pDC->LineT o(m_nLeft+5*m_ndx, m_nBottom-2*m_ndy);pDC->MoveTo(m_nLeft+3*m_ndx, m_nBottom-2*m_ndy);pDC->LineT o(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(0,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->LineT o(m_nLeft+col*m_ndx+ 3, m_nTop+row*m_ndy-3);pDC->LineT o(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->LineT o(m_nLeft+col*m_ndx+ 3, m_nTop+row*m_ndy+3);pDC->LineT o(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->LineT o(m_nLeft+col*m_ndx- 3, m_nT op+row*m_ndy-3);pDC->LineT o(m_nLeft+col*m_ndx-10, m_nT op+row*m_ndy-3);pDC->MoveTo(m_nLeft+col*m_ndx- 3, m_nTop+row*m_ndy+10);pDC->LineT o(m_nLeft+col*m_ndx- 3, m_nTop+row*m_ndy+3);pDC->LineT o(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;}// 棋子类// 定义棋子名称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;}int GetCol(){return m_nCol;}int GetRow(){return m_nRow;}BOOL GetRed(){return m_bRed;}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;}BOOL QiZi(int row,int col){if(m_nCol==col&&m_nRow==row&&m_bShow)return TRUE;elsereturn FALSE;}};//棋子类的成员函数// 棋子类的构造函数:初始化棋子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 = 28;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, 1, RGB(128,0,0));CBrush *pOldBrush, brushNormal, brushSelected;brushNormal.CreateSolidBrush(RGB(222,184,135));brushSelected.CreateSolidBrush(RGB(127,255,212));pOldPen = pDC->SelectObject(&penNormal);// 被选中的棋子颜色不同if(m_bSelected)pOldBrush = pDC->SelectObject(&brushSelected);elsepOldBrush = pDC->SelectObject(&brushNormal);// 显示棋子CRect r(m_rectStone);r.left ++;r.top ++;r.right --;r.bottom--;pDC->Ellipse(r);r.left ++;r.top ++;r.right --;r.bottom--;r.left ++;r.top ++;r.right --;r.bottom--;pDC->Ellipse(r);pDC->SelectObject(pOldPen);pDC->SelectObject(pOldBrush);// 准备显示棋子名称的字体CFont *pOldFont, fontStone;fontStone.CreateFont(47, 0, 0, 0, 600, 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->SetT extColor(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; // 信息显示位置BOOL m_turn;public:CMyWnd(){InitGame();}void InitGame();BOOL KillSelfStone(int col, int row, BOOL red);void Show(CDC *pDC);int KillEnemy(int col, int row, BOOL red);void MoveStone(CPoint point);void SelectStone(CPoint point);void Switch(CPoint);void BingGo(CPoint point);void PaoGo(CPoint point);void JuGo(CPoint point);void MaGo(CPoint point);void XiangGo(CPoint point);void ShiGo(CPoint point);void JiangGo(CPoint point);BOOL MoveTo(int, int);int OnCreate( LPCREATESTRUCT lpCreateStruct );BOOL HaveQi(int row,int col);BOOL OnCommand(WPARAM wParam, LPARAM lParam); void CMyWnd::Go(CPoint point,int col,int row);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_CREATE()ON_WM_LBUTTONDOWN()END_MESSAGE_MAP()// 框架窗口类的成员函数int CMyWnd::OnCreate( LPCREATESTRUCT lpCreateStruct ) {CMenu MyMenu0,MyMenu1;//下面这几条创建下拉菜单MyMenu1.CreateMenu();MyMenu1.AppendMenu(MF_STRING,IDM_ITEM0,"拷贝");MyMenu1.AppendMenu(MF_STRING,IDM_ITEM1,"剪切");MyMenu1.AppendMenu(MF_STRING,IDM_ITEM2,"粘贴");MyMenu1.AppendMenu(MF_STRING,IDM_ITEM4,"全选");MyMenu1.AppendMenu(MF_STRING,IDM_ITEM6,"删除");//下面这两条创建鼠标未点击时看到的那个菜单//其中第二句将下拉菜单张贴到第一个菜单上。
中国象棋人机对弈

中国象棋人机对弈[摘要]文章主要是研究中国象棋的人机对弈,包括象棋的界面和引擎部分。
界面主要是方便人与电脑进行交互的可视化界面。
界面包括棋盘区、菜单项和功能按钮区。
主要实现棋子的移动、悔棋、记录棋谱、难度选择等选项功能。
引擎部分主要包括,棋子棋盘的表示即数据结构,走法的生成,局面优劣的评估即评估函数,搜索算法及其优化和改进。
界面的设计是采用MFC的框架来实现界面部分,MFC是微软公司提供的一个类库,以C++类的形式封装了Windows的API,并且包含一个应用程序框架,以减少应用程序开发人员的工作量,其中包含大量的Windows句柄封装类和很多Windows控件和组件的封装类。
象棋对弈其实是一种博弈。
双人对弈,轮流走步;信息完备,双方得到的信息都是一样的;零和,即对一方有利的棋,对另一方肯定是不利的,不存在对双方均有利或无利的棋。
如果轮到自己落子的时候,一定会选择使局面分数最高的着法,如果轮到对手落子,他一定会选择使你得分最低的局面。
这就是我们经常听到的极大极小值搜索,而对局面进行估分的函数就是评估函数。
[主题词]博弈树;极大极小值搜索;alpha-beta剪枝;评估函数Chinese chess computer gameNetwork Engineering[Abstract]This paper mainly explores the Chinese chess computer game,it includes user interface and game engine.UI is a visual interface which helps human to communicate with computer.UI includes the board area,the menu and commonly used buttons.Its functions include pieces move,undoing,saving game record,choosing level and so on.The game engine mainly includes the form of pieces and board,that is data structure,move generaion,evaluation function,search algorithm.The UI is implemented through MFC.MFC is a class library provided by Microsoft.It encapsulates a Windows API in the form of c++ class,and includes a application framework,and reduces the workload of programmers.Chinese chess is a zero-sum game.Two people play,take turns to move piece;Information is the same to the both sides.There is no favorable or bad situation for both parties.If it is your turn,you will choose the favorable situation,in the same way,the opponent will choose the bad situation for you.This thought is called minimax algorithm,the function for estimating is called evaluation function.[Key Words]Game Tree;Minimax Search;Alpha-Beta Pruning;Evaluation Function目录1.综述 (1)1.1选题的意义 (1)1.2国内外研究现状概述 (1)1.3主要研究内容 (2)2.数据结构 (4)2.1棋盘的表示 (4)2.2棋子的表示 (5)3.棋子的走法 (7)4.评估函数 (8)5.搜索算法 (10)5.1极大极小值搜索算法 (10)5.2 alpha-beta剪枝算法 (12)5.3 alpha-beta剪枝算法的改进 (13)6.界面的实现 (15)6.1棋盘区 (15)6.2菜单项的设计 (16)6.3常用按钮的设计 (17)7.开局库 (18)8.系统的实现 (19)9.总结 (26)参考文献 (27)声明 (28)致谢 (29)1.综述1.1选题的意义中国象棋在中国拥有悠久的历史,这个游戏需要两个人进行对弈。
使用C++MFC编写一个简单的五子棋游戏程序

使⽤C++MFC编写⼀个简单的五⼦棋游戏程序MFC简介: MFC(MicrosoftFoundationClasses)是微软基础类库的简称,是微软公司实现的⼀个c++类库,主要封装了⼤部分的windows API函数。
MFC除了是⼀个类库以外,还是⼀个框架,在vc++⾥新建⼀个MFC的⼯程,开发环境会⾃动帮你产⽣许多⽂件,同时它使⽤了mfcxx.dll。
xx是版本,它封装了mfc内核,所以你在你的代码看不到原本的SDK编程中的消息循环等等东西,因为MFC框架帮你封装好了,这样你就可以专⼼的考虑你程序的逻辑,⽽不是这些每次编程都要重复的东西,但是由于是通⽤框架,没有最好的针对性,当然也就丧失了⼀些灵活性和效率。
但是MFC的封装很浅,所以效率上损失不⼤。
开始正⽂简述在学习了C++ MFC编程后,就萌⽣了编写⼀个五⼦棋程序的想法。
诚然,⽤C#来编写的话可能界⾯会好很多,但是我的主要⽬标不在界⾯⽽是算法,所以利⽤了VS的MFC来编写程序。
程序特点⿏标移动过程中有选中框跟随,下棋位置实时⾃动反馈选中框位置只会在合法的下棋位置出现(即棋线的交叉处并且没有下过的位⼦),所以即使你的⿏标不在棋线交叉处,程序也会实时⾃动反馈⿏标在当前位置将要下棋的位置,这避免了下棋过程中的错下。
提供悔棋功能为了进⼀步避免错下,提⾼程序的可⽤性,本程序还设计了⼀个悔棋按钮(程序设置最⼤悔棋次数为1次)提供多种棋盘⼤⼩标准的五⼦棋棋盘为15x15,为了提供更多样的棋盘⼤⼩,本程序还设有17x17和19x19两种⾮常规棋盘。
提供复盘功能在棋局结束后,程序会给出棋局的复盘结果,可以从中分析出棋局胜败的原因。
解决了MFC绘图窗⼝移出屏幕图像消失的问题你可以在完整代码中看到解决刷新问题的解决⽅案代码较为基础,只需要掌握基础的MFC编程知识就可以⾃⼰实现因为本⼈也是编程⼩⽩,算法设计⽐较基础和简单,只掌握基础的C++编程知识就可以看懂代码程序实现step1:在VS中创建⼀个MFC对话框程序⼯程。
中国象棋源代码及文档

摘要象棋程序可以分为两大部分:人工智能和界面程序辅助。
人工智能的主要部分,反映了计算机下象棋的想法,电脑怎么想,最好的方法去完成下一步,优先搜索算法进行搜索,和各种可能的行动,评估,选择胜利面最大的一步;界面和程序协助部分主要是界面友好,以更好地适应用户下象棋的想法。
本文研究了中国象棋在电脑中如何表示,然后讨论如何产生走法的一系列相关技术。
使用MFC架构和Visual C + +开发工具,实现一定技能的中国象棋人机对弈功能。
关键词:中国象棋;人工智能;下棋Title The Design And Realize of human-computerChess GameAbstractChess program can be divided into two major auxiliary artificial intelligence and interface program. The AI part of the main reflected computer chess ideas, computer how to think and go to the best method to complete the next step, first search algorithm to search, and a variety of possible moves, valuations, choose victory surface step; the interface and the program assisted some of the major user-friendly game of chess by the previous step, to better adjust the chess ideas.This paper studies the Chinese chess computer, and then discuss how to generate a series of related moves. MFC architecture and Visual C development tools, to achieve a certain chess skills, Chinese chess, human-computer chess program.Keywords: Chess; artificial intelligence; chess目次1 引言 (1)1.1 象棋设计背景和研究意义 (1)1.2象棋设计研究方法 (1)2 需求分析 (3)2.1 界面要求 (3)2.2规则要求 (3)2.3判定输赢 (4)3系统设计与实现 (6)3.1系统模块划分 (6)3.2系统主要流程图 (6)3.3相关数据定义 (8)4 系统测试运行 (10)4.1测试方案设计 (10)4.2测试过程及结果 (10)4.3系统的优缺点分析及改进方案 (11)4.4系统性能分析 (11)结论 (12)致 (13)参考文献 (14)1引言1.1 象棋设计背景和研究意义经过二十年的发展,电脑游戏行业已成为最重要的工作之一,以跟上全球电影,电视,音乐和其他娱乐行业,其年销售额超过好莱坞的全年收入的步伐。
基于MFC的对战象棋设计与实现

毕业设计说明书学生姓名学号学院计算机科学与技术学院专业软件工程题目基于MFC的对战象棋设计与实现指导教师(姓名)(专业技术职称/学位)毕业论文独创性声明本人郑重声明:本论文是我个人在导师指导下进行的研究工作及取得的研究成果。
本论文除引文外所有实验、数据和有关材料均是真实的。
尽我所知,除了文中特别加以标注和致谢的地方外,论文中不包含其他人已经发表或撰写过的研究成果。
其他同志对本研究所做的任何贡献均已在论文中作了明确的说明并表示了谢意。
作者签名:日期:摘要:中国象棋是我国历史悠久的智力对战游戏,发展至今已有数千年的历史,是中华民族智慧的结晶。
然而,传统象棋存在着棋盘单一、棋子收拾繁琐、下棋场地单一、无法及时找到对弈玩家几大弊端。
随着计算机技术的发展,电子版象棋游戏的诞生,很好的解决了这些问题。
本设计正是开发这样一款电子象棋游戏,它采用MFC文档视图体系结构和Visual C++开发工具,实现了具有背景棋盘和棋子种类的变换,走棋,悔棋,还原和网络对弈功能。
本设计还进行了画面闪烁消除,视觉效果更加人性化,且鼠标操作,简单易用,无须安装,即开即用。
关键词:中国象棋,MFC文档视图,Visual C++,网络对弈,消除闪烁,即开即用Abstract:Chinese Chess is our long history of intellectual battle game, the development has several thousand years of history, is the crystallization of the wisdom of the Chinese nation. However, the traditional chess there is a single board, the pieces pack cumbersome, chess venues single, not been able to find several major drawbacks of the chess players. With the development of computer technology, the electronic version of the birth of a chess game, a good solution to these problems.The design is the development of an electronic chess game, which uses MFC document view architecture and Visual C + + development tools, background board and pieces kind of transformation, playing chess, undo, restore, and network chess function. This design also flickering eliminate, visual effects, more human, mouse operation, easy to use, no need to install.Keywords:Chinese chess, MFC document view, Visual C++, network game, eliminate flicker, instant available目录1 绪论 (4)1.1 课题背景 (4)1.2 象棋简介 (4)1.3 MFC及相关技术 (4)2 概要设计 (4)2.1 设计思路分析 (4)2.2 主要功能 (4)2.3 软件信息 (4)2.4 流程图 (4)3 程序详细设计说明 (4)3.1 界面设计 (4)3.2 数据结构 (4)3.3 棋子走法 (4)3.3.1 車 (4)3.3.2 馬 (4)3.3.3 相(象) (4)3.3.4 仕(士) (4)3.3.5 帅(将) (4)3.3.6 炮(砲) (4)3.3.7 兵(卒) (4)3.4 功能 (4)3.4.1 走棋 (4)3.4.2 悔棋 (4)3.4.3 还原 (4)3.4.4 认输 (4)3.4.5 摆棋 (4)3.5 网络 (4)3.6 关键技术 (4)3.6.1 遮罩技术 (4)3.6.2 双缓冲技术 (4)4 总结和进一步的研究工作 (4)结束语 (4)参考文献 (4)致谢 (4)1 绪论1.1 课题背景随着时代的进步,电子技术日新月异的发展,单纯的户外实体游戏已经无法满足人们的需求,于是电子游戏诞生了,它创造出的虚拟游戏环境,帮助人们摆脱实体的束缚,更增添了游戏的趣味性。
简单的c#winform象棋游戏(附带源码)

简单的c#winform象棋游戏(附带源码)整体思路:⽤⼆维数组构建棋盘每⼀个数组元素封装为⼀个picturebox附带若⼲属性(例如:棋⼦归属⽅、棋⼦的类型),⽤⼀个抽象基类规定基本的棋⼦移动规则(例如:不能选中空⽩picturebox、该红⽅⾛棋时不能选中蓝⽅棋⼦),具体的棋⼦单独从基类棋⼦类派⽣ 重写派⽣类的⽅法规定其⾛棋规则,和相应填充picturebox的图⽚(例如: 炮不能斜着⾛不能直线吃⼦,翻⼭炮必须吃⼦且移动路径上只能有⼀个棋⼦)窗体所⽤控件:lable、timer、picturebox1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Windows.Forms;6using System.Drawing;78namespace象棋_封_9{10enum chess_type11 {12 blank,13 jiang,14 che,15 ma,16 pao,17 xiang,18 zu,19 shi20 };//棋类的枚举类型21enum player_type22 {23 blank,24 red,25 blue,26 };//玩家类别的枚举类型27abstract class Chess28 {29private static Chess[] cover_blue =new Chess[16];//被吃区30private static int r=0;31private static Chess[] cover_red = new Chess[16];//被吃区32private static int f=0;33protected static int chosenX;34protected static int chosenY;35public static bool chosen;36static int n = 1;37public static player_type control_side;38public PictureBox PB;39public chess_type type;40public player_type side;41public Chess()42 {43 side = player_type.blank;44 type = chess_type.blank;45 }46public abstract bool Move_judge(object sender, int X, int Y, Chess[][] checkerboard); 47public abstract void Put_picture();48//public void Assignment(chess_type ct, player_type pt, Image pic)49//{50// side = pt;51// type = ct;52// PB.Image = pic;53//}//给棋⼦赋值54public void Bg_Tored()55 {56this.PB.BackColor = Color.Red;57 }//背景变为红⾊58public void Bg_Toblank()59 {60this.PB.BackColor = Color.Transparent;61 }//背景变为⽩⾊62private void Chess_Toblank(ref Chess a, PictureBox PB)63 {64this.Bg_Toblank();65this.PB.Image = null;66 a = new Chess_blank(PB);67 }//棋⼦属性清空68public static void Clearground(Main f, ref Chess[][] checkerboard)//布置战场69 {70int i, j;71 Control[] col_blue=null;72 Control[] col_red=null;73 Chess.control_side = player_type.red;74 Chess.chosen = false;75//*********************初始化被吃区**************//76for (i = 0; i < 16; i++)77 {78 cover_blue[i] = new Chess_blank();79 cover_red[i] = new Chess_blank();80 }81for (i = 1; i <= 16; i++)82 {83 col_blue = f.Controls.Find("Cover_PBox_blue" + i, false);//检索控件84 col_red = f.Controls.Find("Cover_PBox_red" + i, false);//检索控件85 cover_blue[i - 1].PB = col_blue[0] as PictureBox;86 cover_red[i - 1].PB = col_red[0] as PictureBox;87 }88//*********************初始话被吃区**************//8990//*****************构建棋盘********************//91 checkerboard = new Chess[10][];92for (i = 0; i < 10; i++)93 {94 checkerboard[i] = new Chess[9];//10⾏9列95 }96for (i = 0; i < 10; i++)97for (j = 0; j < 9; j++)98 {99 checkerboard[i][j] = new Chess_blank();100 }101 checkerboard[0][0] = new Chess_che(player_type.blue);102 checkerboard[0][1] = new Chess_ma(player_type.blue);103 checkerboard[0][2] = new Chess_xiang(player_type.blue);104 checkerboard[0][3] = new Chess_shi(player_type.blue);105 checkerboard[0][4] = new Chess_jiang(player_type.blue);106 checkerboard[0][5] = new Chess_shi(player_type.blue);107 checkerboard[0][6] = new Chess_xiang(player_type.blue);108 checkerboard[0][7] = new Chess_ma(player_type.blue);109 checkerboard[0][8] = new Chess_che(player_type.blue);110 checkerboard[2][1] = new Chess_pao(player_type.blue);111 checkerboard[2][7] = new Chess_pao(player_type.blue);112 checkerboard[3][0] = new Chess_zu(player_type.blue);113 checkerboard[3][2] = new Chess_zu(player_type.blue);114 checkerboard[3][4] = new Chess_zu(player_type.blue);115 checkerboard[3][6] = new Chess_zu(player_type.blue);116 checkerboard[3][8] = new Chess_zu(player_type.blue);117118119 checkerboard[6][0] = new Chess_zu(player_type.red);120 checkerboard[6][2] = new Chess_zu(player_type.red);121 checkerboard[6][4] = new Chess_zu(player_type.red);122 checkerboard[6][6] = new Chess_zu(player_type.red);123 checkerboard[6][8] = new Chess_zu(player_type.red);124 checkerboard[7][1] = new Chess_pao(player_type.red);125 checkerboard[7][7] = new Chess_pao(player_type.red);126 checkerboard[9][0] = new Chess_che(player_type.red);127 checkerboard[9][1] = new Chess_ma(player_type.red);128 checkerboard[9][2] = new Chess_xiang(player_type.red);129 checkerboard[9][3] = new Chess_shi(player_type.red);130 checkerboard[9][4] = new Chess_jiang(player_type.red);131 checkerboard[9][5] = new Chess_shi(player_type.red);132 checkerboard[9][6] = new Chess_xiang(player_type.red);133 checkerboard[9][7] = new Chess_ma(player_type.red);134 checkerboard[9][8] = new Chess_che(player_type.red);135for (i = 0; i < 10; i++)136for (j = 0; j < 9; j++)//control 是⼀个存放控件类的容器137 {138 Control[] col = f.Controls.Find("pictureBox" + (i * 9 + j + 1), false);//检索控件139 checkerboard[i][j].PB = col[0] as PictureBox;//类型转换140 checkerboard[i][j].PB.Location = new Point(170 + 60 * j, 57 * i);141 }142for (i = 0; i < 10; i++)143for (j = 0; j < 9; j++)144 {145 checkerboard[i][j].Put_picture();146 }147//*****************构建棋盘********************//148 }149protected static int Getx(object sender)150 {151int x;152string name = (sender as PictureBox).Name;153string number = name.Substring(10);//在字符串中从指定位开始检索(这⾥是在x后也就是picturebox控件ID不同的尾号)154int index = Convert.ToInt32(number);155 x = (index - 1) / 9;//列156return x;157 }158protected static int Gety(object sender)159 {160int y;161string name = (sender as PictureBox).Name;162string number = name.Substring(10);//在字符串中从指定位开始检索(这⾥是在x后也就是picturebox控件ID不同的尾号)163int index = Convert.ToInt32(number);164 y = (index - 1) % 9;//⾏165return y;166 }167public static void Nochozen_dispose(object sender, Chess[][] checkerboard)168 {169 Chess.chosen = true;170 chosenX = Chess.Getx(sender);171 chosenY = Chess.Gety(sender);172if (checkerboard[chosenX][chosenY].side != control_side)//选择的不是当前应该⾛棋的⼀⽅173 {174 chosen = false;175return;176 }177if (checkerboard[chosenX][chosenY].side != player_type.blank)//选择的不是空⽩178 checkerboard[chosenX][chosenY].Bg_Tored();179return;180 }181public static bool Chozen_dispose(object sender, Chess[][] checkerboard)182 {183 Chess.chosen = false;184int x= Getx(sender);185int y= Gety(sender);186if (checkerboard[chosenX][chosenY].side == checkerboard[x][y].side)//移动位置与选择位是同⼀阵营187 {188 checkerboard[chosenX][chosenY].Bg_Toblank();189return false;190 }191if (checkerboard[chosenX][chosenY].side == player_type.blank)//选中的是空⽩192return false;193if (checkerboard[chosenX][chosenY].type == chess_type.ma || checkerboard[chosenX][chosenY].type == chess_type.xiang || checkerboard[ chosenX][chosenY].type == chess_type.shi)//马象⼠的移动不能同y或同x194 {195if (chosenX == x || chosenY == y)196 {197 checkerboard[chosenX][chosenY].Bg_Toblank();198return false;199 }200 }201else202 {203if (chosenX != x && chosenY != y)//其他棋类都必须同x或同y移动204 {205 checkerboard[chosenX][chosenY].Bg_Toblank();206return false;207 }208 }209210if (!checkerboard[chosenX][chosenY].Move_judge(sender, x, y, checkerboard))211 {212 checkerboard[chosenX][chosenY].Bg_Toblank();213return false;214 }215if (Chess.Setmove(checkerboard, x, y))216return true;217else218return false;219 }220static bool Setmove(Chess[][] checkerboard,int X,int Y)221 {222 n++;223bool over = false;224 player_type z=player_type.blank;225if (checkerboard[X][Y].type == chess_type.jiang)226 {227 over = true;228 z = checkerboard[X][Y].side;229 }230if (checkerboard[X][Y].PB.Image!=null)231 Cover(checkerboard[X][Y]);232 Chess.Change(ref checkerboard[Chess.chosenX][Chess.chosenY],ref checkerboard[X][Y]); 233 Chess.chosen = false;234if (over)235 {236if (z == player_type.blue)237 MessageBox.Show("红⽅胜利");238else239 MessageBox.Show("蓝⽅胜利");240 End f = new End(Time.fen + "分" + Time.miao + "秒");241 f.Show();242return true;243 }244if (n % 2 == 0)245 Chess.control_side = player_type.blue;246else247 Chess.control_side = player_type.red;248return false;249 }250private static void Change(ref Chess a,ref Chess b)//a⼦位移到b⼦251 {252 Chess c = new Chess_blank();253 c.PB = b.PB;254switch (a.type)255 {256case chess_type.zu:257 b = new Chess_zu(a.side);break;258case chess_type.pao:259 b = new Chess_pao(a.side);break;260case chess_type.che:261 b = new Chess_che(a.side); break;262case chess_type.ma:263 b = new Chess_ma(a.side); break;264case chess_type.xiang:265 b = new Chess_xiang(a.side); break;266case chess_type.shi:267 b = new Chess_shi(a.side); break;268case chess_type.jiang:269 b = new Chess_jiang(a.side); break;270 }271 b.type = a.type;272 b.side = a.side;273 b.PB = c.PB;274 b.PB.Image = a.PB.Image;275 a.Chess_Toblank(ref a,a.PB);276 }277private static void Cover(Chess a)278 {279if (a.side == player_type.blue)280 {281 Change(ref a, ref cover_red[r]);282 r++;283 }284else285 {286 Change(ref a, ref cover_blue[f]);287 f++;288 }289290 }//棋⼦被吃后移动到被吃区291292 }293 }棋⼦抽象基类1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Windows.Forms;67namespace象棋_封_8{9class Chess_blank : Chess10 {11public Chess_blank()12 {13base.type = chess_type.blank;14base.side = player_type.blank;15 }16public Chess_blank(PictureBox PB)17 {18base.type = chess_type.blank;19base.side = player_type.blank;20this.PB = PB;21 }22public override bool Move_judge(object sender, int X, int Y, Chess[][] checkerboard) 23 {24return false;25 }26public override void Put_picture()27 {28this.PB.Image = null;29 }30 }31 }空⽩属性棋⼦类1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;56namespace象棋_封_7{8class Chess_che:Chess9 {10public Chess_che(player_type b)11 {12base.type = chess_type.che;13base.side = b;14 }15public override bool Move_judge(object sender, int X, int Y, Chess[][] checkerboard) 16 {17int i, j, k;18if (Chess.chosenX == X)19 {20 i = Chess.chosenY < Y ? Chess.chosenY : Y;21 j = Chess.chosenY > Y ? Chess.chosenY : Y;22for (k = i + 1; k < j; k++)23 {24if (checkerboard[X][k].side != player_type.blank)//在移动路径上有棋⼦情况25 {26return false;27 }28 }29 }30if (Chess.chosenY == Y)31 {32 i = Chess.chosenX < X ? Chess.chosenX : X;33 j = Chess.chosenX > X ? Chess.chosenX : X;34for (k = i + 1; k < j; k++)35 {36if (checkerboard[k][Y].side != player_type.blank)37 {38return false;39 }40 }41 }42return true;43 }44public override void Put_picture()45 {46if (base.side == player_type.red)47this.PB.Image = global::象棋_封_.Properties.Resources.红⾞;48else49this.PB.Image = global::象棋_封_.Properties.Resources.蓝⾞;50 }51 }52 }车类1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;56namespace象棋_封_7{8class Chess_jiang:Chess9 {10public Chess_jiang(player_type b)11 {12base.type = chess_type.jiang;13base.side = b;14 }15public override bool Move_judge(object sender, int X, int Y, Chess[][] checkerboard)16 {17int i, j, k;18if (checkerboard[X][Y].type == chess_type.jiang && Chess.chosenY == Y)//王吃王情况19 {20 i = Chess.chosenX < X ? Chess.chosenX : X;//棋⼦移动的起点⾏21 j = Chess.chosenX > X ? Chess.chosenX : X;//棋⼦移动的结束⾏22for (k = i + 1; k < j; k++)23 {24if (checkerboard[k][Y].side != player_type.blank)//中间有棋⼦情况25 {26return false;27 }28 }29return true;30 }31if (this.side == player_type.blue)32 {3334if (Y < 3 || Y > 5 || X > 2)//当控制者是蓝⾊⽅限制将的移动范围35 {36return false;37 }38 }39else40 {41if (Y < 3 || Y > 5 || X < 7)//当控制者是红⾊⽅限制将的移动范围42 {43return false;44 }45 }46if ((Chess.chosenX - X) * (Chess.chosenX - X) + (Chess.chosenY - Y) * (Chess.chosenY - Y) != 1)//点到点距离x平⽅加y平⽅47 {48return false;49 }50return true;51 }52public override void Put_picture()53 {54if (base.side == player_type.red)55this.PB.Image = global::象棋_封_.Properties.Resources.红帥;56else57this.PB.Image = global::象棋_封_.Properties.Resources.蓝将;58 }59 }60 }将类1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;56namespace象棋_封_7{8class Chess_ma:Chess9 {10public Chess_ma(player_type b)11 {12base.type = chess_type.ma;13base.side = b;14 }15public override bool Move_judge(object sender, int X, int Y, Chess[][] checkerboard)16 {17if (Math.Abs(Chess.chosenX - X) == 1 && Math.Abs(Chess.chosenY - Y) == 2)//移动X⽅向绝对值为1y⽅向为218 {19if (checkerboard[Chess.chosenX][Chess.chosenY + (Y - Chess.chosenY) / Math.Abs(Y - Chess.chosenY)].side != player_type.blank)//不是撇脚马20 {21return false;22 }23 }24else if (Math.Abs(Chess.chosenX - X) == 2 && Math.Abs(Chess.chosenY - Y) == 1)//移动y⽅向绝对值为2x⽅向为125 {26if (checkerboard[Chess.chosenX + (X - Chess.chosenX) / Math.Abs(X - Chess.chosenX)][Chess.chosenY].side != player_type.blank)//不是撇脚马27 {28return false;29 }30 }31else32 {33return false;34 }35return true;36 }37public override void Put_picture()38 {39if (base.side == player_type.red)40this.PB.Image = global::象棋_封_.Properties.Resources.红马;41else42this.PB.Image = global::象棋_封_.Properties.Resources.蓝马;43 }44 }45 }马类1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;56namespace象棋_封_7{8class Chess_pao:Chess9 {10public Chess_pao(player_type b)11 {12base.type = chess_type.pao;13base.side = b;14 }15public override bool Move_judge(object sender, int X, int Y, Chess[][] checkerboard)16 {17int p = 0;//⽤于记录路径上的棋⼦数18int i, j, k;19if (Chess.chosenX == X)//同列移动情况20 {21 i = Chess.chosenY < Y ? Chess.chosenY : Y;//定起始点22 j = Chess.chosenY > Y ? Chess.chosenY : Y;//定起始点23 p = 0;24for (k = i + 1; k < j; k++)25 {26if (checkerboard[X][k].side != player_type.blank)27 {28 p++;//移动路径上有棋⼦的个数29 }30 }31if (p > 1)32 {33return false;34 }35 }363738else if (Chess.chosenY == Y)//同⾏移动情况39 {40 i = Chess.chosenX < X ? Chess.chosenX : X;41 j = Chess.chosenX > X ? Chess.chosenX : X;42 p = 0;43for (k = i + 1; k < j; k++)44 {45if (checkerboard[k][Y].side != player_type.blank)46 {47 p++;48 }49 }50if (p > 1)51 {52return false;53 }54 }55else56 {57return false;58 }59if (p == 0 && checkerboard[X][Y].side != player_type.blank)//中间没有棋⼦炮不能吃⼦60 {61return false;62 }63if (p == 1 && checkerboard[X][Y].side == player_type.blank)//中间有棋⼦但是不能打空炮64 {65return false;66 }67return true;68 }69public override void Put_picture()70 {71if (base.side == player_type.red)72this.PB.Image = global::象棋_封_.Properties.Resources.红炮;73else74this.PB.Image = global::象棋_封_.Properties.Resources.蓝炮;75 }76 }77 }炮类1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;56namespace象棋_封_7{8class Chess_shi:Chess9 {10public Chess_shi(player_type b)11 {12base.type = chess_type.shi;13base.side = b;14 }15public override bool Move_judge(object sender, int X, int Y, Chess[][] checkerboard)16 {17if (this.side == player_type.blue)18 {19if (Y < 3 || Y > 5 || X > 2)//限制将的移动范围20 {21return false;22 }23 }24else25 {26if (Y < 3 || Y > 5 || X < 7)//限制将的移动范围27 {28return false;29 }30 }31if (Math.Abs(X - Chess.chosenX) != 1 || Math.Abs(Chess.chosenY - Y) != 1)//只能跨⼀个⽅格32 {33return false;34 }35return true;36 }37public override void Put_picture()38 {39if (base.side == player_type.red)40this.PB.Image = global::象棋_封_.Properties.Resources.红⼠;41else42this.PB.Image = global::象棋_封_.Properties.Resources.蓝⼠;43 }44 }45 }⼠类1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;56namespace象棋_封_7{8class Chess_xiang:Chess9 {10public Chess_xiang(player_type b)11 {12base.type = chess_type.xiang;13base.side = b;14 }15public override bool Move_judge(object sender, int X, int Y, Chess[][] checkerboard)16 {17if (this.side == player_type.blue)18 {19if (X > 4)20 {21return false;22 }23 }24else25 {26if (X < 5)27 {28return false;29 }30 }31if ((X - Chess.chosenX) * (X - Chess.chosenX) + (Chess.chosenY - Y) * (Chess.chosenY - Y) != 8) 32 {33return false;34 }35if (checkerboard[(X + Chess.chosenX) / 2][(Y + Chess.chosenY) / 2].side != player_type.blank) 36 {37return false;38 }39return true;40 }41public override void Put_picture()42 {43if (base.side == player_type.red)44this.PB.Image = global::象棋_封_.Properties.Resources.红象;45else46this.PB.Image = global::象棋_封_.Properties.Resources.蓝象;47 }48 }49 }象类1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;56namespace象棋_封_7{8class Chess_zu:Chess9 {10public Chess_zu(player_type b)11 {12base.type = chess_type.zu;13base.side = b;14 }15public override bool Move_judge(object sender, int X, int Y, Chess[][] checkerboard)16 {17if (this.side == player_type.blue)//蓝⽅卒18 {19if (Chess.chosenX < 5 && X - Chess.chosenX != 1)//在5⾏后选中的卒20 {21return false;22 }23if (Chess.chosenX > 4)//移动布数不能⼤于124 {25if (X == Chess.chosenX && Math.Abs(Y - Chess.chosenY) != 1)26 {27return false;28 }29if (Y == Chess.chosenY && X - Chess.chosenX != 1)30 {31return false;32 }33 }34 }35else//红⽅卒36 {37if (Chess.chosenX > 4 && Chess.chosenX - X != 1)38 {39return false;40 }41if (Chess.chosenX < 5)42 {43if (X == Chess.chosenX && Math.Abs(Y - Chess.chosenY) != 1) 44 {45return false;46 }47if (Y == Chess.chosenY && Chess.chosenX - X != 1)48 {49return false;50 }51 }52 }53return true;54 }55public override void Put_picture()56 {57if (base.side == player_type.red)58this.PB.Image = global::象棋_封_.Properties.Resources.红兵;59else60this.PB.Image = global::象棋_封_.Properties.Resources.蓝卒;61 }62 }63 }卒类1using System;2using System.Collections.Generic;3using ponentModel;4using System.Data;5using System.Drawing;6using System.Linq;7using System.Text;8using System.Windows.Forms;910namespace象棋_封_11{12public partial class Start : Form13 {14public Start()15 {16 InitializeComponent();17 }1819private void LB_Begin_Click(object sender, EventArgs e)20 {21 Main f = new Main();22 f.Show();23this.Hide();24 }2526private void label2_Click(object sender, EventArgs e)27 {28 About f = new About();29 f.Show();30this.Hide();31 }3233private void LB_exit_Click(object sender, EventArgs e)34 {35 Application.Exit();36 }37 }38 }主窗体代码1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Windows.Forms;67namespace象棋_封_8{9class Time10 {11public Timer t;12private Label l;13private double s ;14private double m ;15public double sum=0;16public static string fen="0";17public static string miao ="0";18public Time(Timer t,Label L)19 {20 s = 0;21 m = 0;22this.t = t;23this.l = L;24 }25public string outtime()26 {27string second;28string minute;29 s++;30if (s >= 60)31 {32 m = s / 60;33 s = s % 60;34 }35 second = Convert.ToString(s);36 minute = Convert.ToString(m);37string sum = minute + ":" + second;38return sum;39 }40public void clear()41 {42 fen = (Convert.ToInt32(fen) + m).ToString();43 miao = (Convert.ToInt32(miao) + s).ToString();44 l.Text = "";45 m = 0;46 s = 0;47 }48 }49 }timer类。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ChineseChessBoard.h
class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMainWindow : public CFrameWnd
{
public:
CMyApp myApp;
//CMyApp member functions
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);
dc.MoveTo(rect.left,rect.top + nGridHeight * 4);
dc.LineTo(rect.left,rect.top + nGridHeight * 5);
dc.MoveTo(rect.right,rect.top);
dc.LineTo(rect.right,rect.bottom);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP()
CMainWindow::CMainWindow()
dc.SelectObject(pOldFont);
}
font.CreatePointFont(520,_T("宋体"));
CFont *pOldFont = dc.SelectObject(&font);
dc.SetBkMode(TRANSPARENT);
dc.DrawText(_T("楚河 汉界"),-1,textRect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
{
Create(NULL,_T("象棋棋盘"),WS_OVERLAPPEDWINDOW);
}
//CMainWindow mesage map and member functions
void CMainWindow::OnPaint()
{
CPaintDC dc(this);
//输出文字“楚河汉界”
dc.SelectObject(pOldPen);
CRect textRect(rect.left,rect.top + nGridHeight * 4,
rect.right,rect.top + nGridHeight * 5);
CFont font;
rect.OffsetRect(0,15);
//画下象棋棋盘的背景
CBrush brush(RGB(128,128,128));
dc.FillRect(rect,&brush);
//无聊,给点立体感
rect.InflateRect(2,2);
dc.Draw3dRect(rect,RGB(255,255,255),RGB(255,255,255));
CMainWindow();
protected:
afx_msg void OnPaint();
DECLARE_MESSAGE_MAP()
};
ChineseChessBoard.cpp
ห้องสมุดไป่ตู้
#include <afxwin.h>
#include <cmath>
#include "Hello.h"
CRect rect;
GetClientRect(&rect);
//画背景
CBrush bkBrush(RGB(192,192,192));
dc.FillRect(rect,&bkBrush);
//确定画象棋棋盘的范围
rect.DeflateRect(200,30);
int nGridHeight = rect.Height()/9; //纵向宽度,共格
for(int i = 0; i < 10; ++i) //画横线,10笔
{
int y = (nGridHeight * i) + rect.top;
dc.MoveTo(rect.left,y);
Windows 下利用MFC实现的中国象棋棋盘绘制程序 收藏 最近在啃 Jeff Prosise《MFC Windows 程序设计》这本书,以前虽然也学过MFC,当时用的是孙鑫的视频教程和书,学完后,似乎感觉有收获,也的确可以编点小的MFC程序,不过总感觉没有吃透,所以下决心,还是学这本书好,当时急于学习,偏偏网上这本书缺货,所以买了大家评价都不错的孙鑫那本,结果感觉还是不太好。突然想起某人说过,凡是教Windows编程的,书中图片过多,基本不要看,说的虽然很过,不过的确感觉用IDE引导出来的程序自己还是不能完全吃透,《MFC Windows 程序设计》就真的是主要靠手工代码,最近也学完一部分了,突发奇想画个象棋棋盘,巩固一下知识。完全手工代码输入,仅以抛砖引玉,因为没有考虑太多的缩放和分辨率问题,所以程序在不同的机子上可能会有效果不好的情况,假如有时间再改改。不要奇怪我怎么会在.NET横行的时代还在学大家都认为已经不行的MFC,我在网上晃了很久,发现懂MFC是很多公司的基本要求,无奈。。。。。。。。。。
rect.DeflateRect(2,2);
//开始画纵横线
CPen pen(PS_SOLID,2,RGB(0,0,0));
CPen *pOldPen = dc.SelectObject(&pen);
int nGridWidth = rect.Width()/8; //横向宽度,共格
dc.LineTo(x,rect.top + nGridHeight * 4);
dc.MoveTo(x,rect.top + nGridHeight * 5);
dc.LineTo(x,rect.bottom);
}
//补上左界限的竖笔及最右的竖线,此以rect.right画最右竖线,最重合
dc.LineTo(rect.right,y);
}
for(int i = 0; i < 8; ++i) //画竖线,画笔,空下最右的竖线
{
int x = (nGridWidth * i) + rect.left;
//中间为界限,无竖线
dc.MoveTo(x,rect.top);