三子棋c代码
C语言编写象棋软件源代码
chess.c*/ #include °'dos.h”include "stdio.h”/* ----------------------------------------------------- */ #define RED 7#define BLACK 14#define tnie 1#define false 0#define SELECTT 0#define MOVE 1#define RED_UPOxllOO#define RED_DOWN OxlfOO#define RED_LEFT OxleOO#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 OxleOO#define BLACK_UNDO Ox2bOO#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 BLACKJIANG 12#define BLACK_PAO 13#define BLACK_BIN 14/* ----------------------------------------------------- */ int firsttime=l;int savemode;char page_new=0.page_old=0;int finish=faIse,turn=BLACK r winner=O;int key;int redstate=SELECT,blackstate=SELECT;int board[10][9];/* ----------------------------------------------------- */char ♦chess]={',,\”bmp\\iju・wfb”,”bmp\\rma・wfb”「bmp\\rxiang・wft/「bmp\\rshi・wfly;H bmp\\ijiang.wfb,\H bmp\\rpao.wfb,,/bmp\\rbin.wfb,\"bmpWbju.wfb*;,'bmp\\bma.wfb,\ H bmp\\bxiangAvfb^M bmp\\bshi.wfb,\H bmp\\bjiang.wfb,\u bmp\\bpao.wfb H,H bmp\\bbin.wfb,'};char *board][9]={{”bmp\\ll・wfbTbmp\\lt・wfbTbmp\\lt・wfbTbmp\\14・wfbTbmp\\15.wfbTbmp\\16・wfb'「bmp \\ltwfb^,,bmp\\lt.wfb,\H bmp\\19.wfb,t)t{”bmp\\21・wny「bmp\\2c・wny「bmp\\2c・wfly「bmp\\24・wflyT'bmp\\25・wfb;・bmp\\26・wfb;'bm p\\2c・wfb;'bmp\\2c・wfb;'bmp\\29・wflT},{”bmp\\21 ・ wfly「bmp\\3a. wfiy「bmp\\3L wfbTbmp\\34・ wfly「bmp\\3t・ wfbTbmp\\36. wfb”「bmp \\3t・ wfb;'bmp\\3a・wfb・「bmp\\29. wfb”},{”bmp\\41・wfly「bmp\\4t・wfbTbmp\\4a・wfb”「bmp\\4(・wfb;・bmp\\4a・wfb”「bmp\\4t・wfb;'bmp\\4a・wfb”,”bmp\\4L wfbTbmp\\49・wfb”},{H bmp\\51 .wfb^u bmp\\52. wfb^^mp\\5t.wfb^M bmp\\54. wfb^M bmp\\5t Avfb^^mpXXSd.wfb^'bmp\\5t.wfb;'bmp\\5&wfbTbmp\\59.wfb”},{”bmp\\61・wfly「bmp\\62・wnyT'bmp\\6Lwny「bmp\\64・wnyT'bmp\\6匸wflyT'bmp\\66・wflyT'bmp\\6t・wfb;'bmp\\6 &wfbTbmp\\69.wfb”},{”bmp\\71 ・wny「bmp\\7(・wfb”「bmp\\7a・wfb”rbmp\\7t. wfb;'bmp\\7a・wfb”「bmp\\7t.wfb・T・bmp\ \7a・wflA”bmp\\7t・wfbTbmp\\79・wfW},{”bmp\\81・wfb”「bmp\\8a・wfb”rbmp\\8f・wfbTbmp\\84・wfb”「bmp\\85・wfb"「bmp\\86・wfb;'binp\\8t・ wfb;'bmp\\8a・ wfb・「bmp\\89. wfb”},rbmp\\91・wfly「bmp\\9(・wfb”Fbmp\\9t・wfb;'bmp\\9Lwfb”「bmp\\95・wfb;・bmp\\9(・wnyT'bmp\\9t・ wfbTbmp\\9t・ wfb;bmp\\99・wfb”},{”bmp\\101・wfbTbmp\\102・wfbTbmp\\102・wfbTbmp\\104・wfbTbmp\\105・wfbTbmp\\106・wf b^H bmp\\108.wfb\H bmp\\108.wfb\,,bmp\\109.wfb H)};char cursor[14][14]={O,O,OQO,O,O,OQO,O,0,1 丄0,0,0,000,0,0,0,0,0,1,1,1, 0,255,255,255,255,255,255,255Q0丄11, 0,255,255,255,255,255,255,(X0,1,1,11, 0,255,255,255,255,255,25500,1 丄1,1 丄0,255,255,255,255,255,255,255,0,0,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,255Q0丄1,0,255,255,0,255,255,255,255.255,255,255,0,0.1, 0,25501」0255,255,255,255,255,255,255,0,0,0,1,1」,1,0,255,255,255,255,255,0」, 0,1,1 丄1 丄1,0,255,255,255,0,1 丄1,1,1丄1丄1丄0,255,0丄1丄1,1,1丄1」」丄1Q1」丄1struct 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=O;r.x.dx=page; /*选择页而引int86(0xl0,&r,&r);}unsigned char sef_SVGA_mode(int vmode) /*设置SVGA 屏幕模式*/{union REGS r;匚x.ax=0x4f02;r.x.bx=vmode;int86(0xl0,&r,&r);return(r.h.ah);}unsigned int gcl_SVGA_mode() /*获取当前SVGA 屏幕模式*/union REGS r;匚x.ax=Ox4fO3;int86(0xl0,&r,&r);return(r.x.bx);}drawbmp(int start_xjnt start_j;char []){char buffer[640];int ij,k,nj,g,b,widthJength:long position;HLE *fp;if((fp=fopen(/rb M))==NULL){printf(M Error! Can't open file!”);getch();return;}fscck(fp,28,SEEK_SET);frcad(&i,2,l,fp);if(i!=8)/*检查是否为256色位图*/{puts(,,Error!Can,t find bitmap!n);fclose(fp);getch();exit(O);}fseck(fp,l&SEEK_SET);fread(& widths J ,fp);fread(&length.4J、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 分量*7 outportb(0x3c8,i);outportb(0x3c9.r»2); /*右移是要转化为VGA的6位寄存器形式*/ outportb(0x3c9,g»2);outportb(0x3c9,b»2);fgetc(fp);elsefseek(fp300,SEEK_SET);k=(width%4)?(4-width%4):0;严宽度修正值 */for(j=length-l +start_x ;j>=start_x:j-){fread(buffer,widthJ Jp);for(i=start_};n=0;i<width+start_y;i++.n++){position=j*6401+i; /*计算要显示点的显存位置*/page_new=position/65536; /* 计算显示页 */if(page_new!=page_old)/*当显示页不同时更换页而,提髙一立的输出速度*/{selectpage(page_new);page_old=page_new;} pokeb(OxaOOO,position%65536,buffer(n]); /* 写到显存位置 */}fseek(fp,k,SEEK_CUR);/*每行绘制完后修正宽度旬}fclose(fp);}init(){savemode=geCSVGA_mode(); /*先保存原来的屏幕模式*/ set_SVGA_mode(0xl01); /*硬件无关性初始化屏幕为640*480 256色模式旬}end(){set_SVGA_mode(savemode); /* 恢复屏幕*7}/* ----------------------------------------------------- */initpos(){int ij;for(i=0;i<10;i++)for Q=0;j<9 J 卄){position[i][j].x=35+i*39;position[i][j].y=43+j*40;}}initchessmapO{board[0][0]=BLACKJU: board[0][l]=BLACK…MA;board[0n2]=BLACK_XIANG;board[0][3]=BLACK_SHI;board[0][4]=BLACKJIANG;board[0][5]=BLACK.SHI;board[0][6]=BLACK_XIANG;board[0][7]=BLACK_MA;board[0][8]=BLACKJU;board[2][l]=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]=REDJU;board[9][l]=RED_MA;board[9][2]=RED_XIANG;board[9][3]=RED_SHI;board[9][4]=REDJIANG;board[9][5]=RED_SHI;board ⑼[6]=RED_XIANG;board[9][7]=RED_MA;board[9][8]=REDJU;board[7][l]=RED_PAO;board[7][7]=RED_PAO;board ⑹[0]=RED_BIN;board ⑹[2]=RED_BIN;board ⑹[4]=RED_BIN;board ⑹[6]=RED_BIN;board ⑹[8]=RED_BIN;}initdrawchess(){int i,j;;fbr(i=0;i<10;i++)for(j=0;j<9:j 卄){if(board[i]|j])drawbmp(position[i][j].x,position[i][j].y,chess[i][j]]);drawcursor(stmct 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{for(i=y,n=0;i< 13+y;i++,n++){thisposition=j *6401+i; /*计算要显示点的显存位置*/pagejie w=t hi sposi t ion/65536: /* 汁算显示页*/ if(page_new!=pagc_old) /*当显示页不同时更换页而,提高一定的输岀速度*/{selectpage(page_new);page_old=page_new;}if(cursor[m][n]!=l)if(cursor[m] [ n ]=0)pokeb(0xa000,thisposition%65536,0);elseif(turn=RED) pokeb(0xa000,thisposition%65536,153);elsepokeb(OxaOOO,thisposition%65536,255);}drawselecursor(struct pos p){int ilong thisposition;x=position[p.x][p.y].x+20;y=position[p.x][p.y].y+25;for(j= 13-1 +x,m= 13{for(i=y,n=0;i< 13+y;i++,n++){thisposition=j *6401+i; /*计算要显示点的显存位置*/ page.new=thisposition/65536; /* 汁算显示页 */ if(page_new!=page_old)/*当显示页不同时更换页而,提髙一立的输出速度*/{selectpage(page_new);page_old=page_new;}if(cursor[m][n]!=l)pokeb(0xa000,thisposition%65536,0);/* ----------------------------------- */int getkeyO{int press;while(bioskey( 1) == 0);press=bioskey(0);press=press&OxffOO:return(press);}/* ------------- 红方操作------------- */int redcanselect(){int x,y;x=redcurpos.x;y=redcurpos.y;if(board[x][y]>=REDJU&&board[x][y]<=RED_BIN)return 1;elsereturn 0;}int redcanmove(){int iJ.min,maxQldx,oldy,x.y;oldx=redoldpos.x;oldy=redoldpos.y;x=redcurpos.x;y=redcurpos.y;/*casel目标位置是否是自己人*/if(board[x][y]>=RED_JU&&board[x][y]<=RED_BIN) return 0;/*军、马、炮、相、士、将、卒的走法正确性的判断可switch(board[oldx][oldyj){case RED.BIN: 产完成 */if(oldx>=5){ if(y!=oldyll(oldx-x)!=l) return 0;}else{ if(x==(oldx-l)&&y=oldy) return 1;elseif(x=oldx&&y=(oldy+l)) return 1;elseif(x==oldx&&y=(oldy-l)) return 1;elsereturn 0;}break;case RED JIANG: /* 完成*7if(x!=oldx&&y!=oldy) return 0;if(x!=oldx)if((x-oldx)> 1 ll(oldx-x)> 1) return 0; else if(x<7) return 0;else if(y!=oldy)if((y-oldy)> 1 ll(oldy-y)> 1) return 0; else if(y<3lly>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+l ;i<max;i++) if(board[i][y]!=0) return 0;}else if(y!=oldy){ min=(y>oldy)?o!dy:y;max=(y>oldy)?y:oldy;for(i=min+l ;i<max;i++) if(board[x][i]!=0) return 0;} break;case RED.MA: /* 完成可if((x-oldx)=2&&((y-oldy)= lll(oldy-y)== 1)) (if(board[oldx+l][oidy]!=0) return 0; }else if((oldx-x)==2&&((y・oldy)== lll(oldy-y)== 1)) { if(board[oldx-1 ][oldy] !=0) return 0;}else if((y-oldy)==2&&((x・oldx)= lll(oldx-x)== 1)) { if(board[oldx][oldy+1 ] !=0) return 0; }else if(⑹ dy-y)==2&&((x・oldx)=lll(oldx・x)==l))if(board[oldx][oldy-l]!=0) return 0;}else return 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+l ;i<max;i++)if(board[i][y]!=0) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(yx)ldy)?y:oldy;for(i=min+l ;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+l.j=0;i<max:i++)if(board[i][y] !=0) j++;if(j!=l) return 0:}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+l .j=0;i<max:i++)if(board[x][i]!=0)j++;if(j!=l) return 0;}} break;case RED_SHI: if(oldx=9lk)ldx=7) {if(x!=8lly!=4) return 0;} elseif(oldx==8){if(x==9&&y==3) return 1; else /*完成引严完成if(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: /* 完成*7if(x<5) return 0;if(x!=oldx&&y!=oldy)(if((x-oldx)==2&&(y-oldy)==2){i=oldx+1 y=oldy+1;}else if((x-oldx)=2&&(oldy-y)==2){i=oldx+l y=oldy-l;}else if((oldx-x)=2&&(y-oldy)==2){i=oldx-l j=oldy+1;}else if((oldx-x)=2&&(oldy-y)=2){i=oldx-l;j=oldy-l;}else return 0;if(board[i][j]!=0) return 0;}else return 0;break;}return 1;redupO{int xyn;if(redcurpos.x>0){redcurpos.x-;x=positionfredtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]=0)drawbmp(x,y,board][redtemppos.y]);else if(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE))n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);if(redtemppos.x==redoldpos.x&&redtemppos.y=redoldpos.y&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);{redtemppos.x=redcurpos.x;redtemppos.y=rcdcurpos.y;}}reddown(){int x,y,n;if(redcurpos.x<9){redcurpos.x++;x=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,board][redtemppos.y]);else if( !(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)) { n=board[redtemppos.x](redtemppos.y];drawbmp(x,y,chessfi lefn]);}if(redtemppos.x==redoldpos.x&&redtemppos.y=redoldpos.y&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);redtemppos.x=redcurpos.x;redtemppos.y=rcdcurpos.y;}}redleft(){int x,y,n;if(redcurpos.y>0){redcurpos.y-;x=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]=0)drawbmp(x,y5board][redtemppos.y]);else if(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)){n=board[redtemppos.x][redtemppos.y];d rawbmp(x ychcssfi le[n]);}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 xyn;if(redcurpos.y<8){redcurpos.y++;x=positionfredtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y5board][redtemppos.y]);else if( !(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE)) { n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}if(redtemppos.x==redoldpos.x&&redtemppos.y=redoldpos.y&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);redtemppos.x=redcurpos.x;redtemppos.y=redcurpos.y;}} reddo()int i,j,x,y,n;if(redstate==SELECT&&redcanselectO)if(board [redcurpos.x] [redcurpos. y]<=RED&&board[redcurpos.x] [redcurpos.y]>0){redstate=MOVE;drawselecursor(redcurpos);redoldpos.x=redcurpos.x;redoldpos.y=redcurpos.y;}}else if(redstate=MOVE&&redcanmoveO){x=position[redoldpos.x][redoldpos.y].x:y=position[redoldpos.x][redoldpos.y].y;drawbmp(x,y,board][rcdoldpos.y]);{x=position[redcurpos.x] [redcurpos. y].x;y=position[redcurpos.x][redcurpos. y].y;n=board[redoldpos.x][redoldpos. y];drawbmp(x,y,chessfile[n]);if(board[redcurpos.x][redcurpos.y]=BLACK_JIANG){winner=RED;finish=l;return;}board[redcurpos.x][redcurpos.yj=n;board [ redoldpos.x ] [rcdoldpos.y ]=0;for(i=0;i<=2;i++)for(j=3;j<=5;j++)if(board[i][j]=BLACKJIANG){x=i;y=j;}for(i=x+1 ,j=y,n=O;i<=9;i++){if(board[i][j]=RED JIANG&&n=0){winner=BLACK:finish=l ;break;}else if(board[i][j]!=0) n++;}turn=BLACK;blackstate=SELECT;drawcursor(blackcurpos);drawbmp(30,43&”bmp\\bzq.wfb”);/* 转交控制权给黑方引redundo(){int xyn;if(redstate==MOVE){ x=position[redoldpos.x][redoldpos. y].x; y=position[redoldpos.x][redoldpos.y].y;n=board[redoldpos.x][redoldpos.y]; drawbmp(xychcssfilc[n]);redoldpos.x=redcurpos.x;redoldpos.y=redcurpos.y; drawcursor(redcurpos);redstate=SELECT;}}/* ------------- 黑方操作 -------------- */int blackcanselect(){int x,y;x=blackcurpos.x;y=blackcurpos.y;if(board[x][y]>=BLACK_JU&&board[x][y]<=BLACK_BIN) return 1;elsereturn 0;}int blackcanmoveO{int ij,miiLmax,oldx,oldy,x,y;oldx=blackoldpos.x;oldy=blackoldpos.y;x=blackcurpos.x;y=blackcurpos.y;/*casel目标位置是否是自己人*/if(board[x][y]>=BLACK_J U&&board[x][y]<=BLACK_BIN) return 0;/*军、马、炮、相、士、将、卒的上法正确性的判断*/s witch(board[oldx] [oldy])case BLACKJU: /* 完成勺if(x!=oldx&&y!=oldy) return 0;else if(x!=oldx){ min=(x>oldx)?oldx:x;max=(x>oldx)?x:oldx;for(i=min+l ;i<max;i++) if(board[i][y]!=0) return 0;}else if(y!=oldy){ min=(y r>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+l ;i<max;i++) if(board[x][i]!=0) return 0;{}break;case BLACK.MA: /*完成*/ if((x-oldx)==2&&((y-oldy)== 1 ll(oldy-y)== 1)) {if(board[oldx+1 ] [oldy] !=0) return 0;}elseif((oldx-x)==2&&((y・oldy)= 1 ll(oldy-y)== 1)) ( if(board[oldx-1 ][oldy] !=0) return 0;}else if((y-oldy)==2&&((x・oldx)= lll(oldx-x)== 1)) {if(board[oldx][oldy+1 ] !=0) return 0:}elseif((oldy-y)==2&&((x-oldx)= lll(oldx-x )== 1)) {if(board[oldx][oldy-1 ] !=0) return 0;}elsereturn 0;break;case BLACK.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+l ;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+l :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+l ;i<max;i++)if(board[i][y] !=0) j++;if(j!=l) return 0;}else if(y!=oldy){ min=(y>oldy)?oldy:y;max=(y>oldy)?y:oldy;for(i=min+l ;i<max;i++)if(board[x][i]!=0) j++;if(j!=l) return 0:}}break;case BLACK_XIANG: if(x>4) return 0;if(x!=oldx&&y!=oldy){if((x-oldx)==2&&(y-oldy)==2){i=oldx+1 y=oldy+1;}else if((x-oldx)=2&&(oldy・y)==2) {i=oldx+l:j=oldy-l;}else if((oldx-x)=2&&(y-oldy)==2) (i=oldx-l ;j=oldy+l;}else if((oldx-x)=2&&(oldy-y)==2) {i=oldx-l;j=oldy-l;}else return 0; if(board[i][j]!=0) return 0;}else return 0;break;case BLACK SHI:if(oldx==0lloldx=2){if(x!=llly!=4) return 0;} else if(oldx==l ){if(x==0&&y==3) return 1;elseif(x=O&&y=5) return 1;elseif(x=2&&y=3) return 1;elseif(x=2&&y=5) return 1;else return 0;}else return 0:break;case BLACKJIANG:if(x!=oldx&&y!=oldy) return 0; if(x!=oldx)if((x-oldx)> 1 ll(oldx-x)> 1) return 0: else if(x>2) return 0;else if(y!=oldy)if((y-oldy)> 1 ll(oldy-y)> 1) return 0; else if(y<3lly>5) return 0; break;case BLACK_BIN:if(oldx<=4){ if(y!=oldyll(x-oldx)!=l) return 0;} else{ if(x==(oldx+l )&&y=oldy) return 1;elseif(x==oldx&&y=(oldy+1)) return 1; elseif(x==oldx&&y=(oldy-l)) return 1; elsereturn 0;} break;}return 1;}blackupO{int x,y,n;if(blackcurpos.x>0)blackcurpos.x-;x=position[blacktemppos.x][blacktemppos.y].x;y=position[blacktemppos.x][blacktemppos.y].y;if(board[blacktemppos.x][blacktemppos.y]=0)drawbmp(x,y,board][blacktemppos.y]);elseif(!(blacktemppos.x==blackoldpos.x&&blacktemppos.y=blackoldpos.y&&blackstate==MOVE)) { n=board[blacktemppos.x][blacktemppos.y];drawbmp(x,y,chessfi lefn]);}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 xyn;if(blackcurpos.x<9){blackcurpos.x++;x=position[blacktemppos.x][blacktemppos.y].x;y=position[blacktemppos.x][blacktemppos.y].y;if(board[blacktemppos.x]|blacktemppos.y]==0)drawbmp(x,y,board][blacktemppos.y]);elseif(!(blacktemppos.x==blackoldpos.x&&blacktemppos.y==blackoldpos.y&& blackstate==MOVE)) { n=board[blacktemppos.x][blacktemppos.y];drawbmp(x,y5chessfile[n]);if(blacktemppos.x=blackoldpos.x&&blacktemppos.y==blackoldpos.y&&blackstate==MO VE) drawselecursor(blackoldpos);drawcursor(blackcurpos);blacktemppos.x=blackcurpos.x;blacktemppos.y=blackcurpos.y;blackleft(){int xyn;if(blackcurpos.y>0){blackcurpos.y-;x=position[blacktemppos.x][blacktemppos.y].x;y=position[blacktemppos.x][blacktemppos.y].y;if(board[blacktemppos.x]|blacktenippos.y]==0)drawbmp(x,y,board][blacktemppos.y]);elseif(!(blacktemppos.x==blackoldpos.x&&blacktemppos.y=blackoldpos.y&&blackstate==MOVE)) { n=board[blacktemppos.x][blacktemppos.y];d rawbmp(x ychcssfi le[n]);}if(blacktemppos.x=blackoldpos.x&&blacktemppos.y==blackoldpos.y&&blackstate==MO VE) drawselecursor(blackoldpos);drawcursor(blackcurpos);blacktemppos.x=blackcurpos.x;blacktemppos.y=blackcurpos.y;}blackright(){int xyn;if(blackcurpos.y<8){blackcurpos.y++;x=position[blacktemppos.x][blacktemppos.y].x;y=position[blacktemppos.x][blacktemppos.y].y;if(board[blacktemppos.x][blacktenippos.y]==0)drawbmp(x,y,board][blacktemppos.y]);elseif(!(blacktemppos.x==blackoldpos.x&&blacktemppos.y==blackoldpos.y&&blackstate==MOVE)) { n=board[blacktemppos.x][blacktemppos.y];drawbmp(x,y,chessfile[n]);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&&blackcanselectO){if(board[blackcurpos.x][blackcurpos.y]<=BLACK&&board[blackcurpos.x][blackcurpos.y]>0) { blackstate=MOVE;drawselecursor(blackcurpos);blackoldpos.x=blackcurpos.x;blackoldpos.y=blackcuipos.y;}}else if(blackstate=MOVE &&blackcanmoveO){x=position[blackoldpos.x][blackoldpos.y].x;y=position|blackoldpos.x][blackoldpos.y].y;drawbmp(x,y,board][blackoldpos.y]);x=position[blackcurpos.x][blackcurpos.y].x;y=position(blackcurpos.x][blackcurpos.y].y;n=board[blackoldpos.x][blackoldpos.y];drawbmp(x,y,chessfile[n]);if(board[blackcurpos.x][blackcurpos.y]=RED_JIANG){winner=BLACK;finish=l;return;}board[blackcurpos.x][blackcurpos.y]=n: board[blacko!dpos.x][blackoldpos.y]=0; for(i=0;i<=2;i++)for(j=3;j<=5;j++)if(board[i] [j]=BLACKJIANG){x=i;y=j;}for(i=x+1 ,j=y,n=0;iv=9;i++){if(board [ i ] fj ]=RED_J IANG&&n=0) {winner=RED;finish= 1:break:} else if(board[i][j]!=0) n++;turn=RED: redstate=SELECT; drawcursor(redcurpos); drawbmp(30.43&”bmp\\rzq・wfb”);严转交控制权给红方勺}blackundo(){int xyn;if(b!ackstate==MOVE){x=position[blackoldpos.x][blackoldpos.y].x; y=position[blackoldpos.x][blackoldpos.y].y;n=board[blackoldpos.x][blackoldpos.y]: drawbmp(x,yxhessfile[n]);blackoldpos.x=blackcurpos.x;blackoldpos.y=blackcurpos.y; drawcursor(blackcurpos); blackstate=SELECT;}}/* ----------------------------------------------------- */start(){drawcursor(blackcurpos);drawbmp(30,43&”bmp\\bzq.wfb”);while(Jfinish){key=getkey();switch(key){case RED_UP:if(turn==RED) redupO;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)blackupO;break;case BLACK_DOWN:if(turn==BLACK)blackdown();break;case BLACK_LEFT: if(turn==BLACK) blackleft();break;case BLACK.RIGHT: if(turn==BLACK) blackrightO; break;case BLACK_DO: if(turn=BLACK) blackdo();break;case BLACK.UNDO: if(turn=BLACK) blackundo();break;case ESCAPE: finish=l;break;}main()init();initpos();initchessmapO;drawbmp(0.0/bmp\\board.wfb M); initdrawchess();/*初始化光标位苣*/redcurpos.x=redoldpos.x=redtenippos.x=9;redcurpos.y=redoldpos.y=redtemppos.y=8;blackcurpos.x=blackoldpos.x=blacktemppos.x=0; blackcurpos.y=blacko!dpos.y=blacktemppos.y=0; /*开始*/ start();if(winner==RED)drawbmp(2OO,2OO;,bmp\\redwin.wfb M);else if(winner==BLACK)drawbmp(2OO,2OO,H bmp\\blackwin.wfb H);elsedrawbmp(2OO,2OO;,bmp\\exit.wfb H); getch();end();}。
一担挑游戏 三子棋完整版(地大版)
C程序设计实习报告题目:一担挑游戏学院:机械与电子信息学院专业:机械设计制造及其自动化姓名:王国飞班级学号: 072093-05 20091000880 指导教师:薛思清2011 年 07 月 03 日目录一、题目要求 (3)二、需求分析 (3)三、总体设计 (3)四、详细设计 (4)4.1主函数流程图 (4)4.2各功能模块设计 (5)4.2.1棋盘设计模块流程图 (5)4.2.2游戏记录载入模块 (5)4.2.3玩家移动棋子模块 (6)4.2.4计算机移动棋子模块 (7)4.2.5判断胜负模块 (7)4.2.6游戏保存模块 (8)五、源程序及测试结果分析 (8)5.1源程序 (8)5.2测试结果分析 (12)5.2.1初次游戏 (12)5.2.2容错 (13)5.2.3 下棋结果 (13)5.2.4再次游戏 (14)六、程序设计总结 (14)七、参考文献 (15)一、题目要求模拟人机对弈--------“一担挑游戏”简单设计计算机模拟棋盘对弈。
其中棋盘左上角坐标为(1,1),右下角坐标为(3,3)。
游戏规则如下:(1)计算机移动时,使用相应函数扫描棋盘矩阵,寻找未占单元:发现空单元后,置为“O”,未发现时,报告平局后退出。
(2)轮到对弈者时,使用相应函数要求对弈者回答想把“X”放在哪里。
(3)谁先三点一线了,则报告谁胜出。
(4)计算机与对弈者每下一步棋都会使用相应位置变为“O”和“X”,便于在屏上显示矩阵。
二、需求分析计算机移动时,使用for函数扫描棋盘矩阵,寻找未占单元:发现空单元之后,置为“O”,未发现时,报告平局后退出。
轮到对弈者时,使用想要函数要求对弈者回答想把“X"放在哪里,通过x、y坐标实现。
谁先三点连成一线了,则报告谁胜出,通过循环把每一横行竖行,对角线分别判断。
计算机与对弈者每下一步棋都会使相应位置变为“O”和“X”,便于在屏上显示矩阵。
全程序主要模块包括“设计棋盘格式模块”,“游戏记录载入模块”,“玩家移动棋子模块”,“计算机移动棋子模块”,“判断胜负模块”和“游戏数据保存模块”。
C语言#字棋人人对战代码
#include<stdio.h>#include<string.h>void panduan(char a[3][3]){if(('X'==a[0][0]&&'X'==a[0][1]&&'X'==a[0][2])||('X'==a[1][0]&&'X'==a[1][1]&&'X'==a[1][2])|| ('X'==a[2][0]&&'X'==a[2][1]&&'X'==a[2][2])||('X'==a[2][0]&&'X'==a[1][0]&&'X'==a[0][0])||('X'==a[2][1]&&'X'==a[1][1]&&'X'==a[0][1])||('X'==a [2][2]&&'X'==a[1][2]&&'X'==a[0][2])||('X'==a[2][2]&&'X'==a[1][1]&&'X'==a[0][0])||('X'==a[2][0]&&'X'==a[1][1]&&'X'==a[0][2])) {printf("甲方获胜!\n");}if((a[0][0]=='Y'&&a[0][1]=='Y'&&a[0][2]=='Y')||(a[1][0]=='Y'&&a[1][1]=='Y'&&a[1][2]=='Y')||( a[2][0]=='Y'&&a[2][1]=='Y'&&a[2][2]=='Y')||(a[2][0]=='Y'&&a[1][0]=='Y'&&a[0][0]=='Y')||(a[2][1]=='Y'&&a[1][1]=='Y'&&a[0][1]=='Y')||(a[2][2] =='Y'&&a[1][2]=='Y'&&a[0][2]=='Y')||(a[2][2]=='Y'&&a[1][1]=='Y'&&a[0][0]=='Y')||(a[2][0]=='Y'&&a[1][1]=='Y'&&a[0][2]=='Y')){printf("乙方获胜!\n");}}void shuchu(char a[3][3]){printf("_________________________\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[0][0],a[0][1],a[0][2]);printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[1][0],a[1][1],a[1][2]);printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[2][0],a[2][1],a[2][2]);printf("|_______|_______|_______|\n");printf("\n");}void qipu(){int i,j;char a[3][3]={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};shuchu(a);//第一次输入printf("甲方请输入:");scanf("%d %d",&i,&j);// if(a[i][j]=='\0')// {a[i][j]='X';// }else// printf("输入错误,");shuchu(a);printf("乙方请输入:");scanf("%d %d",&i,&j);a[i][j]='Y';shuchu(a);//第二次printf("甲方请输入:");scanf("%d %d",&i,&j);a[i][j]='X';shuchu(a);printf("乙方请输入:");scanf("%d %d",&i,&j);a[i][j]='Y';shuchu(a);//第三次printf("甲方请输入:");scanf("%d %d",&i,&j);a[i][j]='X';shuchu(a);panduan(a);printf("乙方请输入:");scanf("%d %d",&i,&j);a[i][j]='Y';shuchu(a);panduan(a);//第四次printf("甲方请输入:");scanf("%d %d",&i,&j);a[i][j]='X';shuchu(a);panduan(a);printf("乙方请输入:");scanf("%d %d",&i,&j);a[i][j]='Y';shuchu(a);//printf("甲方请输入:");scanf("%d %d",&i,&j);a[i][j]='X';shuchu(a);panduan(a);if(('X'==a[0][0]&&'X'==a[0][1]&&'X'==a[0][2])||('X'==a[1][0]&&'X'==a[1][1]&&'X'==a[1][2])|| ('X'==a[2][0]&&'X'==a[2][1]&&'X'==a[2][2])||('X'==a[2][0]&&'X'==a[1][0]&&'X'==a[0][0])||('X'==a[2][1]&&'X'==a[1][1]&&'X'==a[0][1])||('X'==a [2][2]&&'X'==a[1][2]&&'X'==a[0][2])||('X'==a[2][2]&&'X'==a[1][1]&&'X'==a[0][0])||('X'==a[2][0]&&'X'==a[1][1]&&'X'==a[0][2])) printf("甲方获胜!\n");if((a[0][0]=='Y'&&a[0][1]=='Y'&&a[0][2]=='Y')||(a[1][0]=='Y'&&a[1][1]=='Y'&&a[1][2]=='Y')||( a[2][0]=='Y'&&a[2][1]=='Y'&&a[2][2]=='Y')||(a[2][0]=='Y'&&a[1][0]=='Y'&&a[0][0]=='Y')||(a[2][1]=='Y'&&a[1][1]=='Y'&&a[0][1]=='Y')||(a[2][2] =='Y'&&a[1][2]=='Y'&&a[0][2]=='Y')||(a[2][2]=='Y'&&a[1][1]=='Y'&&a[0][0]=='Y')||(a[2][0]=='Y'&&a[1][1]=='Y'&&a[0][2]=='Y')) printf("乙方获胜!\n");elseprintf("和棋!");printf("\n");}void main(){printf("_________________________\n");printf("| | | |\n");printf("| 0 0 | 0 1 | 0 2 |\n");printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| 1 0 | 1 1 | 1 2 |\n");printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| 2 0 | 2 1 | 2 2 |\n");printf("|_______|_______|_______|\n");printf("\n");printf("对应的数字带表对应的位置,输入数字则带表选择对应的位置!X代表甲方,Y 代表乙方\n");qipu();}完美1#include<stdio.h>#include<string.h>void qipu();void shuru();void shuchu();void panduan();void panduan(char a[3][3]){if(('X'==a[0][0]&&'X'==a[0][1]&&'X'==a[0][2])||('X'==a[1][0]&&'X'==a[1][1]&&'X'==a[1][2])|| ('X'==a[2][0]&&'X'==a[2][1]&&'X'==a[2][2])||('X'==a[2][0]&&'X'==a[1][0]&&'X'==a[0][0])||('X'==a[2][1]&&'X'==a[1][1]&&'X'==a[0][1])||('X'==a [2][2]&&'X'==a[1][2]&&'X'==a[0][2])||('X'==a[2][2]&&'X'==a[1][1]&&'X'==a[0][0])||('X'==a[2][0]&&'X'==a[1][1]&&'X'==a[0][2])) {printf("甲方获胜!\n");}if((a[0][0]=='Y'&&a[0][1]=='Y'&&a[0][2]=='Y')||(a[1][0]=='Y'&&a[1][1]=='Y'&&a[1][2]=='Y')||( a[2][0]=='Y'&&a[2][1]=='Y'&&a[2][2]=='Y')||(a[2][0]=='Y'&&a[1][0]=='Y'&&a[0][0]=='Y')||(a[2][1]=='Y'&&a[1][1]=='Y'&&a[0][1]=='Y')||(a[2][2] =='Y'&&a[1][2]=='Y'&&a[0][2]=='Y')||(a[2][2]=='Y'&&a[1][1]=='Y'&&a[0][0]=='Y')||(a[2][0]=='Y'&&a[1][1]=='Y'&&a[0][2]=='Y')){printf("乙方获胜!\n");}}void shuchu(char a[3][3]){printf("_________________________\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[0][0],a[0][1],a[0][2]);printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[1][0],a[1][1],a[1][2]);printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[2][0],a[2][1],a[2][2]);printf("|_______|_______|_______|\n");printf("\n");}void shuru(char a[3][3]){int i,j;for(int m=0;m<9;m++){if(m%2==0){printf("甲方请输入:");scanf("%d %d",&i,&j);if(a[i][j]==' '){a[i][j]='X';shuchu(a);}else{printf("输入错误,请重新输入!\n");i--;}}else{printf("乙方请输入:");scanf("%d %d",&i,&j);if(a[i][j]==' '){a[i][j]='Y';shuchu(a);}else{printf("输入错误,请重新输入!\n");m--;}}}}void qipu(){int i,j;char a[3][3]={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};shuchu(a);shuru(a);//printf("甲方请输入:");scanf("%d %d",&i,&j);a[i][j]='X';shuchu(a);panduan(a);if(('X'==a[0][0]&&'X'==a[0][1]&&'X'==a[0][2])||('X'==a[1][0]&&'X'==a[1][1]&&'X'==a[1][2])|| ('X'==a[2][0]&&'X'==a[2][1]&&'X'==a[2][2])||('X'==a[2][0]&&'X'==a[1][0]&&'X'==a[0][0])||('X'==a[2][1]&&'X'==a[1][1]&&'X'==a[0][1])||('X'==a [2][2]&&'X'==a[1][2]&&'X'==a[0][2])||('X'==a[2][2]&&'X'==a[1][1]&&'X'==a[0][0])||('X'==a[2][0]&&'X'==a[1][1]&&'X'==a[0][2])) printf("甲方获胜!\n");if((a[0][0]=='Y'&&a[0][1]=='Y'&&a[0][2]=='Y')||(a[1][0]=='Y'&&a[1][1]=='Y'&&a[1][2]=='Y')||( a[2][0]=='Y'&&a[2][1]=='Y'&&a[2][2]=='Y')||(a[2][0]=='Y'&&a[1][0]=='Y'&&a[0][0]=='Y')||(a[2][1]=='Y'&&a[1][1]=='Y'&&a[0][1]=='Y')||(a[2][2] =='Y'&&a[1][2]=='Y'&&a[0][2]=='Y')||(a[2][2]=='Y'&&a[1][1]=='Y'&&a[0][0]=='Y')||(a[2][0]=='Y'&&a[1][1]=='Y'&&a[0][2]=='Y')) printf("乙方获胜!\n");elseprintf("和棋!");printf("\n");}void main(){printf("_________________________\n");printf("| | | |\n");printf("| 0 0 | 0 1 | 0 2 |\n");printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| 1 0 | 1 1 | 1 2 |\n");printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| 2 0 | 2 1 | 2 2 |\n");printf("|_______|_______|_______|\n");printf("\n");printf("对应的数字带表对应的位置,输入数字则带表选择对应的位置!X代表甲方,Y 代表乙方\n");qipu();}完美2#include<stdio.h>#include<string.h>void qipu();void shuru();void shuchu();void panduan();void panduan(char a[3][3]){if(('X'==a[0][0]&&'X'==a[0][1]&&'X'==a[0][2])||('X'==a[1][0]&&'X'==a[1][1]&&'X'==a[1][2])|| ('X'==a[2][0]&&'X'==a[2][1]&&'X'==a[2][2])||('X'==a[2][0]&&'X'==a[1][0]&&'X'==a[0][0])||('X'==a[2][1]&&'X'==a[1][1]&&'X'==a[0][1])||('X'==a [2][2]&&'X'==a[1][2]&&'X'==a[0][2])||('X'==a[2][2]&&'X'==a[1][1]&&'X'==a[0][0])||('X'==a[2][0]&&'X'==a[1][1]&&'X'==a[0][2])) {printf("甲方获胜!\n");}if((a[0][0]=='Y'&&a[0][1]=='Y'&&a[0][2]=='Y')||(a[1][0]=='Y'&&a[1][1]=='Y'&&a[1][2]=='Y')||( a[2][0]=='Y'&&a[2][1]=='Y'&&a[2][2]=='Y')||(a[2][0]=='Y'&&a[1][0]=='Y'&&a[0][0]=='Y')||(a[2][1]=='Y'&&a[1][1]=='Y'&&a[0][1]=='Y')||(a[2][2] =='Y'&&a[1][2]=='Y'&&a[0][2]=='Y')||(a[2][2]=='Y'&&a[1][1]=='Y'&&a[0][0]=='Y')||(a[2][0]=='Y'&&a[1][1]=='Y'&&a[0][2]=='Y')){printf("乙方获胜!\n");}}void shuchu(char a[3][3]){printf("_________________________\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[0][0],a[0][1],a[0][2]);printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[1][0],a[1][1],a[1][2]);printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[2][0],a[2][1],a[2][2]);printf("|_______|_______|_______|\n");printf("\n");}void shuru(char a[3][3]){int i,j;for(int m=0;m<9;m++){if(m%2==0)printf("甲方请输入:");scanf("%d %d",&i,&j);if(a[i][j]==' '){a[i][j]='X';shuchu(a);panduan(a);}else{printf("输入错误,请重新输入!\n");m--;}}else{printf("乙方请输入:");scanf("%d %d",&i,&j);if(a[i][j]==' '){a[i][j]='Y';shuchu(a);panduan(a);}else{printf("输入错误,请重新输入!\n");m--;}}}}void qipu(){char a[3][3]={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};shuchu(a);shuru(a);if(('X'==a[0][0]&&'X'==a[0][1]&&'X'==a[0][2])||('X'==a[1][0]&&'X'==a[1][1]&&'X'==a[1][2])|| ('X'==a[2][0]&&'X'==a[2][1]&&'X'==a[2][2])||('X'==a[2][0]&&'X'==a[1][0]&&'X'==a[0][0])||('X'==a[2][1]&&'X'==a[1][1]&&'X'==a[0][1])||('X'==a [2][2]&&'X'==a[1][2]&&'X'==a[0][2])||('X'==a[2][2]&&'X'==a[1][1]&&'X'==a[0][0])||('X'==a[2][0]&&'X'==a[1][1]&&'X'==a[0][2])) printf("甲方获胜!\n");if((a[0][0]=='Y'&&a[0][1]=='Y'&&a[0][2]=='Y')||(a[1][0]=='Y'&&a[1][1]=='Y'&&a[1][2]=='Y')||( a[2][0]=='Y'&&a[2][1]=='Y'&&a[2][2]=='Y')||(a[2][0]=='Y'&&a[1][0]=='Y'&&a[0][0]=='Y')||(a[2][1]=='Y'&&a[1][1]=='Y'&&a[0][1]=='Y')||(a[2][2] =='Y'&&a[1][2]=='Y'&&a[0][2]=='Y')||(a[2][2]=='Y'&&a[1][1]=='Y'&&a[0][0]=='Y')||(a[2][0]=='Y'&&a[1][1]=='Y'&&a[0][2]=='Y')) printf("乙方获胜!\n");elseprintf("和棋!");printf("\n");}void main(){printf("_________________________\n");printf("| | | |\n");printf("| 0 0 | 0 1 | 0 2 |\n");printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| 1 0 | 1 1 | 1 2 |\n");printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| 2 0 | 2 1 | 2 2 |\n");printf("|_______|_______|_______|\n");printf("\n");printf("对应的数字带表对应的位置,输入数字则带表选择对应的位置!X代表甲方,Y 代表乙方\n");qipu();}完美#include<stdio.h>void qipu();void shuru();void shuchu();int panduan();int panduan(char a[3][3]){if(('X'==a[0][0]&&'X'==a[0][1]&&'X'==a[0][2])||('X'==a[1][0]&&'X'==a[1][1]&&'X'==a[1][2])|| ('X'==a[2][0]&&'X'==a[2][1]&&'X'==a[2][2])||('X'==a[2][0]&&'X'==a[1][0]&&'X'==a[0][0])||('X'==a[2][1]&&'X'==a[1][1]&&'X'==a[0][1])||('X'==a [2][2]&&'X'==a[1][2]&&'X'==a[0][2])||('X'==a[2][2]&&'X'==a[1][1]&&'X'==a[0][0])||('X'==a[2][0]&&'X'==a[1][1]&&'X'==a[0][2])) {printf("甲方获胜!\n");return 1;}if((a[0][0]=='Y'&&a[0][1]=='Y'&&a[0][2]=='Y')||(a[1][0]=='Y'&&a[1][1]=='Y'&&a[1][2]=='Y')||( a[2][0]=='Y'&&a[2][1]=='Y'&&a[2][2]=='Y')||(a[2][0]=='Y'&&a[1][0]=='Y'&&a[0][0]=='Y')||(a[2][1]=='Y'&&a[1][1]=='Y'&&a[0][1]=='Y')||(a[2][2] =='Y'&&a[1][2]=='Y'&&a[0][2]=='Y')||(a[2][2]=='Y'&&a[1][1]=='Y'&&a[0][0]=='Y')||(a[2][0]=='Y'&&a[1][1]=='Y'&&a[0][2]=='Y')){printf("乙方获胜!\n");return 1;}elsereturn 0;}void shuchu(char a[3][3]){printf("_________________________\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[0][0],a[0][1],a[0][2]);printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[1][0],a[1][1],a[1][2]);printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[2][0],a[2][1],a[2][2]);printf("|_______|_______|_______|\n");printf("\n");}void shuru(char a[3][3]){int i,j,m;for(m=0;m<9;m++){if(m%2==0){printf("甲方请输入:");scanf("%d %d",&i,&j);if(a[i][j]==' '){a[i][j]='X';shuchu(a);if(panduan(a)){break;}}else{printf("输入错误,请重新输入!\n");m--;}}else{printf("乙方请输入:");scanf("%d %d",&i,&j);if(a[i][j]==' '){a[i][j]='Y';shuchu(a);if(panduan(a)){break;}}else{printf("输入错误,请重新输入!\n");m--;}}}if(m-1==8){if(panduan(a)==0)printf("和棋!\n");}}void qipu(){char a[3][3]={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};shuchu(a);shuru(a);printf("\n");}void main(){printf("_________________________\n");printf("| | | |\n");printf("| 0 0 | 0 1 | 0 2 |\n");printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| 1 0 | 1 1 | 1 2 |\n");printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| 2 0 | 2 1 | 2 2 |\n");printf("|_______|_______|_______|\n");printf("\n");printf("对应的数字带表对应的位置,输入数字则带表选择对应的位置!X代表甲方,Y 代表乙方\n");qipu();}调试完成#include<stdio.h>void qipu();void shuru();void shuchu();int panduan1();int panduan();void shuchu(char a[3][3]){printf("_________________________\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[0][0],a[0][1],a[0][2]);printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[1][0],a[1][1],a[1][2]);printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[2][0],a[2][1],a[2][2]);printf("|_______|_______|_______|\n");printf("\n");}int panduan1(char a[3][3]){for(int i=0;i<3;i++){if(a[0][i]=='X'&&a[1][i]=='X'&&a[2][i]=='X'){return 1;}if(a[i][0]=='X'&&a[i][1]=='X'&&a[i][2]=='X'){return 1;}}for(int j=0;j<3;j++){if(a[0][j]=='Y'&&a[1][j]=='Y'&&a[2][j]=='Y'){return 2;}if(a[j][0]=='Y'&&a[j][1]=='Y'&&a[j][2]=='Y'){return 2;}}if(a[2][2]=='X'&&a[1][1]=='X'&&a[0][0]=='X'){return 1;}if(a[2][2]=='Y'&&a[1][1]=='Y'&&a[0][0]=='Y'){return 2;}if(a[2][0]=='X'&&a[1][1]=='X'&&a[0][2]=='X'){return 1;}if(a[2][0]=='Y'&&a[1][1]=='Y'&&a[0][2]=='Y'){return 2;}else return 0;}int panduan(char a[3][3]){if(panduan1(a)==1){printf("甲方获胜!\n");return 1;}if(panduan1(a)==2){printf("乙方获胜!\n");return 1;}elsereturn 0;}void shuru(char a[3][3]){int i,j,m;for(m=0;m<9;m++){if(m%2==0){printf("甲方请输入:");scanf("%d %d",&i,&j);if(a[i-1][j-1]==' '){a[i-1][j-1]='X';shuchu(a);if(panduan(a)==1){break;}}else{printf("输入错误,请按格式重新输入!\n");m--;}}else{printf("乙方请输入:");scanf("%d %d",&i,&j);if(a[i-1][j-1]==' '){a[i-1][j-1]='Y';shuchu(a);if(panduan(a)==1){break;}}else{printf("输入错误,请按格式重新输入!\n");m--;}}}if(m-1==8){if(panduan(a)==0)printf("和棋!\n");}printf("输入1则再来一次!\n");int k;scanf("%d",&k);if(k==1)qipu();}void qipu(){char a[3][3]={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};shuchu(a);shuru(a);printf("\n");}void main(){printf("_________________________\n");printf("| | | |\n");printf("| 1 1 | 1 2 | 1 3 |\n");printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| 2 1 | 2 2 | 2 3 |\n");printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| 3 1 | 3 2 | 3 3 |\n");printf("|_______|_______|_______|\n");printf("\n");printf("对应的数字带表对应的位置,输入数字则带表选择对应的位置!X代表甲方,Y 代表乙方\n");qipu();}人机#include<stdio.h>#include<string.h>void qipu();void shuru();void shuchu();int panduan1();int panduan();int guzhi();void zoufa();void shuchu(char a[3][3]){printf("_________________________\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[0][0],a[0][1],a[0][2]);printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[1][0],a[1][1],a[1][2]);printf("|_______|_______|_______|\n");printf("| | | |\n");printf("| %3c | %3c | %3c |\n",a[2][0],a[2][1],a[2][2]);printf("|_______|_______|_______|\n");printf("\n");}////////////////////////////////////////////////////////////////////////////////////// void qipu(){char a[3][3]={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};shuchu(a);shuru(a);printf("\n");}//////////////////////////////////////////////////////////////////////////////////////// int panduan1(char a[3][3]){for(int i=0;i<3;i++){if(a[0][i]=='X'&&a[1][i]=='X'&&a[2][i]=='X'){return 1;}if(a[i][0]=='X'&&a[i][1]=='X'&&a[i][2]=='X'){return 1;}}for(int j=0;j<3;j++){if(a[0][j]=='Y'&&a[1][j]=='Y'&&a[2][j]=='Y'){return 2;}if(a[j][0]=='Y'&&a[j][1]=='Y'&&a[j][2]=='Y'){return 2;}}if(a[2][2]=='X'&&a[1][1]=='X'&&a[0][0]=='X'){return 1;}if(a[2][2]=='Y'&&a[1][1]=='Y'&&a[0][0]=='Y'){return 2;}if(a[2][0]=='X'&&a[1][1]=='X'&&a[0][2]=='X'){return 1;}if(a[2][0]=='Y'&&a[1][1]=='Y'&&a[0][2]=='Y'){return 2;}elsereturn 0;}////////////////////////////////////////////////////////////// int panduan(char a[3][3]){if(panduan1(a)==1){printf("电脑方获胜!\n");return 1;}if(panduan1(a)==2){printf("人方获胜!\n");return 1;}elsereturn 0;}///////////////////////////////////////////////////////////// int guzhi(char a[3][3]){int p=0;for(int j=0;j<3;j++){if(a[0][j]=='X'&&a[1][j]=='X'&&a[2][j]=='X'){p++;}if(a[j][0]=='X'&&a[j][1]=='X'&&'X'==a[j][2]){p++;}}if(a[0][0]=='X'&&'X'==a[1][1]&&'X'==a[2][2]){p++;}if(a[2][0]=='X'&&'X'==a[1][1]&&'X'==a[0][2]){p++;}for(int i=0;i<3;i++){if(a[0][i]=='Y'&&a[1][i]=='Y'&&a[2][i]=='Y'){p++;}if(a[i][0]=='Y'&&a[i][1]=='Y'&&'Y'==a[i][2]){p++;}}if(a[0][0]=='Y'&&'Y'==a[1][1]&&'Y'==a[2][2]){p++;}if(a[2][0]=='Y'&&'Y'==a[1][1]&&'Y'==a[0][2]){p++;}return p;}//////////////////////////////////////////////////////////////////////// void zoufa(char a[3][3]){int p=0,q=0,m,n,max1,max=-100;char b[3][3];strcpy(*b,*a);for(int i=0;i<3;i++){for(int j=0;j<3;j++){if(b[i][j]==' '){b[i][j]='X';for(int m=0;m<3;m++){for(int n=0;n<3;n++){if(b[m][n]==' '){b[m][n]='X';}}}p=guzhi(b);strcpy(*b,*a);for(int k=0;k<3;k++){for(int h=0;h<3;h++){if(b[k][h]==' '){b[k][h]='Y';}}}q=guzhi(b);max1=p-q;if(max1>max){max=max1;m=i;n=j;}}}}a[m][n]='X';}/////////////////////////////////////////////////////////////////////////////// void shuru(char a[3][3]){int i,j,m,n;printf("输入1选择先手,输入2选择后手!\n");scanf("%d",&n);if(n==2){for(m=0;m<9;m++){if(m%2==0){printf("电脑方输入:\n");zoufa(a);shuchu(a);if(panduan(a)==1){break;}}else{printf("请输入:");scanf("%d %d",&i,&j);if(a[i-1][j-1]==' '){a[i-1][j-1]='Y';shuchu(a);if(panduan(a)==1){break;}}else{printf("输入错误,请按格式重新输入!\n");m--;}}}}if(n==1){for(m=0;m<9;m++){if(m%2==0){printf("请输入:");scanf("%d %d",&i,&j);if(a[i-1][j-1]==' '){a[i-1][j-1]='Y';shuchu(a);if(panduan(a)==1){break;}}else{printf("输入错误,请按格式重新输入!\n");m--;}}else{printf("电脑方输入:\n");zoufa(a);shuchu(a);if(panduan(a)==1){break;}}。
C语言实现三子棋的步骤和代码详解
C语⾔实现三⼦棋的步骤和代码详解⽬录⼀、问题描述⼆、基本流程三、步骤1.菜单界⾯2.创建棋盘3.棋盘初始化4.打印棋盘5.玩家落⼦6.电脑落⼦7.判断胜负四、结果演⽰1.玩家胜利2.电脑胜利3.和棋五、代码实现⼀、问题描述⽤c语⾔实现三⼦棋。
⼆、基本流程在写三⼦棋的代码之前,我们来看看实现这个游戏的逻辑:1.菜单界⾯选择开始或者退出游戏。
2.创建棋盘并初始化。
3.打印棋盘。
4.玩家落⼦(玩家输⼊⾏列坐标的⽅式来落⼦),'x'表⽰玩家落⼦。
5.判定胜负关系(输,赢,和棋),'q'表⽰和棋。
6.电脑落⼦(随机位置落⼦) ,'o'表⽰电脑落⼦。
7.判定胜负关系。
8.回到步骤2 继续执⾏。
三、步骤1.菜单界⾯1.开始游戏 0.退出游戏int menu(){printf("--------------------------\n");printf("--------1.开始游戏--------\n");printf("--------0.退出游戏--------\n");printf("--------------------------\n");int choice = 0;printf("请输⼊你的选择:");scanf("%d", &choice);return choice;}2.创建棋盘棋盘:使⽤3⾏3列的⼆维数组来表⽰,元素类型是char。
使⽤宏定义的原因:1.推⾼代码可读性,后续代码中遇到3,⽅便理解含义。
2.提⾼扩展性,如果将来要修改棋盘尺⼨,代码修改会很⽅便。
#define MAX_ROW 3#define MAX_COL 3char chessBoard[MAX_ROW][MAX_COL] = { 0 };3.棋盘初始化' '表⽰棋盘上的空⽩区域void init(char chessBoard[MAX_ROW][MAX_COL]){for (int row = 0; row < MAX_ROW; row++){for (int col = 0; col < MAX_COL; col++){chessBoard[row][col] = ' ';}}}4.打印棋盘(1)简陋⽅法:void print_chessBoard(char chessBoard[MAX_ROW][MAX_COL]){for (int row = 0; row <MAX_ROW; row++){for (int col = 0; col < MAX_COL; col++){printf("%c",chessBoard[row][col]) ;}printf("\n");}}结果:注意:这⾥不是棋盘没打印出来,⽽是我们使⽤' '表⽰空⽩区域,所以我们看到的棋盘是⼀⽚⿊⾊。
C语言编写的与电脑下棋程序代码
#include<iostream>using namespace std;void check(int i,int j); //检查每条直线上的各方棋子的数量int other(int b,int c);void check_win(); //检查是否胜利void begin(char ch);void computer(); //电脑下棋void user(); //用户下棋int a[4][4],deep;int line[3][3][2]; //全局变量,用来存放每条直线上的各方棋子数!//第一维下标表示是行列还是对角线,第二维下标表示第几条直线//第三维下标表示各方的棋子数!void main(){char ch;cout<<"欢迎与本机下#字棋!您的棋子将用#表示,电脑的棋子用X表示\n请选择先后手,输入u则您先手,输入c则电脑先手!(小写)\n"<<endl;do{cin>>ch;if(ch!='u'&&ch!='c') cout<<"您输入的数据不合法,请重新输入!"<<endl;}while(ch!='u'&&ch!='c'); //非法处理if(ch=='u') cout<<"您选择了您先手!\n"<<endl;else if(ch=='c') cout<<"您选择了电脑先手\n"<<endl;begin(ch);}void begin(char ch){int i,j,k;a[1][1]=1;cout<<"\n 1 | 2 | 3 \n ——————\n 4 | 5 | 6 \n ——————\n 7 | 8 | 9for(i=1;i<4;i++)for(j=1;j<4;j++){if(j==1)if(i==1);else a[i][j]=a[i-1][3]+1;else a[i][j]=a[i][j-1]+1;} //初始化棋盘for(i=0;i<3;i++)for(j=0;j<3;j++)for(k=0;k<3;k++)line[i][j][k]=0;deep=0;if(ch=='u') user();else computer();}void computer(){int i,j,flag=0,m; //flag判断是否落子for(i=0;i<3;i++)for(j=0;j<3;j++){if (line[i][j][0]==2&&line[i][j][1]==0&&flag==0){m=other(i,j);if(m%3==0) {a[m/3][3]=40;check(m/3,3);}else {a[m/3+1][m%3]=40; check(m/3+1,m%3);} //40是#的ASCII码减48flag=1;} //当某条直线上用户有两棋子而我方无棋子时,在该直线的另一点落子if (line[i][j][1]==2&&line[i][j][0]==0&&flag==0){m=other(i,j);if(m%3==0) {a[m/3][3]=40;check(m/3,3);}else {a[m/3+1][m%3]=40; check(m/3+1,m%3);} //当某条直线上我方有两棋子而对方无棋子时,在该直线的另一点落子flag=1;}if (line[i][j][1]==1&&line[i][j][0]==0&&flag==0){m=other(i,j);if(m%3==0) {a[m/3][3]=40;check(m/3,3);}else {a[m/3+1][m%3]=40; check(m/3+1,m%3);} //当某条直线上我方有一棋子而对方无棋子时,在该直线的另一点落子flag=1;}}if(flag==0)if (a[2][2]==5){a[2][2]=72;flag=1;check(2,2);} //优先下第5个位置else{for(i=2;i<9&&flag==0&&a[i][j]==(i-1)*3+j;i+=2){if(i%3==0) {a[i/3][3]=40;check(i/3,3);}else {a[i/3+1][i%3]=40; check(i/3+1,i%3);}flag=1;} //2,4,6,8其次for(i=1;i<9&&flag==0&&a[i][j]==(i-1)*3+j;i+=2){if(i%3==0) {a[i/3][3]=40;check(i/3,3);}else {a[i/3+1][i%3]=40; check(i/3+1,i%3);}flag=1;} //1,3,7,9再次for(i=1;i<=3;i++){cout<<" ";for(j=1;j<=3;j++)cout<<" "<<char(a[i][j]+48)<<" |";cout<<"\n ——————"<<endl;}deep++;if(flag==1) user();else check_win();}void user(){int down;cout<<"\n轮到您下了,请输入您要下的棋子位置:"<<endl;do{cin>>down;if(down<1||down>9)cout<<"数字不合法,请重新输入\n"<<endl;if ((down%3==0&&a[down/3][3]!=down)||(down%3!=0&&a[down/3+1][down%3]!=down)) cout<<"您所输入的位置已经有棋子了,请重新输入\n"<<endl;}while(down<1||down>9||((down%3==0&&a[down/3][3]!=down)||(down%3!=0&&a[down/3+ 1][down%3]!=down)));//判断输入是否异常if(down%3==0) {a[down/3][3]=-13; check(down/3,3);}else {a[down/3+1][down%3]=-13;check(down/3+1,down%3);} //-13是X的ASCII码减48deep++;computer();}int other(int b,int c)int i,m;if(b<2){if (b==0)m=3*c+1;if (b==1)m=c+1;for(i=m;i<m+3;i++){if(i%3==0)if(a[i/3][3]==i) return i;else;else if(a[i/3+1][i%3]) return i;}} //水平与垂直直线的情况 if(b==2){if(c==0){if(a[1][1]==1) return 1;if(a[2][2]==5) return 5;if(a[3][3]==9) return 9;}if(c==1){if(a[1][3]==3) return 3;if(a[2][2]==5) return 5;if(a[3][1]==7) return 7;}} //对角线的情况}void check(int i,int j){if(a[i][j]==-13)line[0][i-1][0]++;line[1][j-1][0]++;if((i==1&&j==1)||(i==3&&j==3)||(i==2&&j==2))line[2][0][0]++;if((i==1&&j==3)||(i==3&&j==1)||(i==2&&j==2))line[2][1][0]++;}if(a[i][j]==40){line[0][i-1][1]++;line[1][j-1][1]++;if((i==1&&j==1)||(i==3&&j==3)||(i==2&&j==2))line[2][0][1]++;if((i==1&&j==3)||(i==3&&j==1)||(i==2&&j==2))line[2][1][1]++;}check_win();}void check_win(){int i,j,k;bool end=false;char ch;for(i=0;i<3;i++)for(j=0;j<3;j++)for(k=0;k<2;k++)if(line[i][j][k]==3){if(k==1) cout<<"您失败了,电脑获得了胜利"<<endl;if(k==0) cout<<"恭喜您,您获得了胜利"<<endl;end=true;}if(deep>=9) {cout<<"和棋!"<<endl;end=true;}if(end){cout<<"\n您是否继续是输入Y,按其余任意键结束本游戏!:"<<endl;cin>>ch;if(ch!='y') exit(0);else{cout<<"\n请选择先后手,输入U则您先手,输入C则电脑先手!\n"<<endl;do{cin>>ch;if(ch!='u'&&ch!='c') cout<<"您输入的数据不合法,请重新输入!"<<endl;}while(ch!='u'&&ch!='c'); //非法处理if(ch=='u') cout<<"您选择了您先手!\n"<<endl;else if(ch=='c') cout<<"您选择了电脑先手\n"<<endl;begin(ch);}}}。
c语言围棋程序代码
c语言围棋程序代码以下是一个简单的C语言围棋程序的示例代码:```c#include <stdio.h>#define BOARD_SIZE 9enum Stone {EMPTY,BLACK,WHITE};enum Stone board[BOARD_SIZE][BOARD_SIZE];void init_board() {for (int i = 0; i < BOARD_SIZE; i++) {for (int j = 0; j < BOARD_SIZE; j++) {board[i][j] = EMPTY;}}}void print_board() {for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { switch (board[i][j]) {case EMPTY:printf(".");break;case BLACK:printf("X");break;case WHITE:printf("O");break;}}printf("\n");}}int main() {init_board();print_board();return 0;}```这个程序定义了一个9x9的围棋棋盘,使用`enum Stone`枚举类型表示棋盘上的三种棋子状态:空、黑子、白子。
`init_board()`函数用于初始化棋盘,将所有位置都设为EMPTY。
`print_board()`函数用于打印当前棋盘状态。
在`main()`函数中,首先调用`init_board()`初始化棋盘,然后调用`print_board()`打印初始棋盘状态。
你可以根据需要进一步扩展和完善这个程序,例如添加下子功能、判断胜负等等。
详解用C语言实现三子棋游戏流程
详解⽤C语⾔实现三⼦棋游戏流程⽬录三⼦棋游戏简介⼀、分析及实现1.棋盘2.落⼦3.判断输赢⼆、程序演⽰三、完整代码1.main.c2.game.c3.game.h总结三⼦棋游戏简介这是⼀个简单的三⼦棋⼩游戏,估计⼤家⼩时候都玩⼉过,只要能连成三个,⽆论是⾏,列还是对⾓线,就可以胜利了。
是不是很简单呢,那我们来尝试⼀下⽤C语⾔来写⼀个简单的三⼦棋游戏。
⼀、分析及实现1.棋盘想要玩游戏,那⾸先应该有⼀个棋盘吧。
因为我们玩⼉的是三⼦棋,所以来说⼀个3X3的棋盘应该可以满⾜要求。
这⾥使⽤了⼀个三⾏三列的⼆维数组来模拟三⼦棋的棋盘。
有了棋盘之后在开始游戏之前先把棋盘初始化⼀下,这样看起来更⽅便。
//初始化棋盘void InitBoard(char board[][COL], int row, int col){int i = 0;for (i = 0; i < ROW; i++){int j = 0;for (j = 0; j < COL; j++){board[i][j] = ' ';}}}之后呢,再来打印看看,要优化⼀下再打印出来//打印棋盘void DisplayBoard(char board[ROW][COL], int row, int col){int i = 0;for (i = 0; i < row; i++){printf("+---+---+---+\n");int j = 0;for (j = 0; j < col; j++){printf("| %c ", board[i][j]);}printf("|\n");}printf("+---+---+---+\n");}这是我设计的⼀个简单的⼩棋盘棋盘的风格和样式可以根据⾃⼰的喜好更改。
好了,现在我们有了棋盘,有了打印出来的样⼦,那接下来就应该向棋盘上落⼦了!2.落⼦下棋呢可以是两个⼈下,当然也可以⾃⼰和电脑下棋,这⾥我选择和电脑对弈,这样就需要两个功能了,⼀个是玩家下棋,⼀个就是电脑下棋了。
c语言围棋程序代码
c语言围棋程序代码你可以参考以下围棋程序代码,希望对你有所帮助:```cpp// 这个实现 vector 首尾颠倒template< typename Vector_Reverse>void Reserve_Vector(vector< Vector_Reverse> & arr){for (int i = 0; i < (arr.size() >> 1); i++){Vector_Reverse temp = arr(i);arr(i) = arr(arr.size() - i - 1);arr(arr.size() - i - 1) = temp;}}// 这个是函数模板template< typename VectorInclude>bool ifNotInVector(vector< VectorInclude> arr, VectorInclude num){for (VectorInclude i : arr){if (i == num)return false;}return true;}// 这个实现判断两个 vector 是否相等template< typename VectorEqual>bool ifTwoVectorEqual(vector< VectorEqual> arr, vector< VectorEqual> ano){if (arr.size() != ano.size())return false;for (int i = 0; i < arr.size(); i++){if (arr(i) != ano(i))return false;}return true;}MapPoint地图点的类,由 indexX 存放列数,indexY 存放行数,有 PathDir 枚举类型枚举四个方向,能通过 MapPoint getDirPoint(PathDir turn) 这个函数获得四个方向的点,这个函数长这样MapPoint getDirPoint(PathDir turn){switch (turn){case path_up:return MapPoint(this-> indexX, this-> indexY - 1);break;case path_down:return MapPoint(this-> indexX, this-> indexY + 1);break;case path_left:return MapPoint(this-> indexX - 1, this-> indexY);break;case path_right:return MapPoint(this-> indexX + 1, this-> indexY);break;default:break;}}同时这个类也用于保存 BoundingBox 类的坐标,因为 easyx 里的每个点都是整型,所以保存的坐标也是整型。
C语言编写象棋程序代码文档推荐
/**/#include ""#include ""/*----------------------------------------------------*/ #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\\", "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\\"} };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;/*选择页面*/int86(0x10,&r,&r);}unsigned char set_SVGA_mode(int vmode) /*设置SVGA屏幕模式*/{union REGS r;int86(0x10,&r,&r);return int get_SVGA_mode() /*获取当前SVGA屏幕模式*/{union REGS r;int86(0x10,&r,&r);return 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[][].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) /*当显示页不同时更换页面,提高一定的输出速度*/ {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[][].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) /*当显示页不同时更换页面,提高一定的输出速度*/ {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=;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=;oldy=;x=;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;}redup(){int x,y,n;if>0){;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);else if(!==&&==&&redstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);=;=;}}reddown(){int x,y,n;if<9){++;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);else if(!==&&==&&redstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);=;}}redleft(){int x,y,n;if>0){;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);else if(!==&&==&&redstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);=;=;}}redright(){int x,y,n;if<8){++;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);else if(!==&&==&&redstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);if==&&==&&redstate==MOVE)drawselecursor(redoldpos);drawcursor(redcurpos);=;=;}}reddo(){int i,j,x,y,n;if(redstate==SELECT&&redcanselect()){if(board[][]<=RED&&board[][]>0){redstate=MOVE;drawselecursor(redcurpos);=;=;}}else if(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;}else if(board[i][j]!=0) n++;}turn=BLACK;blackstate=SELECT;drawcursor(blackcurpos);drawbmp(30,438,"bmp\\"); /*转交控制权给黑方*/ }}redundo(){int x,y,n;if(redstate==MOVE){x=position[][].x;y=position[][].y;n=board[][];drawbmp(x,y,chessfile[n]);=;=;drawcursor(redcurpos);redstate=SELECT;}}/*--------------------黑方操作----------------------*/int blackcanselect(){int x,y;x=;y=;if(board[x][y]>=BLACK_JU&&board[x][y]<=BLACK_BIN)return 1;elsereturn 0;}int blackcanmove(){int i,j,min,max,oldx,oldy,x,y;oldx=;oldy=;x=;y=;/*case1 目标位置是否是自己人*/if(board[x][y]>=BLACK_JU&&board[x][y]<=BLACK_BIN)return 0;/* 军、马、炮、相、士、将、卒的走法正确性的判断*/switch(board[oldx][oldy]){case BLACK_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 BLACK_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 BLACK_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{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) 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) j++;if(j!=1) return 0;}}break;case BLACK_XIANG:if(x>4) 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;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;elseif(x==0&&y==5) return 1;elseif(x==2&&y==3) return 1;elseif(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(x>2) 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 BLACK_BIN:if(oldx<=4){ if(y!=oldy||(x-oldx)!=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;}return 1;}blackup(){int x,y,n;if>0){;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);else if(!==&&==&&blackstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&blackstate==MOVE)drawselecursor(blackoldpos);drawcursor(blackcurpos);=;=;}}blackdown(){int x,y,n;if<9){++;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);else if(!==&&==&&blackstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&blackstate==MOVE)drawselecursor(blackoldpos);drawcursor(blackcurpos);=;=;}}blackleft(){int x,y,n;if>0){;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);else if(!==&&==&&blackstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&blackstate==MOVE)drawselecursor(blackoldpos);drawcursor(blackcurpos);=;=;}}blackright(){int x,y,n;if<8){++;x=position[][].x;y=position[][].y;if(board[][]==0)drawbmp(x,y,boardfile[][]);else if(!==&&==&&blackstate==MOVE)){n=board[][];drawbmp(x,y,chessfile[n]);}if==&&==&&blackstate==MOVE)drawselecursor(blackoldpos);drawcursor(blackcurpos);=;=;}}blackdo(){int i,j,x,y,n;if(blackstate==SELECT&&blackcanselect()){if(board[][]<=BLACK&&board[][]>0){blackstate=MOVE;drawselecursor(blackcurpos);=;=;}}else if(blackstate==MOVE&&blackcanmove()) {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[][]==RED_JIANG){winner=BLACK;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=RED;finish=1;break;}else if(board[i][j]!=0) n++;}turn=RED;redstate=SELECT;drawcursor(redcurpos);drawbmp(30,438,"bmp\\"); /*转交控制权给红方*/ }}blackundo(){int x,y,n;if(blackstate==MOVE){x=position[][].x;y=position[][].y;n=board[][];drawbmp(x,y,chessfile[n]);=;=;drawcursor(blackcurpos);blackstate=SELECT;}}/*----------------------------------------------------*/start(){drawcursor(blackcurpos); drawbmp(30,438,"bmp\\"); 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();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,"bmp\\");initdrawchess();/*初始化光标位置*/===9;===8;===0;===0;/*开始*/start();if(winner==RED)drawbmp(200,200,"bmp\\");else if(winner==BLACK)drawbmp(200,200,"bmp\\");elsedrawbmp(200,200,"bmp\\");getch();end();}。
中国象棋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;}。
C语言编写象棋程序代码上课讲义
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\\1 6.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\\3 6.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","bmp\\4a.wfb","bmp\\4t.wfb","bmp\\49.wfb"},{"bmp\\51.wfb","bmp\\52.wfb","bmp\\5t.wfb","bmp\\54.wfb","bmp\\5t.wfb","bmp\\5 6.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\\6 6.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","bmp\\7a.wfb","bmp\\7t.wfb","bmp\\79.wfb"},{"bmp\\81.wfb","bmp\\8a.wfb","bmp\\8t.wfb","bmp\\84.wfb","bmp\\85.wfb","bmp\\8 6.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","bmp\\9t.wfb","bmp\\9t.wfb","bmp\\99.wfb"},{"bmp\\101.wfb","bmp\\102.wfb","bmp\\102.wfb","bmp\\104.wfb","bmp\\105.wfb"," bmp\\106.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,blackold pos;/*----------------------------------------------------*/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;}redup(){int x,y,n;if(redcurpos.x>0){redcurpos.x--;x=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);elseif(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE )){n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}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=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);elseif(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE )){n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}if(redtemppos.x==redoldpos.x&&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=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);elseif(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE )){n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}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.y<8){redcurpos.y++;x=position[redtemppos.x][redtemppos.y].x;y=position[redtemppos.x][redtemppos.y].y;if(board[redtemppos.x][redtemppos.y]==0)drawbmp(x,y,boardfile[redtemppos.x][redtemppos.y]);elseif(!(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE )){n=board[redtemppos.x][redtemppos.y];drawbmp(x,y,chessfile[n]);}if(redtemppos.x==redoldpos.x&&redtemppos.y==redoldpos.y&&redstate==MOVE) drawselecursor(redoldpos);drawcursor(redcurpos);redtemppos.x=redcurpos.x;redtemppos.y=redcurpos.y;}}reddo(){int i,j,x,y,n;if(redstate==SELECT&&redcanselect()){if(board[redcurpos.x][redcurpos.y]<=RED&&board[redcurpos.x][redcurpos.y]>0) {redstate=MOVE;drawselecursor(redcurpos);redoldpos.x=redcurpos.x;redoldpos.y=redcurpos.y;}}else if(redstate==MOVE&&redcanmove()){x=position[redoldpos.x][redoldpos.y].x;y=position[redoldpos.x][redoldpos.y].y;drawbmp(x,y,boardfile[redoldpos.x][redoldpos.y]);x=position[redcurpos.x][redcurpos.y].x;y=position[redcurpos.x][redcurpos.y].y;n=board[redoldpos.x][redoldpos.y];drawbmp(x,y,chessfile[n]);if(board[redcurpos.x][redcurpos.y]==BLACK_JIANG){winner=RED;finish=1;return;}board[redcurpos.x][redcurpos.y]=n;board[redoldpos.x][redoldpos.y]=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;}else if(board[i][j]!=0) n++;}turn=BLACK;blackstate=SELECT;drawcursor(blackcurpos);drawbmp(30,438,"bmp\\bzq.wfb"); /*转交控制权给黑方*/ }}redundo(){int x,y,n;if(redstate==MOVE){x=position[redoldpos.x][redoldpos.y].x;y=position[redoldpos.x][redoldpos.y].y;n=board[redoldpos.x][redoldpos.y];drawbmp(x,y,chessfile[n]);redoldpos.x=redcurpos.x;redoldpos.y=redcurpos.y;drawcursor(redcurpos);redstate=SELECT;}}/*--------------------黑方操作----------------------*/int blackcanselect(){int x,y;x=blackcurpos.x;y=blackcurpos.y;if(board[x][y]>=BLACK_JU&&board[x][y]<=BLACK_BIN)return 1;elsereturn 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(board[x][y]>=BLACK_JU&&board[x][y]<=BLACK_BIN) return 0;/* 军、马、炮、相、士、将、卒的走法正确性的判断*/ switch(board[oldx][oldy]){case BLACK_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 BLACK_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 BLACK_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{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) 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) j++;if(j!=1) return 0;}}break;case BLACK_XIANG:if(x>4) 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;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;elseif(x==0&&y==5) return 1;elseif(x==2&&y==3) return 1;elseif(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(x>2) 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 BLACK_BIN:if(oldx<=4){ if(y!=oldy||(x-oldx)!=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;}return 1;}blackup(){int x,y,n;if(blackcurpos.x>0){blackcurpos.x--;x=position[blacktemppos.x][blacktemppos.y].x;y=position[blacktemppos.x][blacktemppos.y].y;if(board[blacktemppos.x][blacktemppos.y]==0)drawbmp(x,y,boardfile[blacktemppos.x][blacktemppos.y]);elseif(!(blacktemppos.x==blackoldpos.x&&blacktemppos.y==blackoldpos.y&&blackstat e==MOVE)){n=board[blacktemppos.x][blacktemppos.y];drawbmp(x,y,chessfile[n]);}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.x<9){blackcurpos.x++;x=position[blacktemppos.x][blacktemppos.y].x;y=position[blacktemppos.x][blacktemppos.y].y;if(board[blacktemppos.x][blacktemppos.y]==0)drawbmp(x,y,boardfile[blacktemppos.x][blacktemppos.y]);elseif(!(blacktemppos.x==blackoldpos.x&&blacktemppos.y==blackoldpos.y&&blackstat e==MOVE)){n=board[blacktemppos.x][blacktemppos.y];drawbmp(x,y,chessfile[n]);}if(blacktemppos.x==blackoldpos.x&&blacktemppos.y==blackoldpos.y&&blackstate ==MOVE)drawselecursor(blackoldpos);drawcursor(blackcurpos);blacktemppos.x=blackcurpos.x;blacktemppos.y=blackcurpos.y;}}blackleft(){int x,y,n;if(blackcurpos.y>0){blackcurpos.y--;x=position[blacktemppos.x][blacktemppos.y].x;y=position[blacktemppos.x][blacktemppos.y].y;if(board[blacktemppos.x][blacktemppos.y]==0)drawbmp(x,y,boardfile[blacktemppos.x][blacktemppos.y]);elseif(!(blacktemppos.x==blackoldpos.x&&blacktemppos.y==blackoldpos.y&&blackstat e==MOVE)){n=board[blacktemppos.x][blacktemppos.y];drawbmp(x,y,chessfile[n]);}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.y<8){blackcurpos.y++;x=position[blacktemppos.x][blacktemppos.y].x;y=position[blacktemppos.x][blacktemppos.y].y;if(board[blacktemppos.x][blacktemppos.y]==0)drawbmp(x,y,boardfile[blacktemppos.x][blacktemppos.y]);elseif(!(blacktemppos.x==blackoldpos.x&&blacktemppos.y==blackoldpos.y&&blackstat e==MOVE)){n=board[blacktemppos.x][blacktemppos.y];drawbmp(x,y,chessfile[n]);}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(board[blackcurpos.x][blackcurpos.y]<=BLACK&&board[blackcurpos.x][blackcurp os.y]>0){blackstate=MOVE;drawselecursor(blackcurpos);blackoldpos.x=blackcurpos.x;blackoldpos.y=blackcurpos.y;}else if(blackstate==MOVE&&blackcanmove()){x=position[blackoldpos.x][blackoldpos.y].x;y=position[blackoldpos.x][blackoldpos.y].y;drawbmp(x,y,boardfile[blackoldpos.x][blackoldpos.y]);x=position[blackcurpos.x][blackcurpos.y].x;y=position[blackcurpos.x][blackcurpos.y].y;n=board[blackoldpos.x][blackoldpos.y];drawbmp(x,y,chessfile[n]);if(board[blackcurpos.x][blackcurpos.y]==RED_JIANG){winner=BLACK;finish=1;return;}board[blackcurpos.x][blackcurpos.y]=n;board[blackoldpos.x][blackoldpos.y]=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=RED;finish=1;break;}else if(board[i][j]!=0) n++;}turn=RED;redstate=SELECT;drawcursor(redcurpos);drawbmp(30,438,"bmp\\rzq.wfb"); /*转交控制权给红方*/ }}blackundo(){int x,y,n;if(blackstate==MOVE){x=position[blackoldpos.x][blackoldpos.y].x; y=position[blackoldpos.x][blackoldpos.y].y; n=board[blackoldpos.x][blackoldpos.y];drawbmp(x,y,chessfile[n]);blackoldpos.x=blackcurpos.x;blackoldpos.y=blackcurpos.y;drawcursor(blackcurpos);blackstate=SELECT;}}/*----------------------------------------------------*/ start(){drawcursor(blackcurpos);drawbmp(30,438,"bmp\\bzq.wfb");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();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,"bmp\\board.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,"bmp\\redwin.wfb");else if(winner==BLACK)drawbmp(200,200,"bmp\\blackwin.wfb");elsedrawbmp(200,200,"bmp\\exit.wfb");getch();end();}。
三子棋小游戏
C语言实现三子棋小游戏建立头文件game.h建立game.c源文件#define _CRT_SECURE_NO_WARNINGS 1#include<stdlib.h>#include"game.h"void init_board(char arr[ROW][COL]){int i = 0;int j = 0;for (i = 0; i < ROW; i++){for (j = 0; j < COL; j++){arr[i][j] = ' ';}}}void display_board(char arr[ROW][COL]){int i = 0;printf("____________\n");for (i = 0; i < ROW; i++){printf(" %c | %c | %c |\n", arr[i][0], arr[i][1], arr[i][2]);if (i < 2){printf("---|---|---|");}printf("\n");}}void player_chess(char arr[ROW][COL]){int x = 0;int y = 0;while (1){printf("请输入你所要下棋的方位:");scanf("%d%d", &x, &y);x--;y--;if (arr[x][y] == ' '){arr[x][y] = '*';break;}else{printf("这个位置已经被占,请重新选择方位。
\n");continue;}}}void computer_chess(char arr[ROW][COL]){srand((unsigned)time(NULL));while (1){int x = 0;int y = 0;srand((unsigned)time(NULL));x = rand() % 3;y = rand() % 3;if (arr[x][y] == ' '){arr[x][y] = 'O';break;}else{if (check_full(arr) == 0){continue;}else{break;}}}}int check_full(char arr[ROW][COL]){int i = 0;int j = 0;for (i = 0; i < ROW; i++){for (j = 0; j < COL; j++){if (arr[i][j] == ' '){return 0;}}}return 1;}char check_win(char arr[ROW][COL]){int i = 0;int ret = 0;for (i = 0; i < ROW; i++){if ((arr[i][0] == arr[i][1]) && (arr[i][2] == arr[i][1])){return arr[i][0];}}for (i = 0; i < COL; i++){if ((arr[0][i] == arr[1][i]) && (arr[2][i] == arr[1][i])){return arr[0][i];}}if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2])) {return arr[0][0];}if ((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0])) {return arr[2][0];}ret = check_full(arr);if (ret == 1){return'q';}return' ';}建立testc源文件#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>#include<stdlib.h>#include<time.h>#include"game.h"int main(){char arr[ROW][COL] = { 0 };int input = 1;while (input){printf("请选择是否开始游戏:\n");printf("1.开始游戏 \n");printf("0.退出游戏 \n");scanf("%d", &input);switch (input){case 1:{char ret = 0;init_board(arr);do{display_board(arr);player_chess(arr);display_board(arr);if (check_win(arr) != ' '){ret = check_win(arr);break;}computer_chess(arr);ret = check_win(arr);} while (ret == ' ');display_board(arr);if (ret == '*'){printf("玩家胜 \n");}elseif (ret == 'O'){printf("电脑胜 \n");}elseif (ret == 'q'){printf("游戏平局 \n");}}break;case 0:break;}}return 0;}。
c语言小游戏代码
c语言小游戏代码#include <stdio.h>#include <stdlib.h>#include <windows.h>// 定义元素类型#define ELEMENT char// 游戏行数#define ROW 10// 游戏显示延迟#define SLEEPTIME 100int main(int argc, char *argv[]){// 定义游戏的棋盘,用数组存放ELEMENT array[ROW][ROW];// 定义获胜条件int winCondition = 5;// 初始化,把棋盘清空system("cls");int i,j;for(i = 0; i < ROW; i++){for(j = 0; j < ROW; j++){array[i][j] = ' ';}}// 循环游戏,当有一方满足胜利条件时终止int tmp;int count = 0; // 存放棋子数while(1){// 依次取出玩家记录的棋子int x, y;// 如果已经有子落下,则计算是第几步if(count > 0){printf("第%d步:\n", count);}// 显示游戏棋盘for(i = 0; i < ROW; i++){printf(" ");for(j = 0; j < ROW; j++){printf("---");}printf("\n|");for(j = 0; j < ROW; j++){printf("%c |", array[i][j]);}printf("\n");}printf(" ");for(j = 0; j < ROW; j++){printf("---");}printf("\n");// 要求玩家输入放下棋子的位置printf("请玩家输入要放弃棋子的位置(1-%d)\n", ROW); printf("横坐标:");scanf("%d", &x);printf("纵坐标:");scanf("%d", &y);// 判断棋子位置是否有效if(x < 1 || x > ROW || y < 1 || y > ROW || array[x-1][y-1] != ' '){printf("输入错误!\n");system("pause");system("cls");continue;}// 把棋子记录,并计数if(count % 2 == 0){array[x-1][y-1] = 'X';}else{array[x-1][y-1] = 'O';}count++;// 判断是否有获胜者int i, j, k;int tempx, tempy;for(i = 0; i < ROW; i++){for(j = 0; j < ROW; j++){if(array[i][j] == 'X' || array[i][j] == 'O') {// 判断横向是否有获胜者tmp = 1;for(k = 1; k < winCondition; k++){// 注意边界,必须验证范围有效if(j + k > ROW - 1) break;// 如果和前一个位置的棋子相同,则计数加1,否则跳出if(array[i][j+k] == array[i][j])tmp++;else break;}// 如果计数满足获胜条件,则显示获胜者if(tmp >= winCondition){printf("玩家 %c 获胜!\n", array[i][j]);system("pause");return 0;}// 判断纵向是否有获胜者tmp = 1;for(k。
C语言实现井字棋(三子棋)
C 语⾔实现井字棋(三⼦棋)本⽂实例为⼤家分享了C 语⾔实现井字棋的具体代码,供⼤家参考,具体内容如下⼀、实现思路三⼦棋是九宫格的格式,所以⽤⼆维数组接收数据。
⽤‘O'代表电脑下的⼦,‘X'代表玩家下的⼦。
未下⼦的时候初始化 '‘(space)。
则⼆维数组为“char”类型,⼤⼩为char board[3][3]。
打印出井字的棋盘,同时为了将数据显⽰在每格的中间,⽤空格隔开(“ %c |”)的格式设置棋盘“|”⽤来形成竖,接着打印“- - -|”⽤来形成⾏。
将两部⽤for 循环按照逻辑链接起来,即可打印出“井”。
同时“%c”处初始化为‘ '(space )<1> 玩家下的⼦⽤数组的坐标表⽰,输⼊提⽰:(请输⼊坐标:),输⼊格式为(1 1),范围为1~3。
<2> 玩家下⼦的时候,如果遇到已经下过⼦的坐标,返回并提⽰错误,重新输⼊。
<3> 如果输⼊的坐标超过范围,提⽰错误,重新输⼊。
<4> 打印棋盘,将玩家下⼦的坐标处⽤'X'替换。
<1> 电脑下⼦,利⽤范围为1~3,随机产⽣条件下的坐标,如果遇到已经下过⼦的坐标,就重新产⽣条件下的坐标,这⾥利⽤循环进⾏调试。
<2> 有⼀个电脑下⼦优先规则:a 、电脑下⼦的第⼀个是随机产⽣,在电脑先⼿的时候,第⼆个也是随机产⽣。
b 、判断是否有两个 ‘O”O' 在⾏、列或者斜对⾓,如果有,就将第三个⼦下在可以连成⼀直线的空⽩处(即三点成线,赢得⽐赛)。
如果有连成线但是没⽤空⽩处,进⾏c 步骤。
c 、判断是不是有两个 ‘X”X' 在⾏、列或者斜对⾓练成线,并且第三个空为空⽩。
如果有就将⼦下在该空⽩处(即对玩家进⾏堵截)。
如果没⽤,进⾏d 步骤。
d 、在范围内随机下⼦。
<3> 打印棋盘,将电脑下⼦的坐标处⽤'O'代替。
C++实现三子棋游戏详细介绍(附代码)
C++实现三⼦棋游戏详细介绍(附代码)⽬录游戏描述:游戏思维导图:(从头开始)头⽂件也写在前⾯:先把游戏与主函数写在前⾯:游戏函数:主函数:1.构建游戏菜单2.构建棋盘3.玩家和电脑下棋3.1玩家先下3.2电脑下棋4.判断胜负与下棋结果代码附上:总结游戏描述:先来点真实的!三⼦棋其实就是我们⼩时候经常玩的井字棋,如果⼀⽅玩家实现横着三个或者竖着三个,或者对⾓线三个就算游戏胜利!游戏思维导图:(从头开始)头⽂件也写在前⾯:#include <stdio.h>#include <stdlib.h>#include <time.h>#define H 3//⾏#define L 3//列先把游戏与主函数写在前⾯:游戏函数:void game()//游戏实现步骤{char gamemap[L][H] = {0};//存储数据⼆维数组initgamemap(gamemap, H, L);//棋盘初始化showgamemap(gamemap, H, L);//打印出棋盘char ret = 0;while (1)//循环下棋{playgame1(gamemap, H, L);//玩家下棋showgamemap(gamemap, H, L);//玩家下完棋后并打印出该棋盘 ret=gameover(gamemap, H, L);//判断玩家是否胜利if (ret != 'C')break;}if (ret == 'X'){printf("玩家赢了!");}else if (ret == 'O'){printf("电脑赢了!");}else{printf("平局");}}主函数:int main(void){Menu();//显⽰游戏菜单srand((unsigned int)time(NULL));int input = 0;do{printf("请输⼊的你选择!");scanf_s("%d", &input);switch (input){case 1:game();//游戏函数实现三⼦棋游戏!break;case 0:printf("退出游戏!");break;default:printf("输⼊错误,请从新输⼊!");break;}}while (input);return 0;}1.构建游戏菜单我们⽤没有范围值类型的Menu函数表⽰游戏菜单,等下在主函数中直接读取就⾏。
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(){intgdriver,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);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);{ 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,"chinesechess");red_shuai(190,30);red_shi(150,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_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+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,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);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;else 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)(arra y((y-40)/40+1,x/40+1)=='Z'))||(((a-x)==40&&(b-y)==80)&&(array((y+40)/40+1,x/4 0+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){}}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)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)(arra y((y-40)/40+1,x/40+1)=='Z'))||(((a-x)==40&&(b-y)==80)&&(array((y+40)/40+1,x/4 0+1)=='Z'))return 1;elsereturn 0;break; 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;}}* /。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
三子棋问题一目的运用所学课程的知识来研究、解决一些具有一定综合性问题的专业课题。
通过课程设计提高学生综合运用所学知识来解决实际问题及进行科学实验或技术设计的初步能力。
二需求分析1、该程序主要为设计简单的三子棋游戏;2、三子棋问题即在3 * 3的二维数组上下棋,只要有棋子在行、列或对角线连成一线即取得胜利。
3、可实现玩家与电脑对弈。
三概要设计1、主函数模块int main(){do{开始新的对弈;}while(玩家选择停止游戏);return 0;}2、调用函数模块①int chess();/*函数功能:开始对弈函数参数:无函数返回值:return 0*/{初始化棋局;选择玩家或电脑先行;do{ //开始下棋输出当前棋局;if(该玩家走棋){do{玩家输出走棋位置;检查走棋位置的合法性;}while(玩家输入合法的位置);}else if(电脑走棋){调用函数使电脑走棋}判断是否有一方胜出;}while(有一方胜出或平局);printf("最终棋局:\n");输出最终棋局;if(平局)输出平局;}②int check(char *chess,char sign);/*函数功能:使用了指针,判断位置可行则走棋,不可行则返回值为1 函数参数:使用指针传递棋局,以及欲走棋的位置函数返回值:0代表可以走棋,1代表不可走棋 */{判断走棋位置是否合法;不合法则返回1;否则返回0;}③int judge(char *chess);/*函数功能:判断是否有胜出函数参数:当前棋局函数返回值:0代表未有胜出,1代表玩家胜出,2代表电脑胜出*/ {for(行检查){if(某行三子连线){if(连线棋子为‘O’)玩家胜出;else电脑胜出;}}for(列检查)//思想同上if(对角三字连线检查)//思想同上}④int computer(char *chess);/*函数功能:电脑走棋函数参数:当前棋局函数返回值:return 0*/{for(某行出现两个同样的棋)在该行第三个位置走棋;for(某列出现两个同样的棋)在该列第三个位置走棋;if(对角线出现两个相同的棋)在该对角线第三个位置走棋;抢占中心位置;寻找可走位置;}整个程序的流程图如下所示:四详细设计//功能:简易三子棋。
三子连线即成功。
可选择人先行或电脑先行//system("cls")实现刷屏功能#include <stdio.h>#include <stdlib.h>int chess();int check(char *chess,char sign);int judge(char *chess);int computer(char *chess);int main(){char order;do{chess();printf("1:再来一次\n");printf("2:退出\n");scanf(" %c",&order); //scanf(" %c\n",&order)时出现错误}while(order=='1');return 0;}int chess(){int i,j;char sign;int loc;char choose;int count;int result;float peo,com;char chess[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};count=0;printf("请选择:\n");printf("1:玩家先行\n");printf("2:电脑先行\n");scanf(" %c",&choose);while((choose!='1')&&(choose!='2')){ //人脑或电脑先行printf("请输入1或2:");scanf(" %c",&choose);}if(choose=='1'){peo=0;com=0.5;}else{peo=0.5;com=0;}do{ //开始下棋system("cls"); //刷屏printf("------------------------\n");for(i=0;i<3;i++){for(j=0;j<3;j++)printf("%5c",chess[i][j]);printf("\n\n");}printf("------------------------\n");if(peo<com){printf("玩家走棋:\n");printf("请走棋\n");do{printf("输入想要走棋位置的标号:");scanf(" %c",&sign);while((((int)sign)<49)||(((int)sign)>57)){printf("输入错误,请重新输入:");scanf(" %c",&sign);}loc=check(*chess,sign); //走棋位置检查}while(loc!=0);peo=peo+1;}else if(peo>com){printf("电脑走棋:\n");computer(*chess); //电脑走棋com=com+1;}else{printf("com和peo计算出错\n");return 0;}result=judge(*chess); //判断是否有一方胜出count++;}while((count<9)&&(result==0));printf("最终棋局:\n");printf("------------------------\n");for(i=0;i<3;i++){for(j=0;j<3;j++)printf("%5c",chess[i][j]);printf("\n\n");}printf("------------------------\n");if(count==9)printf("平局\n");return 0;}int computer(char *chess){int i,j,sign;sign=1;for(i=0;i<3;i++) //行内是否有两个相同{if((chess[i*3+0]==chess[i*3+1])&&(chess[i*3+2]!='O')&&(chess[i*3+2]!='X')){chess[i*3+2]='X';return 0;}elseif((chess[i*3+1]==chess[i*3+2])&&(chess[i*3+0]!='O')&&(chess[i*3+0]!='X')){chess[i*3+0]='X';return 0;}elseif((chess[i*3+0]==chess[i*3+2])&&(chess[i*3+1]!='O')&&(chess[i*3+1]!='X')){chess[i*3+1]='X';return 0;}else;}for(j=0;j<3;j++) //列内是否有个相同{if((chess[0*3+j]==chess[1*3+j])&&(chess[2*3+j]!='O')&&(chess[2*3+j]!='X')){chess[2*3+j]='X';return 0;}elseif((chess[0*3+j]==chess[2*3+j])&&(chess[1*3+j]!='O')&&(chess[1*3+j]!='X')){chess[1*3+j]='X';return 0;}elseif((chess[1*3+j]==chess[2*3+j])&&(chess[0*3+j]!='O')&&(chess[0*3+j]!='X')){chess[0*3+j]='X';return 0;}else;}//关于对角的判断if(((chess[0]==chess[8])||(chess[2]==chess[6]))&&(chess[4]!='O')&&(chess[4]!='X')) chess[4]='X';else if((chess[0]==chess[4])&&(chess[8]!='O')&&(chess[8]!='X'))chess[8]='X';else if((chess[8]==chess[4])&&(chess[0]!='O')&&(chess[0]!='X'))chess[0]='X';else if((chess[2]==chess[4])&&(chess[6]!='O')&&(chess[6]!='X'))chess[6]='X';else if((chess[6]==chess[4])&&(chess[2]!='O')&&(chess[2]!='X'))chess[2]='X';else if((chess[4]!='X')&&(chess[4]!='O')) //抢占中心位置chess[4]='X';else{for(i=0;i<3;i++)for(j=0;j<3;j++)if((chess[i*3+j]!='O')&&(chess[i*3+j]!='X')){chess[i*3+j]='X';sign=0;return 0;}if(sign==1){printf("电脑走棋错误\n"); //无子可落exit(0);}}return 0;}int check(char *chess,char sign) //使用了指针,判断位置可行则走棋,不可行则返回值为1//int check(char chess[3][3],char sign) //未使用指针,则不可在方法内走棋{int i,j;int check;check=0;switch(sign){case '1':i=0;j=0;break;case '2':i=0;j=1;break;case '3':i=0;j=2;break;case '4':i=1;j=0;break;case '5':i=1;j=1;break;case '6':i=1;j=2;break;case '7':i=2;j=0;break;case '8':i=2;j=1;break;case '9':i=2;j=2;break;default:check=1;}if((chess[i*3+j]=='O')||(chess[i*3+j]=='X')){printf("输入位置无法走棋\n");check=1;}else{chess[i*3+j]='O';}return check;}int judge(char *chess){int i,j;int result;result=0;for(i=0;i<3;i++){if((chess[i*3+0]==chess[i*3+1])&&(chess[i*3+1]==chess[i*3+2])&&(chess[i*3+0]==ch ess[i*3+2]))//行判断{if(chess[i*3+0]=='O')result=1;elseresult=2;break;}else;}for(j=0;j<3;j++){if((chess[0*3+j]==chess[1*3+j])&&(chess[1*3+j]==chess[2*3+j])&&(chess[0*3+j]==ch ess[2*3+j]))//列判断{if(chess[0*3+j]=='O')result=1;elseresult=2;break;}else;}if(((chess[0]==chess[4])&&(chess[4]==chess[8])&&(chess[0]==chess[8]))||((chess[2]==chess[4])&&(chess[4]==chess[6])&&(chess[2]==chess[6])))//对角判断{if(chess[4]=='O')result=1; //人胜出elseresult=2;}if(result==1){printf("玩家胜出\n");}else if(result==2){printf("电脑胜出\n");}else;return result;}五测试结果。