语言编写象棋程序代码_第1页
语言编写象棋程序代码_第2页
语言编写象棋程序代码_第3页
语言编写象棋程序代码_第4页
语言编写象棋程序代码_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、/*-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#defi

2、ne 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

3、 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 board109;/*-*/char *chessfil

4、e15="","bmprju.wfb","bmprma.wfb","bmprxiang.wfb","bmprshi.wfb","bmprjiang.wfb","bmprpao.wfb","bmprbin.wfb","bmpbju.wfb","bmpbma.wfb","bmpbxiang.wfb","bmpbshi.wfb","bmpbjiang.w

5、fb","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",&qu

6、ot;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&

7、quot;,"bmp3t.wfb","bmp3a.wfb","bmp29.wfb", "bmp41.wfb","bmp4t.wfb","bmp4a.wfb","bmp4t.wfb","bmp4a.wfb","bmp4t.wfb","bmp4a.wfb","bmp4t.wfb","bmp49.wfb", "bmp51.wfb","

8、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&quo

9、t;,"bmp6t.wfb","bmp68.wfb","bmp69.wfb", "bmp71.wfb","bmp7t.wfb","bmp7a.wfb","bmp7t.wfb","bmp7a.wfb","bmp7t.wfb","bmp7a.wfb","bmp7t.wfb","bmp79.wfb", "bmp81.wfb","bmp

10、8a.wfb","bmp8t.wfb","bmp84.wfb","bmp85.wfb","bmp86.wfb","bmp8t.wfb","bmp8a.wfb","bmp89.wfb", "bmp91.wfb","bmp9t.wfb","bmp9t.wfb","bmp9t.wfb","bmp95.wfb","bmp9t.wfb",

11、"bmp9t.wfb","bmp9t.wfb","bmp99.wfb", "bmp101.wfb","bmp102.wfb","bmp102.wfb","bmp104.wfb","bmp105.wfb","bmp106.wfb","bmp108.wfb","bmp108.wfb","bmp109.wfb"char cursor1414=0,0,0,0,0,0

12、,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

13、,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,bl

14、acktemppos,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);unsign

15、ed 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 buffer640; int i,j,k,n,r,g,b,width,length; long position; FILE *fp; if(fp=fopen(filename,"rb")=NULL) printf("Error! Can

16、9;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);

17、 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); else fseek(fp,300,SEEK_SET); k=(width%4)?(4-width%4):0; /*宽度修正值*/ f

18、or(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,buf

19、fern); /*写到显存位置*/ 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+) positionij.x=35+i*39; positionij.

20、y=43+j*40; initchessmap() board00=BLACK_JU; board01=BLACK_MA; 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; board3

21、6=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

22、_BIN;initdrawchess() int i,j; for(i=0;i<10;i+) for(j=0;j<9;j+) if(boardij)drawbmp(positionij.x,positionij.y,chessfileboardij); drawcursor(struct pos p) int i,j,n,m,x,y; long thisposition; x=positionp.xp.y.x+20; y=positionp.xp.y.y+25; for(j=13-1+x,m=13;j>=x;j-,m-) for(i=y,n=0;i<13+y;i+,n+

23、) thisposition=j*640l+i; /*计算要显示点的显存位置*/ page_new=thisposition/65536; /*计算显示页*/ if(page_new!=page_old) /*当显示页不同时更换页面,提高一定的输出速度*/ selectpage(page_new);page_old=page_new; if(cursormn!=1)if(cursormn=0) pokeb(0xa000,thisposition%65536,0);else if(turn=RED) pokeb(0xa000,thisposition%65536,153); else pokeb

24、(0xa000,thisposition%65536,255); drawselecursor(struct pos p) int i,j,n,m,x,y; long thisposition; x=positionp.xp.y.x+20; y=positionp.xp.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!=pag

