绘制中国象棋棋盘(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); }}。
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。
观察棋盘特点黑白相间可知排号与列号相加为偶数的是白色方块,反之为黑色方块。
象棋c语言
象棋c语言
对中国象棋的认识应用与开发
所谓“象棋C语言“”是对中国象棋进一步认知的一个面向过程的、抽象化的通用的设计新型象棋设计样板,广泛应用于民间新型象棋开发。
“象棋C语言”能以简易和便利的方式、普遍的文化基础,设计规画各种新式象棋。
象棋C语言(象棋China语言)是仅产生少量的棋具、简明易记的规则及不需要任何运行环境支持便能自行使用设计出新型象棋的实用工具。
尽管象棋C语言局限了许多复杂的的规则,但仍然保持着天马行空的可设计规画性,以一个基本规则规画出的象棋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语言课程设计-中国象棋南昌航空大学借息工程学院课程设计说明书课程名称:C语言课程设计设计3目:中国象棋专计算机科学与技术班级:业:姓名: 学号:一评分: 指导教师:2012年6月26日I摘要II前言m功能描述IV配置要求v总体设计(个人负责模块)一、功能模块设计二、数据结构设计三、函数功能描述四、代码实现五、运行结果VI小结I摘要中国象棋是一款很古老、很受欢迎的游戏,其开发过程有一定的技巧和方法,其中涉及到函数调用、二维数组、键盘操作等方面的知识。
本游戏的开发者需要基本掌握复杂情况下函数的编写以及调用能力、二维数组的运用能力、复杂算法的设计能力等。
II前言中国象棋是一款经典的智力游戏,具有悠久的历史,I摘要早在战国时期就有了关于中国象棋的记载,经过几千年的流传,目前仍然是中国家喻户晓的棋类游戏,颇受欢迎。
因此,我们决定借这次机会通过用C语言将中国象棋实现出来,当然,我们也借鉴了前人的一些技巧经验。
有不足之处,希望老师能够谅解,我们以后将会再接再厉。
m功能描述本人负责棋子帅(将)、象(相)、士(仕)、卒(兵)子函数的编写,它们的所能实现的功能分别是:(1)帅(将):控制棋子帅(将)能符合现实情况下的游戏规则而行走,例如帅(将)只能在规定范围内向上或向左、右、下行走一格,最后返回一个行走正确或行走错误的数据。
(2)象(相):控制棋子象(相)能符合现实情况下的游戏规则而行走,例如象(相)只能在自己领域内走“田”字格,且中间不能有其他棋子阻挡,最后返回一个行走正确或行走错误的数据。
(3)士(仕):控制棋子士(仕)能符合现实情况下的游戏规则而行走,例如士(仕)只能在规定范围内斜着跨一格,然后返回一个行走正确或行走错误的数据。
(4)卒(兵):控制棋子卒(兵)能符合现实情况下的游戏规则而行走,例如卒(兵)只能一次走一格,同时在自己领域内只能向前走,而在对方领域内可向前、左、右方向走一格,最后返回一个行走正确或行走错误的数据。
中国象棋 C语言编程
当危险时显示被“将军”
• “将军”情况复杂,如何判断将军呢
判断将军的方法
• 首先得先找到对方老将的位置 • 以移动的子落点位置为起始位置,以对方老将位 置为要移动到的位置,调用规则函数验证,如果 可以吃到,则显示 “将军!”。
调用broad函数
分支思路
1.走子的完成加走子的规则。
各个棋子的移动
• 获取移动位置的方法:运用坐标 • 判断是否合法 • 重新输出整个棋盘
移动的实现方法
• 首先让玩家输入要移动的位置,然后输入要走到 的位置 • 将前者的位置的值赋给后者位置,同时将前者位 置值赋值为0,再次输出各个位置。
判断移动是否合法
双人对弈中国象棋
程序所负责内容介绍
• 计算机生成红黑双方以及棋盘。 • 各个棋子的移动。
• 当危险时显示被“将军”。
分支思路
1.棋盘棋子的制作与输出
计算机生成红黑双方以及棋盘
• 第一想法
• 第二想法→有子无子两种状态分别对应0和非0
输出 对每个位置赋值 非0 判断是否为0 为0
调用shift函数
悔棋程序
Goal! Step 13 Step 2 Step
走之前 保存要 走位置 的棋子 的数学 信息。
走之后 保存要 走到位 置的棋 子的数 学信息
如果悔 棋,将原 来的值重 新覆回到 棋盘中
完成
判断输赢
简单的方法
判断九格宫内 是否存在帅的 数学值
复盘
三维数组a[棋子的步数][横坐标]三 维数组
C++中国象棋的实现流程详解
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语言实现国际棋盘
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语言源代码
*--------------------chess.c----------------------*/ #include "dos.h"#include "stdio.h"/*----------------------------------------------------*/ #define RED 7#define BLACK 14#define true 1#define false 0#define SELECT 0#define MOVE 1#define RED_UP 0x1100#define RED_DOWN 0x1f00#define RED_LEFT 0x1e00#define RED_RIGHT 0x2000#define RED_DO 0x3900#define RED_UNDO 0x1000#define BLACK_UP 0x4800#define BLACK_DOWN 0x5000#define BLACK_LEFT 0x4b00#define BLACK_RIGHT 0x4d00#define BLACK_DO 0x1c00#define BLACK_UNDO 0x2b00#define ESCAPE 0x0100#define RED_JU 1#define RED_MA 2#define RED_XIANG 3#define RED_SHI 4#define RED_JIANG 5#define RED_PAO 6#define RED_BIN 7#define BLACK_JU 8#define BLACK_MA 9#define BLACK_XIANG 10#define BLACK_SHI 11#define BLACK_JIANG 12#define BLACK_PAO 13#define BLACK_BIN 14/*----------------------------------------------------*/ int firsttime=1;int savemode;char page_new=0,page_old=0;int finish=false,turn=BLACK,winner=0;int key;int redstate=SELECT,blackstate=SELECT;int board[10][9];/*----------------------------------------------------*/char *chessfile[15]={"","bmp\\rju.wfb", "bmp\\rma.wfb", "bmp\\rxiang.wfb","bmp\\rshi.wfb","bmp\\rjiang.wfb","bmp\\rpao.wfb","bmp\\rbin.wfb","bmp\\bju.wfb", "bmp\\bma.wfb", "bmp\\bxiang.wfb","bmp\\bshi.wfb","bmp\\bjiang.wfb","bmp\\bpao.wfb","bmp\\bbin.wfb"};char *boardfile[10][9]={{"bmp\\11.wfb","bmp\\1t.wfb","bmp\\1t.wfb","bmp\\14.wfb","bmp\\15.wfb","bmp\\16.wfb"," bmp\\1t.wfb","bmp\\1t.wfb","bmp\\19.wfb"},{"bmp\\21.wfb","bmp\\2c.wfb","bmp\\2c.wfb","bmp\\24.wfb","bmp\\25.wfb","bmp\\26.wfb"," bmp\\2c.wfb","bmp\\2c.wfb","bmp\\29.wfb"},{"bmp\\21.wfb","bmp\\3a.wfb","bmp\\3t.wfb","bmp\\34.wfb","bmp\\3t.wfb","bmp\\36.wfb"," bmp\\3t.wfb","bmp\\3a.wfb","bmp\\29.wfb"},{"bmp\\41.wfb","bmp\\4t.wfb","bmp\\4a.wfb","bmp\\4t.wfb","bmp\\4a.wfb","bmp\\4t.wfb","b mp\\4a.wfb","bmp\\4t.wfb","bmp\\49.wfb"},{"bmp\\51.wfb","bmp\\52.wfb","bmp\\5t.wfb","bmp\\54.wfb","bmp\\5t.wfb","bmp\\56.wfb"," bmp\\5t.wfb","bmp\\58.wfb","bmp\\59.wfb"},{"bmp\\61.wfb","bmp\\62.wfb","bmp\\6t.wfb","bmp\\64.wfb","bmp\\6t.wfb","bmp\\66.wfb"," bmp\\6t.wfb","bmp\\68.wfb","bmp\\69.wfb"},{"bmp\\71.wfb","bmp\\7t.wfb","bmp\\7a.wfb","bmp\\7t.wfb","bmp\\7a.wfb","bmp\\7t.wfb","b mp\\7a.wfb","bmp\\7t.wfb","bmp\\79.wfb"},{"bmp\\81.wfb","bmp\\8a.wfb","bmp\\8t.wfb","bmp\\84.wfb","bmp\\85.wfb","bmp\\86.wfb"," bmp\\8t.wfb","bmp\\8a.wfb","bmp\\89.wfb"},{"bmp\\91.wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\95.wfb","bmp\\9t.wfb","b mp\\9t.wfb","bmp\\9t.wfb","bmp\\99.wfb"},{"bmp\\101.wfb","bmp\\102.wfb","bmp\\102.wfb","bmp\\104.wfb","bmp\\105.wfb","bmp\\10 6.wfb","bmp\\108.wfb","bmp\\108.wfb","bmp\\109.wfb"}};char cursor[14][14]={0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,255,255,255,255,255,255,255,0,0,1,1,1,1,0,255,255,255,255,255,255,0,0,1,1,1,1,1,0,255,255,255,255,255,255,0,0,1,1,1,1,1,0,255,255,255,255,255,255,255,0,0,1,1,1,1,0,255,255,255,255,255,255,255,255,0,0,1,1,1,0,255,255,255,255,255,255,255,255,255,0,0,1,1,0,255,255,0,255,255,255,255,255,255,255,0,0,1,0,255,0,1,1,0,255,255,255,255,255,255,255,0,0,0,1,1,1,1,0,255,255,255,255,255,0,1,0,1,1,1,1,1,1,0,255,255,255,0,1,1,1,1,1,1,1,1,1,1,0,255,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1};struct pos{int x;int y;}position[10][9],redcurpos,redtemppos,redoldpos,blackcurpos,blacktemppos,blackoldpos; /*----------------------------------------------------*/selectpage(register char page) /*换页函数*/{union REGS r;r.x.ax=0x4f05;r.x.bx=0;r.x.dx=page; /*选择页面*/int86(0x10,&r,&r);}unsigned char set_SVGA_mode(int vmode) /*设置SVGA屏幕模式*/{union REGS r;r.x.ax=0x4f02;r.x.bx=vmode;int86(0x10,&r,&r);return(r.h.ah);}unsigned int get_SVGA_mode() /*获取当前SVGA屏幕模式*/{union REGS r;r.x.ax=0x4f03;int86(0x10,&r,&r);return(r.x.bx);}drawbmp(int start_x,int start_y,char filename[]){char buffer[640];int i,j,k,n,r,g,b,width,length;long position;FILE *fp;if((fp=fopen(filename,"rb"))==NULL){printf("Error! Can't open file!");getch();return;}fseek(fp,28,SEEK_SET);fread(&i,2,1,fp);if(i!=8) /*检查是否为256色位图*/{puts("Error!Can't find bitmap!");fclose(fp);getch();exit(0);}fseek(fp,18,SEEK_SET);fread(&width,4,1,fp);fread(&length,4,1,fp);if(firsttime){fseek(fp,54,SEEK_SET);for(i=0;i<256;i++) /*按照该图片的DAC色表设置色彩寄存器*/{b=fgetc(fp);g=fgetc(fp);r=fgetc(fp); /*获取R、G、B分量*/outportb(0x3c8,i);outportb(0x3c9,r>>2); /*右移是要转化为VGA的6位寄存器形式*/ outportb(0x3c9,g>>2);outportb(0x3c9,b>>2);fgetc(fp);}}elsefseek(fp,300,SEEK_SET);k=(width%4)?(4-width%4):0; /*宽度修正值*/for(j=length-1+start_x;j>=start_x;j--){fread(buffer,width,1,fp);for(i=start_y,n=0;i<width+start_y;i++,n++){position=j*640l+i; /*计算要显示点的显存位置*/page_new=position/65536; /*计算显示页*/if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/{selectpage(page_new);page_old=page_new;}pokeb(0xa000,position%65536,buffer[n]); /*写到显存位置*/}fseek(fp,k,SEEK_CUR); /*每行绘制完后修正宽度*/}fclose(fp);}init(){savemode=get_SVGA_mode(); /*先保存原来的屏幕模式*/set_SVGA_mode(0x101); /*硬件无关性初始化屏幕为640*480 256色模式*/ }end(){set_SVGA_mode(savemode); /*恢复屏幕*/}/*----------------------------------------------------*/initpos(){int i,j;for(i=0;i<10;i++)for (j=0;j<9;j++){position[i][j].x=35+i*39;position[i][j].y=43+j*40;}}initchessmap(){board[0][0]=BLACK_JU;board[0][1]=BLACK_MA;board[0][2]=BLACK_XIANG;board[0][3]=BLACK_SHI;board[0][4]=BLACK_JIANG;board[0][5]=BLACK_SHI;board[0][6]=BLACK_XIANG;board[0][7]=BLACK_MA;board[0][8]=BLACK_JU;board[2][1]=BLACK_PAO;board[2][7]=BLACK_PAO;board[3][0]=BLACK_BIN;board[3][2]=BLACK_BIN;board[3][4]=BLACK_BIN;board[3][6]=BLACK_BIN;board[3][8]=BLACK_BIN;board[9][0]=RED_JU;board[9][1]=RED_MA;board[9][2]=RED_XIANG;board[9][3]=RED_SHI;board[9][4]=RED_JIANG;board[9][5]=RED_SHI;board[9][6]=RED_XIANG;board[9][7]=RED_MA;board[9][8]=RED_JU;board[7][1]=RED_PAO;board[7][7]=RED_PAO;board[6][0]=RED_BIN;board[6][2]=RED_BIN;board[6][4]=RED_BIN;board[6][6]=RED_BIN;board[6][8]=RED_BIN;}initdrawchess(){int i,j;;for(i=0;i<10;i++)for(j=0;j<9;j++){if(board[i][j])drawbmp(position[i][j].x,position[i][j].y,chessfile[board[i][j]]); }}drawcursor(struct pos p){int i,j,n,m,x,y;long thisposition;x=position[p.x][p.y].x+20;y=position[p.x][p.y].y+25;for(j=13-1+x,m=13;j>=x;j--,m--){for(i=y,n=0;i<13+y;i++,n++){thisposition=j*640l+i; /*计算要显示点的显存位置*/page_new=thisposition/65536; /*计算显示页*/if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/ {selectpage(page_new);page_old=page_new;}if(cursor[m][n]!=1)if(cursor[m][n]==0)pokeb(0xa000,thisposition%65536,0);elseif(turn==RED)pokeb(0xa000,thisposition%65536,153);elsepokeb(0xa000,thisposition%65536,255);}}}drawselecursor(struct pos p){int i,j,n,m,x,y;long thisposition;x=position[p.x][p.y].x+20;y=position[p.x][p.y].y+25;for(j=13-1+x,m=13;j>=x;j--,m--){for(i=y,n=0;i<13+y;i++,n++){thisposition=j*640l+i; /*计算要显示点的显存位置*/page_new=thisposition/65536; /*计算显示页*/if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/ {selectpage(page_new);page_old=page_new;}if(cursor[m][n]!=1)pokeb(0xa000,thisposition%65536,0);}}}/*----------------------------------------------------*/int getkey(){int press;while(bioskey(1) == 0);press=bioskey(0);press=press&0xff00;return(press);}/*--------------------红方操作--------------------*/int redcanselect(){int x,y;x=redcurpos.x;y=redcurpos.y;if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)return 1;elsereturn 0;}int redcanmove(){int i,j,min,max,oldx,oldy,x,y;oldx=redoldpos.x;oldy=redoldpos.y;x=redcurpos.x;y=redcurpos.y;/*case1 目标位置是否是自己人*/if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)return 0;/* 军、马、炮、相、士、将、卒的走法正确性的判断*/ switch(board[oldx][oldy]){case RED_BIN: /*完成*/if(oldx>=5){ if(y!=oldy||(oldx-x)!=1) return 0;}else{ if(x==(oldx-1)&&y==oldy) return 1;elseif(x==oldx&&y==(oldy+1)) return 1;elseif(x==oldx&&y==(oldy-1)) return 1;elsereturn 0;}break;case RED_JIANG: /*完成*/if(x!=oldx&&y!=oldy) return 0;if(x!=oldx)if((x-oldx)>1||(oldx-x)>1) return 0;else if(x<7) return 0;else if(y!=oldy)if((y-oldy)>1||(oldy-y)>1) return 0;else if(y<3||y>5) return 0;break;case RED_JU: /*完成*/if(x!=oldx&&y!=oldy) return 0;else if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) return 0;}break;case RED_MA: /*完成*/if((x-oldx)==2&&((y-oldy)==1||(oldy-y)==1)) {if(board[oldx+1][oldy]!=0) return 0;}elseif((oldx-x)==2&&((y-oldy)==1||(oldy-y)==1)) {if(board[oldx-1][oldy]!=0) return 0;}elseif((y-oldy)==2&&((x-oldx)==1||(oldx-x)==1)) {if(board[oldx][oldy+1]!=0) return 0;}elseif((oldy-y)==2&&((x-oldx)==1||(oldx-x)==1)) {if(board[oldx][oldy-1]!=0) return 0;}elsereturn 0;break;case RED_PAO: /*完成*/if(x!=oldx&&y!=oldy) return 0;if(board[x][y]==0){if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0) return 0; }else if(y!=oldy){ min=(y>oldy)?oldy:y; max=(y>oldy)?y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0) return 0; }}else{if(x!=oldx){ min=(x>oldx)?oldx:x; max=(x>oldx)?x:oldx;for(i=min+1,j=0;i<max;i++) if(board[i][y]!=0) j++;if(j!=1) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y; max=(y>oldy)?y:oldy;for(i=min+1,j=0;i<max;i++) if(board[x][i]!=0) j++;if(j!=1) return 0;}}break;case RED_SHI: /*完成*/if(oldx==9||oldx==7){if(x!=8||y!=4) return 0;} else if(oldx==8){if(x==9&&y==3) return 1; elseif(x==9&&y==5) return 1; elseif(x==7&&y==3) return 1; elseif(x==7&&y==5) return 1; else return 0;}else return 0;break;case RED_XIANG: /*完成*/ if(x<5) return 0;if(x!=oldx&&y!=oldy){if((x-oldx)==2&&(y-oldy)==2){i=oldx+1;j=oldy+1;}else if((x-oldx)==2&&(oldy-y)==2) {i=oldx+1;j=oldy-1;}else if((oldx-x)==2&&(y-oldy)==2) {i=oldx-1;j=oldy+1;}else if((oldx-x)==2&&(oldy-y)==2) {i=oldx-1;j=oldy-1;}else return 0;if(board[i][j]!=0) return 0;}else return 0;break;}return 1;}。
中国象棋C代码
中国象棋#include<stdio.h>#include<conio.h>#include<string.h>#include<stdlib.h>#include<windows.h>int x, y, i, j, k, p, q, num = 1, round; //象棋游戏的全局变量int px1 = 0, py1 = 0, px2 = 0, py2 = 0;int ck_x, ck_y, ck_t; //基本参数char ch, tn = 'O', tn1 = 'N', tp, tp1;char ck_1[9][3] ={"車","馬","相","仕","帥","砲","兵","+-"}; //取棋子时只判断前8合法char ck_2[9][3] ={"车","马","象","士","将","炮","卒","+-"};//下棋子时判断多一个空位合法char check[3];void ckm1(char* tp,char* tp1,char* tn,char* tn1,int *num,int *if_ov,char map[100][100]) {//象棋函数判断将方下棋是否合法check[0] = *tp; check[1] = *tp1; check[2] = '\0';char a,b;for ( i = 0; i < 8; i++){ if ( strcmp(ck_2[i],check) == 0){ *tp = *tn; *tp1 = *tn1; *tn = 'O'; *tn1 = 'N';if( i < 7){ printf(" 将方的%s被吃",ck_2[i]); Sleep(500); }*num = *num + 1;for( k = 4; k <= 8; k = k + 2)//判断将是否死亡{for(j = 15; j <= 23; j= j+ 4){ if (map[k][j] == ck_2[4][0] && map[k][j+1] == ck_2[4][1]){ px2 = k; py2 = j; break; }}if( j <= 23) break;}if( k == 10){printf(" 将被将死帥方获得胜利\n"); printf("按任意键返回菜单");getch( ); *if_ov = 1; return;}for( k = 18; k <= 22; k = k + 2) //判断帥是否死亡{for(j = 15; j <= 23; j= j+ 4){if(map[k][j] == ck_1[4][0] && map[k][j+1] == ck_1[4][1]){px1 = k; py1 = j; break; }}if( j <= 23) break;}if ( k == 24){printf(" 帥被将死将方获得胜利\n"); printf("按任意键返回菜单");getch( ); *if_ov = 1; return;}if ( py1 == py2){for( k = px2 + 2; k <= px1 - 2; k = k +2) {if(map[k][py1] != '+') break;}if( k == px1){if(round == 1) printf(" 帥方对将将方胜利");else if( round == 2) printf(" 将方对将帥方胜利");printf("按任意键返回菜单"); getch( ); *if_ov = 1; return;}}break;}} // for ( i = 0; i < 8; i++)循环结束if( i == 8) {printf("不合法的走法\n"); Sleep(500); }}void ckm2(char* tp,char* tp1,char* tn,char* tn1,int *num,int *if_ov,char map[100][100]) {//象棋函数判断帥方下棋是否合法check[0] = *tp; check[1] = *tp1; check[2] = '\0';char a,b;for ( i = 0; i < 8; i++){if ( strcmp(ck_1[i],check) == 0){ *tp = *tn; *tp1 = *tn1; *tn = 'O'; *tn1 = 'N';if( i < 7) {printf(" 帥方的%s被吃",ck_1[i]); Sleep(500); }*num = *num + 1;for( k = 4; k <= 8; k = k + 2) //判断将是否死亡{for(j = 15; j <= 23; j= j+ 4){if(map[k][j] == ck_2[4][0] && map[k][j+1] == ck_2[4][1]){px2 = k; py2 = j; break; }}if( j <= 23) break;}if( k == 10){printf(" 将被将死帥方获得胜利\n");printf("按任意键返回菜单"); getch( );*if_ov = 1; return;}for( k = 18; k <= 22; k = k + 2) //判断帥是否死亡{for(j = 15; j <= 23; j= j+ 4){if(map[k][j] == ck_1[4][0] && map[k][j+1] == ck_1[4][1]){px1 = k; py1 = j; break; }}if( j <= 23) break;}if( k == 24){printf(" 帥被将死将方获得胜利\n");printf("按任意键返回菜单"); getch( );*if_ov = 1; return;}if( py1 == py2){for( k = px2 + 2; k <= px1 - 2; k = k +2) {if(map[k][py1] != '+') break; }if( k == px1){if(round == 1) printf(" 帥方对将将方胜利");else if( round == 2) printf(" 将方对将帥方胜利");printf("按任意键返回菜单"); getch( ); *if_ov = 1; return;}}break;}} // for ( i = 0; i < 8; i++)循环结束if( i == 8) {printf("不合法的走法\n"); Sleep(500); }}void xiangqi( )//象棋主程序{char map[100][100]= { "[[===================================]]","[| ①帥【象棋】②将|]","[[===================================]]","[[-----------------------------------]]","[[ 车—-马—-象—-士—-将—-士—-象—-马—-车]]","[[ | | | | \\ | / | | | | ]]","[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]","[[ | | | | / | \\ | | | | ]]","[[ +-—-炮—-+-—-+-—-+-—-+-—-+-—-炮—-+-]]","[[ | | | | | | | | | ]]","[[ 卒—-+-—-卒—-+-—-卒—-+-—-卒—-+-—-卒]]","[[ | | | | | | | | | ]]","[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]","[[===================================]]","[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]","[[ | | | | | | | | | ]]","[[ 兵—-+-—-兵—-+-—-兵—-+-—-兵—-+-—-兵]]","[[ | | | | | | | | | ]]","[[ +-—-砲—-+-—-+-—-+-—-+-—-+-—-砲—-+-]]","[[ | | | | \\ | / | | | | ]]","[[ +-—-+-—-+-—-+-—-+-—-+-—-+-—-+-—-+-]]","[[ | | | | / | \\ | | | | ]]","[[ 車—-馬—-相—-仕—-帥—-仕—-相—-馬—-車]]","[[-----------------------------------]]","[[===================================]]"};int if_ov = 0;system("mode con cols=42 lines=32"); //迷你界面system("color 70");printf("[[==================================]]\n");printf("[[ -------------------------------- ]]\n");printf("[[ | | ]]\n");printf("[[ | 【<<游戏规则>>】| ]]\n");printf("[[ | | ]]\n");printf("[[ |------------------------------| ]]\n");printf("[[ | 控制wasd双方轮流控制指针下棋| ]]\n");printf("[[ |------------------------------| ]]\n");printf("[[ | 键盘输入大小写‘M ’| ]]\n");printf("[[ | 都视为确认下棋| ]]\n");printf("[[ |------------------------------| ]]\n");printf("[[ | 为了方便区分棋子| ]]\n");printf("[[ | 先手方全设为繁体复杂字体| ]]\n");printf("[[ |------------------------------| ]]\n");printf("[[ |------------------------------| ]]\n");printf("[[ | 我已阅读规则,按任意键继续| ]]\n");printf("[[ |------------------------------| ]]\n");printf("[[==================================]]\n");getch( );system("mode con cols=42 lines=32"); //迷你界面system("color 70");for ( i = 0; i < 27; i++){ puts(map[i]); Sleep(100); }x = 6, y = 19; tp = map[x][y]; tp1 = map[x][y+1];while(num){ if (num % 2 == 1 &&num / 2 % 2 == 0){ printf(" 现在是'帥'的回合\n");round = 1; } else if( num %2 == 1){ printf(" 现在轮到'将'的回合了\n");round = 2; }ch = getch( );if ( ch == 's') //下移{ if ( map[x+1][y]!= '-'){map[x][y] =tp; map[x][y+1] = tp1;x = x + 2; tp = map[x][y]; tp1 = map[x][y+1];map[x][y] = tn; map[x][y+1] = tn1;}}else if ( ch == 'a') //左移{ if (map[x][y-1]!=' '){map[x][y] =tp; map[x][y+1] = tp1;y = y - 4; tp = map[x][y]; tp1 = map[x][y+1];map[x][y] = tn; map[x][y+1] = tn1;}}else if ( ch == 'w') //上移{ if ( map[x-1][y]!= '-'){map[x][y] =tp; map[x][y+1] = tp1;x = x - 2; tp = map[x][y]; tp1 = map[x][y+1];map[x][y] = tn; map[x][y+1] = tn1;}}else if ( ch == 'd') //右移{ if (map[x][y+2]!=']'){map[x][y] =tp; map[x][y+1] = tp1;y = y + 4; tp = map[x][y]; tp1 = map[x][y+1];map[x][y] = tn; map[x][y+1] = tn1;}}else if( ch == 'm' || ch =='M')//M确认要移动的棋子,或确认要移到的目的地{ if (num % 2 == 1 && tp != '+' && tp1 != '-') //取子{check[0] = tp; check[1] = tp1; check[2] = '\0';if ( round == 1){ for ( i = 0; i < 7; i++) //将方{ if ( strcmp(ck_1[i],check) == 0){tn = tp; tn1 = tp1; tp = '+'; tp1 = '-';ck_x = x; ck_y = y; ck_t = 10 + i;num++; break;}}if( i == 7){ printf("这不是你的棋子\n"); Sleep(500); }}else if( round == 2){for ( i = 0; i < 7; i++) //帅方{ if( strcmp(ck_2[i],check) == 0){tn = tp; tn1 = tp1; tp = '+'; tp1 = '-';ck_x = x; ck_y = y; ck_t = 20 + i;num++; break;}}if( i == 7){ printf("这不是你的棋子\n"); Sleep(500); }}}else if( num % 2 == 0) //放子{ char ck_1[8][3] ={"车","马","象","士","将","炮","卒","+-"};char ck_2[8][3] ={"俥","馬","相","仕","帥","軳","兵","+-"};//中界楚河上下坐标12 15 往下2 往右4if ( ck_t < 20){if( ck_t == 10) //车的走法规范(将方){ if((x == ck_x && y == ck_y)){tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( y == ck_y ){ if( x > ck_x){ for(j = ck_x + 2; j < x;j = j + 2){ if(map[j][y] == '+'); else{printf("不合法的下法\n"); Sleep(500); break; }}if( j >= x) ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}if( x < ck_x){ for(j = ck_x - 2; j > x;j = j - 2){ if(map[j][y] == '+'); else{printf("不合法的下法\n"); Sleep(500); break; }}if( j <= x) ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}}else if( x == ck_x ){if( y > ck_y){for(j = ck_y + 4; j < y;j = j + 4){if(map[x][j] == '+'); else {printf("不合法的下法\n"); Sleep(500); break; }}if( j >= y) ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}if( y < ck_y){for(j = ck_y - 4; j > y;j = j - 4){ if(map[x][j] == '+'); else { printf("不合法的下法\n"); Sleep(500); break; }}if( j <= y) ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}}else { printf("不合法的下法\n"); Sleep(500); }}if( ck_t == 11) //马的走法规范{if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( (abs( x - ck_x) == 2&& abs( y - ck_y) == 8)&& map[ck_x][(y+ck_y)/2] =='+') {ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else if( (abs( x - ck_x) == 4&& abs( y - ck_y) == 4)&& map[(x + ck_x)/2][ck_y] == '+' ) {ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else { printf("不合法的下法\n");Sleep(500); }}if( ck_t == 12) //相的走法规范{ if((x == ck_x && y == ck_y)){tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( x >= 15 &&(abs(y - ck_y) == 8 && abs(x - ck_x) == 4)){if((x == 22 && (y == 11 || y == 27))||(x == 18 &&( y == 3 || y == 19 || y == 35)) ||(x == 14 && (y == 11|| y ==27))){ if( map[(x+ck_x)/2][(y+ck_y)/2] == '+')ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else {printf("棋子卡住,不可执行"); Sleep(500); }}else {printf("不合法的下法\n");Sleep(500); }}else {printf("不合法的下法\n"); Sleep(500); }}if( ck_t == 13) //士的走法规范{ if((x == ck_x && y == ck_y)){tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( abs(x - ck_x)== 2 && abs( y - ck_y) == 4 &&((x==22 && (y == 15 || y == 23)) || ( x == 20 && y == 19) || ( x == 18 && ( y == 15 || y == 23)))){ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else { printf("不合法的下法\n"); Sleep(500); }}if( ck_t == 14) //将的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( ((abs(x - ck_x)== 2 && abs( y - ck_y) == 0 )|| (abs(x - ck_x)== 0 && abs( y - ck_y) == 4)) && x >= 18 && x <= 22 && y >= 15 && y <= 23 ){ ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else { printf("不合法的下法\n"); Sleep(500); }}if( ck_t == 15) //炮的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( y == ck_y ){ int check_pao = 0;if( x > ck_x){ for(j = ck_x + 2; j<= x ;j = j+ 2){ if(map[j][y] == '+' ); else check_pao++;}if(check_pao == 1&& tp == '+') // 直线行走但不可吃棋子ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n"); Sleep(500); }}else { for(j = ck_x - 2; j>= x;j = j - 2){ if(map[j][y] == '+' ); else { check_pao++;} }if(check_pao == 1&& tp == '+') //直线行走但不可吃棋子ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n"); Sleep(500); }}}else if( x == ck_x ){ int check_pao = 0;if( y > ck_y){ for(j = ck_y + 4; j<= y ;j = j+4){ if(map[x][j] == '+' ); else check_pao++;}if(check_pao == 1&& tp == '+') //直线行走但不可吃棋子ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n"); Sleep(500); }}else {for(j = ck_y - 4; j>= y;j = j - 4){if(map[x][j] == '+' ); else check_pao++;}if(check_pao == 1&& tp == '+') //直线行走但不可吃棋子ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n"); Sleep(500); }}}else { printf("不合法的下法\n");Sleep(500); }}if( ck_t == 16) //卒的走法规范{ if ( x >= 14){ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( x == ck_x - 2 && y == ck_y)ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n"); Sleep(500); }}else{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if((x - ck_x == 0 && abs(y-ck_y) ==4) ||( x - ck_x == -2 && abs(y-ck_y) == 0)) ckm1(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n"); Sleep(500); }}}}else { if( ck_t == 20) //车的走法规范(帅方){ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( y == ck_y ){ if( x > ck_x){ for(j = ck_x + 2; j < x;j = j + 2){ if(map[j][y] == '+'); else {printf("不合法的下法\n"); Sleep(500); break; } }if( j >= x) ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}if( x < ck_x){ for(j = ck_x - 2; j > x;j = j - 2){ if(map[j][y] == '+'); else { printf("不合法的下法\n"); Sleep(500); break; } }if( j <= x) ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}}else if( x == ck_x ){ if( y > ck_y){ for(j = ck_y + 4; j < y;j = j + 4){ if(map[x][j] == '+'); else { printf("不合法的下法\n"); Sleep(500); break; } }if( j >= y) ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}if( y < ck_y){ for(j = ck_y - 4; j > y;j = j - 4){ if(map[x][j] == '+'); else { printf("不合法的下法\n");Sleep(500); break; } }if( j <= y) ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);}}else { printf("不合法的下法\n"); Sleep(500); }}if( ck_t == 21) //马的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n");printf("还是你的回合"); Sleep(500);}else if( (abs( x - ck_x) == 2&& abs( y - ck_y) == 8)&&map[ck_x][(y+ck_y)/2] =='+'){ ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else if( (abs( x - ck_x) == 4&& abs( y - ck_y) == 4)&&map[(x + ck_x)/2][ck_y] == '+' ){ ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else { printf("不合法的下法\n");Sleep(500); }}if( ck_t == 22) //相的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n");printf("还是你的回合"); Sleep(500);}else if( x <= 12 && (abs(y - ck_y) == 8 && abs(x - ck_x) == 4)){ if((x == 4 && (y == 11 || y == 27))||(x == 8 && ( y == 3 || y == 19 || y == 35)) ||(x == 12 && (y == 11|| y ==27))){ if( map[(x+ck_x)/2][(y+ck_y)/2] == '+')ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("棋子卡住,不可执行");Sleep(500); }}else {printf("不合法的下法\n");Sleep(500); }}else { printf("不合法的下法\n");Sleep(500); }}if( ck_t == 23) //士的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n");printf("还是你的回合"); Sleep(500);}else if( abs(x - ck_x)== 2 && abs( y - ck_y) == 4 &&((x==4 &&(y == 15 || y == 23)) || ( x == 6 && y == 19) || ( x == 8 && ( y == 15 || y == 23)))) { ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else { printf("不合法的下法\n");Sleep(500); }}if( ck_t == 24) //将的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n");printf("还是你的回合"); Sleep(500);}else if( ((abs(x - ck_x)== 2 && abs( y - ck_y) == 0 )|| (abs(x - ck_x)== 0 && abs( y - ck_y) == 4)) && x >= 4 && x <= 8 && y >= 15 && y <= 23 ){ ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map); }else {printf("不合法的下法\n");Sleep(500); }}if( ck_t == 25) //炮的走法规范{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n");printf("还是你的回合"); Sleep(500);}else if( y == ck_y ){ int check_pao = 0;if( x > ck_x){ for(j = ck_x + 2; j<= x ;j = j+ 2){ if(map[j][y] == '+' ); else check_pao++;}if(check_pao == 1&& tp == '+') //直线行走但不可吃棋子ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n");Sleep(500); }}else { for(j = ck_x - 2; j>= x;j = j - 2){ if(map[j][y] == '+' ); else { check_pao++;} }if(check_pao == 1&& tp== '+') //直线行走但不可吃棋子ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n");Sleep(500); }}}else if( x == ck_x ){ int check_pao = 0;if( y > ck_y){ for(j = ck_y + 4; j<= y ;j = j+4){ if(map[x][j] == '+' ); else check_pao++;}if(check_pao == 1&& tp == '+') //直线行走但不可吃棋子ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2 && tp != '+') //跳跃吃棋ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n");Sleep(500); }}else{ for(j = ck_y - 4 ; j>= y;j = j - 4){ if(map[x][j] == '+' ); else check_pao++;}if(check_pao ==1&& tp == '+') //直线行走但不可吃棋子ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else if( check_pao == 2&& tp != '+') //跳跃吃棋ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n");Sleep(500); }}}else { printf("不合法的下法\n");Sleep(500); }}if( ck_t == 26) //卒的走法规范{ if( x <= 12){ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n"); printf("还是你的回合"); Sleep(500);}else if( x == ck_x + 2 && y == ck_y)ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n");Sleep(500); }}else{ if((x == ck_x && y == ck_y)){ tp = tn; tp1 = tn1; tn = 'O'; tn1 = 'N'; num--;printf("三思而后行\n");printf("还是你的回合"); Sleep(500);}else if((x - ck_x == 0 && abs(y-ck_y) ==4) ||( x - ck_x == 2&& abs(y-ck_y) == 0))ckm2(&tp,&tp1,&tn,&tn1,&num,&if_ov,map);else { printf("不合法的下法\n");Sleep(500); }}}}}}system("cls");if( if_ov) return;for(i = 0; i < 27; i++)puts(map[i]);}Sleep(5000);}int main( ){while(1){xiangqi( );printf("\n 重来,请按键.\n");getch( );}return 0;}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//编译运行环境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;
}
//运行效果:。