C語言編寫象棋程序代碼_第1頁
C語言編寫象棋程序代碼_第2頁
C語言編寫象棋程序代碼_第3頁
C語言編寫象棋程序代碼_第4頁
C語言編寫象棋程序代碼_第5頁
已閱讀5頁,還剩20頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

C語言編寫象棋程序代碼C語言編寫象棋程序代碼C語言編寫象棋程序代碼資料僅供參考文件編號:2022年4月C語言編寫象棋程序代碼版本號:A修改號:1頁次:1.0審核:批準(zhǔn):發(fā)布日期:/**/#include""#include""/**/#defineRED7#defineBLACK14#definetrue1#definefalse0#defineSELECT0#defineMOVE1#defineRED_UP0x1100#defineRED_DOWN0x1f00#defineRED_LEFT0x1e00#defineRED_RIGHT0x2000#defineRED_DO0x3900#defineRED_UNDO0x1000#defineBLACK_UP0x4800#defineBLACK_DOWN0x5000#defineBLACK_LEFT0x4b00#defineBLACK_RIGHT0x4d00#defineBLACK_DO0x1c00#defineBLACK_UNDO0x2b00#defineESCAPE0x0100#defineRED_JU1#defineRED_MA2#defineRED_XIANG3#defineRED_SHI4#defineRED_JIANG5#defineRED_PAO6#defineRED_BIN7#defineBLACK_JU8#defineBLACK_MA9#defineBLACK_XIANG10#defineBLACK_SHI11#defineBLACK_JIANG12#defineBLACK_PAO13#defineBLACK_BIN14/**/intfirsttime=1;intsavemode;charpage_new=0,page_old=0;intfinish=false,turn=BLACK,winner=0;intkey;intredstate=SELECT,blackstate=SELECT;intboard[10][9];/**/char*chessfile[15]={"", "bmp\\", "bmp\\", "bmp\\","bmp\\", "bmp\\","bmp\\","bmp\\", "bmp\\", "bmp\\", "bmp\\","bmp\\", "bmp\\","bmp\\","bmp\\"};char*boardfile[10][9]={{"bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\"},{"bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\"},{"bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\"},{"bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\"},{"bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\"},{"bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\"},{"bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\"},{"bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\"},{"bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\"},{"bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\","bmp\\"}};charcursor[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};structpos{intx;inty;}position[10][9],redcurpos,redtemppos,redoldpos,blackcurpos,blacktemppos,blackoldpos;/**/selectpage(registercharpage)/*換頁函數(shù)*/{unionREGSr;/*選擇頁面*/int86(0x10,&r,&r);}unsignedcharset_SVGA_mode(intvmode)/*設(shè)置SVGA屏幕模式*/{unionREGSr;int86(0x10,&r,&r);returnintget_SVGA_mode()/*獲取當(dāng)前SVGA屏幕模式*/{unionREGSr;int86(0x10,&r,&r);returnstart_x,intstart_y,charfilename[]){charbuffer[640];inti,j,k,n,r,g,b,width,length;longposition;FILE*fp;if((fp=fopen(filename,"rb"))==NULL){printf("Error!Can'topenfile!");getch();return;}fseek(fp,28,SEEK_SET);fread(&i,2,1,fp);if(i!=8)/*檢查是否為256色位圖*/{puts("Error!Can'tfindbitmap!");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色表設(shè)置色彩寄存器*/{b=fgetc(fp);g=fgetc(fp);r=fgetc(fp);/*獲取R、G、B分量*/outportb(0x3c8,i);outportb(0x3c9,r>>2);/*右移是要轉(zhuǎn)化為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)/*當(dāng)顯示頁不同時更換頁面,提高一定的輸出速度*/{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);/*硬件無關(guān)性初始化屏幕為640*480256色模式*/}end(){set_SVGA_mode(savemode);/*恢復(fù)屏幕*/}/**/initpos(){inti,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(){inti,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(structposp){inti,j,n,m,x,y;longthisposition;x=position[][].x+20;y=position[][].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)/*當(dāng)顯示頁不同時更換頁面,提高一定的輸出速度*/{selectpage(page_new);page_old=page_new;}if(cursor[m][n]!=1) if(cursor[m][n]==0) pokeb(0xa000,thisposition%65536,0); else if(turn==RED) pokeb(0xa000,thisposition%65536,153); else pokeb(0xa000,thisposition%65536,255);}}}drawselecursor(structposp){inti,j,n,m,x,y;longthisposition;x=position[][].x+20;y=position[][].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)/*當(dāng)顯示頁不同時更換頁面,提高一定的輸出速度*/{selectpage(page_new);page_old=page_new;}if(cursor[m][n]!=1) pokeb(0xa000,thisposition%65536,0);}}}/**/intgetkey(){intpress;while(bioskey(1)==0);press=bioskey(0);press=press&0xff00;return(press);}/*紅方操作*/intredcanselect(){intx,y;x=;y=;if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)return1;elsereturn0;}intredcanmove(){inti,j,min,max,oldx,oldy,x,y;oldx=;oldy=;x=;y=;/*case1目標(biāo)位置是否是自己人*/if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN)return0;/*軍、馬、炮、相、士、將、卒的走法正確性的判斷*/switch(board[oldx][oldy]){caseRED_BIN:/*完成*/if(oldx>=5){if(y!=oldy||(oldx-x)!=1)return0;}else{if(x==(oldx-1)&&y==oldy)return1;elseif(x==oldx&&y==(oldy+1))return1;elseif(x==oldx&&y==(oldy-1))return1;elsereturn0;}break;caseRED_JIANG:/*完成*/if(x!=oldx&&y!=oldy)return0;if(x!=oldx)if((x-oldx)>1||(oldx-x)>1)return0;elseif(x<7)return0;elseif(y!=oldy)if((y-oldy)>1||(oldy-y)>1)return0;elseif(y<3||y>5)return0;break;caseRED_JU:/*完成*/if(x!=oldx&&y!=oldy)return0;elseif(x!=oldx){min=(x>oldx)oldx:x;max=(x>oldx)x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0)return0;}elseif(y!=oldy){min=(y>oldy)oldy:y;max=(y>oldy)y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0)return0;}break;caseRED_MA:/*完成*/if((x-oldx)==2&&((y-oldy)==1||(oldy-y)==1)){ if(board[oldx+1][oldy]!=0)return0;}elseif((oldx-x)==2&&((y-oldy)==1||(oldy-y)==1)){ if(board[oldx-1][oldy]!=0)return0;}elseif((y-oldy)==2&&((x-oldx)==1||(oldx-x)==1)){ if(board[oldx][oldy+1]!=0)return0;}elseif((oldy-y)==2&&((x-oldx)==1||(oldx-x)==1)){ if(board[oldx][oldy-1]!=0)return0;}elsereturn0; break;caseRED_PAO:/*完成*/if(x!=oldx&&y!=oldy)return0;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)return0;}elseif(y!=oldy){min=(y>oldy)oldy:y;max=(y>oldy)y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0)return0;}}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)return0;}elseif(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)return0;}}break;caseRED_SHI:/*完成*/if(oldx==9||oldx==7){if(x!=8||y!=4)return0;}elseif(oldx==8){if(x==9&&y==3)return1;elseif(x==9&&y==5)return1;elseif(x==7&&y==3)return1;elseif(x==7&&y==5)return1;elsereturn0;}elsereturn0;break;caseRED_XIANG:/*完成*/if(x<5)return0;if(x!=oldx&&y!=oldy){if((x-oldx)==2&&(y-oldy)==2){i=oldx+1;j=oldy+1;}elseif((x-oldx)==2&&(oldy-y)==2){i=oldx+1;j=oldy-1;}elseif((oldx-x)==2&&(y-oldy)==2){i=oldx-1;j=oldy+1;}elseif((oldx-x)==2&&(oldy-y)==2){i=oldx-1;j=oldy-1;}elsereturn0;if(board[i][j]!=0)return0;}elsereturn0;break;}return1;}redup(){intx,y,n;if>0){;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);elseif(!==&&==&&redstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);=;=;}}reddown(){intx,y,n;if<9){++;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);elseif(!==&&==&&redstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);=;=;}}redleft(){intx,y,n;if>0){;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);elseif(!==&&==&&redstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);=;=;}}redright(){intx,y,n;if<8){++;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);elseif(!==&&==&&redstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);=;=;}}reddo(){inti,j,x,y,n;if(redstate==SELECT&&redcanselect()){if(board[][]<=RED&&board[][]>0){redstate=MOVE;drawselecursor(redcurpos);=;=;}}elseif(redstate==MOVE&&redcanmove()){x=position[][].x;y=position[][].y;drawbmp(x,y,boardfile[][]);x=position[][].x;y=position[][].y;n=board[][];drawbmp(x,y,chessfile[n]);if(board[][]==BLACK_JIANG){winner=RED;finish=1;return;}board[][]=n;board[][]=0;for(i=0;i<=2;i++)for(j=3;j<=5;j++)if(board[i][j]==BLACK_JIANG){x=i;y=j;}for(i=x+1,j=y,n=0;i<=9;i++){if(board[i][j]==RED_JIANG&&n==0){winner=BLACK;finish=1;break;}elseif(board[i][j]!=0)n++;}turn=BLACK;blackstate=SELECT;drawcursor(blackcurpos);drawbmp(30,438,"bmp\\");/*轉(zhuǎn)交控制權(quán)給黑方*/}}redundo(){intx,y,n;if(redstate==MOVE){x=position[][].x;y=position[][].y;n=board[][];drawbmp(x,y,chessfile[n]);=;=;drawcursor(redcurpos);redstate=SELECT;}}/*黑方操作*/intblackcanselect(){intx,y;x=;y=;if(board[x][y]>=BLACK_JU&&board[x][y]<=BLACK_BIN)return1;elsereturn0;}intblackcanmove(){inti,j,min,max,oldx,oldy,x,y;oldx=;oldy=;x=;y=;/*case1目標(biāo)位置是否是自己人*/if(board[x][y]>=BLACK_JU&&board[x][y]<=BLACK_BIN)return0;/*軍、馬、炮、相、士、將、卒的走法正確性的判斷*/switch(board[oldx][oldy]){caseBLACK_JU:/*完成*/if(x!=oldx&&y!=oldy)return0;elseif(x!=oldx){min=(x>oldx)oldx:x;max=(x>oldx)x:oldx;for(i=min+1;i<max;i++)if(board[i][y]!=0)return0;}elseif(y!=oldy){min=(y>oldy)oldy:y;max=(y>oldy)y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0)return0;}break;caseBLACK_MA:/*完成*/if((x-oldx)==2&&((y-oldy)==1||(oldy-y)==1)){ if(board[oldx+1][oldy]!=0)return0;}elseif((oldx-x)==2&&((y-oldy)==1||(oldy-y)==1)){ if(board[oldx-1][oldy]!=0)return0;}elseif((y-oldy)==2&&((x-oldx)==1||(oldx-x)==1)){ if(board[oldx][oldy+1]!=0)return0;}elseif((oldy-y)==2&&((x-oldx)==1||(oldx-x)==1)){ if(board[oldx][oldy-1]!=0)return0;}elsereturn0;break;caseBLACK_PAO:/*完成*/if(x!=oldx&&y!=oldy)return0;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)return0;}elseif(y!=oldy){min=(y>oldy)oldy:y;max=(y>oldy)y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0)return0;}}else{j=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)j++;if(j!=1)return0;}elseif(y!=oldy){min=(y>oldy)oldy:y;max=(y>oldy)y:oldy;for(i=min+1;i<max;i++)if(board[x][i]!=0)j++;if(j!=1)return0;}}break;caseBLACK_XIANG:if(x>4)return0;if(x!=oldx&&y!=oldy){if((x-oldx)==2&&(y-oldy)==2){i=oldx+1;j=oldy+1;}elseif((x-oldx)==2&&(oldy-y)==2){i=oldx+1;j=oldy-1;}elseif((oldx-x)==2&&(y-oldy)==2){i=oldx-1;j=oldy+1;}elseif((oldx-x)==2&&(oldy-y)==2){i=oldx-1;j=oldy-1;}elsereturn0;if(board[i][j]!=0)return0;}elsereturn0;break;caseBLACK_SHI:if(oldx==0||oldx==2){if(x!=1||y!=4)return0;}elseif(oldx==1){if(x==0&&y==3)return1;elseif(x==0&&y==5)return1;elseif(x==2&&y==3)return1;elseif(x==2&&y==5)return1;elsereturn0;}elsereturn0;break;caseBLACK_JIANG:if(x!=oldx&&y!=oldy)return0;if(x!=oldx)if((x-oldx)>1||(oldx-x)>1)return0;elseif(x>2)return0;elseif(y!=oldy)if((y-oldy)>1||(oldy-y)>1)return0;elseif(y<3||y>5)return0;break;caseBLACK_BIN:if(oldx<=4){if(y!=oldy||(x-oldx)!=1)return0;}else{if(x==(oldx+1)&&y==oldy)return1;elseif(x==oldx&&y==(oldy+1))return1;elseif(x==oldx&&y==(oldy-1))return1;elsereturn0;}break;}return1;}blackup(){intx,y,n;if>0){;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);elseif(!==&&==&&blackstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&blackstate==MOVE)drawselecursor(blackoldpos);drawcursor(blackcurpos);=;=;}}blackdown(){intx,y,n;if<9){++;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);elseif(!==&&==&&blackstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&blackstate==MOVE)drawselecursor(blackoldpos);drawcursor(blackcurpos);=;=;}}blackleft(){intx,y,n;if>0){;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);elseif(!==&&==&&blackstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&blackstate==MOVE)drawselecursor(blackoldpos);drawcursor(blackcurpos);=;=;}}blackright(){intx,y,n;if<8){++;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);elseif(!==&&==&&blackstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&blackstate==MOVE)drawselecursor(blackoldpos);drawcursor(blackcurpos);=;=;}}blackdo(){inti,j,x,y,n;if(blackstate==SELECT&&blackcanselect()){if(board[][]<=BLACK&&board[][]>0){blackstate=MOVE;drawselecursor(blackcurpos);=;=;}}elseif(blackstate==MOVE&&blackcanmove()){x=position[][].x;y=position[][].y;drawbmp(x,y,boardfile[][]);x

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論