vc++象棋程序设计与实现-程序流程图
- 格式:doc
- 大小:85.50 KB
- 文档页数:2
象棋游戏C#程序设计报告这个程序中主要训练了象棋的一些算法,以及关于多态和继承的思想。
程序没有花俏的界面,就是一个程序主窗体,也为棋盘部分。
用一个函数来记录棋盘上所有点的坐标,再把所有坐标点均采用一个数学公式求出此点映射到一维数组对应的索引,由索引来确定棋子的走动。
其中程序中的各种判断机制,特别是各种情况效验,主要为if的使用,包括类型比对,空值判断等等。
首先确定用户操作,在编写程序前,我们应该先确定我们的用户共有哪些行为和操作,在确定这些行为和操作以后,我们再对它进行模块划分。
我们的用户所做的所有操作仅两个,一是点击棋子,二是点击棋盘。
而点击棋子可能带来的操作为选中棋子和吃掉棋子,而我们点击棋盘则会带来移动棋子的过程。
分割程序模块,所有的操作和行为已经确定,接下来应该划分具体的模块,大体上,我们可以划分出棋盘和棋子两个大模块,但是在我们象棋中,棋子一共有7种,各有个性,单也有共性。
比如,所有棋子移动均不能超越棋盘边界,均不能阁棋子移动,这些都是象棋棋子的共性。
角色的事件激发过程,接下来分析一下每个参与的角色的事件激发过程。
用户点中棋子包含两个操作,选中棋子和吃棋子。
所有的操作均来自棋子被选中以后,只有当有了目标棋子以后,所有的包括移动,吃棋子才产生意义。
在象棋中有伦次概念,即蓝方移动以后,伦次应转换为红方移动。
那么当第二次操作,即移动或者吃棋子以后应该转换伦次。
棋盘为9X10格,那么此时不可能使用象素点来做为定位网格。
但是可以假定一个虚拟的网格模型,必须确定整个棋盘的边界。
编码实现一、Class1是一个自定义类,记录了旗盘位置特点类,以非常技巧的数据结构,记录了每一时刻旗盘上的旗子情况。
GetNearPoint 求两点间的距离公式N (x2-x1)2-(y2-y2)2CountIndex已知一个点,求出此点映射到一维数组对应的索引,既是代表在棋盘上的位置。
公式为:(y2-y1)/this.height*9+(x2-x1)/this.width由index决定是前进还是往后退:int index=this.CountIndex(point);if(index>=0&&index<45)return "up";else{ if(index>=45&&index<90)return "down";二、class2继承于class1,也是一个自定义类,记录了旗盘位置特点类,规定了各个旗子的走法。
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 源码链接。
目录摘要 (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)。
//// main.c// 象棋// 車马相仕帅仕相马車// 十十十十十十十十十// 十炮十十十十十炮十// 兵十兵十兵十兵十兵// 十十十十十十十十十// --楚河-汉界--// 十十十十十十十十十// 卒十卒十卒十卒十卒// 十炮十十十十十炮十// 十十十十十十十十十// 車马象士将士象马車// Created by tarena121 on 15/8/12.// Copyright (c) 2015年Tarena. All rights reserved.//#include <stdio.h>#include <stdbool.h>#include <math.h>#include <stdlib.h>#define R(piece) "\033[31m"#piece"\033[0m"//红色棋子#define B(piece) "\033[30m"#piece"\033[0m"//黑色棋子#define CROSS "\033[33m十\033[0m"//定义外部变量,棋盘坐标char* array[11][9];int xi,yi;//要移动的棋子int xj,yj;//移动的目标位置bool isStandard = 1;//是否符合规则,初始值1,符合bool gameOverSign = 1;//游戏是否结束,0结束bool restart = 0;//生成棋盘void chessboardBuilding();//打印棋盘void printChessboard();//判断是红棋还是黑棋,红旗返回1,黑棋返回-1,否则返回0 int redOrBlack(int x,int y);//红棋移动void redMove();//黑棋移动void blackMove();//每种棋子的规则void rulesOfAllKindsOfChessPieces();//判断游戏结束void isGameOver();//**************************主函数******************************int main(){//生成棋盘chessboardBuilding();//打印棋盘printChessboard();//开始下棋int turn = -1;while (gameOverSign) {isStandard = 1;turn *= (-1);//双方交替下棋switch (turn) {case1:redMove();turn = (restart) ? (turn*-1) : turn;break;case -1:blackMove();turn = (restart) ? (turn*-1) : turn;break;}isGameOver();}printf("游戏结束!\n");}//主函数结束//*************************自定义函数*****************************//生成棋盘void chessboardBuilding(){for (int i = 0; i < 11; i ++) {for (int j = 0; j < 9 ; j ++) {array[i][j] = CROSS;}printf("\n");}array[5][0] = array[5][1] = array[5][4] = array[5][7] = array[5][8] = "-";array[5][2] = B(楚);array[5][3] = B(河);array[5][5] = B(汉);array[5][6] = B(界);//布置红棋array[0][0] = array[0][8] = R(車);array[0][1] = array[0][7] = R(马);array[0][2] = array[0][6] = R(相);array[0][3] = array[0][5] = R(仕);array[0][4] = R(帅);array[2][1] = array[2][7] = R(炮);array[3][0] = array[3][2] = array[3][4] = array[3][6] = array[3][8] = R(兵);//布置黑棋array[10][0] = array[10][8] = B(車);array[10][1] = array[10][7] = B(马);array[10][2] = array[10][6] = B(相);array[10][3] = array[10][5] = B(仕);array[10][4] = B(将);array[8][1] = array[8][7] = B(炮);array[7][0] = array[7][2] = array[7][4] = array[7][6] = array[7][8] = B(卒);}//打印棋盘void printChessboard(){//显示printf(" \033[43;30m中国象棋欢迎您\033[0m\n\n");for (int i = 0; i < 11; i ++) {for (int j = 0; j < 9; j ++) {printf("%s",array[i][j]);}printf("\n");}}//判断是红棋还是黑棋,红旗返回1,黑棋返回-1,否则返回0int redOrBlack(int x,int y){if (array[x][y] == R(車) || array[x][y] == R(马) || array[x][y] == R(相) || array[x][y] == R(仕) || array[x][y] == R(帅) || array[x][y] == R(炮) || array[x][y] == R(兵)){return1;}else if (array[x][y] == B(車) || array[x][y] == B(马) || array[x][y] == B(象) || array[x][y] == B(仕) || array[x][y] == B(将) || array[x][y] == B(炮) || array[x][y] == B(卒)){return -1;}elsereturn0;}//红棋移动void redMove(){if (restart) {printf("违反游戏规则,请重新输入\n");restart = 0;}printf("[红棋]请输入你要移动的棋子:\n");scanf("%d %d",&xi,&yi);printf("[红棋]请输入你要放置的位置:\n");scanf("%d %d",&xj,&yj);rulesOfAllKindsOfChessPieces();printChessboard();}//黑棋移动void blackMove(){if (restart) {printf("违反游戏规则,请重新输入\n");restart = 0;}printf("[黑棋]请输入你要移动的棋子:\n");scanf("%d %d",&xi,&yi);printf("[黑棋]请输入你要放置的位置:\n");scanf("%d %d",&xj,&yj);rulesOfAllKindsOfChessPieces();printChessboard();}//判断游戏结束void isGameOver(){bool sign_r = 0;bool sign_b = 0;for (int i = 0; i < 11; i ++) {for (int j = 0; j < 9; j ++) {if (array[i][j] == R(帅)) {sign_r = 1;}else if (array[i][j] == B(将)){sign_b = 1;}}}if ((sign_r == 0)||(sign_b == 0)) {gameOverSign = 0;}}//每种棋子的规则void rulesOfAllKindsOfChessPieces(){//R(車)----------------------------------------if (array[xi][yi] == R(車)){if (yi == yj)//列坐标不变,同列移动{for (int i = xi+1; i < xj; i ++){if (i == 5)continue;//如果行等于5,跳过if (array[i][yi] != CROSS)isStandard = 0;//如果初始位置和目标位置之间有棋子,则不符合规则}for (int i = xi-1; i > xj; i --){if (i == 5)continue;//如果行等于5,跳过if (array[xi][yi] != CROSS)isStandard = 0;}}else if (xi == xj)//行坐标不变,同行移动{for (int i = yi+1; i < yj; i ++)if (array[xi][i] != CROSS)isStandard = 0;for (int i = yi-1; i > yj; i --)if (array[xi][i] != CROSS)isStandard = 0;}if ((xi == xj || yi == yj)&& isStandard && (redOrBlack(xj, yj) != 1))//如果棋子直行、没有犯规且落点不是红棋,可以移动{array[xi][yi] = CROSS;array[xj][yj] = R(車);}else{restart = 1;}}//B(車)----------------------------------------else if (array[xi][yi] == B(車)){if (yi == yj)//列坐标不变,同列移动{for (int i = xi+1; i < xj; i ++){if (i == 5)continue;//如果行等于5,跳过if (array[i][yi] != CROSS)isStandard = 0;//如果初始位置和目标位置之间有棋子,则不符合规则}for (int i = xi-1; i > xj; i --){if (i == 5)continue;//如果行等于5,跳过if (array[i][yi] != CROSS)isStandard = 0;}}else if (xi == xj)//行坐标不变,同行移动{for (int i = yi+1; i < yj; i ++)if (array[xi][i] != CROSS)isStandard = 0;for (int i = yi-1; i > yj; i --)if (array[xi][i] != CROSS)isStandard = 0;}if ((xi == xj || yi == yj)&& isStandard && redOrBlack(xj, yj) != -1)//如果棋子直行、没有犯规且落点不是红棋,可以移动{array[xi][yi] = CROSS;array[xj][yj] = B(車);}else{restart = 1;}}//R(马)----------------------------------------else if (array[xi][yi] == R(马)){if ((redOrBlack(xj, yj) != 1) && ((xj == xi-2 && yj == yi-1 &&redOrBlack(xi-1, yi) == 0) || (xj == xi-2 && yj == yi+1 &&redOrBlack(xi-1, yi) == 0) || (xj == xi-1 && yj == yi-2 &&redOrBlack(xi, yi-1) == 0) || (xj == xi-1 && yj == yi+2 &&redOrBlack(xi, yi+1) == 0) || (xj == xi+1 && yj == yi-2 &&redOrBlack(xi, yi-1) == 0) || (xj == xi+1 && yj == yi+2 &&redOrBlack(xi, yi+1) == 0) || (xj == xi+2 && yj == yi-1 &&redOrBlack(xi+1, yi) == 0) || (xj == xi+2 && yj == yi+1 &&redOrBlack(xi+1, yi) == 0))){array[xi][yi] = CROSS;array[xj][yj] = R(马);}else{restart = 1;}}//B(马)----------------------------------------else if (array[xi][yi] == B(马)){if ((redOrBlack(xj, yj) != -1) && ((xj == xi-2 && yj == yi-1 &&redOrBlack(xi-1, yi) == 0) || (xj == xi-2 && yj == yi+1 &&redOrBlack(xi-1, yi) == 0) || (xj == xi-1 && yj == yi-2 &&redOrBlack(xi, yi-1) == 0) || (xj == xi-1 && yj == yi+2 &&redOrBlack(xi, yi+1) == 0) || (xj == xi+1 && yj == yi-2 &&redOrBlack(xi, yi-1) == 0) || (xj == xi+1 && yj == yi+2 &&redOrBlack(xi, yi+1) == 0) || (xj == xi+2 && yj == yi-1 &&redOrBlack(xi+1, yi) == 0) || (xj == xi+2 && yj == yi+1 &&redOrBlack(xi+1, yi) == 0))){array[xi][yi] = CROSS;array[xj][yj] = B(马);}else{restart = 1;}}//R(炮)----------------------------------------else if (array[xi][yi] == R(炮)){int count = 0;//起始位置间棋子的个数if (yi == yj)//列坐标不变,同列移动{for (int i = xi+1; i < xj; i ++){if (i == 5)continue;//如果行等于5,跳过if (redOrBlack(i, yi) != 0)count++;}for (int i = xi-1; i > xj; i --){if (i == 5)continue;//如果行等于5,跳过if (redOrBlack(i, yi) != 0)count++;}}else if (xi == xj)//行坐标不变,同行移动{for (int i = yi+1; i < yj; i ++)if (redOrBlack(xi, i) != 0)count++;for (int i = yi-1; i > yj; i --)if (redOrBlack(xi, i) != 0)count++;}if ((xi == xj || yi == yj)&& (count <= 1) && redOrBlack(xj, yj) != 1)//如果棋子直行、没有犯规且落点不是红棋,可以移动{array[xi][yi] = CROSS;array[xj][yj] = R(炮);else{restart = 1;}}//B(炮)----------------------------------------else if (array[xi][yi] == B(炮)){int count = 0;//起始位置间棋子的个数if (yi == yj)//列坐标不变,同列移动{for (int i = xi+1; i < xj; i ++){if (i == 5)continue;//如果行等于5,跳过if (redOrBlack(i, yi) != 0)count++;}for (int i = xi-1; i > xj; i --){if (i == 5)continue;//如果行等于5,跳过if (redOrBlack(i, yi) != 0)count++;}}else if (xi == xj)//行坐标不变,同行移动{for (int i = yi+1; i < yj; i ++)if (redOrBlack(xi, i) != 0)count++;for (int i = yi-1; i > yj; i --)if (redOrBlack(xi, i) != 0)count++;if ((xi == xj || yi == yj)&& (count <= 1) && redOrBlack(xj, yj) != -1)//如果棋子直行、没有犯规且落点不是红棋,可以移动{array[xi][yi] = CROSS;array[xj][yj] = B(炮);}else{restart = 1;}}//R(兵)----------------------------------------else if (array[xi][yi] == R(兵)){if (xi > xj)isStandard = 0;//如果倒退,则不符合规范if (xi == 3)if ((xj != xi+1) || (yi != yj))isStandard = 0;//第3行时只能前进一步if (xi == 4)if ((xj != xi+2) || (yi != yj))isStandard = 0;//第4行时只能前进两步if (xi > 4) {if ((xj == xi+1 && yi ==yj)|| (xj == xi && yi ==yj+1)||(xj == xi && yi ==yj-1)){}elseisStandard = 0;}if ((xi == xj || yi == yj)&& isStandard && redOrBlack(xj, yj) != 1)//{array[xi][yi] = CROSS;array[xj][yj] = R (兵);}else{restart = 1;}}//B(卒)----------------------------------------else if (array[xi][yi] == B(卒)){if (xi < xj)isStandard = 0;//如果倒退,则不符合规范if (xi == 7)if ((xj != xi-1) || (yi != yj))isStandard = 0;//第3行时只能前进一步if (xi == 6)if ((xj != xi-2) || (yi != yj))isStandard = 0;//第4行时只能前进两步if (xi < 4) {if ((xj == xi-1 && yi ==yj)|| (xj == xi && yi ==yj+1)||(xj == xi && yi ==yj-1)){}elseisStandard = 0;}if (isStandard && redOrBlack(xj, yj) != -1)//{array[xi][yi] = CROSS;array[xj][yj] = R (卒);}else{restart = 1;}}//R(相)----------------------------------------else if (array[xi][yi] == R(相)){if ((xj <= 4)&&(redOrBlack(xj, yj) != 1) && ((xj == xi-2 && yj == yi-2 &&redOrBlack(xi-1, yi-1) == 0) || (xj == xi-2 && yj == yi+2 &&redOrBlack(xi-1, yi+1) == 0) || (xj == xi+2 && yj == yi-2 &&redOrBlack(xi+1, yi-1) == 0) || (xj == xi+2 && yj == yi+2 &&redOrBlack(xi+1, yi+1) == 0))){array[xi][yi] = CROSS;array[xj][yj] = R(相);}else{restart = 1;}}//B(象)----------------------------------------else if (array[xi][yi] == B(象)){if ((xj >= 6)&&(redOrBlack(xj, yj) != -1) && ((xj == xi-2 && yj == yi-2 &&redOrBlack(xi-1, yi-1) == 0) || (xj == xi-2 && yj == yi+2 &&redOrBlack(xi-1, yi+1) == 0) || (xj == xi+2 && yj == yi-2&&redOrBlack(xi+1, yi-1) == 0) || (xj == xi+2 && yj == yi+2 &&redOrBlack(xi+1, yi+1) == 0))) {array[xi][yi] = CROSS;array[xj][yj] = B(象);}else{restart = 1;}}//R(仕)----------------------------------------else if (array[xi][yi] == R(仕)){if ((xj <= 2)&&(redOrBlack(xj, yj) != 1) && ((xj == xi-1 && yj == yi-1 ) || (xj == xi-1 && yj == yi+1 ) || (xj == xi+1 && yj == yi-1 ) || (xj == xi+1 && yj == yi+1 ))){array[xi][yi] = CROSS;array[xj][yj] = R(仕);}else{restart = 1;}}//B(士)----------------------------------------else if (array[xi][yi] == B(士)){if ((xj >= 8)&&(redOrBlack(xj, yj) != 1) && ((xj == xi-1 && yj == yi-1 ) || (xj == xi-1 && yj == yi+1 ) || (xj == xi+1 && yj == yi-1 ) || (xj == xi+1 && yj == yi+1 ))){array[xi][yi] = CROSS;array[xj][yj] = B(士);}else{restart = 1;}}//R(帅)----------------------------------------else if (array[xi][yi] == R(帅)){if ((xj <= 2 && yj <= 5 && yj >=3)&&(redOrBlack(xj, yj) != 1) && (((xj == xi)&&(yj == yi + 1 || yj == yi - 1))||((yj == yi)&&(xj == xi + 1 || xj == xi - 1)))){array[xi][yi] = CROSS;array[xj][yj] = R(帅);}else{restart = 1;}}//B(将)----------------------------------------else if (array[xi][yi] == B(将)){if ((xj >= 8 && yj <= 5 && yj >=3)&&(redOrBlack(xj, yj) != -1) && (((xj == xi)&&(yj == yi + 1 || yj == yi - 1))||((yj == yi)&&(xj == xi + 1 || xj == xi - 1)))){array[xi][yi] = CROSS;array[xj][yj] = B(将);}else{restart = 1;}}else {restart = 1;}}。
国际象棋c语言课程设计一、课程目标知识目标:1. 学生能够理解国际象棋的基本规则和棋谱表示方法。
2. 学生能够掌握C语言编程中数组、函数和指针等基础知识,并能运用到国际象棋程序设计中。
3. 学生能够了解计算机算法在国际象棋游戏中的应用。
技能目标:1. 学生能够运用C语言编写简单的国际象棋游戏程序,实现棋盘初始化、棋子移动和胜负判断等功能。
2. 学生能够通过分析问题,设计合适的算法,提高国际象棋程序的智能化水平。
3. 学生能够运用调试工具,对国际象棋程序进行测试和优化,提高程序的稳定性和性能。
情感态度价值观目标:1. 学生通过国际象棋的学习,培养逻辑思维、策略规划和团队协作能力。
2. 学生在编程实践过程中,增强自信,培养面对挑战、解决问题的勇气和毅力。
3. 学生能够认识到计算机编程在现实生活中的广泛应用,激发对计算机科学的兴趣和热爱。
课程性质:本课程为实践性较强的学科,结合国际象棋与C语言编程,旨在培养学生的编程能力和逻辑思维。
学生特点:学生已具备一定的C语言基础,对国际象棋有一定的了解,喜欢挑战性任务,具有较强的学习兴趣。
教学要求:教师应注重理论与实践相结合,引导学生运用所学知识解决实际问题,关注学生个体差异,提高学生的编程能力和综合素质。
通过本课程的学习,使学生将所学知识转化为具体的学习成果,为后续相关课程打下坚实基础。
二、教学内容1. 国际象棋基本规则及棋谱表示方法:包括棋盘布局、棋子种类、移动规则、胜负判定等,结合教材相关章节,让学生深入了解国际象棋的基本知识。
2. C语言基础知识复习:回顾数组、函数、指针等基本概念,巩固学生的C语言基础,为后续编程实践打下基础。
3. 国际象棋程序设计:按照以下步骤制定教学大纲:a. 棋盘初始化:学习如何在C语言中创建二维数组表示棋盘,并初始化各棋子的位置。
b. 棋子移动:掌握如何实现棋子的合法移动,包括判断移动是否符合规则、更新棋盘状态等。
c. 胜负判断:学习如何编写算法判断游戏胜负,如将军、重复局面等。
C语言课程设计-中国象棋南昌航空大学借息工程学院课程设计说明书课程名称:C语言课程设计设计3目:中国象棋专计算机科学与技术班级:业:姓名: 学号:一评分: 指导教师:2012年6月26日I摘要II前言m功能描述IV配置要求v总体设计(个人负责模块)一、功能模块设计二、数据结构设计三、函数功能描述四、代码实现五、运行结果VI小结I摘要中国象棋是一款很古老、很受欢迎的游戏,其开发过程有一定的技巧和方法,其中涉及到函数调用、二维数组、键盘操作等方面的知识。
本游戏的开发者需要基本掌握复杂情况下函数的编写以及调用能力、二维数组的运用能力、复杂算法的设计能力等。
II前言中国象棋是一款经典的智力游戏,具有悠久的历史,I摘要早在战国时期就有了关于中国象棋的记载,经过几千年的流传,目前仍然是中国家喻户晓的棋类游戏,颇受欢迎。
因此,我们决定借这次机会通过用C语言将中国象棋实现出来,当然,我们也借鉴了前人的一些技巧经验。
有不足之处,希望老师能够谅解,我们以后将会再接再厉。
m功能描述本人负责棋子帅(将)、象(相)、士(仕)、卒(兵)子函数的编写,它们的所能实现的功能分别是:(1)帅(将):控制棋子帅(将)能符合现实情况下的游戏规则而行走,例如帅(将)只能在规定范围内向上或向左、右、下行走一格,最后返回一个行走正确或行走错误的数据。
(2)象(相):控制棋子象(相)能符合现实情况下的游戏规则而行走,例如象(相)只能在自己领域内走“田”字格,且中间不能有其他棋子阻挡,最后返回一个行走正确或行走错误的数据。
(3)士(仕):控制棋子士(仕)能符合现实情况下的游戏规则而行走,例如士(仕)只能在规定范围内斜着跨一格,然后返回一个行走正确或行走错误的数据。
(4)卒(兵):控制棋子卒(兵)能符合现实情况下的游戏规则而行走,例如卒(兵)只能一次走一格,同时在自己领域内只能向前走,而在对方领域内可向前、左、右方向走一格,最后返回一个行走正确或行走错误的数据。
C++中国象棋的实现流程详解中国象棋的中国棋⽂化,也是中华民族的⽂化瑰宝,它源远流长,趣味浓厚,基本规则简明易懂。
中国象棋在中国的群众中基础远远超过围棋,是普及最⼴的棋类项⽬,中国象棋已流传到⼗⼏个国家和地区。
中国象棋使⽤⽅形格状棋盘,圆形棋⼦共有32个,红⿊⼆⾊各有16个棋⼦,摆放和活动在交叉点上。
双⽅交替⾏棋,先把对⽅的将(帅)“将死”的⼀⽅获胜。
我们今天就来看看我们⾃⼰能不能写出这样⼀个游戏呢?今天就不话不多说了,先说⼀下,今天我们做的是⼀个简易版的单机中国象棋,希望⼤家理解,联⽹对弈的话需要⽤到的知识过多,数据库以及⽹络协议这些⼤部分同学都没有学,所以我们今天就简单的实现《中国象棋》的简单对弈,主要是希望同学们可以理解其中的逻辑关系,之后就可以更好的去完善⾏吧,我们现在就开始吧今天先出场的就不是我们的⽼朋友结构体了,⽽是我们的新朋友枚举类型enum Pieces //棋⼦{NONE = -1,⾞, ⾺, 象, ⼠, 将, 砲, 卒,俥, 马, 相, 仕, 帥, 炮, 兵,BEGIN, END,};//给id赋值enum Pieces redChess[] = { ⾞, ⾺, 象, ⼠, 将, 砲, 卒 };enum Pieces blackChess[] = { 俥, 马, 相, 仕, 帥, 炮, 兵 };//绘制时转化成字符串const char* ChessName[] = { "⾞","⾺","象","⼠","将","砲","卒","俥", "马", "相", "仕", "帥", "炮", "兵" };接下来出场的是我们的⽼朋友结构体//每⼀个棋⼦的属性struct Chess{enum Pieces id; //棋⼦名称DWORD type; //棋⼦类型,红?⿊?short x;short y;bool isRiver; //是否过了河};struct Chess map[ROW][COL];struct State{int begr;int begc;int endr;int endc;int state;}state = {-1,-1,-1,-1,BEGIN};我们的初始化函数,⼀定要想好其中的逻辑//初始化数据void init(){//遍历地图for (size_t i = 0; i < ROW; i++){size_t temp = 0;for (size_t k = 0; k < COL; k++){map[i][k].id = NONE; //先把棋⼦置为没有if (i <= 4) //⿊棋⼦{map[i][k].type = BLACK;if (i == 0) //放置第⼀⾏的棋⼦{//0 1 2 3 4if (k <= 4){temp = k;}// 3 2 1 0else{// k == 5temp = 4 - (k - 4);/*4 - (5-4) //34 - (6-4) //24 - (7-4) //14 - (8-4) //0*/}map[i][k].id = blackChess[temp];}//设置炮if (i == 2 && (k == 1 || k == 7)){map[i][k].id = blackChess[5];}//设置兵if (i == 3 && k % 2 == 0){map[i][k].id = blackChess[6];}}else //红棋{map[i][k].type = RED;if (i == 9) //放置第⼀⾏的棋⼦{//0 1 2 3 4if (k <= 4){temp = k;}// 3 2 1 0else{// k == 5temp = 4 - (k - 4);/*4 - (5-4) //34 - (6-4) //24 - (7-4) //14 - (8-4) //0*/}map[i][k].id = redChess[temp];}//设置炮if (i == 7 && (k == 1 || k == 7))}//设置兵if (i == 6 && k % 2 == 0){map[i][k].id = redChess[6];}}map[i][k].isRiver = false;map[i][k].x = k * GRID_SIZE + INTERVAL;map[i][k].y = i * GRID_SIZE + INTERVAL;}}}接下来是我们的绘制函数//绘制void draw(){setfillcolor(RGB(252, 215, 162));setlinestyle(PS_SOLID, 2);//设置⽂字的样式settextstyle(30, 0, "楷体");for (size_t i = 0; i < ROW; i++){for (size_t k = 0; k < COL; k++){if (map[i][k].id == NONE)continue;settextcolor(map[i][k].type);setlinecolor(map[i][k].type);//绘制棋⼦fillcircle(map[i][k].x, map[i][k].y, 30);fillcircle(map[i][k].x, map[i][k].y, 25);outtextxy(map[i][k].x - 15, map[i][k].y - 15, ChessName[map[i][k].id]);}}}后⾯是我们的重点,⿏标控制函数,以后类似的游戏项⽬都会有这样的函数,好好理解//⿏标操作void mouseEvent(){ExMessage msg; //定义消息结构体变量if(peekmessage(&msg, EM_MOUSE)){if (msg.message == WM_LBUTTONDOWN) //⿏标左键按下{//通过⿏标坐标得出点击的数组的下标//k * GRID_SIZE + INTERVAL = x;int col = (msg.x - INTERVAL) / GRID_SIZE;int row = (msg.y - INTERVAL) / GRID_SIZE;//下标校准if (msg.x > map[row][col].x + 30 && msg.y < map[row][col].y + 30){col++;}if (msg.x < map[row][col].x + 30 && msg.y > map[row][col].y + 30){row++;}if (msg.x > map[row][col].x + 30 && msg.y > map[row][col].y + 30){row++;col++;}//printf("(%d %d)\n", row, col);if (state.state == BEGIN){state.begr = row;state.begc = col;state.state = END;}else if (state.state == END){state.endr = row;state.endc = col;state.state = BEGIN;}chessMove();}重点中的重点,棋⼦的移动函数,游戏的规则也就在这⾥体现出来//移动棋⼦void chessMove(){printf("beg(%d %d) end(%d %d)\n", state.begr, state.begc, state.endr, state.endc);bool canMove = false;//什么情况下能够移动棋⼦if (!(state.begr == state.endr && state.begc == state.endc) && //点击的不是同⼀个棋⼦state.endr!=-1 && state.begr!=-1&& //下标必须合法map[state.begr][state.begc].id != NONE//没有棋⼦不能移动/*&&map[state.begr][state.begc].type != map[state.endr][state.endc].type*/) //不能⾃⼰吃⾃⼰ {switch (map[state.begr][state.begc].id){case ⾞:case 俥:if (state.begr == state.endr || state.begc == state.endc){//起始点和结束点之间是否有阻碍if (hasBlock(&state)){canMove = true;}}break;case ⾺:case 马:break;case 象:case 相:break;case ⼠:case 仕:break;case 将:case 帥:break;case 砲:case 炮:break;case 卒:case 兵:break;default:break;}if (canMove){printf("canMove\n");map[state.endr][state.endc].id = map[state.begr][state.begc].id;map[state.begr][state.begc].id = NONE;map[state.endr][state.endc].isRiver = map[state.begr][state.begc].isRiver;map[state.endr][state.endc].type = map[state.begr][state.begc].type;}}}最后就是我们的主函数,进⾏调⽤,让项⽬运⾏起来int main(){//创建图形窗⼝initgraph(740, 820,EW_SHOWCONSOLE);//设置背景模式setbkmode(TRANSPARENT);//贴棋盘IMAGE img_board;loadimage(&img_board, "./res/ChessBoard.png");init();//双缓冲绘图,防⽌闪屏BeginBatchDraw();while (true){cleardevice();putimage(0, 0, &img_board);draw();mouseEvent();EndBatchDraw();getchar();return 0;}这样⼀个简易版的《中国象棋》游戏项⽬就解决啦,重点就是逻辑,⼀定要想清楚,想要实现联⽹的同学就要更加的去想清楚,以及去提⾼⾃⼰的能⼒,好啦,希望可以让⼤家从中感受到编程的快乐吧,也希望⼤家可以给UP主⼀个关注,⾮常感谢⼤家了到此这篇关于C++ 中国象棋的实现流程详解的⽂章就介绍到这了,更多相关C++ 中国象棋内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。
中国象棋网上对弈系统摘要随着信息技术的发展,人民生活水平的不断提高。
联网游戏作为一种娱乐手段,正以其独特的魅力吸引着越来越多的玩家。
为了满足广大象棋爱好者也可以享受到网络所带来的便利,本设计在当前局域网条件下实现了中国象棋的网络对战。
鉴于局域网的特点和游戏本身的要求,本设计采用两层C/S架构来实现相互之间的通信。
它主要包含以下几大模块:网络通信模块,图像绘制模块和规则设置模块。
网络通信模块使得玩家可以方便的迅速建立起网络连接,从而实现联机对弈和聊天功能;图像绘制模块实现棋盘更新以及棋子动态表示等功能;规则设置模块用于约束玩家的棋步。
电脑游戏是计算机应用领域的一个重要主题,而当前网上最热门的休闲对战类游戏当属棋牌游戏。
通过对象棋的数据结构,相关算法与网络联机,以及对网络对战平台系统的分析,设计成一套基于VC++平台的棋牌类对战系统。
关键词:网络通信;联机对弈;图像绘制;消息响应CHINESE CHESS ONLINE GAME SYSTEMAbstractWith the development of information technology, peoples standard of living have improved constantly. On-line game as a means of entertainment has a unique charm to attract more and more players. To meet chess lovers ,this design has realized the Challenges of Chinese-chess between players under the current LAN environment.In view of the characteristics of local area networks and the requirements of the game itself, the design uses a two-tier C/S structure to achieve mutual communication. It contains the following major modules: the network communication module, image rendering module and the rules set up module. Network Communication Module allows gamers can quickly set up a convenient network connection in order to achieve an online game and chat function; image rendering module to update the board as well as functional pieces, such as the dynamic that; rules binding settings module for step-by-step player's game.Computer games is the field of computer applications as an important subject, and present the most popular casual online games war among board games. By the data structure, algorithms and network online, as well as the network platform for systematic analysis of the war, designed a set of VC + + platform based on the category Card battle system.Keywords: Network Communication;Online game; Image Rendering; Message Response目录摘要 (I)Abstract (II)1绪论 (3)1.1课题背景 (3)1.2课题发展概况 (3)1.3本文主要工作 (4)2系统介绍 (5)2.1系统简介 (5)2.2 系统构成 (5)2.3相关技术 (5)2.3.1 点对点通信 (5)2.3.2TCP/IP协议 (6)2.3.3UPD基础 (7)2.3.4屏幕作图与用户交互 (7)2.3.5远程控制原理 (8)3设计与实现 (9)3.1数据结构 (9)3.1.1棋盘 (9)3.1.2棋子信息数组 (10)3.1.3变量与函数 (11)3.2图像绘制 (12)3.2.1主窗口 (12)3.2.2棋盘的绘制 (12)3.2.3棋子的绘制及初始化 (13)3.2.4动态显示 (14)3.2.5回看功能 (15)3.3规则设置 (15)3.3.1棋子规则 (15)3.3.2规则算法 (15)3.4网络通信 (17)3.4.1CCOM类 (17)3.4.2数据代码 (18)3.4.3数据更新 (19)3.4.4聊天功能 (19)3.5棋子操作 (20)3.5.1获取点击 (20)3.5.2走棋判断 (21)3.5.3光标变化 (21)4总结与展望 (23)4.1总结 (23)4.2展望 (23)参考文献 (24)致谢 (25)1绪论1.1课题背景电脑游戏就是以计算机为操作平台,通过人机互动形式实现的能够体现当前计算机技术较高水平的一种新形式的娱乐方式。