绘制中国象棋棋盘(c语言)
- 格式:docx
- 大小:413.71 KB
- 文档页数:5
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#实现中国象棋【棋盘,棋⼦】本⽂是利⽤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)。
目录一、设计任务,目的与要求 (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。
观察棋盘特点黑白相间可知排号与列号相加为偶数的是白色方块,反之为黑色方块。
象棋c语言
对中国象棋的认识应用与开发
所谓“象棋C语言“”是对中国象棋进一步认知的一个面向过程的、抽象化的通用的设计新型象棋设计样板,广泛应用于民间新型象棋开发。
“象棋C语言”能以简易和便利的方式、普遍的文化基础,设计规画各种新式象棋。
象棋C语言(象棋China语言)是仅产生少量的棋具、简明易记的规则及不需要任何运行环境支持便能自行使用设计出新型象棋的实用工具。
尽管象棋C语言局限了许多复杂的的规则,但仍然保持着天马行空的可设计规画性,以一个基本规则规画出的象棋C语言,可在包括类似各种象棋变体中嵌入基础规则对弈,以及用计算机等游戏平台上进行分析和对弈。
@@@@@@@@@@@。
#include<graphics.h>#include<conio.h>#include<string.h>#include<bios.h>#include<stdlib.h>#include"c:\tc\LIB\1.c"#define W 119#define S 115#define A 97#define D 100#define space 32#define UP 72#define DOWN 80#define LEFT 75#define RIGHT 77#define ENTER 13void qipan();void jiemian(int);void guangbiao1(int,int);void guangbiao2(int,int);void xuanzhong(int,int);void gaizi(int,int);char array(int,int);void xiazi(int,int,int,int);/*int panding(char,int,int,int,int);*/main(){int gdriver,gmode,i=0,c=0,x=190,y=190,m,n; char p;FILE *fp;gdriver=DETECT;gmode=0;if((fp=fopen("file.txt","at")) == NULL) {printf("Cannot open file!");system("pause");exit(0);}printf("%d,%d",gdriver,gmode); registerbgidriver(EGAVGA_driver);initgraph(&gdriver,&gmode,"c:\\tc"); cleardevice();while(c!=27){c=getch();clrscr();jiemian(i);if(c==80){fputs("down ",fp);i++;if(i==4){i=0;}}if(i==1){if(c==13){fputs("enter ",fp);qipan();c=getch();while(c!=27){c=getch();if(c==115){fputs("S ",fp);y=y+40;guangbiao1(x,y);guangbiao2(x,y-40);}if(c==119){fputs("W ",fp);y=y-40;guangbiao1(x,y);guangbiao2(x,y+40);}if(c==97){ fputs("A\n",fp);x=x-40;guangbiao1(x,y);guangbiao2(x+40,y);}if(c==100){ fputs("D\n",fp);x=x+40;guangbiao1(x,y);guangbiao2(x-40,y);}if(c==13){fputs("enter\n",fp);xuanzhong(x,y);m=x;n=y;}if(c==32){fputs("space\n",fp);xiazi(m,n,x,y);fputs("gaizi\n",fp);gaizi(m,n);}if(x>350||y>390||x<30||y<30){x=190;y=30;}}}}}getch();closegraph();fclose(fp);restorecrtmode();return 0;}void qipan(){int i,j;setbkcolor(GREEN);cleardevice();setlinestyle(0,0,3);setcolor(1);rectangle(10,10,370,410);rectangle(30,30,350,390);for(i=1;i<8;i++){setlinestyle(0,0,3);line(i*40+30,30,i*40+30,190);line(i*40+30,230,i*40+30,390);}for(j=1;j<9;j++){setlinestyle(0,0,3);line(30,j*40+30,350,j*40+30);}setlinestyle(3,0,3);line(150,30,230,110);line(230,30,150,110);line(150,310,230,390);line(230,310,150,390); setusercharsize(4,1,2,1); settextstyle(1,0,4);outtextxy(70,195,"chinese chess"); red_shuai(190,30);red_shi(150,30);red_shi(230,30);red_xiang(110,30);red_xiang(270,30);red_ma(70,30);red_ma(310,30);red_ju(30,30);red_ju(350,30);red_pao(70,110);red_pao(310,110);red_bing(30,150);red_bing(110,150);red_bing(190,150);red_bing(270,150);red_bing(350,150);black_jiang(190,390);black_shi(150,390);black_shi(230,390);black_xiang(110,390);black_xiang(270,390);black_ma(70,390);black_ma(310,390);black_ju(30,390);black_ju(350,390);black_pao(70,310);black_pao(310,310);black_zu(30,270);black_zu(110,270);black_zu(190,270);black_zu(270,270);black_zu(350,270);setcolor(BLUE);rectangle(400,30,600,320);setcolor(4);settextstyle(1,0,2);outtextxy(420,50,"A->shuai B->shi"); outtextxy(420,80,"C->xiang D->ma"); outtextxy(420,110,"E->ju F->pao"); outtextxy(420,140,"G->bing"); setcolor(8);outtextxy(420,200,"H->jiang I->shi"); outtextxy(420,230,"J->xiang K->ma"); outtextxy(420,260,"L->ju M->pao"); outtextxy(420,290,"N->zu");}void jiemian(int i){setbkcolor(GREEN); cleardevice();settextstyle(1,0,8);setcolor(BLUE);outtextxy(50,70,"chinese chess"); settextstyle(0,0,3);setcolor(RED);outtextxy(260,215,"start"); outtextxy(260,255,"again"); outtextxy(260,295,"undo"); outtextxy(260,335,"exit"); rectangle(250,210+i*40,390,240+i*40); }void guangbiao1(int x,int y){setcolor(WHITE);setlinestyle(0,0,3);line(x-17,y-7,x-17,y-17);line(x-7,y-17,x-17,y-17);line(x+7,y-17,x+17,y-17);line(x+17,y-7,x+17,y-17);line(x-7,y+17,x-17,y+17);line(x-17,y+7,x-17,y+17);line(x+17,y+7,x+17,y+17);line(x+7,y+17,x+17,y+17);}void guangbiao2(int x,int y){setcolor(GREEN);setlinestyle(0,0,3);line(x-17,y-7,x-17,y-17);line(x-7,y-17,x-17,y-17);line(x+7,y-17,x+17,y-17);line(x+17,y-7,x+17,y-17);line(x-7,y+17,x-17,y+17);line(x-17,y+7,x-17,y+17);line(x+17,y+7,x+17,y+17);line(x+7,y+17,x+17,y+17);}void xuanzhong(int x,int y){setcolor(CYAN);setlinestyle(0,0,3);circle(x,y,15);}void gaizi(int x1,int y1){setlinestyle(0,0,3);setcolor(GREEN);circle(x1,y1,15);setfillstyle(0,3);floodfill(x1,y1,GREEN);setcolor(1);setlinestyle(0,0,3);if((30<x1<350)&&((y1==30)||(y1==230))) {line(x1-18,y1,x1+18,y1);line(x1,y1,x1,y1+18);if((30<x1<350)&&(y1==390||y1==190)) {line(x1-18,y1,x1+18,y1);line(x1,y1-18,x1,y1);}if((30<y1<390)&&x1==30){line(x1,y1,x1+18,y1);line(x1,y1-18,x1,y1+18);}if((30<y1<390)&&(x1==350)){line(x1-18,y1,x1,y1);line(x1,y1-18,x1,y1+18);}if((x1==30)&&(y1==30)){line(x1,y1,x1+18,y1);line(x1,y1,x1,y1+18);}if((x1==350)&&(y1==30)){line(x1-18,y1,x1,y1);line(x1,y1,x1,y1+18);}if((x1==30)&&(y1==390)){line(x1,y1,x1+18,y1);line(x1,y1,x1,y1-18);}if((x1==350)&&(y1==390)){line(x1,y1,x1-18,y1);line(x1,y1,x1,y1-18);}else{line(x1-18,y1,x1+18,y1);line(x1,y1-18,x1,y1+18);}}char array(int i,int j)char a[13][13];int c,b;c=i;b=j;for(c=1;c<10;c++){for(b=1;b<11;b++){a[c][b]='Z';}}a[1][5]='A';a[1][4]='B';a[1][6]='B';a[1][3]='C';a[1][7]='C';a[1][2]='D';a[1][8]='D';a[1][1]='E';a[1][9]='E';a[3][2]='F';a[3][8]='F';a[4][1]=a[4][3]=a[4][5]=a[4][7]=a[4][9]='G';a[10][5]='H';a[10][4]='I';a[10][6]='I';a[10][3]='J';a[10][7]='J';a[10][2]='K';a[10][8]='K';a[10][1]='L';a[10][ 9]='L';a[2][3]='M';a[8][3]='M';a[7][1]=a[7][3]=a[7][5]=a[7][7]=a[7][9]='N';return a[i][j];}void xiazi(int x6,int y6,int x7,int y7){switch(array(y6/40+1,x6/40+1)){case 'A':red_shuai(x7,y7);break;case 'B':red_shi(x6,y7);break;case 'C':red_xiang(x7,y7);break;case 'D':red_ma(x7,y7);break;case 'E':red_ju(x7,y7);break;case 'F':red_pao(x7,y7);break;case 'G':red_bing(x7,y7);break;case 'H':black_jiang(x7,y7);break;case 'I':black_shi(x7,y7);break;case 'J':black_xiang(x7,y7);break;case 'K':black_ma(x7,y7);break;case 'L':black_ju(x7,y7);break;case 'M':black_pao(x7,y7);break;case 'N':black_zu(x7,y7);break;case 'Z':gaizi(x6,x6);break;}}/*int panding(char q,int x,int y,int a,int b){switch(q){case 'A':if(y>110||x>230||x<150||(a-x)>40||(x-a)>40||(y-b)>40||(b-y)>40)return 0;elsereturn 1;break;case'B':if(((x-a)==40&&(y-b)==40)&&y<=110&&230<x<150||((a-x)==40&&(b-y)==40)&&y<=110& &230>x>150)return 1;elsereturn 0;break;case'C':if((((x-a)==80&&(y-b)==80)&&y<=190)&&(array((y+b)/2/40+1,(x+a)/2/40+1,)=='Z')))||(((a-x)==80&&(b-y)==80)&&y<=190)&&(array((y+b)/2/40+1,(x+a)/2/40+1)=='Z'))))return 1;elsereturn 0;break;case'D':if((((x-a)==80&&(y-b)==40&&(array(y/40+1,(x-40)/40+1)=='Z'))||(((a-x)==80&&(b-y)==40)&&(array(y/40+1,(x+40)/40+1)=='Z'))||(((x-a)==40&&(y-b)==80)(array((y-40)/40+1,x/40+1)==' Z'))||(((a-x)==40&&(b-y)==80)&&(array((y+40)/40+1,x/40+1)=='z'))))return 1;elsereturn 0;break;case 'E':return 1;break;case 'F':return 1;break;case 'G':if(y<190){if(y>b||x!=a){return 0;}elsereturn 1;}else{if((b-y)>40||(a-x)>40||(x-a)>40||y>b){return 0;}elsereturn 1;}break;case 'H':if(y<310||x>230||x<150||(a-x)>40||(x-a)>40||(y-b)>40||(b-y)>40)return 0;elsereturn 1;break;case'I':if(((x-a)==40&&(y-b)==40)&&y>=310&&230<x<150||((a-x)==40&&(b-y)==40)&&y>310&& 230>x>150)return 1;elsereturn 0;break;case可编辑'J':if(((((x-a)==80&&(y-b)==80)&&y>=230)&&array(((y+b)/2/40+1,(x+a)/2/40+1)=='Z')))||(((a-x )==80&&(b-y)==80)&&y>=230)&&(array((y+b)/2/40+1,(x+a)/2/40+1)=='Z'))))return 1;elsereturn 0;break;case'K':if((((x-a)==80&&(y-b)==40&&(array(y/40+1,(x-40)/40+1)=='Z'))||(((a-x)==80&&(b-y)==40) &&(array(y/40+1,(x+40)/40+1)=='Z'))||(((x-a)==40&&(y-b)==80)(array((y-40)/40+1,x/40+1)==' Z'))||(((a-x)==40&&(b-y)==80)&&(array((y+40)/40+1,x/40+1)=='Z'))return 1;elsereturn 0;break;case 'L':return 1;break;case 'M':return 1;break;case 'N':if(y>230){if(y<b||x!=a){return 0;}elsereturn 1;}else{if(y-b>40||(a-x)>40||(x-a)>40||y<b){return 0;}elsereturn 1;}default:return 0;}}*/.。
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语言实现国际象棋盘的输出,不用画图头文件,不用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); }}。
//编译运行环境vs2013
//作者:E蓑烟雨
//QQ:1194758555,一起交流学习。
//修改时间按:2015-05-13
#include<graphics.h>
#include<conio.h>
int main()
{
initgraph(600,800);//初始化绘图环境
setorigin(300, 400);//设置原点
setbkcolor(RGB(225,160,115));//设置绘图背景色
cleardevice();//用上面背景色清空屏幕
setlinecolor(RED);//设置线条颜色
setlinestyle(PS_SOLID | PS_ENDCAP_FLAT, 4);//设置线条格式
for (int i = -280; i <= 280; i = i + 70)//画y轴负半轴平行于y轴线{
line(i, -360, i, -78);
}
for (int i = -360; i <= -70; i = i + 70)//画y轴负半轴平行于x轴线{
line(-280, i, 280, i);
}
for (int i = 0; i <= 280; i = i + 70)//画y轴正半轴平行于y轴线
{
line(-280, i, 280, i);
}
for (int i = -280; i <= 280; i = i + 70)//画y轴正半轴平行于x轴线{
line(i, 280, i, 0);
}
line(-71, -360, 70, -219);//画y轴负半轴帅的位置
line(-70, -219, 70, -360);
line(-70, 280, 70, 140);//画y轴正半轴将的位置
line(-70, 140, 70, 280);
line(-274, -153, -274, -170);//画y轴负半轴车的位置line(-276, -155, -276 + 15, -155);
line(-274, -147, -274, -129);
line(-276, -145, -276 + 15, -145);
for (int i = -135; i <= 275;i+=140)
{
line(i, -153, i, -170);
line(i, -147, i, -129);
line(i, -155, i + 15, -155);
line(i, -145, i + 15, -145);
}
line(274, -153, 274, -170);
line(276, -155, 276 - 15, -155);
line(274, -147, 274, -129);
line(276, -145, 276 - 15, -145);
for (int i = -145; i <= 272; i += 140)
{
line(i, -153, i, -170);
line(i, -147, i, -129);
line(i, -155, i-15, -155);
line(i, -145, i - 15, -145);
}
line(-215, -223, -215, -240);
line(-215, -225, -215 - 15, -225);
line(-215, -217, -215, -200);
line(-215, -215, -215 - 15, -215);
line(-205, -223, -205, -240);
line(-205, -225, -205 + 15, -225);
line(-205, -217, -205, -200);
line(-205, -215, -205 + 15, -215);
line(215, -223, 215, -240);
line(215, -225, 215 + 15, -225);
line(215, -217, 215, -200);
line(215, -215, 215 + 15, -215);//
line(205, -223, 205, -240);
line(205, -225, 205 - 15, -225);
line(205, -217, 205, -200);
line(205, -215, 205 - 15, -215);
line(-274, 67, -274, 50);//画y轴正半轴车的位置
line(-274, 65, -274 + 15, 65);
line(-274, 73, -274, 90);
line(-274, 75, -274 + 15, 75);
for (int i = -135; i <= 275; i += 140)
{
line(i, 67, i, 50);
line(i, 73, i, 90);
line(i, 65, i + 15, 65);
line(i, 75, i + 15,75 );
}
line(274, 67, 274, 50);
line(274, 65, 274 - 15, 65);
line(274, 73, 274, 90);
line(274, 75, 274 - 15, 75);
for (int i = -145; i <= 272; i += 140)
{
line(i, 67, i, 50);
line(i, 73, i, 90);
line(i, 65, i - 15, 65);
line(i, 75, i - 15, 75);
}
line(-215, 135, -215, 118);//画y轴负半轴半炮的位置
line(-213, 135, -213 - 15, 135);
line(-215, 145, -215, 162);
line(-213, 145, -213 - 15, 145);
line(-205, 137, -205, 118);
line(-205, 135, -205 + 15, 135);
line(-205, 143, -205, 162);
line(-205, 145, -205 + 15, 145);//画y轴负正半轴炮的位置line(215, 135, 215, 118);
line(213, 135, 213 + 15, 135);
line(215, 145, 215, 162);
line(213, 145, 213 + 15, 145);
line(205, 137, 205, 118);
line(205, 135, 205 - 15, 135);
line(205, 143, 205, 162);
line(205, 145, 205 - 15, 145);
setlinecolor(RED);//设置线条颜色
setlinestyle(PS_SOLID | PS_ENDCAP_FLAT, 5);//设置线条格式
line(-280, -362, -280, 282); //画边框
line(-280, -360, 280, -360);
line(280, -362, 280, 282);
line(-280, 280, 280, 280);
settextcolor(RED);//设置字体颜色
settextstyle(30, 25, _T("楷体"), 0, -900, 100, false, false, false);//设置字体高度,宽度,字体样式,旋转角度
outtextxy(-190, -35, _T("河楚"));//在指定位置输出字体
settextstyle(30, 25, _T("楷体"), 0, 900, 100, false, false, false);//设置字体高度,宽度,字体样式,旋转角度
outtextxy(120, -35, _T("汗界"));//在指定位置输出字体
getch();//按任意键退出
closegraph();//关闭图形界面
return 0;
}
//运行效果:。