2022年C语言编写象棋程序代码 .pdf
/*-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 0 x1100 #define RED_DOWN 0 x1f00 #define RED_LEFT 0 x1e00 #define RED_RIGHT 0 x2000 #define RED_DO 0 x3900 #define RED_UNDO 0 x1000 #define BLACK_UP 0 x4800 #define BLACK_DOWN 0 x5000 #define BLACK_LEFT 0 x4b00 #define BLACK_RIGHT 0 x4d00 #define BLACK_DO 0 x1c00 #define BLACK_UNDO 0 x2b00 #define ESCAPE 0 x0100 #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; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 24 页 - - - - - - - - - char page_new=0,page_old=0; int finish=false,turn=BLACK,winner=0; int key; int redstate=SELECT,blackstate=SELECT; int board109; /*-*/ char *chessfile15=,bmprju.wfb, bmprma.wfb, bmprxiang.wfb,bmprshi.wfb, bmprjiang.wfb,bmprpao.wfb,bmprbin.wfb, bmpbju.wfb, bmpbma.wfb, bmpbxiang.wfb,bmpbshi.wfb, bmpbjiang.wfb,bmpbpao.wfb,bmpbbin.wfb ; char *boardfile109= bmp11.wfb,bmp1t.wfb,bmp1t.wfb,bmp14.wfb,bmp15.wfb,bmp16.wfb,bmp1t.wfb,bmp1t.wfb,bmp19.wfb, bmp21.wfb,bmp2c.wfb,bmp2c.wfb,bmp24.wfb,bmp25.wfb,bmp26.wfb,bmp2c.wfb,bmp2c.wfb,bmp29.wfb, bmp21.wfb,bmp3a.wfb,bmp3t.wfb,bmp34.wfb,bmp3t.wfb,bmp36.wfb,bmp3t.wfb,bmp3a.wfb,bmp29.wfb, 4a.wfb,bmp4t.wfb,bmp49.wfb, bmp51.wfb,bmp52.wfb,bmp5t.wfb,bmp54.wfb,bmp5t.wfb,bmp56.wfb,bmp5t.wfb,bmp58.wfb,bmp59.wfb, bmp61.wfb,bmp62.wfb,bmp6t.wfb,bmp64.wfb,bmp6t.wfb,bmp66.wfb,bmp6t.wfb,bmp68.wfb,bmp69.wfb, 7a.wfb,bmp7t.wfb,bmp79.wfb, bmp81.wfb,bmp8a.wfb,bmp8t.wfb,bmp84.wfb,bmp85.wfb,bmp86.wfb,bmp8t.wfb,bmp8a.wfb,bmp89.wfb, 9t.wfb,bmp9t.wfb,bmp99.wfb, bmp101.wfb,bmp102.wfb,bmp102.wfb,bmp104.wfb,bmp105.wfb,bmp106.wfb,bmp108.wfb,bmp108.wfb,bmp109.wfb ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 24 页 - - - - - - - - - char cursor1414= 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; position109,redcurpos,redtemppos,redoldpos,blackcurpos,blacktemppos,blackoldpos; /*-*/ selectpage(register char page) /* 换页函数 */ union REGS r; r.x.ax=0 x4f05; r.x.bx=0; r.x.dx=page; /* 选择页面 */ int86(0 x10,&r,&r); unsigned char set_SVGA_mode(int vmode) /*设置 SVGA 屏幕模式 */ union REGS r; r.x.ax=0 x4f02; r.x.bx=vmode; int86(0 x10,&r,&r); return(r.h.ah); unsigned int get_SVGA_mode() /*获取当前SVGA 屏幕模式 */ union REGS r; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 24 页 - - - - - - - - - r.x.ax=0 x4f03; int86(0 x10,&r,&r); return(r.x.bx); drawbmp(int start_x,int start_y ,char filename) char buffer640; int i,j,k,n,r,g,b,width,length; long position; FILE *fp; if(fp=fopen(filename,rb)=NULL) printf(Error! Cant open file!); getch(); return; fseek(fp,28,SEEK_SET); fread(&i,2,1,fp); if(i!=8) /* 检查是否为256 色位图 */ puts(Error!Cant 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;i2); /* 右移是要转化为VGA 的 6 位寄存器形式*/ outportb(0 x3c9,g2); outportb(0 x3c9,b2); fgetc(fp); else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 24 页 - - - - - - - - - fseek(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;iwidth+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(0 xa000,position%65536,buffern); /*写到显存位置*/ fseek(fp,k,SEEK_CUR); /* 每行绘制完后修正宽度*/ fclose(fp); init() savemode=get_SVGA_mode(); /* 先保存原来的屏幕模式*/ set_SVGA_mode(0 x101); /* 硬件无关性初始化屏幕为640*480 256 色模式 */ end() set_SVGA_mode(savemode); /* 恢复屏幕 */ /*-*/ initpos() int i,j; for(i=0;i10;i+) for (j=0;j9;j+) positionij.x=35+i*39; positionij.y=43+j*40; initchessmap() board00=BLACK_JU; board01=BLACK_MA; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 24 页 - - - - - - - - - board02=BLACK_XIANG; board03=BLACK_SHI; board04=BLACK_JIANG; board05=BLACK_SHI; board06=BLACK_XIANG; board07=BLACK_MA; board08=BLACK_JU; board21=BLACK_PAO; board27=BLACK_PAO; board30=BLACK_BIN; board32=BLACK_BIN; board34=BLACK_BIN; board36=BLACK_BIN; board38=BLACK_BIN; board90=RED_JU; board91=RED_MA; board92=RED_XIANG; board93=RED_SHI; board94=RED_JIANG; board95=RED_SHI; board96=RED_XIANG; board97=RED_MA; board98=RED_JU; board71=RED_PAO; board77=RED_PAO; board60=RED_BIN; board62=RED_BIN; board64=RED_BIN; board66=RED_BIN; board68=RED_BIN; initdrawchess() int i,j; for(i=0;i10;i+) for(j=0;j=x;j-,m-) for(i=y,n=0;i=x;j-,m-) for(i=y,n=0;i=RED_JU&boardxy=RED_JU&boardxy=5) if(y!=oldy|(oldx-x)!=1) return 0; else if(x=(oldx-1)&y=oldy) return 1; else if(x=oldx&y=(oldy+1) return 1; else if(x=oldx&y=(oldy-1) return 1; else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 24 页 - - - - - - - - - return 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(x1|(oldy-y)1) return 0; else if(y5) return 0; break; case RED_JU: /* 完成 */ if(x!=oldx&y!=oldy) return 0; else if(x!=oldx) min=(xoldx)?oldx:x; max=(xoldx)?x:oldx; for(i=min+1;ioldy)?oldy:y; max=(yoldy)?y:oldy; for(i=min+1;ioldx)?oldx:x; max=(xoldx)?x:oldx; for(i=min+1;ioldy)?oldy:y; max=(yoldy)?y:oldy; for(i=min+1;ioldx)?oldx:x; max=(xoldx)?x:oldx; for(i=min+1,j=0;ioldy)?oldy:y; max=(yoldy)?y:oldy; for(i=min+1,j=0;imax;i+) if(boardxi!=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; else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 24 页 - - - - - - - - - if(x=9&y=5) return 1; else if(x=7&y=3) return 1; else if(x=7&y=5) return 1; else return 0; else return 0; break; case RED_XIANG: /* 完成 */ if(x0) redcurpos.x-; x=positionredtemppos.xredtemppos.y.x; y=positionredtemppos.xredtemppos.y.y; if(boardredtemppos.xredtemppos.y=0) drawbmp(x,y,boardfileredtemppos.xredtemppos.y); else if(!(redtemppos.x=redoldpos.x&redtemppos.y=redoldpos.y&redstate=MOVE) n=boardredtemppos.xredtemppos.y; drawbmp(x,y,chessfilen); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 24 页 - - - - - - - - - if(redtemppos.x=redoldpos.x&redtemppos.y=redoldpos.y&redstate=MOVE)drawselecursor(redoldpos); drawcursor(redcurpos); redtemppos.x=redcurpos.x; redtemppos.y=redcurpos.y; reddown() int x,y,n; if(redcurpos.x0) redcurpos.y-; x=positionredtemppos.xredtemppos.y.x; y=positionredtemppos.xredtemppos.y.y; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 24 页 - - - - - - - - - if(boardredtemppos.xredtemppos.y=0) drawbmp(x,y,boardfileredtemppos.xredtemppos.y); else if(!(redtemppos.x=redoldpos.x&redtemppos.y=redoldpos.y&redstate=MOVE) n=boardredtemppos.xredtemppos.y; drawbmp(x,y,chessfilen); if(redtemppos.x=redoldpos.x&redtemppos.y=redoldpos.y&redstate=MOVE)drawselecursor(redoldpos); drawcursor(redcurpos); redtemppos.x=redcurpos.x; redtemppos.y=redcurpos.y; redright() int x,y,n; if(redcurpos.y8) redcurpos.y+; x=positionredtemppos.xredtemppos.y.x; y=positionredtemppos.xredtemppos.y.y; if(boardredtemppos.xredtemppos.y=0) drawbmp(x,y,boardfileredtemppos.xredtemppos.y); else if(!(redtemppos.x=redoldpos.x&redtemppos.y=redoldpos.y&redstate=MOVE) n=boardredtemppos.xredtemppos.y; drawbmp(x,y,chessfilen); if(redtemppos.x=redoldpos.x&redtemppos.y=redoldpos.y&redstate=MOVE)drawselecursor(redoldpos); drawcursor(redcurpos); redtemppos.x=redcurpos.x; redtemppos.y=redcurpos.y; reddo() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 24 页 - - - - - - - - - int i,j,x,y,n; if(redstate=SELECT&redcanselect() if(boardredcurpos.xredcurpos.y0) redstate=MOVE; drawselecursor(redcurpos); redoldpos.x=redcurpos.x; redoldpos.y=redcurpos.y; else if(redstate=MOVE&redcanmove() x=positionredoldpos.xredoldpos.y.x; y=positionredoldpos.xredoldpos.y.y; drawbmp(x,y,boardfileredoldpos.xredoldpos.y); x=positionredcurpos.xredcurpos.y.x; y=positionredcurpos.xredcurpos.y.y; n=boardredoldpos.xredoldpos.y; drawbmp(x,y,chessfilen); if(boardredcurpos.xredcurpos.y=BLACK_JIANG) winner=RED; finish=1; return; boardredcurpos.xredcurpos.y=n; boardredoldpos.xredoldpos.y=0; for(i=0;i=2;i+) for(j=3;j=5;j+) if(boardij=BLACK_JIANG)x=i;y=j; for(i=x+1,j=y,n=0;i=BLACK_JU&boardxy=BLACK_JU&boardxyoldx)?oldx:x; max=(xoldx)?x:oldx; for(i=min+1;ioldy)?oldy:y; max=(yoldy)?y:oldy; for(i=min+1;ioldx)?oldx:x; max=(xoldx)?x:oldx; for(i=min+1;ioldy)?oldy:y; max=(yoldy)?y:oldy; for(i=min+1;ioldx)?oldx:x; max=(xoldx)?x:oldx; for(i=min+1;ioldy)?oldy:y; max=(yoldy)?y:oldy; for(i=min+1;i4) 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(boardij!=0) return 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 24 页 - - - - - - - - - else return 0; break; case BLACK_SHI: if(oldx=0|oldx=2) if(x!=1|y!=4) return 0; else if(oldx=1) if(x=0&y=3) return 1; else if(x=0&y=5) return 1; else if(x=2&y=3) return 1; else if(x=2&y=5) return 1; else return 0; else return 0; break; case BLACK_JIANG: if(x!=oldx&y!=oldy) return 0; if(x!=oldx) if(x-oldx)1|(oldx-x)1) return 0; else if(x2) return 0; else if(y!=oldy) if(y-oldy)1|(oldy-y)1) return 0; else if(y5) return 0; break; case BLACK_BIN: if(oldx0) blackcurpos.x-; x=positionblacktemppos.xblacktemppos.y.x; y=positionblacktemppos.xblacktemppos.y.y; if(boardblacktemppos.xblacktemppos.y=0) drawbmp(x,y,boardfileblacktemppos.xblacktemppos.y); else if(!(blacktemppos.x=blackoldpos.x&blacktemppos.y=blackoldpos.y&blackstate=MOVE) n=boardblacktemppos.xblacktemppos.y; drawbmp(x,y,chessfilen); if(blacktemppos.x=blackoldpos.x&blacktemppos.y=blackoldpos.y&blackstate=MOVE)drawselecursor(blackoldpos); drawcursor(blackcurpos); blacktemppos.x=blackcurpos.x; blacktemppos.y=blackcurpos.y; blackdown() int x,y,n; if(blackcurpos.x0) blackcurpos.y-; x=positionblacktemppos.xblacktemppos.y.x; y=positionblacktemppos.xblacktemppos.y.y; if(boardblacktemppos.xblacktemppos.y=0) drawbmp(x,y,boardfileblacktemppos.xblacktemppos.y); else if(!(blacktemppos.x=blackoldpos.x&blacktemppos.y=blackoldpos.y&blackstate=MOVE) n=boardblacktemppos.xblacktemppos.y; drawbmp(x,y,chessfilen); if(blacktemppos.x=blackoldpos.x&blacktemppos.y=blackoldpos.y&blackstate=MOVE)drawselecursor(blackoldpos); drawcursor(blackcurpos); blacktemppos.x=blackcurpos.x; blacktemppos.y=blackcurpos.y; blackright() int x,y,n; if(blackcurpos.y8) blackcurpos.y+; x=positionblacktemppos.xblacktemppos.y.x; y=positionblacktemppos.xblacktemppos.y.y; if(boardblacktemppos.xblacktemppos.y=0) drawbmp(x,y,boardfileblacktemppos.xblacktemppos.y); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 20 页,共 24 页 - - - - - - - - - else if(!(blacktemppos.x=blackoldpos.x&blacktemppos.y=blackoldpos.y&blackstate=MOVE) n=boardblacktemppos.xblacktemppos.y; drawbmp(x,y,chessfilen); if(blacktemppos.x=blackoldpos.x&blacktemppos.y=blackoldpos.y&blackstate=MOVE)drawselecursor(blackoldpos); drawcursor(blackcurpos); blacktemppos.x=blackcurpos.x; blacktemppos.y=blackcurpos.y; blackdo() int i,j,x,y,n; if(blackstate=SELECT&blackcanselect() if(boardblackcurpos.xblackcurpos.y0) blackstate=MOVE; drawselecursor(blackcurpos); blackoldpos.x=blackcurpos.x; blackoldpos.y=blackcurpos.y; else if(blackstate=MOVE&blackcanmove() x=positionblackoldpos.xblackoldpos.y.x; y=positionblackoldpos.xblackoldpos.y.y; drawbmp(x,y,boardfileblackoldpos.xblackoldpos.y); x=positionblackcurpos.xblackcurpos.y.x; y=positionblackcurpos.xblackcurpos.y.y; n=boardblackoldpos.xblackoldpos.y; drawbmp(x,y,chessfilen); if(boardblackcurpos.xblackcurpos.y=RED_JIANG) winner=BLACK; finish=1; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 21 页,共 24 页 - - - - - - - - - return; boardblackcurpos.xblackcurpos.y=n; boardblackoldpos.xblackoldpos.y=0; for(i=0;i=2;i+) for(j=3;j=5;j+) if(boardij=BLACK_JIANG)x=i;y=j; for(i=x+1,j=y,n=0;i=9;i+) if(boardij=RED_JIANG&n=0) winner=RED;finish=1;break; else if(boardij!=0) n+; turn=RED; redstate=SELECT; drawcursor(redcurpos); drawbmp(30,438,bmprzq.wfb); /* 转交控制权给红方*/ blackundo() int x,y,n; if(blackstate=MOVE) x=positionblackoldpos.xblackoldpos.y.x; y=positionblackoldpos.xblackoldpos.y.y; n=boardblackoldpos.xblackoldpos.y; drawbmp(x,y,chessfilen); blackoldpos.x=blackcurpos.x; blackoldpos.y=blackcurpos.y; drawcursor(blackcurpos); blackstate=SELECT; /*-*/ start() drawcursor(blackcurpos); drawbmp(30,438,bmpbzq.wfb); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 22 页,共 24 页 - - - - - - - - - while(!finish) key=getkey(); switch(key) case RED_UP: if(turn=RED) redup(); break; case RED_DOWN: if(turn=RED) reddown(); break; case RED_LEFT: if(turn=RED) redleft(); break; case RED_RIGHT: if(turn=RED) redright(); break; case RED_DO: if(turn=RED) reddo(); break; case RED_UNDO: if(turn=RED) redundo(); break; case BLACK_UP: if(turn=BLACK) blackup(); break; case BLACK_DOWN: if(turn=BLACK) blackdown(); break; case BLACK_LEFT: if(turn=BLACK) blackleft(); break; case BLACK_RIGHT: if(turn=BLACK) blackright(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 23 页,共 24 页 - - - - - - - - - break; case BLACK_DO: if(turn=BLACK) blackdo(); break; case BLACK_UNDO: if(turn=BLACK) blackundo(); break; case ESCAPE: finish=1;break; main() init(); initpos(); initchessmap(); drawbmp(0,0,bmpboard.wfb); initdrawchess(); /*初始化光标位置*/ redcurpos.x=redoldpos.x=redtemppos.x=9; redcurpos.y=redoldpos.y=redtemppos.y=8; blackcurpos.x=blackoldpos.x=blacktemppos.x=0; blackcurpos.y=blackoldpos.y=blacktemppos.y=0; /*开始 */ start(); if(winner=RED) drawbmp(200,200,bmpredwin.wfb); else if(winner=BLACK) drawbmp(200,200,bmpblackwin.wfb); else drawbmp(200,200,bmpexit.wfb); getch(); end(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 24 页,共 24 页 - - - - - - - - -