25、e_old) /*当显示页不同时更换页面,提高一定的输出速度*/ selectpage(page_new);page_old=page_new; if(cursormn!=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(bo

26、ardxy>=RED_JU&&boardxy<=RED_BIN) return 1; else return 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(boardxy>=RED_JU&&boardxy<=RED_BIN) return 0;/* 军、马、炮、相、士、将、卒的走法正确性的判断*/ switch

27、(boardoldxoldy) case RED_BIN: /*完成*/ if(oldx>=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 return 0; break; case RED_JIANG: /*完成*/ if(x!=oldx&&y!=oldy) retu

28、rn 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

29、:oldx; for(i=min+1;i<max;i+) if(boardiy!=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(boardxi!=0) return 0; break; case RED_MA: /*完成*/ if(x-oldx)=2&&(y-oldy)=1|(oldy-y)=1) if(boardoldx+1oldy!=0) return 0; else if(oldx-x)=2&&

30、amp;(y-oldy)=1|(oldy-y)=1) if(boardoldx-1oldy!=0) return 0; else if(y-oldy)=2&&(x-oldx)=1|(oldx-x)=1) if(boardoldxoldy+1!=0) return 0; else if(oldy-y)=2&&(x-oldx)=1|(oldx-x)=1) if(boardoldxoldy-1!=0) return 0; else return 0; break; case RED_PAO: /*完成*/ if(x!=oldx&&y!=oldy) re

31、turn 0; if(boardxy=0) if(x!=oldx) min=(x>oldx)?oldx:x; max=(x>oldx)?x:oldx; for(i=min+1;i<max;i+) if(boardiy!=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(boardxi!=0) return 0; else if(x!=oldx) min=(x>oldx)?oldx:x; max=(x>ol

32、dx)?x:oldx; for(i=min+1,j=0;i<max;i+) if(boardiy!=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(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(

33、x=9&&y=3) return 1; else 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(x<5) return 0; if(x!=oldx&&y!=oldy) if(x-oldx)=2&&(y-oldy)=2) i=oldx+1;j=oldy+1; e

34、lse 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; else return 0; break; return 1;redup() int x,y,n; if(redcurpos.x>0) redcurpos.x-; x=

35、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,ches

36、sfilen); 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.x<9) redcurpos.x+; x=positionredtemppos.xredtemppos.y.x; y=positio

37、nredtemppos.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&&

38、;redtemppos.y=redoldpos.y&&redstate=MOVE) drawselecursor(redoldpos); drawcursor(redcurpos); redtemppos.x=redcurpos.x; redtemppos.y=redcurpos.y; redleft() int x,y,n; if(redcurpos.y>0) redcurpos.y-; x=positionredtemppos.xredtemppos.y.x; y=positionredtemppos.xredtemppos.y.y; if(boardredtempp

39、os.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=M

40、OVE) drawselecursor(redoldpos); drawcursor(redcurpos); redtemppos.x=redcurpos.x; redtemppos.y=redcurpos.y; redright() int x,y,n; if(redcurpos.y<8) redcurpos.y+; x=positionredtemppos.xredtemppos.y.x; y=positionredtemppos.xredtemppos.y.y; if(boardredtemppos.xredtemppos.y=0) drawbmp(x,y,boardfilered

41、temppos.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(r

42、edcurpos); redtemppos.x=redcurpos.x; redtemppos.y=redcurpos.y; reddo() int i,j,x,y,n; if(redstate=SELECT&&redcanselect() if(boardredcurpos.xredcurpos.y<=RED&&boardredcurpos.xredcurpos.y>0) redstate=MOVE; drawselecursor(redcurpos); redoldpos.x=redcurpos.x; redoldpos.y=redcurpos.

43、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(boar

44、dredcurpos.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<=9;i+) if(boardij=RED_JIANG&&n=0) winner=BLACK;finish=1;break; else

45、 if(boardij!=0) n+; turn=BLACK; blackstate=SELECT; drawcursor(blackcurpos); drawbmp(30,438,"bmpbzq.wfb"); /*转交控制权给黑方*/ redundo() int x,y,n; if(redstate=MOVE) x=positionredoldpos.xredoldpos.y.x; y=positionredoldpos.xredoldpos.y.y; n=boardredoldpos.xredoldpos.y; drawbmp(x,y,chessfilen); redo

46、ldpos.x=redcurpos.x; redoldpos.y=redcurpos.y; drawcursor(redcurpos); redstate=SELECT; /*-黑方操作-*/int blackcanselect() int x,y; x=blackcurpos.x; y=blackcurpos.y; if(boardxy>=BLACK_JU&&boardxy<=BLACK_BIN) return 1; else return 0;int blackcanmove() int i,j,min,max,oldx,oldy,x,y; oldx=blackoldpos.x; oldy=blackoldpos.y; x=blackcurpos.x; y=blackcurpos.y;/*case1 目标位置是否是自己人*/ if(boardxy>=BLACK_JU&am

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论